blob: 68f78f152faa9be87d97ce6518f95a5705775873 [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);
Mike Stump11289f42009-09-09 15:08:12 +000073
Chris Lattner882f7882006-11-04 18:52:07 +000074 void PrintExpr(Expr *E) {
75 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000076 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000077 else
Chris Lattner882f7882006-11-04 18:52:07 +000078 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000079 }
Mike Stump11289f42009-09-09 15:08:12 +000080
Chris Lattner0e62c1c2011-07-23 10:55:15 +000081 raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000082 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
83 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000084 return OS;
85 }
Mike Stump11289f42009-09-09 15:08:12 +000086
Mike Stump11289f42009-09-09 15:08:12 +000087 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000088 if (Helper && Helper->handledStmt(S,OS))
89 return;
90 else StmtVisitor<StmtPrinter>::Visit(S);
91 }
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000092
Chandler Carruthb7967b92010-10-23 08:21:37 +000093 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000094 Indent() << "<<unknown stmt type>>\n";
95 }
Chandler Carruthb7967b92010-10-23 08:21:37 +000096 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000097 OS << "<<unknown expr type>>";
98 }
99 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000100
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000101#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +0000102#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +0000103 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000104#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000105 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000106}
107
Chris Lattner71e23ce2006-11-04 20:18:38 +0000108//===----------------------------------------------------------------------===//
109// Stmt printing methods.
110//===----------------------------------------------------------------------===//
111
Chris Lattner073926e2007-05-20 23:04:55 +0000112/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
113/// with no newline after the }.
114void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
115 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000116 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000117 I != E; ++I)
118 PrintStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattner073926e2007-05-20 23:04:55 +0000120 Indent() << "}";
121}
122
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000123void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000124 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000125}
126
Eli Friedmanf86e5072012-10-16 23:45:15 +0000127void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
128 DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000129 SmallVector<Decl*, 2> Decls;
Mike Stump11289f42009-09-09 15:08:12 +0000130 for ( ; Begin != End; ++Begin)
Eli Friedman79635842009-05-30 04:20:30 +0000131 Decls.push_back(*Begin);
Eli Friedman15ea8802009-05-30 00:19:54 +0000132
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000133 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000134}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000135
136void StmtPrinter::VisitNullStmt(NullStmt *Node) {
137 Indent() << ";\n";
138}
139
140void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000141 Indent();
142 PrintRawDeclStmt(Node);
143 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000144}
145
Chris Lattner073926e2007-05-20 23:04:55 +0000146void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
147 Indent();
148 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000149 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000150}
151
Chris Lattner6c0ff132006-11-05 00:19:50 +0000152void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000153 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000154 PrintExpr(Node->getLHS());
155 if (Node->getRHS()) {
156 OS << " ... ";
157 PrintExpr(Node->getRHS());
158 }
159 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000160
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000161 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000162}
163
164void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000165 Indent(-1) << "default:\n";
166 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000167}
168
169void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000170 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000171 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000172}
173
Richard Smithc202b282012-04-14 00:33:13 +0000174void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
175 OS << "[[";
176 bool first = true;
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000177 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
178 end = Node->getAttrs().end();
179 it != end; ++it) {
Richard Smithc202b282012-04-14 00:33:13 +0000180 if (!first) {
181 OS << ", ";
182 first = false;
183 }
184 // TODO: check this
Richard Smith52f04a22012-08-16 02:43:29 +0000185 (*it)->printPretty(OS, Policy);
Richard Smithc202b282012-04-14 00:33:13 +0000186 }
187 OS << "]] ";
188 PrintStmt(Node->getSubStmt(), 0);
189}
190
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000191void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000192 OS << "if (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000193 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
194 PrintRawDeclStmt(DS);
195 else
196 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000197 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000198
Chris Lattner073926e2007-05-20 23:04:55 +0000199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
200 OS << ' ';
201 PrintRawCompoundStmt(CS);
202 OS << (If->getElse() ? ' ' : '\n');
203 } else {
204 OS << '\n';
205 PrintStmt(If->getThen());
206 if (If->getElse()) Indent();
207 }
Mike Stump11289f42009-09-09 15:08:12 +0000208
Chris Lattner073926e2007-05-20 23:04:55 +0000209 if (Stmt *Else = If->getElse()) {
210 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000211
Chris Lattner073926e2007-05-20 23:04:55 +0000212 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
213 OS << ' ';
214 PrintRawCompoundStmt(CS);
215 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000216 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
217 OS << ' ';
218 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000219 } else {
220 OS << '\n';
221 PrintStmt(If->getElse());
222 }
Chris Lattner882f7882006-11-04 18:52:07 +0000223 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000224}
225
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000226void StmtPrinter::VisitIfStmt(IfStmt *If) {
227 Indent();
228 PrintRawIfStmt(If);
229}
230
Chris Lattnerf2174b62006-11-04 20:59:27 +0000231void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
232 Indent() << "switch (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000233 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
234 PrintRawDeclStmt(DS);
235 else
236 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000237 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000238
Chris Lattner073926e2007-05-20 23:04:55 +0000239 // Pretty print compoundstmt bodies (very common).
240 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
241 OS << " ";
242 PrintRawCompoundStmt(CS);
243 OS << "\n";
244 } else {
245 OS << "\n";
246 PrintStmt(Node->getBody());
247 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000248}
249
Chris Lattner85ed8732006-11-04 20:40:44 +0000250void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
251 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000252 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
253 PrintRawDeclStmt(DS);
254 else
255 PrintExpr(Node->getCond());
Chris Lattner85ed8732006-11-04 20:40:44 +0000256 OS << ")\n";
257 PrintStmt(Node->getBody());
258}
259
260void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000261 Indent() << "do ";
262 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
263 PrintRawCompoundStmt(CS);
264 OS << " ";
265 } else {
266 OS << "\n";
267 PrintStmt(Node->getBody());
268 Indent();
269 }
Mike Stump11289f42009-09-09 15:08:12 +0000270
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000271 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000272 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000273 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000274}
275
Chris Lattner71e23ce2006-11-04 20:18:38 +0000276void StmtPrinter::VisitForStmt(ForStmt *Node) {
277 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000278 if (Node->getInit()) {
279 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000280 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000281 else
282 PrintExpr(cast<Expr>(Node->getInit()));
283 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000284 OS << ";";
285 if (Node->getCond()) {
286 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000287 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000288 }
289 OS << ";";
290 if (Node->getInc()) {
291 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000292 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000293 }
294 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000295
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000296 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
297 PrintRawCompoundStmt(CS);
298 OS << "\n";
299 } else {
300 OS << "\n";
301 PrintStmt(Node->getBody());
302 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000303}
304
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000305void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000306 Indent() << "for (";
307 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000308 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000309 else
310 PrintExpr(cast<Expr>(Node->getElement()));
311 OS << " in ";
312 PrintExpr(Node->getCollection());
313 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000314
Fariborz Jahanian83615522008-01-02 22:54:34 +0000315 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
316 PrintRawCompoundStmt(CS);
317 OS << "\n";
318 } else {
319 OS << "\n";
320 PrintStmt(Node->getBody());
321 }
322}
323
Richard Smith02e85f32011-04-14 22:09:26 +0000324void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
325 Indent() << "for (";
326 PrintingPolicy SubPolicy(Policy);
327 SubPolicy.SuppressInitializers = true;
328 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
329 OS << " : ";
330 PrintExpr(Node->getRangeInit());
331 OS << ") {\n";
332 PrintStmt(Node->getBody());
Ted Kremenek88446602013-12-11 23:44:02 +0000333 Indent() << "}";
334 if (Policy.IncludeNewlines) OS << "\n";
Richard Smith02e85f32011-04-14 22:09:26 +0000335}
336
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000337void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
338 Indent();
339 if (Node->isIfExists())
340 OS << "__if_exists (";
341 else
342 OS << "__if_not_exists (";
343
344 if (NestedNameSpecifier *Qualifier
345 = Node->getQualifierLoc().getNestedNameSpecifier())
346 Qualifier->print(OS, Policy);
347
348 OS << Node->getNameInfo() << ") ";
349
350 PrintRawCompoundStmt(Node->getSubStmt());
351}
352
Chris Lattner16976d32006-11-05 01:46:01 +0000353void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000354 Indent() << "goto " << Node->getLabel()->getName() << ";";
355 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000356}
357
358void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000359 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000360 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000361 OS << ";";
362 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000363}
364
365void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000366 Indent() << "continue;";
367 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000368}
369
370void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000371 Indent() << "break;";
372 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000373}
374
375
Chris Lattner882f7882006-11-04 18:52:07 +0000376void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
377 Indent() << "return";
378 if (Node->getRetValue()) {
379 OS << " ";
380 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000381 }
Ted Kremenek88446602013-12-11 23:44:02 +0000382 OS << ";";
383 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000384}
385
Chris Lattner73c56c02007-10-29 04:04:16 +0000386
Chad Rosierde70e0e2012-08-25 00:11:56 +0000387void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000388 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000389
Anders Carlsson660bdd12007-11-23 23:12:25 +0000390 if (Node->isVolatile())
391 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000392
Anders Carlsson660bdd12007-11-23 23:12:25 +0000393 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000394 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000395
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000396 // Outputs
397 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
398 Node->getNumClobbers() != 0)
399 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000400
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000401 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
402 if (i != 0)
403 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000404
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000405 if (!Node->getOutputName(i).empty()) {
406 OS << '[';
407 OS << Node->getOutputName(i);
408 OS << "] ";
409 }
Mike Stump11289f42009-09-09 15:08:12 +0000410
Chris Lattner72bbf172009-03-10 04:59:06 +0000411 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000412 OS << " ";
413 Visit(Node->getOutputExpr(i));
414 }
Mike Stump11289f42009-09-09 15:08:12 +0000415
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000416 // Inputs
417 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
418 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000419
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000420 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
421 if (i != 0)
422 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000423
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000424 if (!Node->getInputName(i).empty()) {
425 OS << '[';
426 OS << Node->getInputName(i);
427 OS << "] ";
428 }
Mike Stump11289f42009-09-09 15:08:12 +0000429
Chris Lattner72bbf172009-03-10 04:59:06 +0000430 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000431 OS << " ";
432 Visit(Node->getInputExpr(i));
433 }
Mike Stump11289f42009-09-09 15:08:12 +0000434
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000435 // Clobbers
436 if (Node->getNumClobbers() != 0)
437 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000438
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000439 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
440 if (i != 0)
441 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000442
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000443 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Ted Kremenek88446602013-12-11 23:44:02 +0000446 OS << ");";
447 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000448}
449
Chad Rosier32503022012-06-11 20:47:18 +0000450void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
451 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000452 Indent() << "__asm ";
453 if (Node->hasBraces())
454 OS << "{\n";
John McCallf413f5e2013-05-03 00:10:13 +0000455 OS << Node->getAsmString() << "\n";
Chad Rosierb6f46c12012-08-15 16:53:30 +0000456 if (Node->hasBraces())
457 Indent() << "}\n";
Chad Rosier32503022012-06-11 20:47:18 +0000458}
459
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000460void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000461 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000462}
463
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000464void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000465 Indent() << "@try";
466 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
467 PrintRawCompoundStmt(TS);
468 OS << "\n";
469 }
Mike Stump11289f42009-09-09 15:08:12 +0000470
Douglas Gregor96c79492010-04-23 22:50:49 +0000471 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
472 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000473 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000474 if (catchStmt->getCatchParamDecl()) {
475 if (Decl *DS = catchStmt->getCatchParamDecl())
476 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000477 }
478 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000479 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
480 PrintRawCompoundStmt(CS);
481 OS << "\n";
482 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
485 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
486 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000487 Indent() << "@finally";
488 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000489 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000490 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000491}
492
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000493void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000494}
495
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000496void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000497 Indent() << "@catch (...) { /* todo */ } \n";
498}
499
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000500void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000501 Indent() << "@throw";
502 if (Node->getThrowExpr()) {
503 OS << " ";
504 PrintExpr(Node->getThrowExpr());
505 }
506 OS << ";\n";
507}
508
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000509void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000510 Indent() << "@synchronized (";
511 PrintExpr(Node->getSynchExpr());
512 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000513 PrintRawCompoundStmt(Node->getSynchBody());
514 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000515}
516
John McCall31168b02011-06-15 23:02:42 +0000517void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
518 Indent() << "@autoreleasepool";
519 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
520 OS << "\n";
521}
522
Sebastian Redl9b244a82008-12-22 21:35:02 +0000523void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
524 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000525 if (Decl *ExDecl = Node->getExceptionDecl())
526 PrintRawDecl(ExDecl);
527 else
528 OS << "...";
529 OS << ") ";
530 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000531}
532
533void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
534 Indent();
535 PrintRawCXXCatchStmt(Node);
536 OS << "\n";
537}
538
539void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
540 Indent() << "try ";
541 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000542 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000543 OS << " ";
544 PrintRawCXXCatchStmt(Node->getHandler(i));
545 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000546 OS << "\n";
547}
548
John Wiegley1c0675e2011-04-28 01:08:34 +0000549void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
550 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
551 PrintRawCompoundStmt(Node->getTryBlock());
552 SEHExceptStmt *E = Node->getExceptHandler();
553 SEHFinallyStmt *F = Node->getFinallyHandler();
554 if(E)
555 PrintRawSEHExceptHandler(E);
556 else {
557 assert(F && "Must have a finally block...");
558 PrintRawSEHFinallyStmt(F);
559 }
560 OS << "\n";
561}
562
563void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
564 OS << "__finally ";
565 PrintRawCompoundStmt(Node->getBlock());
566 OS << "\n";
567}
568
569void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
570 OS << "__except (";
571 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000572 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000573 PrintRawCompoundStmt(Node->getBlock());
574 OS << "\n";
575}
576
577void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
578 Indent();
579 PrintRawSEHExceptHandler(Node);
580 OS << "\n";
581}
582
583void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
584 Indent();
585 PrintRawSEHFinallyStmt(Node);
586 OS << "\n";
587}
588
Chris Lattner71e23ce2006-11-04 20:18:38 +0000589//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000590// OpenMP clauses printing methods
591//===----------------------------------------------------------------------===//
592
593namespace {
594class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
595 raw_ostream &OS;
Alexey Bataev756c1962013-09-24 03:17:45 +0000596 /// \brief Process clauses with list of variables.
597 template <typename T>
598 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000599public:
600 OMPClausePrinter(raw_ostream &OS) : OS(OS) { }
601#define OPENMP_CLAUSE(Name, Class) \
602 void Visit##Class(Class *S);
603#include "clang/Basic/OpenMPKinds.def"
604};
605
606void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
607 OS << "default("
608 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
609 << ")";
610}
611
Alexey Bataev756c1962013-09-24 03:17:45 +0000612template<typename T>
613void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
614 for (typename T::varlist_iterator I = Node->varlist_begin(),
615 E = Node->varlist_end();
616 I != E; ++I)
617 OS << (I == Node->varlist_begin() ? StartSym : ',')
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000618 << *cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
Alexey Bataev756c1962013-09-24 03:17:45 +0000619}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000620
621void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
622 if (!Node->varlist_empty()) {
623 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000624 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000625 OS << ")";
626 }
627}
628
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000629void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
630 if (!Node->varlist_empty()) {
631 OS << "firstprivate";
632 VisitOMPClauseList(Node, '(');
633 OS << ")";
634 }
635}
636
Alexey Bataev758e55e2013-09-06 18:03:48 +0000637void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
638 if (!Node->varlist_empty()) {
639 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000640 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000641 OS << ")";
642 }
643}
644
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000645}
646
647//===----------------------------------------------------------------------===//
648// OpenMP directives printing methods
649//===----------------------------------------------------------------------===//
650
651void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
652 Indent() << "#pragma omp parallel ";
653
654 OMPClausePrinter Printer(OS);
655 ArrayRef<OMPClause *> Clauses = Node->clauses();
656 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
657 I != E; ++I)
658 if (*I && !(*I)->isImplicit()) {
659 Printer.Visit(*I);
660 OS << ' ';
661 }
662 OS << "\n";
663 if (Node->getAssociatedStmt()) {
664 assert(isa<CapturedStmt>(Node->getAssociatedStmt()) &&
665 "Expected captured statement!");
666 Stmt *CS = cast<CapturedStmt>(Node->getAssociatedStmt())->getCapturedStmt();
667 PrintStmt(CS);
668 }
669}
670//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +0000671// Expr printing methods.
672//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000673
Chris Lattner882f7882006-11-04 18:52:07 +0000674void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000675 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
676 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000677 if (Node->hasTemplateKeyword())
678 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000679 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000680 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000681 TemplateSpecializationType::PrintTemplateArgumentList(
682 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000683}
684
John McCall8cd78132009-11-19 22:55:06 +0000685void StmtPrinter::VisitDependentScopeDeclRefExpr(
686 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000687 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
688 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000689 if (Node->hasTemplateKeyword())
690 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000691 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000692 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000693 TemplateSpecializationType::PrintTemplateArgumentList(
694 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000695}
696
John McCalld14a8642009-11-21 08:51:07 +0000697void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000698 if (Node->getQualifier())
699 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000700 if (Node->hasTemplateKeyword())
701 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000702 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000703 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000704 TemplateSpecializationType::PrintTemplateArgumentList(
705 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000706}
707
Steve Naroffe46504b2007-11-12 14:29:37 +0000708void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000709 if (Node->getBase()) {
710 PrintExpr(Node->getBase());
711 OS << (Node->isArrow() ? "->" : ".");
712 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000713 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000714}
715
Steve Naroffec944032008-05-30 00:40:33 +0000716void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000717 if (Node->isSuperReceiver())
718 OS << "super.";
719 else if (Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000720 PrintExpr(Node->getBase());
721 OS << ".";
722 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000723
John McCallb7bd14f2010-12-02 01:19:52 +0000724 if (Node->isImplicitProperty())
725 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
726 else
727 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000728}
729
Ted Kremeneke65b0862012-03-06 20:05:56 +0000730void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
731
732 PrintExpr(Node->getBaseExpr());
733 OS << "[";
734 PrintExpr(Node->getKeyExpr());
735 OS << "]";
736}
737
Chris Lattner6307f192008-08-10 01:53:14 +0000738void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000739 switch (Node->getIdentType()) {
740 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000741 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000742 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000743 OS << "__func__";
744 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000745 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000746 OS << "__FUNCTION__";
747 break;
David Majnemerbed356a2013-11-06 23:31:56 +0000748 case PredefinedExpr::FuncDName:
749 OS << "__FUNCDNAME__";
750 break;
Nico Weber3a691a32012-06-23 02:07:59 +0000751 case PredefinedExpr::LFunction:
752 OS << "L__FUNCTION__";
753 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000754 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000755 OS << "__PRETTY_FUNCTION__";
756 break;
757 }
758}
759
Steve Naroffae4143e2007-04-26 20:39:23 +0000760void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000761 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000762
763 switch (Node->getKind()) {
764 case CharacterLiteral::Ascii: break; // no prefix.
765 case CharacterLiteral::Wide: OS << 'L'; break;
766 case CharacterLiteral::UTF16: OS << 'u'; break;
767 case CharacterLiteral::UTF32: OS << 'U'; break;
768 }
769
Chris Lattner666115c2007-07-13 23:58:20 +0000770 switch (value) {
771 case '\\':
772 OS << "'\\\\'";
773 break;
774 case '\'':
775 OS << "'\\''";
776 break;
777 case '\a':
778 // TODO: K&R: the meaning of '\\a' is different in traditional C
779 OS << "'\\a'";
780 break;
781 case '\b':
782 OS << "'\\b'";
783 break;
784 // Nonstandard escape sequence.
785 /*case '\e':
786 OS << "'\\e'";
787 break;*/
788 case '\f':
789 OS << "'\\f'";
790 break;
791 case '\n':
792 OS << "'\\n'";
793 break;
794 case '\r':
795 OS << "'\\r'";
796 break;
797 case '\t':
798 OS << "'\\t'";
799 break;
800 case '\v':
801 OS << "'\\v'";
802 break;
803 default:
Jordan Rose00d1b592013-02-08 22:30:27 +0000804 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +0000805 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +0000806 else if (value < 256)
807 OS << "'\\x" << llvm::format("%02x", value) << "'";
808 else if (value <= 0xFFFF)
809 OS << "'\\u" << llvm::format("%04x", value) << "'";
810 else
811 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000812 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000813}
814
Steve Naroffdf7855b2007-02-21 23:46:25 +0000815void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000816 bool isSigned = Node->getType()->isSignedIntegerType();
817 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000818
Chris Lattner06430412007-05-21 05:45:03 +0000819 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000820 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000821 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000822 // FIXME: The Short and UShort cases are to handle cases where a short
823 // integeral literal is formed during template instantiation. They should
824 // be removed when template instantiation no longer needs integer literals.
825 case BuiltinType::Short:
826 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000827 case BuiltinType::Int: break; // no suffix.
828 case BuiltinType::UInt: OS << 'U'; break;
829 case BuiltinType::Long: OS << 'L'; break;
830 case BuiltinType::ULong: OS << "UL"; break;
831 case BuiltinType::LongLong: OS << "LL"; break;
832 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000833 case BuiltinType::Int128: OS << "i128"; break;
834 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000835 }
Chris Lattner882f7882006-11-04 18:52:07 +0000836}
Benjamin Kramer8a526762012-09-20 14:07:17 +0000837
838static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
839 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000840 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +0000841 Node->getValue().toString(Str);
842 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +0000843 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
844 OS << '.'; // Trailing dot in order to separate from ints.
845
846 if (!PrintSuffix)
847 return;
848
849 // Emit suffixes. Float literals are always a builtin float type.
850 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
851 default: llvm_unreachable("Unexpected type for float literal!");
852 case BuiltinType::Half: break; // FIXME: suffix?
853 case BuiltinType::Double: break; // no suffix.
854 case BuiltinType::Float: OS << 'F'; break;
855 case BuiltinType::LongDouble: OS << 'L'; break;
856 }
857}
858
859void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
860 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +0000861}
Chris Lattner1c20a172007-08-26 03:42:43 +0000862
863void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
864 PrintExpr(Node->getSubExpr());
865 OS << "i";
866}
867
Steve Naroffdf7855b2007-02-21 23:46:25 +0000868void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +0000869 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +0000870}
871void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
872 OS << "(";
873 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000874 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000875}
876void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000877 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000878 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000879
Eli Friedman1cf25362009-06-14 22:39:26 +0000880 // Print a space if this is an "identifier operator" like __real, or if
881 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000882 switch (Node->getOpcode()) {
883 default: break;
John McCalle3027922010-08-25 11:45:40 +0000884 case UO_Real:
885 case UO_Imag:
886 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000887 OS << ' ';
888 break;
John McCalle3027922010-08-25 11:45:40 +0000889 case UO_Plus:
890 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000891 if (isa<UnaryOperator>(Node->getSubExpr()))
892 OS << ' ';
893 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000894 }
895 }
Chris Lattner882f7882006-11-04 18:52:07 +0000896 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000897
Chris Lattner15768702006-11-05 23:54:51 +0000898 if (Node->isPostfix())
899 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000900}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000901
Douglas Gregor882211c2010-04-28 22:16:22 +0000902void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
903 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000904 Node->getTypeSourceInfo()->getType().print(OS, Policy);
905 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000906 bool PrintedSomething = false;
907 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
908 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
909 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
910 // Array node
911 OS << "[";
912 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
913 OS << "]";
914 PrintedSomething = true;
915 continue;
916 }
Douglas Gregord1702062010-04-29 00:18:15 +0000917
918 // Skip implicit base indirections.
919 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
920 continue;
921
Douglas Gregor882211c2010-04-28 22:16:22 +0000922 // Field or identifier node.
923 IdentifierInfo *Id = ON.getFieldName();
924 if (!Id)
925 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000926
Douglas Gregor882211c2010-04-28 22:16:22 +0000927 if (PrintedSomething)
928 OS << ".";
929 else
930 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000931 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000932 }
933 OS << ")";
934}
935
Peter Collingbournee190dee2011-03-11 19:24:49 +0000936void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
937 switch(Node->getKind()) {
938 case UETT_SizeOf:
939 OS << "sizeof";
940 break;
941 case UETT_AlignOf:
Jordan Rose58d54722012-06-30 21:33:57 +0000942 if (Policy.LangOpts.CPlusPlus)
943 OS << "alignof";
944 else if (Policy.LangOpts.C11)
945 OS << "_Alignof";
946 else
947 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +0000948 break;
949 case UETT_VecStep:
950 OS << "vec_step";
951 break;
952 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000953 if (Node->isArgumentType()) {
954 OS << '(';
955 Node->getArgumentType().print(OS, Policy);
956 OS << ')';
957 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +0000958 OS << " ";
959 PrintExpr(Node->getArgumentExpr());
960 }
Chris Lattner882f7882006-11-04 18:52:07 +0000961}
Peter Collingbourne91147592011-04-15 00:35:48 +0000962
963void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
964 OS << "_Generic(";
965 PrintExpr(Node->getControllingExpr());
966 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
967 OS << ", ";
968 QualType T = Node->getAssocType(i);
969 if (T.isNull())
970 OS << "default";
971 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000972 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +0000973 OS << ": ";
974 PrintExpr(Node->getAssocExpr(i));
975 }
976 OS << ")";
977}
978
Chris Lattner882f7882006-11-04 18:52:07 +0000979void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000980 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000981 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000982 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000983 OS << "]";
984}
985
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000986void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +0000987 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000988 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
989 // Don't print any defaulted arguments
990 break;
991 }
992
Chris Lattner882f7882006-11-04 18:52:07 +0000993 if (i) OS << ", ";
994 PrintExpr(Call->getArg(i));
995 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000996}
997
998void StmtPrinter::VisitCallExpr(CallExpr *Call) {
999 PrintExpr(Call->getCallee());
1000 OS << "(";
1001 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001002 OS << ")";
1003}
1004void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +00001005 // FIXME: Suppress printing implicit bases (like "this")
1006 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001007
1008 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +00001009 FieldDecl *ParentDecl = ParentMember
1010 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
David Blaikie8fab8e52012-11-12 19:12:12 +00001011
David Blaikie6bffd6f2012-11-12 19:32:32 +00001012 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +00001013 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +00001014
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001015 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1016 if (FD->isAnonymousStructOrUnion())
1017 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001018
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001019 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1020 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001021 if (Node->hasTemplateKeyword())
1022 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001023 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001024 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001025 TemplateSpecializationType::PrintTemplateArgumentList(
1026 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001027}
Steve Naroffe87026a2009-07-24 17:54:45 +00001028void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1029 PrintExpr(Node->getBase());
1030 OS << (Node->isArrow() ? "->isa" : ".isa");
1031}
1032
Nate Begemance4d7fc2008-04-18 23:10:10 +00001033void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001034 PrintExpr(Node->getBase());
1035 OS << ".";
1036 OS << Node->getAccessor().getName();
1037}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001038void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001039 OS << '(';
1040 Node->getTypeAsWritten().print(OS, Policy);
1041 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001042 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001043}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001044void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001045 OS << '(';
1046 Node->getType().print(OS, Policy);
1047 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001048 PrintExpr(Node->getInitializer());
1049}
Steve Naroff7a5af782007-07-13 16:58:59 +00001050void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001051 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001052 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001053}
Chris Lattner882f7882006-11-04 18:52:07 +00001054void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1055 PrintExpr(Node->getLHS());
1056 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1057 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001058}
Chris Lattner86928112007-08-25 02:00:02 +00001059void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1060 PrintExpr(Node->getLHS());
1061 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1062 PrintExpr(Node->getRHS());
1063}
Chris Lattner882f7882006-11-04 18:52:07 +00001064void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1065 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001066 OS << " ? ";
1067 PrintExpr(Node->getLHS());
1068 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001069 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001070}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001071
Chris Lattnereefa10e2007-05-28 06:56:27 +00001072// GNU extensions.
1073
John McCallc07a0c72011-02-17 10:25:35 +00001074void
1075StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1076 PrintExpr(Node->getCommon());
1077 OS << " ?: ";
1078 PrintExpr(Node->getFalseExpr());
1079}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001080void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001081 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001082}
1083
Chris Lattner366727f2007-07-24 16:58:17 +00001084void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1085 OS << "(";
1086 PrintRawCompoundStmt(E->getSubStmt());
1087 OS << ")";
1088}
1089
Steve Naroff9efdabc2007-08-03 21:21:27 +00001090void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1091 OS << "__builtin_choose_expr(";
1092 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001093 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001094 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001095 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001096 PrintExpr(Node->getRHS());
1097 OS << ")";
1098}
Chris Lattner366727f2007-07-24 16:58:17 +00001099
Douglas Gregor3be4b122008-11-29 04:51:27 +00001100void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1101 OS << "__null";
1102}
1103
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001104void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1105 OS << "__builtin_shufflevector(";
1106 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1107 if (i) OS << ", ";
1108 PrintExpr(Node->getExpr(i));
1109 }
1110 OS << ")";
1111}
1112
Hal Finkelc4d7c822013-09-18 03:29:45 +00001113void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1114 OS << "__builtin_convertvector(";
1115 PrintExpr(Node->getSrcExpr());
1116 OS << ", ";
1117 Node->getType().print(OS, Policy);
1118 OS << ")";
1119}
1120
Anders Carlsson4692db02007-08-31 04:56:16 +00001121void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001122 if (Node->getSyntacticForm()) {
1123 Visit(Node->getSyntacticForm());
1124 return;
1125 }
1126
Anders Carlsson4692db02007-08-31 04:56:16 +00001127 OS << "{ ";
1128 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1129 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001130 if (Node->getInit(i))
1131 PrintExpr(Node->getInit(i));
1132 else
1133 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +00001134 }
1135 OS << " }";
1136}
1137
Nate Begeman5ec4b312009-08-10 23:49:36 +00001138void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1139 OS << "( ";
1140 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1141 if (i) OS << ", ";
1142 PrintExpr(Node->getExpr(i));
1143 }
1144 OS << " )";
1145}
1146
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001147void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001148 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1149 DEnd = Node->designators_end();
1150 D != DEnd; ++D) {
1151 if (D->isFieldDesignator()) {
1152 if (D->getDotLoc().isInvalid())
1153 OS << D->getFieldName()->getName() << ":";
1154 else
1155 OS << "." << D->getFieldName()->getName();
1156 } else {
1157 OS << "[";
1158 if (D->isArrayDesignator()) {
1159 PrintExpr(Node->getArrayIndex(*D));
1160 } else {
1161 PrintExpr(Node->getArrayRangeStart(*D));
1162 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001163 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001164 }
1165 OS << "]";
1166 }
1167 }
1168
1169 OS << " = ";
1170 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001171}
1172
Douglas Gregor0202cb42009-01-29 17:44:32 +00001173void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001174 if (Policy.LangOpts.CPlusPlus) {
1175 OS << "/*implicit*/";
1176 Node->getType().print(OS, Policy);
1177 OS << "()";
1178 } else {
1179 OS << "/*implicit*/(";
1180 Node->getType().print(OS, Policy);
1181 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001182 if (Node->getType()->isRecordType())
1183 OS << "{}";
1184 else
1185 OS << 0;
1186 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001187}
1188
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001189void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001190 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001191 PrintExpr(Node->getSubExpr());
1192 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001193 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001194 OS << ")";
1195}
1196
John McCallfe96e0b2011-11-06 09:01:30 +00001197void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1198 PrintExpr(Node->getSyntacticForm());
1199}
1200
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001201void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001202 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001203 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001204#define BUILTIN(ID, TYPE, ATTRS)
1205#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1206 case AtomicExpr::AO ## ID: \
1207 Name = #ID "("; \
1208 break;
1209#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001210 }
1211 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001212
1213 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001214 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001215 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1216 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001217 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001218 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001219 }
Richard Smithfeea8832012-04-12 05:08:17 +00001220 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1221 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001222 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001223 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001224 }
Richard Smithfeea8832012-04-12 05:08:17 +00001225 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1226 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001227 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001228 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001229 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001230 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1231 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001232 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001233 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001234 if (Node->isCmpXChg()) {
1235 OS << ", ";
1236 PrintExpr(Node->getOrderFail());
1237 }
1238 OS << ")";
1239}
1240
Chris Lattnereefa10e2007-05-28 06:56:27 +00001241// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001242void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1243 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1244 "",
1245#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1246 Spelling,
1247#include "clang/Basic/OperatorKinds.def"
1248 };
1249
1250 OverloadedOperatorKind Kind = Node->getOperator();
1251 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1252 if (Node->getNumArgs() == 1) {
1253 OS << OpStrings[Kind] << ' ';
1254 PrintExpr(Node->getArg(0));
1255 } else {
1256 PrintExpr(Node->getArg(0));
1257 OS << ' ' << OpStrings[Kind];
1258 }
Eli Friedman8e236422012-10-12 22:45:14 +00001259 } else if (Kind == OO_Arrow) {
1260 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001261 } else if (Kind == OO_Call) {
1262 PrintExpr(Node->getArg(0));
1263 OS << '(';
1264 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1265 if (ArgIdx > 1)
1266 OS << ", ";
1267 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1268 PrintExpr(Node->getArg(ArgIdx));
1269 }
1270 OS << ')';
1271 } else if (Kind == OO_Subscript) {
1272 PrintExpr(Node->getArg(0));
1273 OS << '[';
1274 PrintExpr(Node->getArg(1));
1275 OS << ']';
1276 } else if (Node->getNumArgs() == 1) {
1277 OS << OpStrings[Kind] << ' ';
1278 PrintExpr(Node->getArg(0));
1279 } else if (Node->getNumArgs() == 2) {
1280 PrintExpr(Node->getArg(0));
1281 OS << ' ' << OpStrings[Kind] << ' ';
1282 PrintExpr(Node->getArg(1));
1283 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001284 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001285 }
1286}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001287
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001288void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1289 VisitCallExpr(cast<CallExpr>(Node));
1290}
1291
Peter Collingbourne41f85462011-02-09 21:07:24 +00001292void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1293 PrintExpr(Node->getCallee());
1294 OS << "<<<";
1295 PrintCallArgs(Node->getConfig());
1296 OS << ">>>(";
1297 PrintCallArgs(Node);
1298 OS << ")";
1299}
1300
Douglas Gregore200adc2008-10-27 19:41:14 +00001301void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1302 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001303 Node->getTypeAsWritten().print(OS, Policy);
1304 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001305 PrintExpr(Node->getSubExpr());
1306 OS << ")";
1307}
1308
Douglas Gregore200adc2008-10-27 19:41:14 +00001309void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1310 VisitCXXNamedCastExpr(Node);
1311}
1312
1313void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1314 VisitCXXNamedCastExpr(Node);
1315}
1316
1317void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1318 VisitCXXNamedCastExpr(Node);
1319}
1320
1321void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1322 VisitCXXNamedCastExpr(Node);
1323}
1324
Sebastian Redlc4704762008-11-11 11:37:55 +00001325void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1326 OS << "typeid(";
1327 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001328 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001329 } else {
1330 PrintExpr(Node->getExprOperand());
1331 }
1332 OS << ")";
1333}
1334
Francois Pichet9f4f2072010-09-08 12:20:18 +00001335void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1336 OS << "__uuidof(";
1337 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001338 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001339 } else {
1340 PrintExpr(Node->getExprOperand());
1341 }
1342 OS << ")";
1343}
1344
John McCall5e77d762013-04-16 07:28:30 +00001345void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1346 PrintExpr(Node->getBaseExpr());
1347 if (Node->isArrow())
1348 OS << "->";
1349 else
1350 OS << ".";
1351 if (NestedNameSpecifier *Qualifier =
1352 Node->getQualifierLoc().getNestedNameSpecifier())
1353 Qualifier->print(OS, Policy);
1354 OS << Node->getPropertyDecl()->getDeclName();
1355}
1356
Richard Smithc67fdd42012-03-07 08:35:16 +00001357void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1358 switch (Node->getLiteralOperatorKind()) {
1359 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001360 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001361 break;
1362 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001363 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1364 const TemplateArgumentList *Args =
1365 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1366 assert(Args);
1367 const TemplateArgument &Pack = Args->get(0);
1368 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1369 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001370 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001371 OS << C;
1372 }
1373 break;
1374 }
Richard Smith75025ba2012-03-08 09:02:38 +00001375 case UserDefinedLiteral::LOK_Integer: {
1376 // Print integer literal without suffix.
1377 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1378 OS << Int->getValue().toString(10, /*isSigned*/false);
1379 break;
1380 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001381 case UserDefinedLiteral::LOK_Floating: {
1382 // Print floating literal without suffix.
1383 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1384 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1385 break;
1386 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001387 case UserDefinedLiteral::LOK_String:
1388 case UserDefinedLiteral::LOK_Character:
1389 PrintExpr(Node->getCookedLiteral());
1390 break;
1391 }
1392 OS << Node->getUDSuffix()->getName();
1393}
1394
Chris Lattnereefa10e2007-05-28 06:56:27 +00001395void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1396 OS << (Node->getValue() ? "true" : "false");
1397}
1398
Sebastian Redl576fd422009-05-10 18:38:11 +00001399void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1400 OS << "nullptr";
1401}
1402
Douglas Gregor97a9c812008-11-04 14:32:21 +00001403void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1404 OS << "this";
1405}
1406
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001407void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1408 if (Node->getSubExpr() == 0)
1409 OS << "throw";
1410 else {
1411 OS << "throw ";
1412 PrintExpr(Node->getSubExpr());
1413 }
1414}
1415
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001416void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001417 // Nothing to print: we picked up the default argument.
1418}
1419
1420void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1421 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001422}
1423
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001424void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001425 Node->getType().print(OS, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001426 OS << "(";
1427 PrintExpr(Node->getSubExpr());
1428 OS << ")";
1429}
1430
Anders Carlsson993a4b32009-05-30 20:03:25 +00001431void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1432 PrintExpr(Node->getSubExpr());
1433}
1434
Douglas Gregordd04d332009-01-16 18:33:17 +00001435void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001436 Node->getType().print(OS, Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001437 OS << "(";
1438 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001439 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001440 Arg != ArgEnd; ++Arg) {
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001441 if (Arg->isDefaultArgument())
1442 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001443 if (Arg != Node->arg_begin())
1444 OS << ", ";
1445 PrintExpr(*Arg);
1446 }
1447 OS << ")";
1448}
1449
Douglas Gregore31e6062012-02-07 10:09:13 +00001450void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1451 OS << '[';
1452 bool NeedComma = false;
1453 switch (Node->getCaptureDefault()) {
1454 case LCD_None:
1455 break;
1456
1457 case LCD_ByCopy:
1458 OS << '=';
1459 NeedComma = true;
1460 break;
1461
1462 case LCD_ByRef:
1463 OS << '&';
1464 NeedComma = true;
1465 break;
1466 }
1467 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1468 CEnd = Node->explicit_capture_end();
1469 C != CEnd;
1470 ++C) {
1471 if (NeedComma)
1472 OS << ", ";
1473 NeedComma = true;
1474
1475 switch (C->getCaptureKind()) {
1476 case LCK_This:
1477 OS << "this";
1478 break;
1479
1480 case LCK_ByRef:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001481 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
Douglas Gregore31e6062012-02-07 10:09:13 +00001482 OS << '&';
1483 OS << C->getCapturedVar()->getName();
1484 break;
1485
1486 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00001487 OS << C->getCapturedVar()->getName();
1488 break;
1489 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001490
1491 if (C->isInitCapture())
1492 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00001493 }
1494 OS << ']';
1495
1496 if (Node->hasExplicitParameters()) {
1497 OS << " (";
1498 CXXMethodDecl *Method = Node->getCallOperator();
1499 NeedComma = false;
1500 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1501 PEnd = Method->param_end();
1502 P != PEnd; ++P) {
1503 if (NeedComma) {
1504 OS << ", ";
1505 } else {
1506 NeedComma = true;
1507 }
1508 std::string ParamStr = (*P)->getNameAsString();
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001509 (*P)->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001510 }
1511 if (Method->isVariadic()) {
1512 if (NeedComma)
1513 OS << ", ";
1514 OS << "...";
1515 }
1516 OS << ')';
1517
1518 if (Node->isMutable())
1519 OS << " mutable";
1520
1521 const FunctionProtoType *Proto
1522 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001523 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001524
Bill Wendling44426052012-12-20 19:22:21 +00001525 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00001526
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001527 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001528 if (Node->hasExplicitResultType()) {
1529 OS << " -> ";
1530 Proto->getResultType().print(OS, Policy);
1531 }
Douglas Gregore31e6062012-02-07 10:09:13 +00001532 }
1533
1534 // Print the body.
1535 CompoundStmt *Body = Node->getBody();
1536 OS << ' ';
1537 PrintStmt(Body);
1538}
1539
Douglas Gregor747eb782010-07-08 06:14:04 +00001540void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001541 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001542 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001543 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001544 Node->getType().print(OS, Policy);
1545 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001546}
1547
Sebastian Redlbd150f42008-11-21 19:14:01 +00001548void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1549 if (E->isGlobalNew())
1550 OS << "::";
1551 OS << "new ";
1552 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001553 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001554 OS << "(";
1555 PrintExpr(E->getPlacementArg(0));
1556 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001557 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1558 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001559 OS << ", ";
1560 PrintExpr(E->getPlacementArg(i));
1561 }
1562 OS << ") ";
1563 }
1564 if (E->isParenTypeId())
1565 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001566 std::string TypeS;
1567 if (Expr *Size = E->getArraySize()) {
1568 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001569 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00001570 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001571 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001572 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001573 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001574 if (E->isParenTypeId())
1575 OS << ")";
1576
Sebastian Redl6047f072012-02-16 12:22:20 +00001577 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1578 if (InitStyle) {
1579 if (InitStyle == CXXNewExpr::CallInit)
1580 OS << "(";
1581 PrintExpr(E->getInitializer());
1582 if (InitStyle == CXXNewExpr::CallInit)
1583 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001584 }
1585}
1586
1587void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1588 if (E->isGlobalDelete())
1589 OS << "::";
1590 OS << "delete ";
1591 if (E->isArrayForm())
1592 OS << "[] ";
1593 PrintExpr(E->getArgument());
1594}
1595
Douglas Gregorad8a3362009-09-04 17:36:40 +00001596void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1597 PrintExpr(E->getBase());
1598 if (E->isArrow())
1599 OS << "->";
1600 else
1601 OS << '.';
1602 if (E->getQualifier())
1603 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00001604 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00001605
Douglas Gregor678f90d2010-02-25 01:56:36 +00001606 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1607 OS << II->getName();
1608 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001609 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001610}
1611
Anders Carlsson0781ce72009-04-23 02:32:43 +00001612void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithd59b8322012-12-19 01:39:02 +00001613 if (E->isListInitialization())
1614 OS << "{ ";
1615
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001616 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1617 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1618 // Don't print any defaulted arguments
1619 break;
1620 }
1621
1622 if (i) OS << ", ";
1623 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001624 }
Richard Smithd59b8322012-12-19 01:39:02 +00001625
1626 if (E->isListInitialization())
1627 OS << " }";
Anders Carlsson0781ce72009-04-23 02:32:43 +00001628}
1629
Richard Smithcc1b96d2013-06-12 22:31:48 +00001630void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1631 PrintExpr(E->getSubExpr());
1632}
1633
John McCall5d413782010-12-06 08:20:24 +00001634void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00001635 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00001636 PrintExpr(E->getSubExpr());
1637}
1638
Mike Stump11289f42009-09-09 15:08:12 +00001639void
Douglas Gregorce934142009-05-20 18:46:25 +00001640StmtPrinter::VisitCXXUnresolvedConstructExpr(
1641 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001642 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001643 OS << "(";
1644 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001645 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001646 Arg != ArgEnd; ++Arg) {
1647 if (Arg != Node->arg_begin())
1648 OS << ", ";
1649 PrintExpr(*Arg);
1650 }
1651 OS << ")";
1652}
1653
John McCall8cd78132009-11-19 22:55:06 +00001654void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1655 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001656 if (!Node->isImplicitAccess()) {
1657 PrintExpr(Node->getBase());
1658 OS << (Node->isArrow() ? "->" : ".");
1659 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001660 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1661 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001662 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001663 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001664 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001665 if (Node->hasExplicitTemplateArgs())
1666 TemplateSpecializationType::PrintTemplateArgumentList(
1667 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00001668}
1669
John McCall10eae182009-11-30 22:42:35 +00001670void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001671 if (!Node->isImplicitAccess()) {
1672 PrintExpr(Node->getBase());
1673 OS << (Node->isArrow() ? "->" : ".");
1674 }
John McCall10eae182009-11-30 22:42:35 +00001675 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1676 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001677 if (Node->hasTemplateKeyword())
1678 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001679 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001680 if (Node->hasExplicitTemplateArgs())
1681 TemplateSpecializationType::PrintTemplateArgumentList(
1682 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00001683}
1684
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001685static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1686 switch (UTT) {
Alp Toker40f9b1c2013-12-12 21:23:03 +00001687#define TYPE_TRAIT_1(Spelling, Name, Key) \
1688 case clang::UTT_##Name: return #Spelling;
1689#include "clang/Basic/TokenKinds.def"
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001690 }
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001691 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001692}
1693
Douglas Gregor29c42f22012-02-24 07:38:34 +00001694static const char *getTypeTraitName(TypeTrait TT) {
1695 switch (TT) {
Alp Tokercbb90342013-12-13 20:49:58 +00001696#define TYPE_TRAIT_2(Spelling, Name, Key) \
1697case clang::BTT_##Name: return #Spelling;
1698#include "clang/Basic/TokenKinds.def"
Alp Toker40f9b1c2013-12-12 21:23:03 +00001699#define TYPE_TRAIT_N(Spelling, Name, Key) \
1700 case clang::TT_##Name: return #Spelling;
1701#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00001702 }
1703 llvm_unreachable("Type trait not covered by switch");
1704}
1705
John Wiegley6242b6a2011-04-28 00:16:57 +00001706static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1707 switch (ATT) {
1708 case ATT_ArrayRank: return "__array_rank";
1709 case ATT_ArrayExtent: return "__array_extent";
1710 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001711 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001712}
1713
John Wiegleyf9f65842011-04-25 06:54:41 +00001714static const char *getExpressionTraitName(ExpressionTrait ET) {
1715 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001716 case ET_IsLValueExpr: return "__is_lvalue_expr";
1717 case ET_IsRValueExpr: return "__is_rvalue_expr";
1718 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001719 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001720}
1721
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001722void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001723 OS << getTypeTraitName(E->getTrait()) << '(';
1724 E->getQueriedType().print(OS, Policy);
1725 OS << ')';
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001726}
1727
Douglas Gregor29c42f22012-02-24 07:38:34 +00001728void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1729 OS << getTypeTraitName(E->getTrait()) << "(";
1730 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1731 if (I > 0)
1732 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001733 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00001734 }
1735 OS << ")";
1736}
1737
John Wiegley6242b6a2011-04-28 00:16:57 +00001738void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001739 OS << getTypeTraitName(E->getTrait()) << '(';
1740 E->getQueriedType().print(OS, Policy);
1741 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00001742}
1743
John Wiegleyf9f65842011-04-25 06:54:41 +00001744void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001745 OS << getExpressionTraitName(E->getTrait()) << '(';
1746 PrintExpr(E->getQueriedExpression());
1747 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00001748}
1749
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001750void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1751 OS << "noexcept(";
1752 PrintExpr(E->getOperand());
1753 OS << ")";
1754}
1755
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001756void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001757 PrintExpr(E->getPattern());
1758 OS << "...";
1759}
1760
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001761void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001762 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001763}
1764
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001765void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1766 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001767 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001768}
1769
John McCall7c454bb2011-07-15 05:09:51 +00001770void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1771 SubstNonTypeTemplateParmExpr *Node) {
1772 Visit(Node->getReplacement());
1773}
1774
Richard Smithb15fe3a2012-09-12 00:56:43 +00001775void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1776 OS << *E->getParameterPack();
1777}
1778
Douglas Gregorfe314812011-06-21 17:03:29 +00001779void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1780 PrintExpr(Node->GetTemporaryExpr());
1781}
1782
Mike Stump11289f42009-09-09 15:08:12 +00001783// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001784
1785void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1786 OS << "@";
1787 VisitStringLiteral(Node->getString());
1788}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001789
Patrick Beard0caa3942012-04-19 00:25:12 +00001790void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001791 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00001792 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001793}
1794
1795void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1796 OS << "@[ ";
1797 StmtRange ch = E->children();
1798 if (ch.first != ch.second) {
1799 while (1) {
1800 Visit(*ch.first);
1801 ++ch.first;
1802 if (ch.first == ch.second) break;
1803 OS << ", ";
1804 }
1805 }
1806 OS << " ]";
1807}
1808
1809void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1810 OS << "@{ ";
1811 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1812 if (I > 0)
1813 OS << ", ";
1814
1815 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1816 Visit(Element.Key);
1817 OS << " : ";
1818 Visit(Element.Value);
1819 if (Element.isPackExpansion())
1820 OS << "...";
1821 }
1822 OS << " }";
1823}
1824
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001825void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001826 OS << "@encode(";
1827 Node->getEncodedType().print(OS, Policy);
1828 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001829}
1830
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001831void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001832 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001833}
1834
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001835void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001836 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001837}
1838
Steve Naroffd54978b2007-09-18 23:55:05 +00001839void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1840 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001841 switch (Mess->getReceiverKind()) {
1842 case ObjCMessageExpr::Instance:
1843 PrintExpr(Mess->getInstanceReceiver());
1844 break;
1845
1846 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001847 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00001848 break;
1849
1850 case ObjCMessageExpr::SuperInstance:
1851 case ObjCMessageExpr::SuperClass:
1852 OS << "Super";
1853 break;
1854 }
1855
Ted Kremeneka06e7122008-05-02 17:32:38 +00001856 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001857 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001858 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001859 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001860 } else {
1861 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001862 if (i < selector.getNumArgs()) {
1863 if (i > 0) OS << ' ';
1864 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001865 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001866 else
1867 OS << ":";
1868 }
1869 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001870
Steve Naroffc6814ea2007-10-02 20:01:56 +00001871 PrintExpr(Mess->getArg(i));
1872 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001873 }
1874 OS << "]";
1875}
1876
Ted Kremeneke65b0862012-03-06 20:05:56 +00001877void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1878 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1879}
1880
John McCall31168b02011-06-15 23:02:42 +00001881void
1882StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1883 PrintExpr(E->getSubExpr());
1884}
1885
1886void
1887StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001888 OS << '(' << E->getBridgeKindName();
1889 E->getType().print(OS, Policy);
1890 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00001891 PrintExpr(E->getSubExpr());
1892}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001893
Steve Naroffc540d662008-09-03 18:15:37 +00001894void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001895 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001896 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001897
Steve Naroffc540d662008-09-03 18:15:37 +00001898 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001899
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001900 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001901 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001902 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001903 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00001904 for (BlockDecl::param_iterator AI = BD->param_begin(),
1905 E = BD->param_end(); AI != E; ++AI) {
1906 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001907 std::string ParamStr = (*AI)->getNameAsString();
1908 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00001909 }
Mike Stump11289f42009-09-09 15:08:12 +00001910
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001911 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001912 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001913 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001914 OS << "...";
1915 }
1916 OS << ')';
1917 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00001918 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00001919}
1920
Ted Kremenekf551f322011-11-30 22:08:08 +00001921void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1922 PrintExpr(Node->getSourceExpr());
1923}
John McCall8d69a212010-11-15 23:31:06 +00001924
Tanya Lattner55808c12011-06-04 00:47:47 +00001925void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1926 OS << "__builtin_astype(";
1927 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001928 OS << ", ";
1929 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00001930 OS << ")";
1931}
1932
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001933//===----------------------------------------------------------------------===//
1934// Stmt method implementations
1935//===----------------------------------------------------------------------===//
1936
Craig Topperc571c812013-08-22 06:02:26 +00001937void Stmt::dumpPretty(const ASTContext &Context) const {
Richard Smith235341b2012-08-16 03:56:14 +00001938 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001939}
1940
Richard Smith235341b2012-08-16 03:56:14 +00001941void Stmt::printPretty(raw_ostream &OS,
1942 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001943 const PrintingPolicy &Policy,
1944 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001945 if (this == 0) {
1946 OS << "<NULL>";
1947 return;
1948 }
1949
Richard Smith235341b2012-08-16 03:56:14 +00001950 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001951 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001952}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001953
1954//===----------------------------------------------------------------------===//
1955// PrinterHelper
1956//===----------------------------------------------------------------------===//
1957
1958// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001959PrinterHelper::~PrinterHelper() {}