blob: 625ab1624a7add605b5f1a9b46418b0117d78ab9 [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) {
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000129 SmallVector<Decl*, 2> Decls(S->decls());
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000130 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000131}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000132
133void StmtPrinter::VisitNullStmt(NullStmt *Node) {
134 Indent() << ";\n";
135}
136
137void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000138 Indent();
139 PrintRawDeclStmt(Node);
140 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000141}
142
Chris Lattner073926e2007-05-20 23:04:55 +0000143void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
144 Indent();
145 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000146 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000147}
148
Chris Lattner6c0ff132006-11-05 00:19:50 +0000149void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000150 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000151 PrintExpr(Node->getLHS());
152 if (Node->getRHS()) {
153 OS << " ... ";
154 PrintExpr(Node->getRHS());
155 }
156 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000157
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000158 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000159}
160
161void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000162 Indent(-1) << "default:\n";
163 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000164}
165
166void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000167 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000168 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000169}
170
Richard Smithc202b282012-04-14 00:33:13 +0000171void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
172 OS << "[[";
173 bool first = true;
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000174 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
175 end = Node->getAttrs().end();
176 it != end; ++it) {
Richard Smithc202b282012-04-14 00:33:13 +0000177 if (!first) {
178 OS << ", ";
179 first = false;
180 }
181 // TODO: check this
Richard Smith52f04a22012-08-16 02:43:29 +0000182 (*it)->printPretty(OS, Policy);
Richard Smithc202b282012-04-14 00:33:13 +0000183 }
184 OS << "]] ";
185 PrintStmt(Node->getSubStmt(), 0);
186}
187
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000188void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000189 OS << "if (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000190 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
191 PrintRawDeclStmt(DS);
192 else
193 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000194 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattner073926e2007-05-20 23:04:55 +0000196 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
197 OS << ' ';
198 PrintRawCompoundStmt(CS);
199 OS << (If->getElse() ? ' ' : '\n');
200 } else {
201 OS << '\n';
202 PrintStmt(If->getThen());
203 if (If->getElse()) Indent();
204 }
Mike Stump11289f42009-09-09 15:08:12 +0000205
Chris Lattner073926e2007-05-20 23:04:55 +0000206 if (Stmt *Else = If->getElse()) {
207 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000208
Chris Lattner073926e2007-05-20 23:04:55 +0000209 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
210 OS << ' ';
211 PrintRawCompoundStmt(CS);
212 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000213 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
214 OS << ' ';
215 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000216 } else {
217 OS << '\n';
218 PrintStmt(If->getElse());
219 }
Chris Lattner882f7882006-11-04 18:52:07 +0000220 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000221}
222
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000223void StmtPrinter::VisitIfStmt(IfStmt *If) {
224 Indent();
225 PrintRawIfStmt(If);
226}
227
Chris Lattnerf2174b62006-11-04 20:59:27 +0000228void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
229 Indent() << "switch (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000230 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
231 PrintRawDeclStmt(DS);
232 else
233 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000234 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000235
Chris Lattner073926e2007-05-20 23:04:55 +0000236 // Pretty print compoundstmt bodies (very common).
237 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
238 OS << " ";
239 PrintRawCompoundStmt(CS);
240 OS << "\n";
241 } else {
242 OS << "\n";
243 PrintStmt(Node->getBody());
244 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000245}
246
Chris Lattner85ed8732006-11-04 20:40:44 +0000247void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
248 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000249 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
250 PrintRawDeclStmt(DS);
251 else
252 PrintExpr(Node->getCond());
Chris Lattner85ed8732006-11-04 20:40:44 +0000253 OS << ")\n";
254 PrintStmt(Node->getBody());
255}
256
257void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000258 Indent() << "do ";
259 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
260 PrintRawCompoundStmt(CS);
261 OS << " ";
262 } else {
263 OS << "\n";
264 PrintStmt(Node->getBody());
265 Indent();
266 }
Mike Stump11289f42009-09-09 15:08:12 +0000267
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000268 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000269 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000270 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000271}
272
Chris Lattner71e23ce2006-11-04 20:18:38 +0000273void StmtPrinter::VisitForStmt(ForStmt *Node) {
274 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000275 if (Node->getInit()) {
276 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000277 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000278 else
279 PrintExpr(cast<Expr>(Node->getInit()));
280 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000281 OS << ";";
282 if (Node->getCond()) {
283 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000284 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000285 }
286 OS << ";";
287 if (Node->getInc()) {
288 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000289 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000290 }
291 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000292
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000293 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
294 PrintRawCompoundStmt(CS);
295 OS << "\n";
296 } else {
297 OS << "\n";
298 PrintStmt(Node->getBody());
299 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000300}
301
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000302void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000303 Indent() << "for (";
304 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000305 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000306 else
307 PrintExpr(cast<Expr>(Node->getElement()));
308 OS << " in ";
309 PrintExpr(Node->getCollection());
310 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000311
Fariborz Jahanian83615522008-01-02 22:54:34 +0000312 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
313 PrintRawCompoundStmt(CS);
314 OS << "\n";
315 } else {
316 OS << "\n";
317 PrintStmt(Node->getBody());
318 }
319}
320
Richard Smith02e85f32011-04-14 22:09:26 +0000321void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
322 Indent() << "for (";
323 PrintingPolicy SubPolicy(Policy);
324 SubPolicy.SuppressInitializers = true;
325 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
326 OS << " : ";
327 PrintExpr(Node->getRangeInit());
328 OS << ") {\n";
329 PrintStmt(Node->getBody());
Ted Kremenek88446602013-12-11 23:44:02 +0000330 Indent() << "}";
331 if (Policy.IncludeNewlines) OS << "\n";
Richard Smith02e85f32011-04-14 22:09:26 +0000332}
333
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000334void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
335 Indent();
336 if (Node->isIfExists())
337 OS << "__if_exists (";
338 else
339 OS << "__if_not_exists (";
340
341 if (NestedNameSpecifier *Qualifier
342 = Node->getQualifierLoc().getNestedNameSpecifier())
343 Qualifier->print(OS, Policy);
344
345 OS << Node->getNameInfo() << ") ";
346
347 PrintRawCompoundStmt(Node->getSubStmt());
348}
349
Chris Lattner16976d32006-11-05 01:46:01 +0000350void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000351 Indent() << "goto " << Node->getLabel()->getName() << ";";
352 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000353}
354
355void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000356 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000357 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000358 OS << ";";
359 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000360}
361
362void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000363 Indent() << "continue;";
364 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000365}
366
367void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000368 Indent() << "break;";
369 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000370}
371
372
Chris Lattner882f7882006-11-04 18:52:07 +0000373void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
374 Indent() << "return";
375 if (Node->getRetValue()) {
376 OS << " ";
377 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000378 }
Ted Kremenek88446602013-12-11 23:44:02 +0000379 OS << ";";
380 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000381}
382
Chris Lattner73c56c02007-10-29 04:04:16 +0000383
Chad Rosierde70e0e2012-08-25 00:11:56 +0000384void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000385 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000386
Anders Carlsson660bdd12007-11-23 23:12:25 +0000387 if (Node->isVolatile())
388 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000389
Anders Carlsson660bdd12007-11-23 23:12:25 +0000390 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000391 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000392
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000393 // Outputs
394 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
395 Node->getNumClobbers() != 0)
396 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000397
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000398 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
399 if (i != 0)
400 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000401
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000402 if (!Node->getOutputName(i).empty()) {
403 OS << '[';
404 OS << Node->getOutputName(i);
405 OS << "] ";
406 }
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattner72bbf172009-03-10 04:59:06 +0000408 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000409 OS << " ";
410 Visit(Node->getOutputExpr(i));
411 }
Mike Stump11289f42009-09-09 15:08:12 +0000412
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000413 // Inputs
414 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
415 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000416
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000417 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
418 if (i != 0)
419 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000420
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000421 if (!Node->getInputName(i).empty()) {
422 OS << '[';
423 OS << Node->getInputName(i);
424 OS << "] ";
425 }
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattner72bbf172009-03-10 04:59:06 +0000427 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000428 OS << " ";
429 Visit(Node->getInputExpr(i));
430 }
Mike Stump11289f42009-09-09 15:08:12 +0000431
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000432 // Clobbers
433 if (Node->getNumClobbers() != 0)
434 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000435
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000436 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
437 if (i != 0)
438 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000439
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000440 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000441 }
Mike Stump11289f42009-09-09 15:08:12 +0000442
Ted Kremenek88446602013-12-11 23:44:02 +0000443 OS << ");";
444 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000445}
446
Chad Rosier32503022012-06-11 20:47:18 +0000447void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
448 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000449 Indent() << "__asm ";
450 if (Node->hasBraces())
451 OS << "{\n";
John McCallf413f5e2013-05-03 00:10:13 +0000452 OS << Node->getAsmString() << "\n";
Chad Rosierb6f46c12012-08-15 16:53:30 +0000453 if (Node->hasBraces())
454 Indent() << "}\n";
Chad Rosier32503022012-06-11 20:47:18 +0000455}
456
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000457void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000458 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000459}
460
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000461void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000462 Indent() << "@try";
463 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
464 PrintRawCompoundStmt(TS);
465 OS << "\n";
466 }
Mike Stump11289f42009-09-09 15:08:12 +0000467
Douglas Gregor96c79492010-04-23 22:50:49 +0000468 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
469 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000470 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000471 if (catchStmt->getCatchParamDecl()) {
472 if (Decl *DS = catchStmt->getCatchParamDecl())
473 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000474 }
475 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000476 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
477 PrintRawCompoundStmt(CS);
478 OS << "\n";
479 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000480 }
Mike Stump11289f42009-09-09 15:08:12 +0000481
482 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
483 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000484 Indent() << "@finally";
485 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000486 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000487 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000488}
489
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000490void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000491}
492
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000493void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000494 Indent() << "@catch (...) { /* todo */ } \n";
495}
496
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000497void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000498 Indent() << "@throw";
499 if (Node->getThrowExpr()) {
500 OS << " ";
501 PrintExpr(Node->getThrowExpr());
502 }
503 OS << ";\n";
504}
505
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000506void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000507 Indent() << "@synchronized (";
508 PrintExpr(Node->getSynchExpr());
509 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000510 PrintRawCompoundStmt(Node->getSynchBody());
511 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000512}
513
John McCall31168b02011-06-15 23:02:42 +0000514void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
515 Indent() << "@autoreleasepool";
516 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
517 OS << "\n";
518}
519
Sebastian Redl9b244a82008-12-22 21:35:02 +0000520void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
521 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000522 if (Decl *ExDecl = Node->getExceptionDecl())
523 PrintRawDecl(ExDecl);
524 else
525 OS << "...";
526 OS << ") ";
527 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000528}
529
530void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
531 Indent();
532 PrintRawCXXCatchStmt(Node);
533 OS << "\n";
534}
535
536void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
537 Indent() << "try ";
538 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000539 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000540 OS << " ";
541 PrintRawCXXCatchStmt(Node->getHandler(i));
542 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000543 OS << "\n";
544}
545
John Wiegley1c0675e2011-04-28 01:08:34 +0000546void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
547 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
548 PrintRawCompoundStmt(Node->getTryBlock());
549 SEHExceptStmt *E = Node->getExceptHandler();
550 SEHFinallyStmt *F = Node->getFinallyHandler();
551 if(E)
552 PrintRawSEHExceptHandler(E);
553 else {
554 assert(F && "Must have a finally block...");
555 PrintRawSEHFinallyStmt(F);
556 }
557 OS << "\n";
558}
559
560void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
561 OS << "__finally ";
562 PrintRawCompoundStmt(Node->getBlock());
563 OS << "\n";
564}
565
566void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
567 OS << "__except (";
568 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000569 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000570 PrintRawCompoundStmt(Node->getBlock());
571 OS << "\n";
572}
573
574void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
575 Indent();
576 PrintRawSEHExceptHandler(Node);
577 OS << "\n";
578}
579
580void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
581 Indent();
582 PrintRawSEHFinallyStmt(Node);
583 OS << "\n";
584}
585
Chris Lattner71e23ce2006-11-04 20:18:38 +0000586//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000587// OpenMP clauses printing methods
588//===----------------------------------------------------------------------===//
589
590namespace {
591class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
592 raw_ostream &OS;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000593 const PrintingPolicy &Policy;
Alexey Bataev756c1962013-09-24 03:17:45 +0000594 /// \brief Process clauses with list of variables.
595 template <typename T>
596 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000597public:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000598 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
599 : OS(OS), Policy(Policy) { }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000600#define OPENMP_CLAUSE(Name, Class) \
601 void Visit##Class(Class *S);
602#include "clang/Basic/OpenMPKinds.def"
603};
604
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000605void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
606 OS << "if(";
607 Node->getCondition()->printPretty(OS, 0, Policy, 0);
608 OS << ")";
609}
610
Alexey Bataev568a8332014-03-06 06:15:19 +0000611void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
612 OS << "num_threads(";
613 Node->getNumThreads()->printPretty(OS, 0, Policy, 0);
614 OS << ")";
615}
616
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000617void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
618 OS << "default("
619 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
620 << ")";
621}
622
Alexey Bataev756c1962013-09-24 03:17:45 +0000623template<typename T>
624void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
625 for (typename T::varlist_iterator I = Node->varlist_begin(),
626 E = Node->varlist_end();
627 I != E; ++I)
628 OS << (I == Node->varlist_begin() ? StartSym : ',')
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000629 << *cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
Alexey Bataev756c1962013-09-24 03:17:45 +0000630}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000631
632void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
633 if (!Node->varlist_empty()) {
634 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000635 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000636 OS << ")";
637 }
638}
639
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000640void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
641 if (!Node->varlist_empty()) {
642 OS << "firstprivate";
643 VisitOMPClauseList(Node, '(');
644 OS << ")";
645 }
646}
647
Alexey Bataev758e55e2013-09-06 18:03:48 +0000648void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
649 if (!Node->varlist_empty()) {
650 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000651 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000652 OS << ")";
653 }
654}
655
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000656}
657
658//===----------------------------------------------------------------------===//
659// OpenMP directives printing methods
660//===----------------------------------------------------------------------===//
661
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000662void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000663 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000664 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000665 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
666 I != E; ++I)
667 if (*I && !(*I)->isImplicit()) {
668 Printer.Visit(*I);
669 OS << ' ';
670 }
671 OS << "\n";
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000672 if (S->getAssociatedStmt()) {
673 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000674 "Expected captured statement!");
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000675 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000676 PrintStmt(CS);
677 }
678}
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000679
680void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
681 Indent() << "#pragma omp parallel ";
682 PrintOMPExecutableDirective(Node);
683}
684
685void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
686 Indent() << "#pragma omp simd ";
687 PrintOMPExecutableDirective(Node);
688}
689
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000690//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +0000691// Expr printing methods.
692//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000693
Chris Lattner882f7882006-11-04 18:52:07 +0000694void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000695 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
696 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000697 if (Node->hasTemplateKeyword())
698 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000699 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000700 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000701 TemplateSpecializationType::PrintTemplateArgumentList(
702 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000703}
704
John McCall8cd78132009-11-19 22:55:06 +0000705void StmtPrinter::VisitDependentScopeDeclRefExpr(
706 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000707 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
708 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000709 if (Node->hasTemplateKeyword())
710 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000711 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000712 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000713 TemplateSpecializationType::PrintTemplateArgumentList(
714 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000715}
716
John McCalld14a8642009-11-21 08:51:07 +0000717void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000718 if (Node->getQualifier())
719 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000720 if (Node->hasTemplateKeyword())
721 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000722 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000723 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000724 TemplateSpecializationType::PrintTemplateArgumentList(
725 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000726}
727
Steve Naroffe46504b2007-11-12 14:29:37 +0000728void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000729 if (Node->getBase()) {
730 PrintExpr(Node->getBase());
731 OS << (Node->isArrow() ? "->" : ".");
732 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000733 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000734}
735
Steve Naroffec944032008-05-30 00:40:33 +0000736void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000737 if (Node->isSuperReceiver())
738 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +0000739 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000740 PrintExpr(Node->getBase());
741 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +0000742 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
743 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +0000744 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000745
John McCallb7bd14f2010-12-02 01:19:52 +0000746 if (Node->isImplicitProperty())
Aaron Ballmanb190f972014-01-03 17:59:55 +0000747 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +0000748 else
749 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000750}
751
Ted Kremeneke65b0862012-03-06 20:05:56 +0000752void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
753
754 PrintExpr(Node->getBaseExpr());
755 OS << "[";
756 PrintExpr(Node->getKeyExpr());
757 OS << "]";
758}
759
Chris Lattner6307f192008-08-10 01:53:14 +0000760void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000761 switch (Node->getIdentType()) {
762 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000763 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000764 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000765 OS << "__func__";
766 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000767 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000768 OS << "__FUNCTION__";
769 break;
David Majnemerbed356a2013-11-06 23:31:56 +0000770 case PredefinedExpr::FuncDName:
771 OS << "__FUNCDNAME__";
772 break;
Nico Weber3a691a32012-06-23 02:07:59 +0000773 case PredefinedExpr::LFunction:
774 OS << "L__FUNCTION__";
775 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000776 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000777 OS << "__PRETTY_FUNCTION__";
778 break;
779 }
780}
781
Steve Naroffae4143e2007-04-26 20:39:23 +0000782void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000783 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000784
785 switch (Node->getKind()) {
786 case CharacterLiteral::Ascii: break; // no prefix.
787 case CharacterLiteral::Wide: OS << 'L'; break;
788 case CharacterLiteral::UTF16: OS << 'u'; break;
789 case CharacterLiteral::UTF32: OS << 'U'; break;
790 }
791
Chris Lattner666115c2007-07-13 23:58:20 +0000792 switch (value) {
793 case '\\':
794 OS << "'\\\\'";
795 break;
796 case '\'':
797 OS << "'\\''";
798 break;
799 case '\a':
800 // TODO: K&R: the meaning of '\\a' is different in traditional C
801 OS << "'\\a'";
802 break;
803 case '\b':
804 OS << "'\\b'";
805 break;
806 // Nonstandard escape sequence.
807 /*case '\e':
808 OS << "'\\e'";
809 break;*/
810 case '\f':
811 OS << "'\\f'";
812 break;
813 case '\n':
814 OS << "'\\n'";
815 break;
816 case '\r':
817 OS << "'\\r'";
818 break;
819 case '\t':
820 OS << "'\\t'";
821 break;
822 case '\v':
823 OS << "'\\v'";
824 break;
825 default:
Jordan Rose00d1b592013-02-08 22:30:27 +0000826 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +0000827 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +0000828 else if (value < 256)
829 OS << "'\\x" << llvm::format("%02x", value) << "'";
830 else if (value <= 0xFFFF)
831 OS << "'\\u" << llvm::format("%04x", value) << "'";
832 else
833 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000834 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000835}
836
Steve Naroffdf7855b2007-02-21 23:46:25 +0000837void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000838 bool isSigned = Node->getType()->isSignedIntegerType();
839 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000840
Chris Lattner06430412007-05-21 05:45:03 +0000841 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000842 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000843 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000844 // FIXME: The Short and UShort cases are to handle cases where a short
845 // integeral literal is formed during template instantiation. They should
846 // be removed when template instantiation no longer needs integer literals.
847 case BuiltinType::Short:
848 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000849 case BuiltinType::Int: break; // no suffix.
850 case BuiltinType::UInt: OS << 'U'; break;
851 case BuiltinType::Long: OS << 'L'; break;
852 case BuiltinType::ULong: OS << "UL"; break;
853 case BuiltinType::LongLong: OS << "LL"; break;
854 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000855 case BuiltinType::Int128: OS << "i128"; break;
856 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000857 }
Chris Lattner882f7882006-11-04 18:52:07 +0000858}
Benjamin Kramer8a526762012-09-20 14:07:17 +0000859
860static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
861 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000862 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +0000863 Node->getValue().toString(Str);
864 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +0000865 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
866 OS << '.'; // Trailing dot in order to separate from ints.
867
868 if (!PrintSuffix)
869 return;
870
871 // Emit suffixes. Float literals are always a builtin float type.
872 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
873 default: llvm_unreachable("Unexpected type for float literal!");
874 case BuiltinType::Half: break; // FIXME: suffix?
875 case BuiltinType::Double: break; // no suffix.
876 case BuiltinType::Float: OS << 'F'; break;
877 case BuiltinType::LongDouble: OS << 'L'; break;
878 }
879}
880
881void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
882 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +0000883}
Chris Lattner1c20a172007-08-26 03:42:43 +0000884
885void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
886 PrintExpr(Node->getSubExpr());
887 OS << "i";
888}
889
Steve Naroffdf7855b2007-02-21 23:46:25 +0000890void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +0000891 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +0000892}
893void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
894 OS << "(";
895 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000896 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000897}
898void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000899 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000900 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000901
Eli Friedman1cf25362009-06-14 22:39:26 +0000902 // Print a space if this is an "identifier operator" like __real, or if
903 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000904 switch (Node->getOpcode()) {
905 default: break;
John McCalle3027922010-08-25 11:45:40 +0000906 case UO_Real:
907 case UO_Imag:
908 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000909 OS << ' ';
910 break;
John McCalle3027922010-08-25 11:45:40 +0000911 case UO_Plus:
912 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000913 if (isa<UnaryOperator>(Node->getSubExpr()))
914 OS << ' ';
915 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000916 }
917 }
Chris Lattner882f7882006-11-04 18:52:07 +0000918 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000919
Chris Lattner15768702006-11-05 23:54:51 +0000920 if (Node->isPostfix())
921 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000922}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000923
Douglas Gregor882211c2010-04-28 22:16:22 +0000924void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
925 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000926 Node->getTypeSourceInfo()->getType().print(OS, Policy);
927 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000928 bool PrintedSomething = false;
929 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
930 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
931 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
932 // Array node
933 OS << "[";
934 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
935 OS << "]";
936 PrintedSomething = true;
937 continue;
938 }
Douglas Gregord1702062010-04-29 00:18:15 +0000939
940 // Skip implicit base indirections.
941 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
942 continue;
943
Douglas Gregor882211c2010-04-28 22:16:22 +0000944 // Field or identifier node.
945 IdentifierInfo *Id = ON.getFieldName();
946 if (!Id)
947 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000948
Douglas Gregor882211c2010-04-28 22:16:22 +0000949 if (PrintedSomething)
950 OS << ".";
951 else
952 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000953 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000954 }
955 OS << ")";
956}
957
Peter Collingbournee190dee2011-03-11 19:24:49 +0000958void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
959 switch(Node->getKind()) {
960 case UETT_SizeOf:
961 OS << "sizeof";
962 break;
963 case UETT_AlignOf:
Jordan Rose58d54722012-06-30 21:33:57 +0000964 if (Policy.LangOpts.CPlusPlus)
965 OS << "alignof";
966 else if (Policy.LangOpts.C11)
967 OS << "_Alignof";
968 else
969 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +0000970 break;
971 case UETT_VecStep:
972 OS << "vec_step";
973 break;
974 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000975 if (Node->isArgumentType()) {
976 OS << '(';
977 Node->getArgumentType().print(OS, Policy);
978 OS << ')';
979 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +0000980 OS << " ";
981 PrintExpr(Node->getArgumentExpr());
982 }
Chris Lattner882f7882006-11-04 18:52:07 +0000983}
Peter Collingbourne91147592011-04-15 00:35:48 +0000984
985void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
986 OS << "_Generic(";
987 PrintExpr(Node->getControllingExpr());
988 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
989 OS << ", ";
990 QualType T = Node->getAssocType(i);
991 if (T.isNull())
992 OS << "default";
993 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000994 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +0000995 OS << ": ";
996 PrintExpr(Node->getAssocExpr(i));
997 }
998 OS << ")";
999}
1000
Chris Lattner882f7882006-11-04 18:52:07 +00001001void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001002 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001003 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001004 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001005 OS << "]";
1006}
1007
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001008void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001009 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001010 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1011 // Don't print any defaulted arguments
1012 break;
1013 }
1014
Chris Lattner882f7882006-11-04 18:52:07 +00001015 if (i) OS << ", ";
1016 PrintExpr(Call->getArg(i));
1017 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001018}
1019
1020void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1021 PrintExpr(Call->getCallee());
1022 OS << "(";
1023 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001024 OS << ")";
1025}
1026void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +00001027 // FIXME: Suppress printing implicit bases (like "this")
1028 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001029
1030 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +00001031 FieldDecl *ParentDecl = ParentMember
1032 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
David Blaikie8fab8e52012-11-12 19:12:12 +00001033
David Blaikie6bffd6f2012-11-12 19:32:32 +00001034 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +00001035 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +00001036
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001037 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1038 if (FD->isAnonymousStructOrUnion())
1039 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001040
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001041 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1042 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001043 if (Node->hasTemplateKeyword())
1044 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001045 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001046 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001047 TemplateSpecializationType::PrintTemplateArgumentList(
1048 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001049}
Steve Naroffe87026a2009-07-24 17:54:45 +00001050void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1051 PrintExpr(Node->getBase());
1052 OS << (Node->isArrow() ? "->isa" : ".isa");
1053}
1054
Nate Begemance4d7fc2008-04-18 23:10:10 +00001055void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001056 PrintExpr(Node->getBase());
1057 OS << ".";
1058 OS << Node->getAccessor().getName();
1059}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001060void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001061 OS << '(';
1062 Node->getTypeAsWritten().print(OS, Policy);
1063 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001064 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001065}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001066void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001067 OS << '(';
1068 Node->getType().print(OS, Policy);
1069 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001070 PrintExpr(Node->getInitializer());
1071}
Steve Naroff7a5af782007-07-13 16:58:59 +00001072void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001073 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001074 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001075}
Chris Lattner882f7882006-11-04 18:52:07 +00001076void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1077 PrintExpr(Node->getLHS());
1078 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1079 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001080}
Chris Lattner86928112007-08-25 02:00:02 +00001081void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1082 PrintExpr(Node->getLHS());
1083 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1084 PrintExpr(Node->getRHS());
1085}
Chris Lattner882f7882006-11-04 18:52:07 +00001086void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1087 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001088 OS << " ? ";
1089 PrintExpr(Node->getLHS());
1090 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001091 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001092}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001093
Chris Lattnereefa10e2007-05-28 06:56:27 +00001094// GNU extensions.
1095
John McCallc07a0c72011-02-17 10:25:35 +00001096void
1097StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1098 PrintExpr(Node->getCommon());
1099 OS << " ?: ";
1100 PrintExpr(Node->getFalseExpr());
1101}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001102void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001103 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001104}
1105
Chris Lattner366727f2007-07-24 16:58:17 +00001106void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1107 OS << "(";
1108 PrintRawCompoundStmt(E->getSubStmt());
1109 OS << ")";
1110}
1111
Steve Naroff9efdabc2007-08-03 21:21:27 +00001112void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1113 OS << "__builtin_choose_expr(";
1114 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001115 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001116 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001117 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001118 PrintExpr(Node->getRHS());
1119 OS << ")";
1120}
Chris Lattner366727f2007-07-24 16:58:17 +00001121
Douglas Gregor3be4b122008-11-29 04:51:27 +00001122void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1123 OS << "__null";
1124}
1125
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001126void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1127 OS << "__builtin_shufflevector(";
1128 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1129 if (i) OS << ", ";
1130 PrintExpr(Node->getExpr(i));
1131 }
1132 OS << ")";
1133}
1134
Hal Finkelc4d7c822013-09-18 03:29:45 +00001135void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1136 OS << "__builtin_convertvector(";
1137 PrintExpr(Node->getSrcExpr());
1138 OS << ", ";
1139 Node->getType().print(OS, Policy);
1140 OS << ")";
1141}
1142
Anders Carlsson4692db02007-08-31 04:56:16 +00001143void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001144 if (Node->getSyntacticForm()) {
1145 Visit(Node->getSyntacticForm());
1146 return;
1147 }
1148
Anders Carlsson4692db02007-08-31 04:56:16 +00001149 OS << "{ ";
1150 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1151 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001152 if (Node->getInit(i))
1153 PrintExpr(Node->getInit(i));
1154 else
1155 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +00001156 }
1157 OS << " }";
1158}
1159
Nate Begeman5ec4b312009-08-10 23:49:36 +00001160void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1161 OS << "( ";
1162 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1163 if (i) OS << ", ";
1164 PrintExpr(Node->getExpr(i));
1165 }
1166 OS << " )";
1167}
1168
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001169void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001170 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1171 DEnd = Node->designators_end();
1172 D != DEnd; ++D) {
1173 if (D->isFieldDesignator()) {
1174 if (D->getDotLoc().isInvalid())
1175 OS << D->getFieldName()->getName() << ":";
1176 else
1177 OS << "." << D->getFieldName()->getName();
1178 } else {
1179 OS << "[";
1180 if (D->isArrayDesignator()) {
1181 PrintExpr(Node->getArrayIndex(*D));
1182 } else {
1183 PrintExpr(Node->getArrayRangeStart(*D));
1184 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001185 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001186 }
1187 OS << "]";
1188 }
1189 }
1190
1191 OS << " = ";
1192 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001193}
1194
Douglas Gregor0202cb42009-01-29 17:44:32 +00001195void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001196 if (Policy.LangOpts.CPlusPlus) {
1197 OS << "/*implicit*/";
1198 Node->getType().print(OS, Policy);
1199 OS << "()";
1200 } else {
1201 OS << "/*implicit*/(";
1202 Node->getType().print(OS, Policy);
1203 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001204 if (Node->getType()->isRecordType())
1205 OS << "{}";
1206 else
1207 OS << 0;
1208 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001209}
1210
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001211void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001212 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001213 PrintExpr(Node->getSubExpr());
1214 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001215 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001216 OS << ")";
1217}
1218
John McCallfe96e0b2011-11-06 09:01:30 +00001219void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1220 PrintExpr(Node->getSyntacticForm());
1221}
1222
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001223void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001224 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001225 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001226#define BUILTIN(ID, TYPE, ATTRS)
1227#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1228 case AtomicExpr::AO ## ID: \
1229 Name = #ID "("; \
1230 break;
1231#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001232 }
1233 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001234
1235 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001236 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001237 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1238 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001239 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001240 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001241 }
Richard Smithfeea8832012-04-12 05:08:17 +00001242 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1243 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001244 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001245 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001246 }
Richard Smithfeea8832012-04-12 05:08:17 +00001247 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1248 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001249 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001250 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001251 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001252 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1253 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001254 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001255 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001256 if (Node->isCmpXChg()) {
1257 OS << ", ";
1258 PrintExpr(Node->getOrderFail());
1259 }
1260 OS << ")";
1261}
1262
Chris Lattnereefa10e2007-05-28 06:56:27 +00001263// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001264void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1265 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1266 "",
1267#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1268 Spelling,
1269#include "clang/Basic/OperatorKinds.def"
1270 };
1271
1272 OverloadedOperatorKind Kind = Node->getOperator();
1273 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1274 if (Node->getNumArgs() == 1) {
1275 OS << OpStrings[Kind] << ' ';
1276 PrintExpr(Node->getArg(0));
1277 } else {
1278 PrintExpr(Node->getArg(0));
1279 OS << ' ' << OpStrings[Kind];
1280 }
Eli Friedman8e236422012-10-12 22:45:14 +00001281 } else if (Kind == OO_Arrow) {
1282 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001283 } else if (Kind == OO_Call) {
1284 PrintExpr(Node->getArg(0));
1285 OS << '(';
1286 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1287 if (ArgIdx > 1)
1288 OS << ", ";
1289 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1290 PrintExpr(Node->getArg(ArgIdx));
1291 }
1292 OS << ')';
1293 } else if (Kind == OO_Subscript) {
1294 PrintExpr(Node->getArg(0));
1295 OS << '[';
1296 PrintExpr(Node->getArg(1));
1297 OS << ']';
1298 } else if (Node->getNumArgs() == 1) {
1299 OS << OpStrings[Kind] << ' ';
1300 PrintExpr(Node->getArg(0));
1301 } else if (Node->getNumArgs() == 2) {
1302 PrintExpr(Node->getArg(0));
1303 OS << ' ' << OpStrings[Kind] << ' ';
1304 PrintExpr(Node->getArg(1));
1305 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001306 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001307 }
1308}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001309
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001310void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001311 // If we have a conversion operator call only print the argument.
1312 CXXMethodDecl *MD = Node->getMethodDecl();
1313 if (MD && isa<CXXConversionDecl>(MD)) {
1314 PrintExpr(Node->getImplicitObjectArgument());
1315 return;
1316 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001317 VisitCallExpr(cast<CallExpr>(Node));
1318}
1319
Peter Collingbourne41f85462011-02-09 21:07:24 +00001320void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1321 PrintExpr(Node->getCallee());
1322 OS << "<<<";
1323 PrintCallArgs(Node->getConfig());
1324 OS << ">>>(";
1325 PrintCallArgs(Node);
1326 OS << ")";
1327}
1328
Douglas Gregore200adc2008-10-27 19:41:14 +00001329void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1330 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001331 Node->getTypeAsWritten().print(OS, Policy);
1332 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001333 PrintExpr(Node->getSubExpr());
1334 OS << ")";
1335}
1336
Douglas Gregore200adc2008-10-27 19:41:14 +00001337void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1338 VisitCXXNamedCastExpr(Node);
1339}
1340
1341void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1342 VisitCXXNamedCastExpr(Node);
1343}
1344
1345void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1346 VisitCXXNamedCastExpr(Node);
1347}
1348
1349void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1350 VisitCXXNamedCastExpr(Node);
1351}
1352
Sebastian Redlc4704762008-11-11 11:37:55 +00001353void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1354 OS << "typeid(";
1355 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001356 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001357 } else {
1358 PrintExpr(Node->getExprOperand());
1359 }
1360 OS << ")";
1361}
1362
Francois Pichet9f4f2072010-09-08 12:20:18 +00001363void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1364 OS << "__uuidof(";
1365 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001366 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001367 } else {
1368 PrintExpr(Node->getExprOperand());
1369 }
1370 OS << ")";
1371}
1372
John McCall5e77d762013-04-16 07:28:30 +00001373void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1374 PrintExpr(Node->getBaseExpr());
1375 if (Node->isArrow())
1376 OS << "->";
1377 else
1378 OS << ".";
1379 if (NestedNameSpecifier *Qualifier =
1380 Node->getQualifierLoc().getNestedNameSpecifier())
1381 Qualifier->print(OS, Policy);
1382 OS << Node->getPropertyDecl()->getDeclName();
1383}
1384
Richard Smithc67fdd42012-03-07 08:35:16 +00001385void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1386 switch (Node->getLiteralOperatorKind()) {
1387 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001388 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001389 break;
1390 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001391 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1392 const TemplateArgumentList *Args =
1393 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1394 assert(Args);
1395 const TemplateArgument &Pack = Args->get(0);
1396 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1397 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001398 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001399 OS << C;
1400 }
1401 break;
1402 }
Richard Smith75025ba2012-03-08 09:02:38 +00001403 case UserDefinedLiteral::LOK_Integer: {
1404 // Print integer literal without suffix.
1405 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1406 OS << Int->getValue().toString(10, /*isSigned*/false);
1407 break;
1408 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001409 case UserDefinedLiteral::LOK_Floating: {
1410 // Print floating literal without suffix.
1411 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1412 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1413 break;
1414 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001415 case UserDefinedLiteral::LOK_String:
1416 case UserDefinedLiteral::LOK_Character:
1417 PrintExpr(Node->getCookedLiteral());
1418 break;
1419 }
1420 OS << Node->getUDSuffix()->getName();
1421}
1422
Chris Lattnereefa10e2007-05-28 06:56:27 +00001423void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1424 OS << (Node->getValue() ? "true" : "false");
1425}
1426
Sebastian Redl576fd422009-05-10 18:38:11 +00001427void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1428 OS << "nullptr";
1429}
1430
Douglas Gregor97a9c812008-11-04 14:32:21 +00001431void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1432 OS << "this";
1433}
1434
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001435void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1436 if (Node->getSubExpr() == 0)
1437 OS << "throw";
1438 else {
1439 OS << "throw ";
1440 PrintExpr(Node->getSubExpr());
1441 }
1442}
1443
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001444void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001445 // Nothing to print: we picked up the default argument.
1446}
1447
1448void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1449 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001450}
1451
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001452void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001453 Node->getType().print(OS, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001454 OS << "(";
1455 PrintExpr(Node->getSubExpr());
1456 OS << ")";
1457}
1458
Anders Carlsson993a4b32009-05-30 20:03:25 +00001459void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1460 PrintExpr(Node->getSubExpr());
1461}
1462
Douglas Gregordd04d332009-01-16 18:33:17 +00001463void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001464 Node->getType().print(OS, Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001465 OS << "(";
1466 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001467 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001468 Arg != ArgEnd; ++Arg) {
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001469 if (Arg->isDefaultArgument())
1470 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001471 if (Arg != Node->arg_begin())
1472 OS << ", ";
1473 PrintExpr(*Arg);
1474 }
1475 OS << ")";
1476}
1477
Douglas Gregore31e6062012-02-07 10:09:13 +00001478void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1479 OS << '[';
1480 bool NeedComma = false;
1481 switch (Node->getCaptureDefault()) {
1482 case LCD_None:
1483 break;
1484
1485 case LCD_ByCopy:
1486 OS << '=';
1487 NeedComma = true;
1488 break;
1489
1490 case LCD_ByRef:
1491 OS << '&';
1492 NeedComma = true;
1493 break;
1494 }
1495 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1496 CEnd = Node->explicit_capture_end();
1497 C != CEnd;
1498 ++C) {
1499 if (NeedComma)
1500 OS << ", ";
1501 NeedComma = true;
1502
1503 switch (C->getCaptureKind()) {
1504 case LCK_This:
1505 OS << "this";
1506 break;
1507
1508 case LCK_ByRef:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001509 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
Douglas Gregore31e6062012-02-07 10:09:13 +00001510 OS << '&';
1511 OS << C->getCapturedVar()->getName();
1512 break;
1513
1514 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00001515 OS << C->getCapturedVar()->getName();
1516 break;
1517 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001518
1519 if (C->isInitCapture())
1520 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00001521 }
1522 OS << ']';
1523
1524 if (Node->hasExplicitParameters()) {
1525 OS << " (";
1526 CXXMethodDecl *Method = Node->getCallOperator();
1527 NeedComma = false;
Aaron Ballman43b68be2014-03-07 17:50:17 +00001528 for (auto P : Method->params()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001529 if (NeedComma) {
1530 OS << ", ";
1531 } else {
1532 NeedComma = true;
1533 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00001534 std::string ParamStr = P->getNameAsString();
1535 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001536 }
1537 if (Method->isVariadic()) {
1538 if (NeedComma)
1539 OS << ", ";
1540 OS << "...";
1541 }
1542 OS << ')';
1543
1544 if (Node->isMutable())
1545 OS << " mutable";
1546
1547 const FunctionProtoType *Proto
1548 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001549 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001550
Bill Wendling44426052012-12-20 19:22:21 +00001551 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00001552
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001553 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001554 if (Node->hasExplicitResultType()) {
1555 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00001556 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001557 }
Douglas Gregore31e6062012-02-07 10:09:13 +00001558 }
1559
1560 // Print the body.
1561 CompoundStmt *Body = Node->getBody();
1562 OS << ' ';
1563 PrintStmt(Body);
1564}
1565
Douglas Gregor747eb782010-07-08 06:14:04 +00001566void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001567 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001568 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001569 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001570 Node->getType().print(OS, Policy);
1571 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001572}
1573
Sebastian Redlbd150f42008-11-21 19:14:01 +00001574void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1575 if (E->isGlobalNew())
1576 OS << "::";
1577 OS << "new ";
1578 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001579 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001580 OS << "(";
1581 PrintExpr(E->getPlacementArg(0));
1582 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001583 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1584 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001585 OS << ", ";
1586 PrintExpr(E->getPlacementArg(i));
1587 }
1588 OS << ") ";
1589 }
1590 if (E->isParenTypeId())
1591 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001592 std::string TypeS;
1593 if (Expr *Size = E->getArraySize()) {
1594 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001595 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00001596 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001597 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001598 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001599 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001600 if (E->isParenTypeId())
1601 OS << ")";
1602
Sebastian Redl6047f072012-02-16 12:22:20 +00001603 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1604 if (InitStyle) {
1605 if (InitStyle == CXXNewExpr::CallInit)
1606 OS << "(";
1607 PrintExpr(E->getInitializer());
1608 if (InitStyle == CXXNewExpr::CallInit)
1609 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001610 }
1611}
1612
1613void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1614 if (E->isGlobalDelete())
1615 OS << "::";
1616 OS << "delete ";
1617 if (E->isArrayForm())
1618 OS << "[] ";
1619 PrintExpr(E->getArgument());
1620}
1621
Douglas Gregorad8a3362009-09-04 17:36:40 +00001622void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1623 PrintExpr(E->getBase());
1624 if (E->isArrow())
1625 OS << "->";
1626 else
1627 OS << '.';
1628 if (E->getQualifier())
1629 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00001630 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00001631
Douglas Gregor678f90d2010-02-25 01:56:36 +00001632 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1633 OS << II->getName();
1634 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001635 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001636}
1637
Anders Carlsson0781ce72009-04-23 02:32:43 +00001638void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithd59b8322012-12-19 01:39:02 +00001639 if (E->isListInitialization())
1640 OS << "{ ";
1641
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001642 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1643 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1644 // Don't print any defaulted arguments
1645 break;
1646 }
1647
1648 if (i) OS << ", ";
1649 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001650 }
Richard Smithd59b8322012-12-19 01:39:02 +00001651
1652 if (E->isListInitialization())
1653 OS << " }";
Anders Carlsson0781ce72009-04-23 02:32:43 +00001654}
1655
Richard Smithcc1b96d2013-06-12 22:31:48 +00001656void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1657 PrintExpr(E->getSubExpr());
1658}
1659
John McCall5d413782010-12-06 08:20:24 +00001660void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00001661 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00001662 PrintExpr(E->getSubExpr());
1663}
1664
Mike Stump11289f42009-09-09 15:08:12 +00001665void
Douglas Gregorce934142009-05-20 18:46:25 +00001666StmtPrinter::VisitCXXUnresolvedConstructExpr(
1667 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001668 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001669 OS << "(";
1670 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001671 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001672 Arg != ArgEnd; ++Arg) {
1673 if (Arg != Node->arg_begin())
1674 OS << ", ";
1675 PrintExpr(*Arg);
1676 }
1677 OS << ")";
1678}
1679
John McCall8cd78132009-11-19 22:55:06 +00001680void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1681 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001682 if (!Node->isImplicitAccess()) {
1683 PrintExpr(Node->getBase());
1684 OS << (Node->isArrow() ? "->" : ".");
1685 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001686 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1687 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001688 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001689 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001690 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001691 if (Node->hasExplicitTemplateArgs())
1692 TemplateSpecializationType::PrintTemplateArgumentList(
1693 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00001694}
1695
John McCall10eae182009-11-30 22:42:35 +00001696void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001697 if (!Node->isImplicitAccess()) {
1698 PrintExpr(Node->getBase());
1699 OS << (Node->isArrow() ? "->" : ".");
1700 }
John McCall10eae182009-11-30 22:42:35 +00001701 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1702 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001703 if (Node->hasTemplateKeyword())
1704 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001705 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001706 if (Node->hasExplicitTemplateArgs())
1707 TemplateSpecializationType::PrintTemplateArgumentList(
1708 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00001709}
1710
Douglas Gregor29c42f22012-02-24 07:38:34 +00001711static const char *getTypeTraitName(TypeTrait TT) {
1712 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00001713#define TYPE_TRAIT_1(Spelling, Name, Key) \
1714case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00001715#define TYPE_TRAIT_2(Spelling, Name, Key) \
1716case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00001717#define TYPE_TRAIT_N(Spelling, Name, Key) \
1718 case clang::TT_##Name: return #Spelling;
1719#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00001720 }
1721 llvm_unreachable("Type trait not covered by switch");
1722}
1723
John Wiegley6242b6a2011-04-28 00:16:57 +00001724static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1725 switch (ATT) {
1726 case ATT_ArrayRank: return "__array_rank";
1727 case ATT_ArrayExtent: return "__array_extent";
1728 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001729 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001730}
1731
John Wiegleyf9f65842011-04-25 06:54:41 +00001732static const char *getExpressionTraitName(ExpressionTrait ET) {
1733 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001734 case ET_IsLValueExpr: return "__is_lvalue_expr";
1735 case ET_IsRValueExpr: return "__is_rvalue_expr";
1736 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001737 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001738}
1739
Douglas Gregor29c42f22012-02-24 07:38:34 +00001740void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1741 OS << getTypeTraitName(E->getTrait()) << "(";
1742 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1743 if (I > 0)
1744 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001745 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00001746 }
1747 OS << ")";
1748}
1749
John Wiegley6242b6a2011-04-28 00:16:57 +00001750void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001751 OS << getTypeTraitName(E->getTrait()) << '(';
1752 E->getQueriedType().print(OS, Policy);
1753 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00001754}
1755
John Wiegleyf9f65842011-04-25 06:54:41 +00001756void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001757 OS << getExpressionTraitName(E->getTrait()) << '(';
1758 PrintExpr(E->getQueriedExpression());
1759 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00001760}
1761
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001762void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1763 OS << "noexcept(";
1764 PrintExpr(E->getOperand());
1765 OS << ")";
1766}
1767
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001768void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001769 PrintExpr(E->getPattern());
1770 OS << "...";
1771}
1772
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001773void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001774 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001775}
1776
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001777void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1778 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001779 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001780}
1781
John McCall7c454bb2011-07-15 05:09:51 +00001782void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1783 SubstNonTypeTemplateParmExpr *Node) {
1784 Visit(Node->getReplacement());
1785}
1786
Richard Smithb15fe3a2012-09-12 00:56:43 +00001787void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1788 OS << *E->getParameterPack();
1789}
1790
Douglas Gregorfe314812011-06-21 17:03:29 +00001791void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1792 PrintExpr(Node->GetTemporaryExpr());
1793}
1794
Mike Stump11289f42009-09-09 15:08:12 +00001795// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001796
1797void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1798 OS << "@";
1799 VisitStringLiteral(Node->getString());
1800}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001801
Patrick Beard0caa3942012-04-19 00:25:12 +00001802void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001803 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00001804 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001805}
1806
1807void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1808 OS << "@[ ";
1809 StmtRange ch = E->children();
1810 if (ch.first != ch.second) {
1811 while (1) {
1812 Visit(*ch.first);
1813 ++ch.first;
1814 if (ch.first == ch.second) break;
1815 OS << ", ";
1816 }
1817 }
1818 OS << " ]";
1819}
1820
1821void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1822 OS << "@{ ";
1823 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1824 if (I > 0)
1825 OS << ", ";
1826
1827 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1828 Visit(Element.Key);
1829 OS << " : ";
1830 Visit(Element.Value);
1831 if (Element.isPackExpansion())
1832 OS << "...";
1833 }
1834 OS << " }";
1835}
1836
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001837void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001838 OS << "@encode(";
1839 Node->getEncodedType().print(OS, Policy);
1840 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001841}
1842
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001843void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001844 OS << "@selector(";
1845 Node->getSelector().print(OS);
1846 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001847}
1848
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001849void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001850 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001851}
1852
Steve Naroffd54978b2007-09-18 23:55:05 +00001853void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1854 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001855 switch (Mess->getReceiverKind()) {
1856 case ObjCMessageExpr::Instance:
1857 PrintExpr(Mess->getInstanceReceiver());
1858 break;
1859
1860 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001861 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00001862 break;
1863
1864 case ObjCMessageExpr::SuperInstance:
1865 case ObjCMessageExpr::SuperClass:
1866 OS << "Super";
1867 break;
1868 }
1869
Ted Kremeneka06e7122008-05-02 17:32:38 +00001870 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001871 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001872 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001873 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001874 } else {
1875 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001876 if (i < selector.getNumArgs()) {
1877 if (i > 0) OS << ' ';
1878 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001879 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001880 else
1881 OS << ":";
1882 }
1883 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001884
Steve Naroffc6814ea2007-10-02 20:01:56 +00001885 PrintExpr(Mess->getArg(i));
1886 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001887 }
1888 OS << "]";
1889}
1890
Ted Kremeneke65b0862012-03-06 20:05:56 +00001891void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1892 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1893}
1894
John McCall31168b02011-06-15 23:02:42 +00001895void
1896StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1897 PrintExpr(E->getSubExpr());
1898}
1899
1900void
1901StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001902 OS << '(' << E->getBridgeKindName();
1903 E->getType().print(OS, Policy);
1904 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00001905 PrintExpr(E->getSubExpr());
1906}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001907
Steve Naroffc540d662008-09-03 18:15:37 +00001908void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001909 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001910 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001911
Steve Naroffc540d662008-09-03 18:15:37 +00001912 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001913
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001914 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001915 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001916 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001917 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00001918 for (BlockDecl::param_iterator AI = BD->param_begin(),
1919 E = BD->param_end(); AI != E; ++AI) {
1920 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001921 std::string ParamStr = (*AI)->getNameAsString();
1922 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00001923 }
Mike Stump11289f42009-09-09 15:08:12 +00001924
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001925 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001926 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001927 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001928 OS << "...";
1929 }
1930 OS << ')';
1931 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00001932 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00001933}
1934
Ted Kremenekf551f322011-11-30 22:08:08 +00001935void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1936 PrintExpr(Node->getSourceExpr());
1937}
John McCall8d69a212010-11-15 23:31:06 +00001938
Tanya Lattner55808c12011-06-04 00:47:47 +00001939void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1940 OS << "__builtin_astype(";
1941 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001942 OS << ", ";
1943 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00001944 OS << ")";
1945}
1946
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001947//===----------------------------------------------------------------------===//
1948// Stmt method implementations
1949//===----------------------------------------------------------------------===//
1950
Craig Topperc571c812013-08-22 06:02:26 +00001951void Stmt::dumpPretty(const ASTContext &Context) const {
Richard Smith235341b2012-08-16 03:56:14 +00001952 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001953}
1954
Richard Smith235341b2012-08-16 03:56:14 +00001955void Stmt::printPretty(raw_ostream &OS,
1956 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001957 const PrintingPolicy &Policy,
1958 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001959 if (this == 0) {
1960 OS << "<NULL>";
1961 return;
1962 }
1963
Richard Smith235341b2012-08-16 03:56:14 +00001964 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001965 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001966}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001967
1968//===----------------------------------------------------------------------===//
1969// PrinterHelper
1970//===----------------------------------------------------------------------===//
1971
1972// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001973PrinterHelper::~PrinterHelper() {}