blob: a9f49990ee5272deb01edae22a394f8d000f1c80 [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 Bataevaadd52e2014-02-13 05:29:23 +0000596 const PrintingPolicy &Policy;
Alexey Bataev756c1962013-09-24 03:17:45 +0000597 /// \brief Process clauses with list of variables.
598 template <typename T>
599 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000600public:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000601 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
602 : OS(OS), Policy(Policy) { }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000603#define OPENMP_CLAUSE(Name, Class) \
604 void Visit##Class(Class *S);
605#include "clang/Basic/OpenMPKinds.def"
606};
607
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000608void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
609 OS << "if(";
610 Node->getCondition()->printPretty(OS, 0, Policy, 0);
611 OS << ")";
612}
613
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000614void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
615 OS << "default("
616 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
617 << ")";
618}
619
Alexey Bataev756c1962013-09-24 03:17:45 +0000620template<typename T>
621void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
622 for (typename T::varlist_iterator I = Node->varlist_begin(),
623 E = Node->varlist_end();
624 I != E; ++I)
625 OS << (I == Node->varlist_begin() ? StartSym : ',')
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000626 << *cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
Alexey Bataev756c1962013-09-24 03:17:45 +0000627}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000628
629void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
630 if (!Node->varlist_empty()) {
631 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000632 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000633 OS << ")";
634 }
635}
636
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000637void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
638 if (!Node->varlist_empty()) {
639 OS << "firstprivate";
640 VisitOMPClauseList(Node, '(');
641 OS << ")";
642 }
643}
644
Alexey Bataev758e55e2013-09-06 18:03:48 +0000645void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
646 if (!Node->varlist_empty()) {
647 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000648 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000649 OS << ")";
650 }
651}
652
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000653}
654
655//===----------------------------------------------------------------------===//
656// OpenMP directives printing methods
657//===----------------------------------------------------------------------===//
658
659void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
660 Indent() << "#pragma omp parallel ";
661
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000662 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000663 ArrayRef<OMPClause *> Clauses = Node->clauses();
664 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
665 I != E; ++I)
666 if (*I && !(*I)->isImplicit()) {
667 Printer.Visit(*I);
668 OS << ' ';
669 }
670 OS << "\n";
671 if (Node->getAssociatedStmt()) {
672 assert(isa<CapturedStmt>(Node->getAssociatedStmt()) &&
673 "Expected captured statement!");
674 Stmt *CS = cast<CapturedStmt>(Node->getAssociatedStmt())->getCapturedStmt();
675 PrintStmt(CS);
676 }
677}
678//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +0000679// Expr printing methods.
680//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000681
Chris Lattner882f7882006-11-04 18:52:07 +0000682void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000683 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
684 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000685 if (Node->hasTemplateKeyword())
686 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000687 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000688 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000689 TemplateSpecializationType::PrintTemplateArgumentList(
690 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000691}
692
John McCall8cd78132009-11-19 22:55:06 +0000693void StmtPrinter::VisitDependentScopeDeclRefExpr(
694 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +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 McCalle66edc12009-11-24 19:00:30 +0000700 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000701 TemplateSpecializationType::PrintTemplateArgumentList(
702 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000703}
704
John McCalld14a8642009-11-21 08:51:07 +0000705void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000706 if (Node->getQualifier())
707 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000708 if (Node->hasTemplateKeyword())
709 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000710 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000711 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000712 TemplateSpecializationType::PrintTemplateArgumentList(
713 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000714}
715
Steve Naroffe46504b2007-11-12 14:29:37 +0000716void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000717 if (Node->getBase()) {
718 PrintExpr(Node->getBase());
719 OS << (Node->isArrow() ? "->" : ".");
720 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000721 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000722}
723
Steve Naroffec944032008-05-30 00:40:33 +0000724void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000725 if (Node->isSuperReceiver())
726 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +0000727 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000728 PrintExpr(Node->getBase());
729 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +0000730 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
731 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +0000732 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000733
John McCallb7bd14f2010-12-02 01:19:52 +0000734 if (Node->isImplicitProperty())
Aaron Ballmanb190f972014-01-03 17:59:55 +0000735 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +0000736 else
737 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000738}
739
Ted Kremeneke65b0862012-03-06 20:05:56 +0000740void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
741
742 PrintExpr(Node->getBaseExpr());
743 OS << "[";
744 PrintExpr(Node->getKeyExpr());
745 OS << "]";
746}
747
Chris Lattner6307f192008-08-10 01:53:14 +0000748void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000749 switch (Node->getIdentType()) {
750 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000751 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000752 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000753 OS << "__func__";
754 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000755 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000756 OS << "__FUNCTION__";
757 break;
David Majnemerbed356a2013-11-06 23:31:56 +0000758 case PredefinedExpr::FuncDName:
759 OS << "__FUNCDNAME__";
760 break;
Nico Weber3a691a32012-06-23 02:07:59 +0000761 case PredefinedExpr::LFunction:
762 OS << "L__FUNCTION__";
763 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000764 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000765 OS << "__PRETTY_FUNCTION__";
766 break;
767 }
768}
769
Steve Naroffae4143e2007-04-26 20:39:23 +0000770void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000771 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000772
773 switch (Node->getKind()) {
774 case CharacterLiteral::Ascii: break; // no prefix.
775 case CharacterLiteral::Wide: OS << 'L'; break;
776 case CharacterLiteral::UTF16: OS << 'u'; break;
777 case CharacterLiteral::UTF32: OS << 'U'; break;
778 }
779
Chris Lattner666115c2007-07-13 23:58:20 +0000780 switch (value) {
781 case '\\':
782 OS << "'\\\\'";
783 break;
784 case '\'':
785 OS << "'\\''";
786 break;
787 case '\a':
788 // TODO: K&R: the meaning of '\\a' is different in traditional C
789 OS << "'\\a'";
790 break;
791 case '\b':
792 OS << "'\\b'";
793 break;
794 // Nonstandard escape sequence.
795 /*case '\e':
796 OS << "'\\e'";
797 break;*/
798 case '\f':
799 OS << "'\\f'";
800 break;
801 case '\n':
802 OS << "'\\n'";
803 break;
804 case '\r':
805 OS << "'\\r'";
806 break;
807 case '\t':
808 OS << "'\\t'";
809 break;
810 case '\v':
811 OS << "'\\v'";
812 break;
813 default:
Jordan Rose00d1b592013-02-08 22:30:27 +0000814 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +0000815 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +0000816 else if (value < 256)
817 OS << "'\\x" << llvm::format("%02x", value) << "'";
818 else if (value <= 0xFFFF)
819 OS << "'\\u" << llvm::format("%04x", value) << "'";
820 else
821 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000822 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000823}
824
Steve Naroffdf7855b2007-02-21 23:46:25 +0000825void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000826 bool isSigned = Node->getType()->isSignedIntegerType();
827 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000828
Chris Lattner06430412007-05-21 05:45:03 +0000829 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000830 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000831 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000832 // FIXME: The Short and UShort cases are to handle cases where a short
833 // integeral literal is formed during template instantiation. They should
834 // be removed when template instantiation no longer needs integer literals.
835 case BuiltinType::Short:
836 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000837 case BuiltinType::Int: break; // no suffix.
838 case BuiltinType::UInt: OS << 'U'; break;
839 case BuiltinType::Long: OS << 'L'; break;
840 case BuiltinType::ULong: OS << "UL"; break;
841 case BuiltinType::LongLong: OS << "LL"; break;
842 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000843 case BuiltinType::Int128: OS << "i128"; break;
844 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000845 }
Chris Lattner882f7882006-11-04 18:52:07 +0000846}
Benjamin Kramer8a526762012-09-20 14:07:17 +0000847
848static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
849 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000850 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +0000851 Node->getValue().toString(Str);
852 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +0000853 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
854 OS << '.'; // Trailing dot in order to separate from ints.
855
856 if (!PrintSuffix)
857 return;
858
859 // Emit suffixes. Float literals are always a builtin float type.
860 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
861 default: llvm_unreachable("Unexpected type for float literal!");
862 case BuiltinType::Half: break; // FIXME: suffix?
863 case BuiltinType::Double: break; // no suffix.
864 case BuiltinType::Float: OS << 'F'; break;
865 case BuiltinType::LongDouble: OS << 'L'; break;
866 }
867}
868
869void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
870 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +0000871}
Chris Lattner1c20a172007-08-26 03:42:43 +0000872
873void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
874 PrintExpr(Node->getSubExpr());
875 OS << "i";
876}
877
Steve Naroffdf7855b2007-02-21 23:46:25 +0000878void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +0000879 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +0000880}
881void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
882 OS << "(";
883 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000884 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000885}
886void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000887 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000888 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000889
Eli Friedman1cf25362009-06-14 22:39:26 +0000890 // Print a space if this is an "identifier operator" like __real, or if
891 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000892 switch (Node->getOpcode()) {
893 default: break;
John McCalle3027922010-08-25 11:45:40 +0000894 case UO_Real:
895 case UO_Imag:
896 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000897 OS << ' ';
898 break;
John McCalle3027922010-08-25 11:45:40 +0000899 case UO_Plus:
900 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000901 if (isa<UnaryOperator>(Node->getSubExpr()))
902 OS << ' ';
903 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000904 }
905 }
Chris Lattner882f7882006-11-04 18:52:07 +0000906 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000907
Chris Lattner15768702006-11-05 23:54:51 +0000908 if (Node->isPostfix())
909 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000910}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000911
Douglas Gregor882211c2010-04-28 22:16:22 +0000912void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
913 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000914 Node->getTypeSourceInfo()->getType().print(OS, Policy);
915 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000916 bool PrintedSomething = false;
917 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
918 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
919 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
920 // Array node
921 OS << "[";
922 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
923 OS << "]";
924 PrintedSomething = true;
925 continue;
926 }
Douglas Gregord1702062010-04-29 00:18:15 +0000927
928 // Skip implicit base indirections.
929 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
930 continue;
931
Douglas Gregor882211c2010-04-28 22:16:22 +0000932 // Field or identifier node.
933 IdentifierInfo *Id = ON.getFieldName();
934 if (!Id)
935 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000936
Douglas Gregor882211c2010-04-28 22:16:22 +0000937 if (PrintedSomething)
938 OS << ".";
939 else
940 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000941 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000942 }
943 OS << ")";
944}
945
Peter Collingbournee190dee2011-03-11 19:24:49 +0000946void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
947 switch(Node->getKind()) {
948 case UETT_SizeOf:
949 OS << "sizeof";
950 break;
951 case UETT_AlignOf:
Jordan Rose58d54722012-06-30 21:33:57 +0000952 if (Policy.LangOpts.CPlusPlus)
953 OS << "alignof";
954 else if (Policy.LangOpts.C11)
955 OS << "_Alignof";
956 else
957 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +0000958 break;
959 case UETT_VecStep:
960 OS << "vec_step";
961 break;
962 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000963 if (Node->isArgumentType()) {
964 OS << '(';
965 Node->getArgumentType().print(OS, Policy);
966 OS << ')';
967 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +0000968 OS << " ";
969 PrintExpr(Node->getArgumentExpr());
970 }
Chris Lattner882f7882006-11-04 18:52:07 +0000971}
Peter Collingbourne91147592011-04-15 00:35:48 +0000972
973void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
974 OS << "_Generic(";
975 PrintExpr(Node->getControllingExpr());
976 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
977 OS << ", ";
978 QualType T = Node->getAssocType(i);
979 if (T.isNull())
980 OS << "default";
981 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000982 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +0000983 OS << ": ";
984 PrintExpr(Node->getAssocExpr(i));
985 }
986 OS << ")";
987}
988
Chris Lattner882f7882006-11-04 18:52:07 +0000989void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000990 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000991 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000992 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000993 OS << "]";
994}
995
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000996void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +0000997 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000998 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
999 // Don't print any defaulted arguments
1000 break;
1001 }
1002
Chris Lattner882f7882006-11-04 18:52:07 +00001003 if (i) OS << ", ";
1004 PrintExpr(Call->getArg(i));
1005 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001006}
1007
1008void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1009 PrintExpr(Call->getCallee());
1010 OS << "(";
1011 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001012 OS << ")";
1013}
1014void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +00001015 // FIXME: Suppress printing implicit bases (like "this")
1016 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001017
1018 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +00001019 FieldDecl *ParentDecl = ParentMember
1020 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
David Blaikie8fab8e52012-11-12 19:12:12 +00001021
David Blaikie6bffd6f2012-11-12 19:32:32 +00001022 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +00001023 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +00001024
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001025 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1026 if (FD->isAnonymousStructOrUnion())
1027 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001028
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001029 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1030 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001031 if (Node->hasTemplateKeyword())
1032 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001033 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001034 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001035 TemplateSpecializationType::PrintTemplateArgumentList(
1036 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001037}
Steve Naroffe87026a2009-07-24 17:54:45 +00001038void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1039 PrintExpr(Node->getBase());
1040 OS << (Node->isArrow() ? "->isa" : ".isa");
1041}
1042
Nate Begemance4d7fc2008-04-18 23:10:10 +00001043void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001044 PrintExpr(Node->getBase());
1045 OS << ".";
1046 OS << Node->getAccessor().getName();
1047}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001048void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001049 OS << '(';
1050 Node->getTypeAsWritten().print(OS, Policy);
1051 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001052 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001053}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001054void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001055 OS << '(';
1056 Node->getType().print(OS, Policy);
1057 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001058 PrintExpr(Node->getInitializer());
1059}
Steve Naroff7a5af782007-07-13 16:58:59 +00001060void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001061 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001062 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001063}
Chris Lattner882f7882006-11-04 18:52:07 +00001064void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1065 PrintExpr(Node->getLHS());
1066 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1067 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001068}
Chris Lattner86928112007-08-25 02:00:02 +00001069void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1070 PrintExpr(Node->getLHS());
1071 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1072 PrintExpr(Node->getRHS());
1073}
Chris Lattner882f7882006-11-04 18:52:07 +00001074void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1075 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001076 OS << " ? ";
1077 PrintExpr(Node->getLHS());
1078 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001079 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001080}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001081
Chris Lattnereefa10e2007-05-28 06:56:27 +00001082// GNU extensions.
1083
John McCallc07a0c72011-02-17 10:25:35 +00001084void
1085StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1086 PrintExpr(Node->getCommon());
1087 OS << " ?: ";
1088 PrintExpr(Node->getFalseExpr());
1089}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001090void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001091 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001092}
1093
Chris Lattner366727f2007-07-24 16:58:17 +00001094void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1095 OS << "(";
1096 PrintRawCompoundStmt(E->getSubStmt());
1097 OS << ")";
1098}
1099
Steve Naroff9efdabc2007-08-03 21:21:27 +00001100void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1101 OS << "__builtin_choose_expr(";
1102 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001103 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001104 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001105 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001106 PrintExpr(Node->getRHS());
1107 OS << ")";
1108}
Chris Lattner366727f2007-07-24 16:58:17 +00001109
Douglas Gregor3be4b122008-11-29 04:51:27 +00001110void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1111 OS << "__null";
1112}
1113
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001114void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1115 OS << "__builtin_shufflevector(";
1116 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1117 if (i) OS << ", ";
1118 PrintExpr(Node->getExpr(i));
1119 }
1120 OS << ")";
1121}
1122
Hal Finkelc4d7c822013-09-18 03:29:45 +00001123void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1124 OS << "__builtin_convertvector(";
1125 PrintExpr(Node->getSrcExpr());
1126 OS << ", ";
1127 Node->getType().print(OS, Policy);
1128 OS << ")";
1129}
1130
Anders Carlsson4692db02007-08-31 04:56:16 +00001131void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001132 if (Node->getSyntacticForm()) {
1133 Visit(Node->getSyntacticForm());
1134 return;
1135 }
1136
Anders Carlsson4692db02007-08-31 04:56:16 +00001137 OS << "{ ";
1138 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1139 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001140 if (Node->getInit(i))
1141 PrintExpr(Node->getInit(i));
1142 else
1143 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +00001144 }
1145 OS << " }";
1146}
1147
Nate Begeman5ec4b312009-08-10 23:49:36 +00001148void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1149 OS << "( ";
1150 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1151 if (i) OS << ", ";
1152 PrintExpr(Node->getExpr(i));
1153 }
1154 OS << " )";
1155}
1156
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001157void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001158 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1159 DEnd = Node->designators_end();
1160 D != DEnd; ++D) {
1161 if (D->isFieldDesignator()) {
1162 if (D->getDotLoc().isInvalid())
1163 OS << D->getFieldName()->getName() << ":";
1164 else
1165 OS << "." << D->getFieldName()->getName();
1166 } else {
1167 OS << "[";
1168 if (D->isArrayDesignator()) {
1169 PrintExpr(Node->getArrayIndex(*D));
1170 } else {
1171 PrintExpr(Node->getArrayRangeStart(*D));
1172 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001173 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001174 }
1175 OS << "]";
1176 }
1177 }
1178
1179 OS << " = ";
1180 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001181}
1182
Douglas Gregor0202cb42009-01-29 17:44:32 +00001183void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001184 if (Policy.LangOpts.CPlusPlus) {
1185 OS << "/*implicit*/";
1186 Node->getType().print(OS, Policy);
1187 OS << "()";
1188 } else {
1189 OS << "/*implicit*/(";
1190 Node->getType().print(OS, Policy);
1191 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001192 if (Node->getType()->isRecordType())
1193 OS << "{}";
1194 else
1195 OS << 0;
1196 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001197}
1198
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001199void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001200 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001201 PrintExpr(Node->getSubExpr());
1202 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001203 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001204 OS << ")";
1205}
1206
John McCallfe96e0b2011-11-06 09:01:30 +00001207void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1208 PrintExpr(Node->getSyntacticForm());
1209}
1210
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001211void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001212 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001213 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001214#define BUILTIN(ID, TYPE, ATTRS)
1215#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1216 case AtomicExpr::AO ## ID: \
1217 Name = #ID "("; \
1218 break;
1219#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001220 }
1221 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001222
1223 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001224 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001225 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1226 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001227 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001228 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001229 }
Richard Smithfeea8832012-04-12 05:08:17 +00001230 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1231 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001232 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001233 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001234 }
Richard Smithfeea8832012-04-12 05:08:17 +00001235 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1236 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001237 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001238 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001239 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001240 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1241 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001242 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001243 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001244 if (Node->isCmpXChg()) {
1245 OS << ", ";
1246 PrintExpr(Node->getOrderFail());
1247 }
1248 OS << ")";
1249}
1250
Chris Lattnereefa10e2007-05-28 06:56:27 +00001251// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001252void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1253 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1254 "",
1255#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1256 Spelling,
1257#include "clang/Basic/OperatorKinds.def"
1258 };
1259
1260 OverloadedOperatorKind Kind = Node->getOperator();
1261 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1262 if (Node->getNumArgs() == 1) {
1263 OS << OpStrings[Kind] << ' ';
1264 PrintExpr(Node->getArg(0));
1265 } else {
1266 PrintExpr(Node->getArg(0));
1267 OS << ' ' << OpStrings[Kind];
1268 }
Eli Friedman8e236422012-10-12 22:45:14 +00001269 } else if (Kind == OO_Arrow) {
1270 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001271 } else if (Kind == OO_Call) {
1272 PrintExpr(Node->getArg(0));
1273 OS << '(';
1274 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1275 if (ArgIdx > 1)
1276 OS << ", ";
1277 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1278 PrintExpr(Node->getArg(ArgIdx));
1279 }
1280 OS << ')';
1281 } else if (Kind == OO_Subscript) {
1282 PrintExpr(Node->getArg(0));
1283 OS << '[';
1284 PrintExpr(Node->getArg(1));
1285 OS << ']';
1286 } else if (Node->getNumArgs() == 1) {
1287 OS << OpStrings[Kind] << ' ';
1288 PrintExpr(Node->getArg(0));
1289 } else if (Node->getNumArgs() == 2) {
1290 PrintExpr(Node->getArg(0));
1291 OS << ' ' << OpStrings[Kind] << ' ';
1292 PrintExpr(Node->getArg(1));
1293 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001294 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001295 }
1296}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001297
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001298void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001299 // If we have a conversion operator call only print the argument.
1300 CXXMethodDecl *MD = Node->getMethodDecl();
1301 if (MD && isa<CXXConversionDecl>(MD)) {
1302 PrintExpr(Node->getImplicitObjectArgument());
1303 return;
1304 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001305 VisitCallExpr(cast<CallExpr>(Node));
1306}
1307
Peter Collingbourne41f85462011-02-09 21:07:24 +00001308void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1309 PrintExpr(Node->getCallee());
1310 OS << "<<<";
1311 PrintCallArgs(Node->getConfig());
1312 OS << ">>>(";
1313 PrintCallArgs(Node);
1314 OS << ")";
1315}
1316
Douglas Gregore200adc2008-10-27 19:41:14 +00001317void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1318 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001319 Node->getTypeAsWritten().print(OS, Policy);
1320 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001321 PrintExpr(Node->getSubExpr());
1322 OS << ")";
1323}
1324
Douglas Gregore200adc2008-10-27 19:41:14 +00001325void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1326 VisitCXXNamedCastExpr(Node);
1327}
1328
1329void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1330 VisitCXXNamedCastExpr(Node);
1331}
1332
1333void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1334 VisitCXXNamedCastExpr(Node);
1335}
1336
1337void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1338 VisitCXXNamedCastExpr(Node);
1339}
1340
Sebastian Redlc4704762008-11-11 11:37:55 +00001341void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1342 OS << "typeid(";
1343 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001344 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001345 } else {
1346 PrintExpr(Node->getExprOperand());
1347 }
1348 OS << ")";
1349}
1350
Francois Pichet9f4f2072010-09-08 12:20:18 +00001351void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1352 OS << "__uuidof(";
1353 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001354 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001355 } else {
1356 PrintExpr(Node->getExprOperand());
1357 }
1358 OS << ")";
1359}
1360
John McCall5e77d762013-04-16 07:28:30 +00001361void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1362 PrintExpr(Node->getBaseExpr());
1363 if (Node->isArrow())
1364 OS << "->";
1365 else
1366 OS << ".";
1367 if (NestedNameSpecifier *Qualifier =
1368 Node->getQualifierLoc().getNestedNameSpecifier())
1369 Qualifier->print(OS, Policy);
1370 OS << Node->getPropertyDecl()->getDeclName();
1371}
1372
Richard Smithc67fdd42012-03-07 08:35:16 +00001373void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1374 switch (Node->getLiteralOperatorKind()) {
1375 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001376 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001377 break;
1378 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001379 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1380 const TemplateArgumentList *Args =
1381 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1382 assert(Args);
1383 const TemplateArgument &Pack = Args->get(0);
1384 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1385 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001386 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001387 OS << C;
1388 }
1389 break;
1390 }
Richard Smith75025ba2012-03-08 09:02:38 +00001391 case UserDefinedLiteral::LOK_Integer: {
1392 // Print integer literal without suffix.
1393 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1394 OS << Int->getValue().toString(10, /*isSigned*/false);
1395 break;
1396 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001397 case UserDefinedLiteral::LOK_Floating: {
1398 // Print floating literal without suffix.
1399 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1400 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1401 break;
1402 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001403 case UserDefinedLiteral::LOK_String:
1404 case UserDefinedLiteral::LOK_Character:
1405 PrintExpr(Node->getCookedLiteral());
1406 break;
1407 }
1408 OS << Node->getUDSuffix()->getName();
1409}
1410
Chris Lattnereefa10e2007-05-28 06:56:27 +00001411void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1412 OS << (Node->getValue() ? "true" : "false");
1413}
1414
Sebastian Redl576fd422009-05-10 18:38:11 +00001415void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1416 OS << "nullptr";
1417}
1418
Douglas Gregor97a9c812008-11-04 14:32:21 +00001419void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1420 OS << "this";
1421}
1422
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001423void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1424 if (Node->getSubExpr() == 0)
1425 OS << "throw";
1426 else {
1427 OS << "throw ";
1428 PrintExpr(Node->getSubExpr());
1429 }
1430}
1431
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001432void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001433 // Nothing to print: we picked up the default argument.
1434}
1435
1436void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1437 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001438}
1439
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001440void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001441 Node->getType().print(OS, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001442 OS << "(";
1443 PrintExpr(Node->getSubExpr());
1444 OS << ")";
1445}
1446
Anders Carlsson993a4b32009-05-30 20:03:25 +00001447void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1448 PrintExpr(Node->getSubExpr());
1449}
1450
Douglas Gregordd04d332009-01-16 18:33:17 +00001451void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001452 Node->getType().print(OS, Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001453 OS << "(";
1454 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001455 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001456 Arg != ArgEnd; ++Arg) {
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001457 if (Arg->isDefaultArgument())
1458 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001459 if (Arg != Node->arg_begin())
1460 OS << ", ";
1461 PrintExpr(*Arg);
1462 }
1463 OS << ")";
1464}
1465
Douglas Gregore31e6062012-02-07 10:09:13 +00001466void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1467 OS << '[';
1468 bool NeedComma = false;
1469 switch (Node->getCaptureDefault()) {
1470 case LCD_None:
1471 break;
1472
1473 case LCD_ByCopy:
1474 OS << '=';
1475 NeedComma = true;
1476 break;
1477
1478 case LCD_ByRef:
1479 OS << '&';
1480 NeedComma = true;
1481 break;
1482 }
1483 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1484 CEnd = Node->explicit_capture_end();
1485 C != CEnd;
1486 ++C) {
1487 if (NeedComma)
1488 OS << ", ";
1489 NeedComma = true;
1490
1491 switch (C->getCaptureKind()) {
1492 case LCK_This:
1493 OS << "this";
1494 break;
1495
1496 case LCK_ByRef:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001497 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
Douglas Gregore31e6062012-02-07 10:09:13 +00001498 OS << '&';
1499 OS << C->getCapturedVar()->getName();
1500 break;
1501
1502 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00001503 OS << C->getCapturedVar()->getName();
1504 break;
1505 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001506
1507 if (C->isInitCapture())
1508 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00001509 }
1510 OS << ']';
1511
1512 if (Node->hasExplicitParameters()) {
1513 OS << " (";
1514 CXXMethodDecl *Method = Node->getCallOperator();
1515 NeedComma = false;
1516 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1517 PEnd = Method->param_end();
1518 P != PEnd; ++P) {
1519 if (NeedComma) {
1520 OS << ", ";
1521 } else {
1522 NeedComma = true;
1523 }
1524 std::string ParamStr = (*P)->getNameAsString();
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001525 (*P)->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001526 }
1527 if (Method->isVariadic()) {
1528 if (NeedComma)
1529 OS << ", ";
1530 OS << "...";
1531 }
1532 OS << ')';
1533
1534 if (Node->isMutable())
1535 OS << " mutable";
1536
1537 const FunctionProtoType *Proto
1538 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001539 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001540
Bill Wendling44426052012-12-20 19:22:21 +00001541 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00001542
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001543 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001544 if (Node->hasExplicitResultType()) {
1545 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00001546 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001547 }
Douglas Gregore31e6062012-02-07 10:09:13 +00001548 }
1549
1550 // Print the body.
1551 CompoundStmt *Body = Node->getBody();
1552 OS << ' ';
1553 PrintStmt(Body);
1554}
1555
Douglas Gregor747eb782010-07-08 06:14:04 +00001556void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001557 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001558 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001559 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001560 Node->getType().print(OS, Policy);
1561 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001562}
1563
Sebastian Redlbd150f42008-11-21 19:14:01 +00001564void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1565 if (E->isGlobalNew())
1566 OS << "::";
1567 OS << "new ";
1568 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001569 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001570 OS << "(";
1571 PrintExpr(E->getPlacementArg(0));
1572 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001573 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1574 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001575 OS << ", ";
1576 PrintExpr(E->getPlacementArg(i));
1577 }
1578 OS << ") ";
1579 }
1580 if (E->isParenTypeId())
1581 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001582 std::string TypeS;
1583 if (Expr *Size = E->getArraySize()) {
1584 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001585 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00001586 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001587 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001588 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001589 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001590 if (E->isParenTypeId())
1591 OS << ")";
1592
Sebastian Redl6047f072012-02-16 12:22:20 +00001593 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1594 if (InitStyle) {
1595 if (InitStyle == CXXNewExpr::CallInit)
1596 OS << "(";
1597 PrintExpr(E->getInitializer());
1598 if (InitStyle == CXXNewExpr::CallInit)
1599 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001600 }
1601}
1602
1603void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1604 if (E->isGlobalDelete())
1605 OS << "::";
1606 OS << "delete ";
1607 if (E->isArrayForm())
1608 OS << "[] ";
1609 PrintExpr(E->getArgument());
1610}
1611
Douglas Gregorad8a3362009-09-04 17:36:40 +00001612void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1613 PrintExpr(E->getBase());
1614 if (E->isArrow())
1615 OS << "->";
1616 else
1617 OS << '.';
1618 if (E->getQualifier())
1619 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00001620 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00001621
Douglas Gregor678f90d2010-02-25 01:56:36 +00001622 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1623 OS << II->getName();
1624 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001625 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001626}
1627
Anders Carlsson0781ce72009-04-23 02:32:43 +00001628void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithd59b8322012-12-19 01:39:02 +00001629 if (E->isListInitialization())
1630 OS << "{ ";
1631
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001632 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1633 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1634 // Don't print any defaulted arguments
1635 break;
1636 }
1637
1638 if (i) OS << ", ";
1639 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001640 }
Richard Smithd59b8322012-12-19 01:39:02 +00001641
1642 if (E->isListInitialization())
1643 OS << " }";
Anders Carlsson0781ce72009-04-23 02:32:43 +00001644}
1645
Richard Smithcc1b96d2013-06-12 22:31:48 +00001646void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1647 PrintExpr(E->getSubExpr());
1648}
1649
John McCall5d413782010-12-06 08:20:24 +00001650void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00001651 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00001652 PrintExpr(E->getSubExpr());
1653}
1654
Mike Stump11289f42009-09-09 15:08:12 +00001655void
Douglas Gregorce934142009-05-20 18:46:25 +00001656StmtPrinter::VisitCXXUnresolvedConstructExpr(
1657 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001658 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001659 OS << "(";
1660 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001661 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001662 Arg != ArgEnd; ++Arg) {
1663 if (Arg != Node->arg_begin())
1664 OS << ", ";
1665 PrintExpr(*Arg);
1666 }
1667 OS << ")";
1668}
1669
John McCall8cd78132009-11-19 22:55:06 +00001670void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1671 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001672 if (!Node->isImplicitAccess()) {
1673 PrintExpr(Node->getBase());
1674 OS << (Node->isArrow() ? "->" : ".");
1675 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001676 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1677 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001678 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001679 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001680 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001681 if (Node->hasExplicitTemplateArgs())
1682 TemplateSpecializationType::PrintTemplateArgumentList(
1683 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00001684}
1685
John McCall10eae182009-11-30 22:42:35 +00001686void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001687 if (!Node->isImplicitAccess()) {
1688 PrintExpr(Node->getBase());
1689 OS << (Node->isArrow() ? "->" : ".");
1690 }
John McCall10eae182009-11-30 22:42:35 +00001691 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1692 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001693 if (Node->hasTemplateKeyword())
1694 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001695 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001696 if (Node->hasExplicitTemplateArgs())
1697 TemplateSpecializationType::PrintTemplateArgumentList(
1698 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00001699}
1700
Douglas Gregor29c42f22012-02-24 07:38:34 +00001701static const char *getTypeTraitName(TypeTrait TT) {
1702 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00001703#define TYPE_TRAIT_1(Spelling, Name, Key) \
1704case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00001705#define TYPE_TRAIT_2(Spelling, Name, Key) \
1706case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00001707#define TYPE_TRAIT_N(Spelling, Name, Key) \
1708 case clang::TT_##Name: return #Spelling;
1709#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00001710 }
1711 llvm_unreachable("Type trait not covered by switch");
1712}
1713
John Wiegley6242b6a2011-04-28 00:16:57 +00001714static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1715 switch (ATT) {
1716 case ATT_ArrayRank: return "__array_rank";
1717 case ATT_ArrayExtent: return "__array_extent";
1718 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001719 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001720}
1721
John Wiegleyf9f65842011-04-25 06:54:41 +00001722static const char *getExpressionTraitName(ExpressionTrait ET) {
1723 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001724 case ET_IsLValueExpr: return "__is_lvalue_expr";
1725 case ET_IsRValueExpr: return "__is_rvalue_expr";
1726 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001727 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001728}
1729
Douglas Gregor29c42f22012-02-24 07:38:34 +00001730void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1731 OS << getTypeTraitName(E->getTrait()) << "(";
1732 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1733 if (I > 0)
1734 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001735 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00001736 }
1737 OS << ")";
1738}
1739
John Wiegley6242b6a2011-04-28 00:16:57 +00001740void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001741 OS << getTypeTraitName(E->getTrait()) << '(';
1742 E->getQueriedType().print(OS, Policy);
1743 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00001744}
1745
John Wiegleyf9f65842011-04-25 06:54:41 +00001746void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001747 OS << getExpressionTraitName(E->getTrait()) << '(';
1748 PrintExpr(E->getQueriedExpression());
1749 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00001750}
1751
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001752void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1753 OS << "noexcept(";
1754 PrintExpr(E->getOperand());
1755 OS << ")";
1756}
1757
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001758void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001759 PrintExpr(E->getPattern());
1760 OS << "...";
1761}
1762
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001763void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001764 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001765}
1766
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001767void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1768 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001769 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001770}
1771
John McCall7c454bb2011-07-15 05:09:51 +00001772void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1773 SubstNonTypeTemplateParmExpr *Node) {
1774 Visit(Node->getReplacement());
1775}
1776
Richard Smithb15fe3a2012-09-12 00:56:43 +00001777void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1778 OS << *E->getParameterPack();
1779}
1780
Douglas Gregorfe314812011-06-21 17:03:29 +00001781void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1782 PrintExpr(Node->GetTemporaryExpr());
1783}
1784
Mike Stump11289f42009-09-09 15:08:12 +00001785// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001786
1787void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1788 OS << "@";
1789 VisitStringLiteral(Node->getString());
1790}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001791
Patrick Beard0caa3942012-04-19 00:25:12 +00001792void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001793 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00001794 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001795}
1796
1797void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1798 OS << "@[ ";
1799 StmtRange ch = E->children();
1800 if (ch.first != ch.second) {
1801 while (1) {
1802 Visit(*ch.first);
1803 ++ch.first;
1804 if (ch.first == ch.second) break;
1805 OS << ", ";
1806 }
1807 }
1808 OS << " ]";
1809}
1810
1811void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1812 OS << "@{ ";
1813 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1814 if (I > 0)
1815 OS << ", ";
1816
1817 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1818 Visit(Element.Key);
1819 OS << " : ";
1820 Visit(Element.Value);
1821 if (Element.isPackExpansion())
1822 OS << "...";
1823 }
1824 OS << " }";
1825}
1826
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001827void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001828 OS << "@encode(";
1829 Node->getEncodedType().print(OS, Policy);
1830 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001831}
1832
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001833void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001834 OS << "@selector(";
1835 Node->getSelector().print(OS);
1836 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001837}
1838
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001839void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001840 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001841}
1842
Steve Naroffd54978b2007-09-18 23:55:05 +00001843void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1844 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001845 switch (Mess->getReceiverKind()) {
1846 case ObjCMessageExpr::Instance:
1847 PrintExpr(Mess->getInstanceReceiver());
1848 break;
1849
1850 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001851 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00001852 break;
1853
1854 case ObjCMessageExpr::SuperInstance:
1855 case ObjCMessageExpr::SuperClass:
1856 OS << "Super";
1857 break;
1858 }
1859
Ted Kremeneka06e7122008-05-02 17:32:38 +00001860 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001861 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001862 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001863 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001864 } else {
1865 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001866 if (i < selector.getNumArgs()) {
1867 if (i > 0) OS << ' ';
1868 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001869 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001870 else
1871 OS << ":";
1872 }
1873 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001874
Steve Naroffc6814ea2007-10-02 20:01:56 +00001875 PrintExpr(Mess->getArg(i));
1876 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001877 }
1878 OS << "]";
1879}
1880
Ted Kremeneke65b0862012-03-06 20:05:56 +00001881void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1882 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1883}
1884
John McCall31168b02011-06-15 23:02:42 +00001885void
1886StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1887 PrintExpr(E->getSubExpr());
1888}
1889
1890void
1891StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001892 OS << '(' << E->getBridgeKindName();
1893 E->getType().print(OS, Policy);
1894 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00001895 PrintExpr(E->getSubExpr());
1896}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001897
Steve Naroffc540d662008-09-03 18:15:37 +00001898void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001899 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001900 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001901
Steve Naroffc540d662008-09-03 18:15:37 +00001902 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001903
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001904 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001905 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001906 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001907 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00001908 for (BlockDecl::param_iterator AI = BD->param_begin(),
1909 E = BD->param_end(); AI != E; ++AI) {
1910 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001911 std::string ParamStr = (*AI)->getNameAsString();
1912 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00001913 }
Mike Stump11289f42009-09-09 15:08:12 +00001914
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001915 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001916 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001917 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001918 OS << "...";
1919 }
1920 OS << ')';
1921 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00001922 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00001923}
1924
Ted Kremenekf551f322011-11-30 22:08:08 +00001925void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1926 PrintExpr(Node->getSourceExpr());
1927}
John McCall8d69a212010-11-15 23:31:06 +00001928
Tanya Lattner55808c12011-06-04 00:47:47 +00001929void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1930 OS << "__builtin_astype(";
1931 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001932 OS << ", ";
1933 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00001934 OS << ")";
1935}
1936
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001937//===----------------------------------------------------------------------===//
1938// Stmt method implementations
1939//===----------------------------------------------------------------------===//
1940
Craig Topperc571c812013-08-22 06:02:26 +00001941void Stmt::dumpPretty(const ASTContext &Context) const {
Richard Smith235341b2012-08-16 03:56:14 +00001942 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001943}
1944
Richard Smith235341b2012-08-16 03:56:14 +00001945void Stmt::printPretty(raw_ostream &OS,
1946 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001947 const PrintingPolicy &Policy,
1948 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001949 if (this == 0) {
1950 OS << "<NULL>";
1951 return;
1952 }
1953
Richard Smith235341b2012-08-16 03:56:14 +00001954 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001955 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001956}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001957
1958//===----------------------------------------------------------------------===//
1959// PrinterHelper
1960//===----------------------------------------------------------------------===//
1961
1962// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001963PrinterHelper::~PrinterHelper() {}