blob: 9203dc1584bb46b4e36ccf751fd0046f8e57e5a3 [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());
333 Indent() << "}\n";
334}
335
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000336void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
337 Indent();
338 if (Node->isIfExists())
339 OS << "__if_exists (";
340 else
341 OS << "__if_not_exists (";
342
343 if (NestedNameSpecifier *Qualifier
344 = Node->getQualifierLoc().getNestedNameSpecifier())
345 Qualifier->print(OS, Policy);
346
347 OS << Node->getNameInfo() << ") ";
348
349 PrintRawCompoundStmt(Node->getSubStmt());
350}
351
Chris Lattner16976d32006-11-05 01:46:01 +0000352void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000353 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000354}
355
356void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000357 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000358 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000359 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000360}
361
362void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000363 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000364}
365
366void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000367 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000368}
369
370
Chris Lattner882f7882006-11-04 18:52:07 +0000371void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
372 Indent() << "return";
373 if (Node->getRetValue()) {
374 OS << " ";
375 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000376 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000377 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000378}
379
Chris Lattner73c56c02007-10-29 04:04:16 +0000380
Chad Rosierde70e0e2012-08-25 00:11:56 +0000381void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000382 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000383
Anders Carlsson660bdd12007-11-23 23:12:25 +0000384 if (Node->isVolatile())
385 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000386
Anders Carlsson660bdd12007-11-23 23:12:25 +0000387 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000388 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000389
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000390 // Outputs
391 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
392 Node->getNumClobbers() != 0)
393 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000394
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000395 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
396 if (i != 0)
397 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000398
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000399 if (!Node->getOutputName(i).empty()) {
400 OS << '[';
401 OS << Node->getOutputName(i);
402 OS << "] ";
403 }
Mike Stump11289f42009-09-09 15:08:12 +0000404
Chris Lattner72bbf172009-03-10 04:59:06 +0000405 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000406 OS << " ";
407 Visit(Node->getOutputExpr(i));
408 }
Mike Stump11289f42009-09-09 15:08:12 +0000409
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000410 // Inputs
411 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
412 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000413
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000414 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
415 if (i != 0)
416 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000417
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000418 if (!Node->getInputName(i).empty()) {
419 OS << '[';
420 OS << Node->getInputName(i);
421 OS << "] ";
422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattner72bbf172009-03-10 04:59:06 +0000424 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000425 OS << " ";
426 Visit(Node->getInputExpr(i));
427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000429 // Clobbers
430 if (Node->getNumClobbers() != 0)
431 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000432
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000433 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
434 if (i != 0)
435 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000436
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000437 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000438 }
Mike Stump11289f42009-09-09 15:08:12 +0000439
Anders Carlsson81a5a692007-11-20 19:21:03 +0000440 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000441}
442
Chad Rosier32503022012-06-11 20:47:18 +0000443void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
444 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000445 Indent() << "__asm ";
446 if (Node->hasBraces())
447 OS << "{\n";
John McCallf413f5e2013-05-03 00:10:13 +0000448 OS << Node->getAsmString() << "\n";
Chad Rosierb6f46c12012-08-15 16:53:30 +0000449 if (Node->hasBraces())
450 Indent() << "}\n";
Chad Rosier32503022012-06-11 20:47:18 +0000451}
452
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000453void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000454 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000455}
456
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000457void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000458 Indent() << "@try";
459 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
460 PrintRawCompoundStmt(TS);
461 OS << "\n";
462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Douglas Gregor96c79492010-04-23 22:50:49 +0000464 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
465 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000466 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000467 if (catchStmt->getCatchParamDecl()) {
468 if (Decl *DS = catchStmt->getCatchParamDecl())
469 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000470 }
471 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000472 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
473 PrintRawCompoundStmt(CS);
474 OS << "\n";
475 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000476 }
Mike Stump11289f42009-09-09 15:08:12 +0000477
478 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
479 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000480 Indent() << "@finally";
481 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000482 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000483 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000484}
485
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000486void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000487}
488
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000489void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000490 Indent() << "@catch (...) { /* todo */ } \n";
491}
492
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000493void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000494 Indent() << "@throw";
495 if (Node->getThrowExpr()) {
496 OS << " ";
497 PrintExpr(Node->getThrowExpr());
498 }
499 OS << ";\n";
500}
501
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000502void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000503 Indent() << "@synchronized (";
504 PrintExpr(Node->getSynchExpr());
505 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000506 PrintRawCompoundStmt(Node->getSynchBody());
507 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000508}
509
John McCall31168b02011-06-15 23:02:42 +0000510void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
511 Indent() << "@autoreleasepool";
512 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
513 OS << "\n";
514}
515
Sebastian Redl9b244a82008-12-22 21:35:02 +0000516void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
517 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000518 if (Decl *ExDecl = Node->getExceptionDecl())
519 PrintRawDecl(ExDecl);
520 else
521 OS << "...";
522 OS << ") ";
523 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000524}
525
526void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
527 Indent();
528 PrintRawCXXCatchStmt(Node);
529 OS << "\n";
530}
531
532void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
533 Indent() << "try ";
534 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000535 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000536 OS << " ";
537 PrintRawCXXCatchStmt(Node->getHandler(i));
538 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000539 OS << "\n";
540}
541
John Wiegley1c0675e2011-04-28 01:08:34 +0000542void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
543 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
544 PrintRawCompoundStmt(Node->getTryBlock());
545 SEHExceptStmt *E = Node->getExceptHandler();
546 SEHFinallyStmt *F = Node->getFinallyHandler();
547 if(E)
548 PrintRawSEHExceptHandler(E);
549 else {
550 assert(F && "Must have a finally block...");
551 PrintRawSEHFinallyStmt(F);
552 }
553 OS << "\n";
554}
555
556void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
557 OS << "__finally ";
558 PrintRawCompoundStmt(Node->getBlock());
559 OS << "\n";
560}
561
562void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
563 OS << "__except (";
564 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000565 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000566 PrintRawCompoundStmt(Node->getBlock());
567 OS << "\n";
568}
569
570void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
571 Indent();
572 PrintRawSEHExceptHandler(Node);
573 OS << "\n";
574}
575
576void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
577 Indent();
578 PrintRawSEHFinallyStmt(Node);
579 OS << "\n";
580}
581
Chris Lattner71e23ce2006-11-04 20:18:38 +0000582//===----------------------------------------------------------------------===//
583// Expr printing methods.
584//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000585
Chris Lattner882f7882006-11-04 18:52:07 +0000586void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000587 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
588 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000589 if (Node->hasTemplateKeyword())
590 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000591 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000592 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000593 TemplateSpecializationType::PrintTemplateArgumentList(
594 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000595}
596
John McCall8cd78132009-11-19 22:55:06 +0000597void StmtPrinter::VisitDependentScopeDeclRefExpr(
598 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000599 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
600 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000601 if (Node->hasTemplateKeyword())
602 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000603 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000604 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000605 TemplateSpecializationType::PrintTemplateArgumentList(
606 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000607}
608
John McCalld14a8642009-11-21 08:51:07 +0000609void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000610 if (Node->getQualifier())
611 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000612 if (Node->hasTemplateKeyword())
613 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000614 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000615 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000616 TemplateSpecializationType::PrintTemplateArgumentList(
617 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000618}
619
Steve Naroffe46504b2007-11-12 14:29:37 +0000620void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000621 if (Node->getBase()) {
622 PrintExpr(Node->getBase());
623 OS << (Node->isArrow() ? "->" : ".");
624 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000625 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000626}
627
Steve Naroffec944032008-05-30 00:40:33 +0000628void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000629 if (Node->isSuperReceiver())
630 OS << "super.";
631 else if (Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000632 PrintExpr(Node->getBase());
633 OS << ".";
634 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000635
John McCallb7bd14f2010-12-02 01:19:52 +0000636 if (Node->isImplicitProperty())
637 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
638 else
639 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000640}
641
Ted Kremeneke65b0862012-03-06 20:05:56 +0000642void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
643
644 PrintExpr(Node->getBaseExpr());
645 OS << "[";
646 PrintExpr(Node->getKeyExpr());
647 OS << "]";
648}
649
Chris Lattner6307f192008-08-10 01:53:14 +0000650void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000651 switch (Node->getIdentType()) {
652 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000653 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000654 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000655 OS << "__func__";
656 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000657 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000658 OS << "__FUNCTION__";
659 break;
Nico Weber3a691a32012-06-23 02:07:59 +0000660 case PredefinedExpr::LFunction:
661 OS << "L__FUNCTION__";
662 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000663 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000664 OS << "__PRETTY_FUNCTION__";
665 break;
666 }
667}
668
Steve Naroffae4143e2007-04-26 20:39:23 +0000669void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000670 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000671
672 switch (Node->getKind()) {
673 case CharacterLiteral::Ascii: break; // no prefix.
674 case CharacterLiteral::Wide: OS << 'L'; break;
675 case CharacterLiteral::UTF16: OS << 'u'; break;
676 case CharacterLiteral::UTF32: OS << 'U'; break;
677 }
678
Chris Lattner666115c2007-07-13 23:58:20 +0000679 switch (value) {
680 case '\\':
681 OS << "'\\\\'";
682 break;
683 case '\'':
684 OS << "'\\''";
685 break;
686 case '\a':
687 // TODO: K&R: the meaning of '\\a' is different in traditional C
688 OS << "'\\a'";
689 break;
690 case '\b':
691 OS << "'\\b'";
692 break;
693 // Nonstandard escape sequence.
694 /*case '\e':
695 OS << "'\\e'";
696 break;*/
697 case '\f':
698 OS << "'\\f'";
699 break;
700 case '\n':
701 OS << "'\\n'";
702 break;
703 case '\r':
704 OS << "'\\r'";
705 break;
706 case '\t':
707 OS << "'\\t'";
708 break;
709 case '\v':
710 OS << "'\\v'";
711 break;
712 default:
Jordan Rose00d1b592013-02-08 22:30:27 +0000713 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +0000714 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +0000715 else if (value < 256)
716 OS << "'\\x" << llvm::format("%02x", value) << "'";
717 else if (value <= 0xFFFF)
718 OS << "'\\u" << llvm::format("%04x", value) << "'";
719 else
720 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000721 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000722}
723
Steve Naroffdf7855b2007-02-21 23:46:25 +0000724void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000725 bool isSigned = Node->getType()->isSignedIntegerType();
726 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000727
Chris Lattner06430412007-05-21 05:45:03 +0000728 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000729 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000730 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000731 // FIXME: The Short and UShort cases are to handle cases where a short
732 // integeral literal is formed during template instantiation. They should
733 // be removed when template instantiation no longer needs integer literals.
734 case BuiltinType::Short:
735 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000736 case BuiltinType::Int: break; // no suffix.
737 case BuiltinType::UInt: OS << 'U'; break;
738 case BuiltinType::Long: OS << 'L'; break;
739 case BuiltinType::ULong: OS << "UL"; break;
740 case BuiltinType::LongLong: OS << "LL"; break;
741 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000742 case BuiltinType::Int128: OS << "i128"; break;
743 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000744 }
Chris Lattner882f7882006-11-04 18:52:07 +0000745}
Benjamin Kramer8a526762012-09-20 14:07:17 +0000746
747static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
748 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000749 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +0000750 Node->getValue().toString(Str);
751 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +0000752 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
753 OS << '.'; // Trailing dot in order to separate from ints.
754
755 if (!PrintSuffix)
756 return;
757
758 // Emit suffixes. Float literals are always a builtin float type.
759 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
760 default: llvm_unreachable("Unexpected type for float literal!");
761 case BuiltinType::Half: break; // FIXME: suffix?
762 case BuiltinType::Double: break; // no suffix.
763 case BuiltinType::Float: OS << 'F'; break;
764 case BuiltinType::LongDouble: OS << 'L'; break;
765 }
766}
767
768void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
769 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +0000770}
Chris Lattner1c20a172007-08-26 03:42:43 +0000771
772void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
773 PrintExpr(Node->getSubExpr());
774 OS << "i";
775}
776
Steve Naroffdf7855b2007-02-21 23:46:25 +0000777void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +0000778 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +0000779}
780void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
781 OS << "(";
782 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000783 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000784}
785void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000786 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000787 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000788
Eli Friedman1cf25362009-06-14 22:39:26 +0000789 // Print a space if this is an "identifier operator" like __real, or if
790 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000791 switch (Node->getOpcode()) {
792 default: break;
John McCalle3027922010-08-25 11:45:40 +0000793 case UO_Real:
794 case UO_Imag:
795 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000796 OS << ' ';
797 break;
John McCalle3027922010-08-25 11:45:40 +0000798 case UO_Plus:
799 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000800 if (isa<UnaryOperator>(Node->getSubExpr()))
801 OS << ' ';
802 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000803 }
804 }
Chris Lattner882f7882006-11-04 18:52:07 +0000805 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000806
Chris Lattner15768702006-11-05 23:54:51 +0000807 if (Node->isPostfix())
808 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000809}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000810
Douglas Gregor882211c2010-04-28 22:16:22 +0000811void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
812 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000813 Node->getTypeSourceInfo()->getType().print(OS, Policy);
814 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000815 bool PrintedSomething = false;
816 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
817 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
818 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
819 // Array node
820 OS << "[";
821 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
822 OS << "]";
823 PrintedSomething = true;
824 continue;
825 }
Douglas Gregord1702062010-04-29 00:18:15 +0000826
827 // Skip implicit base indirections.
828 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
829 continue;
830
Douglas Gregor882211c2010-04-28 22:16:22 +0000831 // Field or identifier node.
832 IdentifierInfo *Id = ON.getFieldName();
833 if (!Id)
834 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000835
Douglas Gregor882211c2010-04-28 22:16:22 +0000836 if (PrintedSomething)
837 OS << ".";
838 else
839 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000840 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000841 }
842 OS << ")";
843}
844
Peter Collingbournee190dee2011-03-11 19:24:49 +0000845void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
846 switch(Node->getKind()) {
847 case UETT_SizeOf:
848 OS << "sizeof";
849 break;
850 case UETT_AlignOf:
Jordan Rose58d54722012-06-30 21:33:57 +0000851 if (Policy.LangOpts.CPlusPlus)
852 OS << "alignof";
853 else if (Policy.LangOpts.C11)
854 OS << "_Alignof";
855 else
856 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +0000857 break;
858 case UETT_VecStep:
859 OS << "vec_step";
860 break;
861 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000862 if (Node->isArgumentType()) {
863 OS << '(';
864 Node->getArgumentType().print(OS, Policy);
865 OS << ')';
866 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +0000867 OS << " ";
868 PrintExpr(Node->getArgumentExpr());
869 }
Chris Lattner882f7882006-11-04 18:52:07 +0000870}
Peter Collingbourne91147592011-04-15 00:35:48 +0000871
872void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
873 OS << "_Generic(";
874 PrintExpr(Node->getControllingExpr());
875 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
876 OS << ", ";
877 QualType T = Node->getAssocType(i);
878 if (T.isNull())
879 OS << "default";
880 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000881 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +0000882 OS << ": ";
883 PrintExpr(Node->getAssocExpr(i));
884 }
885 OS << ")";
886}
887
Chris Lattner882f7882006-11-04 18:52:07 +0000888void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000889 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000890 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000891 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000892 OS << "]";
893}
894
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000895void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +0000896 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000897 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
898 // Don't print any defaulted arguments
899 break;
900 }
901
Chris Lattner882f7882006-11-04 18:52:07 +0000902 if (i) OS << ", ";
903 PrintExpr(Call->getArg(i));
904 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000905}
906
907void StmtPrinter::VisitCallExpr(CallExpr *Call) {
908 PrintExpr(Call->getCallee());
909 OS << "(";
910 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +0000911 OS << ")";
912}
913void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000914 // FIXME: Suppress printing implicit bases (like "this")
915 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +0000916
917 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +0000918 FieldDecl *ParentDecl = ParentMember
919 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
David Blaikie8fab8e52012-11-12 19:12:12 +0000920
David Blaikie6bffd6f2012-11-12 19:32:32 +0000921 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +0000922 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +0000923
Fariborz Jahanian2990c022010-01-11 21:17:32 +0000924 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
925 if (FD->isAnonymousStructOrUnion())
926 return;
David Blaikie8fab8e52012-11-12 19:12:12 +0000927
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000928 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
929 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000930 if (Node->hasTemplateKeyword())
931 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000932 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000933 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000934 TemplateSpecializationType::PrintTemplateArgumentList(
935 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000936}
Steve Naroffe87026a2009-07-24 17:54:45 +0000937void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
938 PrintExpr(Node->getBase());
939 OS << (Node->isArrow() ? "->isa" : ".isa");
940}
941
Nate Begemance4d7fc2008-04-18 23:10:10 +0000942void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000943 PrintExpr(Node->getBase());
944 OS << ".";
945 OS << Node->getAccessor().getName();
946}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000947void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000948 OS << '(';
949 Node->getTypeAsWritten().print(OS, Policy);
950 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +0000951 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000952}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000953void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000954 OS << '(';
955 Node->getType().print(OS, Policy);
956 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +0000957 PrintExpr(Node->getInitializer());
958}
Steve Naroff7a5af782007-07-13 16:58:59 +0000959void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000960 // No need to print anything, simply forward to the sub expression.
961 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000962}
Chris Lattner882f7882006-11-04 18:52:07 +0000963void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
964 PrintExpr(Node->getLHS());
965 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
966 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000967}
Chris Lattner86928112007-08-25 02:00:02 +0000968void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
969 PrintExpr(Node->getLHS());
970 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
971 PrintExpr(Node->getRHS());
972}
Chris Lattner882f7882006-11-04 18:52:07 +0000973void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
974 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +0000975 OS << " ? ";
976 PrintExpr(Node->getLHS());
977 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000978 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000979}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000980
Chris Lattnereefa10e2007-05-28 06:56:27 +0000981// GNU extensions.
982
John McCallc07a0c72011-02-17 10:25:35 +0000983void
984StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
985 PrintExpr(Node->getCommon());
986 OS << " ?: ";
987 PrintExpr(Node->getFalseExpr());
988}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000989void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000990 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000991}
992
Chris Lattner366727f2007-07-24 16:58:17 +0000993void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
994 OS << "(";
995 PrintRawCompoundStmt(E->getSubStmt());
996 OS << ")";
997}
998
Steve Naroff9efdabc2007-08-03 21:21:27 +0000999void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1000 OS << "__builtin_choose_expr(";
1001 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001002 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001003 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001004 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001005 PrintExpr(Node->getRHS());
1006 OS << ")";
1007}
Chris Lattner366727f2007-07-24 16:58:17 +00001008
Douglas Gregor3be4b122008-11-29 04:51:27 +00001009void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1010 OS << "__null";
1011}
1012
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001013void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1014 OS << "__builtin_shufflevector(";
1015 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1016 if (i) OS << ", ";
1017 PrintExpr(Node->getExpr(i));
1018 }
1019 OS << ")";
1020}
1021
Anders Carlsson4692db02007-08-31 04:56:16 +00001022void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001023 if (Node->getSyntacticForm()) {
1024 Visit(Node->getSyntacticForm());
1025 return;
1026 }
1027
Anders Carlsson4692db02007-08-31 04:56:16 +00001028 OS << "{ ";
1029 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1030 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001031 if (Node->getInit(i))
1032 PrintExpr(Node->getInit(i));
1033 else
1034 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +00001035 }
1036 OS << " }";
1037}
1038
Nate Begeman5ec4b312009-08-10 23:49:36 +00001039void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1040 OS << "( ";
1041 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1042 if (i) OS << ", ";
1043 PrintExpr(Node->getExpr(i));
1044 }
1045 OS << " )";
1046}
1047
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001048void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001049 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1050 DEnd = Node->designators_end();
1051 D != DEnd; ++D) {
1052 if (D->isFieldDesignator()) {
1053 if (D->getDotLoc().isInvalid())
1054 OS << D->getFieldName()->getName() << ":";
1055 else
1056 OS << "." << D->getFieldName()->getName();
1057 } else {
1058 OS << "[";
1059 if (D->isArrayDesignator()) {
1060 PrintExpr(Node->getArrayIndex(*D));
1061 } else {
1062 PrintExpr(Node->getArrayRangeStart(*D));
1063 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001064 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001065 }
1066 OS << "]";
1067 }
1068 }
1069
1070 OS << " = ";
1071 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001072}
1073
Douglas Gregor0202cb42009-01-29 17:44:32 +00001074void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001075 if (Policy.LangOpts.CPlusPlus) {
1076 OS << "/*implicit*/";
1077 Node->getType().print(OS, Policy);
1078 OS << "()";
1079 } else {
1080 OS << "/*implicit*/(";
1081 Node->getType().print(OS, Policy);
1082 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001083 if (Node->getType()->isRecordType())
1084 OS << "{}";
1085 else
1086 OS << 0;
1087 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001088}
1089
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001090void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001091 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001092 PrintExpr(Node->getSubExpr());
1093 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001094 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001095 OS << ")";
1096}
1097
John McCallfe96e0b2011-11-06 09:01:30 +00001098void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1099 PrintExpr(Node->getSyntacticForm());
1100}
1101
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001102void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001103 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001104 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001105#define BUILTIN(ID, TYPE, ATTRS)
1106#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1107 case AtomicExpr::AO ## ID: \
1108 Name = #ID "("; \
1109 break;
1110#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001111 }
1112 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001113
1114 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001115 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001116 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1117 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001118 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001119 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001120 }
Richard Smithfeea8832012-04-12 05:08:17 +00001121 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1122 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001123 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001124 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001125 }
Richard Smithfeea8832012-04-12 05:08:17 +00001126 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1127 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001128 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001129 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001130 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001131 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1132 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001133 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001134 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001135 if (Node->isCmpXChg()) {
1136 OS << ", ";
1137 PrintExpr(Node->getOrderFail());
1138 }
1139 OS << ")";
1140}
1141
Chris Lattnereefa10e2007-05-28 06:56:27 +00001142// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001143void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1144 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1145 "",
1146#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1147 Spelling,
1148#include "clang/Basic/OperatorKinds.def"
1149 };
1150
1151 OverloadedOperatorKind Kind = Node->getOperator();
1152 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1153 if (Node->getNumArgs() == 1) {
1154 OS << OpStrings[Kind] << ' ';
1155 PrintExpr(Node->getArg(0));
1156 } else {
1157 PrintExpr(Node->getArg(0));
1158 OS << ' ' << OpStrings[Kind];
1159 }
Eli Friedman8e236422012-10-12 22:45:14 +00001160 } else if (Kind == OO_Arrow) {
1161 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001162 } else if (Kind == OO_Call) {
1163 PrintExpr(Node->getArg(0));
1164 OS << '(';
1165 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1166 if (ArgIdx > 1)
1167 OS << ", ";
1168 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1169 PrintExpr(Node->getArg(ArgIdx));
1170 }
1171 OS << ')';
1172 } else if (Kind == OO_Subscript) {
1173 PrintExpr(Node->getArg(0));
1174 OS << '[';
1175 PrintExpr(Node->getArg(1));
1176 OS << ']';
1177 } else if (Node->getNumArgs() == 1) {
1178 OS << OpStrings[Kind] << ' ';
1179 PrintExpr(Node->getArg(0));
1180 } else if (Node->getNumArgs() == 2) {
1181 PrintExpr(Node->getArg(0));
1182 OS << ' ' << OpStrings[Kind] << ' ';
1183 PrintExpr(Node->getArg(1));
1184 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001185 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001186 }
1187}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001188
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001189void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1190 VisitCallExpr(cast<CallExpr>(Node));
1191}
1192
Peter Collingbourne41f85462011-02-09 21:07:24 +00001193void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1194 PrintExpr(Node->getCallee());
1195 OS << "<<<";
1196 PrintCallArgs(Node->getConfig());
1197 OS << ">>>(";
1198 PrintCallArgs(Node);
1199 OS << ")";
1200}
1201
Douglas Gregore200adc2008-10-27 19:41:14 +00001202void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1203 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001204 Node->getTypeAsWritten().print(OS, Policy);
1205 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001206 PrintExpr(Node->getSubExpr());
1207 OS << ")";
1208}
1209
Douglas Gregore200adc2008-10-27 19:41:14 +00001210void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1211 VisitCXXNamedCastExpr(Node);
1212}
1213
1214void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1215 VisitCXXNamedCastExpr(Node);
1216}
1217
1218void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1219 VisitCXXNamedCastExpr(Node);
1220}
1221
1222void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1223 VisitCXXNamedCastExpr(Node);
1224}
1225
Sebastian Redlc4704762008-11-11 11:37:55 +00001226void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1227 OS << "typeid(";
1228 if (Node->isTypeOperand()) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001229 Node->getTypeOperand().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001230 } else {
1231 PrintExpr(Node->getExprOperand());
1232 }
1233 OS << ")";
1234}
1235
Francois Pichet9f4f2072010-09-08 12:20:18 +00001236void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1237 OS << "__uuidof(";
1238 if (Node->isTypeOperand()) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001239 Node->getTypeOperand().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001240 } else {
1241 PrintExpr(Node->getExprOperand());
1242 }
1243 OS << ")";
1244}
1245
John McCall5e77d762013-04-16 07:28:30 +00001246void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1247 PrintExpr(Node->getBaseExpr());
1248 if (Node->isArrow())
1249 OS << "->";
1250 else
1251 OS << ".";
1252 if (NestedNameSpecifier *Qualifier =
1253 Node->getQualifierLoc().getNestedNameSpecifier())
1254 Qualifier->print(OS, Policy);
1255 OS << Node->getPropertyDecl()->getDeclName();
1256}
1257
Richard Smithc67fdd42012-03-07 08:35:16 +00001258void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1259 switch (Node->getLiteralOperatorKind()) {
1260 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001261 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001262 break;
1263 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001264 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1265 const TemplateArgumentList *Args =
1266 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1267 assert(Args);
1268 const TemplateArgument &Pack = Args->get(0);
1269 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1270 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001271 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001272 OS << C;
1273 }
1274 break;
1275 }
Richard Smith75025ba2012-03-08 09:02:38 +00001276 case UserDefinedLiteral::LOK_Integer: {
1277 // Print integer literal without suffix.
1278 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1279 OS << Int->getValue().toString(10, /*isSigned*/false);
1280 break;
1281 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001282 case UserDefinedLiteral::LOK_Floating: {
1283 // Print floating literal without suffix.
1284 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1285 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1286 break;
1287 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001288 case UserDefinedLiteral::LOK_String:
1289 case UserDefinedLiteral::LOK_Character:
1290 PrintExpr(Node->getCookedLiteral());
1291 break;
1292 }
1293 OS << Node->getUDSuffix()->getName();
1294}
1295
Chris Lattnereefa10e2007-05-28 06:56:27 +00001296void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1297 OS << (Node->getValue() ? "true" : "false");
1298}
1299
Sebastian Redl576fd422009-05-10 18:38:11 +00001300void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1301 OS << "nullptr";
1302}
1303
Douglas Gregor97a9c812008-11-04 14:32:21 +00001304void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1305 OS << "this";
1306}
1307
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001308void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1309 if (Node->getSubExpr() == 0)
1310 OS << "throw";
1311 else {
1312 OS << "throw ";
1313 PrintExpr(Node->getSubExpr());
1314 }
1315}
1316
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001317void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001318 // Nothing to print: we picked up the default argument.
1319}
1320
1321void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1322 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001323}
1324
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001325void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001326 Node->getType().print(OS, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001327 OS << "(";
1328 PrintExpr(Node->getSubExpr());
1329 OS << ")";
1330}
1331
Anders Carlsson993a4b32009-05-30 20:03:25 +00001332void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1333 PrintExpr(Node->getSubExpr());
1334}
1335
Douglas Gregordd04d332009-01-16 18:33:17 +00001336void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001337 Node->getType().print(OS, Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001338 OS << "(";
1339 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001340 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001341 Arg != ArgEnd; ++Arg) {
1342 if (Arg != Node->arg_begin())
1343 OS << ", ";
1344 PrintExpr(*Arg);
1345 }
1346 OS << ")";
1347}
1348
Douglas Gregore31e6062012-02-07 10:09:13 +00001349void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1350 OS << '[';
1351 bool NeedComma = false;
1352 switch (Node->getCaptureDefault()) {
1353 case LCD_None:
1354 break;
1355
1356 case LCD_ByCopy:
1357 OS << '=';
1358 NeedComma = true;
1359 break;
1360
1361 case LCD_ByRef:
1362 OS << '&';
1363 NeedComma = true;
1364 break;
1365 }
1366 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1367 CEnd = Node->explicit_capture_end();
1368 C != CEnd;
1369 ++C) {
1370 if (NeedComma)
1371 OS << ", ";
1372 NeedComma = true;
1373
1374 switch (C->getCaptureKind()) {
1375 case LCK_This:
1376 OS << "this";
1377 break;
1378
1379 case LCK_ByRef:
1380 if (Node->getCaptureDefault() != LCD_ByRef)
1381 OS << '&';
1382 OS << C->getCapturedVar()->getName();
1383 break;
1384
1385 case LCK_ByCopy:
1386 if (Node->getCaptureDefault() != LCD_ByCopy)
1387 OS << '=';
1388 OS << C->getCapturedVar()->getName();
1389 break;
1390 }
1391 }
1392 OS << ']';
1393
1394 if (Node->hasExplicitParameters()) {
1395 OS << " (";
1396 CXXMethodDecl *Method = Node->getCallOperator();
1397 NeedComma = false;
1398 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1399 PEnd = Method->param_end();
1400 P != PEnd; ++P) {
1401 if (NeedComma) {
1402 OS << ", ";
1403 } else {
1404 NeedComma = true;
1405 }
1406 std::string ParamStr = (*P)->getNameAsString();
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001407 (*P)->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001408 }
1409 if (Method->isVariadic()) {
1410 if (NeedComma)
1411 OS << ", ";
1412 OS << "...";
1413 }
1414 OS << ')';
1415
1416 if (Node->isMutable())
1417 OS << " mutable";
1418
1419 const FunctionProtoType *Proto
1420 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001421 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001422
Bill Wendling44426052012-12-20 19:22:21 +00001423 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00001424
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001425 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001426 if (Node->hasExplicitResultType()) {
1427 OS << " -> ";
1428 Proto->getResultType().print(OS, Policy);
1429 }
Douglas Gregore31e6062012-02-07 10:09:13 +00001430 }
1431
1432 // Print the body.
1433 CompoundStmt *Body = Node->getBody();
1434 OS << ' ';
1435 PrintStmt(Body);
1436}
1437
Douglas Gregor747eb782010-07-08 06:14:04 +00001438void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001439 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001440 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001441 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001442 Node->getType().print(OS, Policy);
1443 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001444}
1445
Sebastian Redlbd150f42008-11-21 19:14:01 +00001446void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1447 if (E->isGlobalNew())
1448 OS << "::";
1449 OS << "new ";
1450 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001451 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001452 OS << "(";
1453 PrintExpr(E->getPlacementArg(0));
1454 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001455 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1456 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001457 OS << ", ";
1458 PrintExpr(E->getPlacementArg(i));
1459 }
1460 OS << ") ";
1461 }
1462 if (E->isParenTypeId())
1463 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001464 std::string TypeS;
1465 if (Expr *Size = E->getArraySize()) {
1466 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001467 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00001468 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001469 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001470 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001471 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001472 if (E->isParenTypeId())
1473 OS << ")";
1474
Sebastian Redl6047f072012-02-16 12:22:20 +00001475 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1476 if (InitStyle) {
1477 if (InitStyle == CXXNewExpr::CallInit)
1478 OS << "(";
1479 PrintExpr(E->getInitializer());
1480 if (InitStyle == CXXNewExpr::CallInit)
1481 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001482 }
1483}
1484
1485void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1486 if (E->isGlobalDelete())
1487 OS << "::";
1488 OS << "delete ";
1489 if (E->isArrayForm())
1490 OS << "[] ";
1491 PrintExpr(E->getArgument());
1492}
1493
Douglas Gregorad8a3362009-09-04 17:36:40 +00001494void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1495 PrintExpr(E->getBase());
1496 if (E->isArrow())
1497 OS << "->";
1498 else
1499 OS << '.';
1500 if (E->getQualifier())
1501 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00001502 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00001503
Douglas Gregor678f90d2010-02-25 01:56:36 +00001504 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1505 OS << II->getName();
1506 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001507 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001508}
1509
Anders Carlsson0781ce72009-04-23 02:32:43 +00001510void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithd59b8322012-12-19 01:39:02 +00001511 if (E->isListInitialization())
1512 OS << "{ ";
1513
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001514 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1515 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1516 // Don't print any defaulted arguments
1517 break;
1518 }
1519
1520 if (i) OS << ", ";
1521 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001522 }
Richard Smithd59b8322012-12-19 01:39:02 +00001523
1524 if (E->isListInitialization())
1525 OS << " }";
Anders Carlsson0781ce72009-04-23 02:32:43 +00001526}
1527
John McCall5d413782010-12-06 08:20:24 +00001528void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001529 // Just forward to the sub expression.
1530 PrintExpr(E->getSubExpr());
1531}
1532
Mike Stump11289f42009-09-09 15:08:12 +00001533void
Douglas Gregorce934142009-05-20 18:46:25 +00001534StmtPrinter::VisitCXXUnresolvedConstructExpr(
1535 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001536 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001537 OS << "(";
1538 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001539 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001540 Arg != ArgEnd; ++Arg) {
1541 if (Arg != Node->arg_begin())
1542 OS << ", ";
1543 PrintExpr(*Arg);
1544 }
1545 OS << ")";
1546}
1547
John McCall8cd78132009-11-19 22:55:06 +00001548void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1549 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001550 if (!Node->isImplicitAccess()) {
1551 PrintExpr(Node->getBase());
1552 OS << (Node->isArrow() ? "->" : ".");
1553 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001554 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1555 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001556 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001557 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001558 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001559 if (Node->hasExplicitTemplateArgs())
1560 TemplateSpecializationType::PrintTemplateArgumentList(
1561 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00001562}
1563
John McCall10eae182009-11-30 22:42:35 +00001564void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001565 if (!Node->isImplicitAccess()) {
1566 PrintExpr(Node->getBase());
1567 OS << (Node->isArrow() ? "->" : ".");
1568 }
John McCall10eae182009-11-30 22:42:35 +00001569 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1570 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001571 if (Node->hasTemplateKeyword())
1572 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001573 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001574 if (Node->hasExplicitTemplateArgs())
1575 TemplateSpecializationType::PrintTemplateArgumentList(
1576 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00001577}
1578
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001579static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1580 switch (UTT) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001581 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
Joao Matosc9523d42013-03-27 01:34:16 +00001582 case UTT_HasNothrowMoveAssign: return "__has_nothrow_move_assign";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001583 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001584 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001585 case UTT_HasTrivialAssign: return "__has_trivial_assign";
Joao Matosc9523d42013-03-27 01:34:16 +00001586 case UTT_HasTrivialMoveAssign: return "__has_trivial_move_assign";
1587 case UTT_HasTrivialMoveConstructor: return "__has_trivial_move_constructor";
Alexis Huntf479f1b2011-05-09 18:22:59 +00001588 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001589 case UTT_HasTrivialCopy: return "__has_trivial_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001590 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1591 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1592 case UTT_IsAbstract: return "__is_abstract";
John Wiegley65497cc2011-04-27 23:09:49 +00001593 case UTT_IsArithmetic: return "__is_arithmetic";
1594 case UTT_IsArray: return "__is_array";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001595 case UTT_IsClass: return "__is_class";
John Wiegley65497cc2011-04-27 23:09:49 +00001596 case UTT_IsCompleteType: return "__is_complete_type";
1597 case UTT_IsCompound: return "__is_compound";
1598 case UTT_IsConst: return "__is_const";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001599 case UTT_IsEmpty: return "__is_empty";
1600 case UTT_IsEnum: return "__is_enum";
Douglas Gregordca70af2011-12-03 18:14:24 +00001601 case UTT_IsFinal: return "__is_final";
John Wiegley65497cc2011-04-27 23:09:49 +00001602 case UTT_IsFloatingPoint: return "__is_floating_point";
1603 case UTT_IsFunction: return "__is_function";
1604 case UTT_IsFundamental: return "__is_fundamental";
1605 case UTT_IsIntegral: return "__is_integral";
John McCallbf4a7d72012-09-25 07:32:49 +00001606 case UTT_IsInterfaceClass: return "__is_interface_class";
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001607 case UTT_IsLiteral: return "__is_literal";
John Wiegley65497cc2011-04-27 23:09:49 +00001608 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1609 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1610 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1611 case UTT_IsMemberPointer: return "__is_member_pointer";
1612 case UTT_IsObject: return "__is_object";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001613 case UTT_IsPOD: return "__is_pod";
John Wiegley65497cc2011-04-27 23:09:49 +00001614 case UTT_IsPointer: return "__is_pointer";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001615 case UTT_IsPolymorphic: return "__is_polymorphic";
John Wiegley65497cc2011-04-27 23:09:49 +00001616 case UTT_IsReference: return "__is_reference";
John Wiegley65497cc2011-04-27 23:09:49 +00001617 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1618 case UTT_IsScalar: return "__is_scalar";
1619 case UTT_IsSigned: return "__is_signed";
1620 case UTT_IsStandardLayout: return "__is_standard_layout";
1621 case UTT_IsTrivial: return "__is_trivial";
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001622 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001623 case UTT_IsUnion: return "__is_union";
John Wiegley65497cc2011-04-27 23:09:49 +00001624 case UTT_IsUnsigned: return "__is_unsigned";
1625 case UTT_IsVoid: return "__is_void";
1626 case UTT_IsVolatile: return "__is_volatile";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001627 }
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001628 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001629}
1630
1631static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1632 switch (BTT) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00001633 case BTT_IsBaseOf: return "__is_base_of";
1634 case BTT_IsConvertible: return "__is_convertible";
1635 case BTT_IsSame: return "__is_same";
1636 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
1637 case BTT_IsConvertibleTo: return "__is_convertible_to";
1638 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001639 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001640 llvm_unreachable("Binary type trait not covered by switch");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001641}
1642
Douglas Gregor29c42f22012-02-24 07:38:34 +00001643static const char *getTypeTraitName(TypeTrait TT) {
1644 switch (TT) {
1645 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1646 }
1647 llvm_unreachable("Type trait not covered by switch");
1648}
1649
John Wiegley6242b6a2011-04-28 00:16:57 +00001650static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1651 switch (ATT) {
1652 case ATT_ArrayRank: return "__array_rank";
1653 case ATT_ArrayExtent: return "__array_extent";
1654 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001655 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001656}
1657
John Wiegleyf9f65842011-04-25 06:54:41 +00001658static const char *getExpressionTraitName(ExpressionTrait ET) {
1659 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001660 case ET_IsLValueExpr: return "__is_lvalue_expr";
1661 case ET_IsRValueExpr: return "__is_rvalue_expr";
1662 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001663 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001664}
1665
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001666void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001667 OS << getTypeTraitName(E->getTrait()) << '(';
1668 E->getQueriedType().print(OS, Policy);
1669 OS << ')';
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001670}
1671
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001672void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001673 OS << getTypeTraitName(E->getTrait()) << '(';
1674 E->getLhsType().print(OS, Policy);
1675 OS << ',';
1676 E->getRhsType().print(OS, Policy);
1677 OS << ')';
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001678}
1679
Douglas Gregor29c42f22012-02-24 07:38:34 +00001680void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1681 OS << getTypeTraitName(E->getTrait()) << "(";
1682 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1683 if (I > 0)
1684 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001685 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00001686 }
1687 OS << ")";
1688}
1689
John Wiegley6242b6a2011-04-28 00:16:57 +00001690void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001691 OS << getTypeTraitName(E->getTrait()) << '(';
1692 E->getQueriedType().print(OS, Policy);
1693 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00001694}
1695
John Wiegleyf9f65842011-04-25 06:54:41 +00001696void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001697 OS << getExpressionTraitName(E->getTrait()) << '(';
1698 PrintExpr(E->getQueriedExpression());
1699 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00001700}
1701
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001702void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1703 OS << "noexcept(";
1704 PrintExpr(E->getOperand());
1705 OS << ")";
1706}
1707
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001708void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001709 PrintExpr(E->getPattern());
1710 OS << "...";
1711}
1712
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001713void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001714 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001715}
1716
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001717void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1718 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001719 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001720}
1721
John McCall7c454bb2011-07-15 05:09:51 +00001722void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1723 SubstNonTypeTemplateParmExpr *Node) {
1724 Visit(Node->getReplacement());
1725}
1726
Richard Smithb15fe3a2012-09-12 00:56:43 +00001727void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1728 OS << *E->getParameterPack();
1729}
1730
Douglas Gregorfe314812011-06-21 17:03:29 +00001731void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1732 PrintExpr(Node->GetTemporaryExpr());
1733}
1734
Mike Stump11289f42009-09-09 15:08:12 +00001735// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001736
1737void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1738 OS << "@";
1739 VisitStringLiteral(Node->getString());
1740}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001741
Patrick Beard0caa3942012-04-19 00:25:12 +00001742void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001743 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00001744 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001745}
1746
1747void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1748 OS << "@[ ";
1749 StmtRange ch = E->children();
1750 if (ch.first != ch.second) {
1751 while (1) {
1752 Visit(*ch.first);
1753 ++ch.first;
1754 if (ch.first == ch.second) break;
1755 OS << ", ";
1756 }
1757 }
1758 OS << " ]";
1759}
1760
1761void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1762 OS << "@{ ";
1763 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1764 if (I > 0)
1765 OS << ", ";
1766
1767 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1768 Visit(Element.Key);
1769 OS << " : ";
1770 Visit(Element.Value);
1771 if (Element.isPackExpansion())
1772 OS << "...";
1773 }
1774 OS << " }";
1775}
1776
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001777void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001778 OS << "@encode(";
1779 Node->getEncodedType().print(OS, Policy);
1780 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001781}
1782
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001783void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001784 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001785}
1786
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001787void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001788 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001789}
1790
Steve Naroffd54978b2007-09-18 23:55:05 +00001791void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1792 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001793 switch (Mess->getReceiverKind()) {
1794 case ObjCMessageExpr::Instance:
1795 PrintExpr(Mess->getInstanceReceiver());
1796 break;
1797
1798 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001799 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00001800 break;
1801
1802 case ObjCMessageExpr::SuperInstance:
1803 case ObjCMessageExpr::SuperClass:
1804 OS << "Super";
1805 break;
1806 }
1807
Ted Kremeneka06e7122008-05-02 17:32:38 +00001808 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001809 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001810 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001811 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001812 } else {
1813 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001814 if (i < selector.getNumArgs()) {
1815 if (i > 0) OS << ' ';
1816 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001817 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001818 else
1819 OS << ":";
1820 }
1821 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001822
Steve Naroffc6814ea2007-10-02 20:01:56 +00001823 PrintExpr(Mess->getArg(i));
1824 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001825 }
1826 OS << "]";
1827}
1828
Ted Kremeneke65b0862012-03-06 20:05:56 +00001829void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1830 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1831}
1832
John McCall31168b02011-06-15 23:02:42 +00001833void
1834StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1835 PrintExpr(E->getSubExpr());
1836}
1837
1838void
1839StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001840 OS << '(' << E->getBridgeKindName();
1841 E->getType().print(OS, Policy);
1842 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00001843 PrintExpr(E->getSubExpr());
1844}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001845
Steve Naroffc540d662008-09-03 18:15:37 +00001846void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001847 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001848 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001849
Steve Naroffc540d662008-09-03 18:15:37 +00001850 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001851
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001852 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001853 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001854 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001855 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00001856 for (BlockDecl::param_iterator AI = BD->param_begin(),
1857 E = BD->param_end(); AI != E; ++AI) {
1858 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001859 std::string ParamStr = (*AI)->getNameAsString();
1860 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00001861 }
Mike Stump11289f42009-09-09 15:08:12 +00001862
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001863 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001864 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001865 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001866 OS << "...";
1867 }
1868 OS << ')';
1869 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00001870 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00001871}
1872
Ted Kremenekf551f322011-11-30 22:08:08 +00001873void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1874 PrintExpr(Node->getSourceExpr());
1875}
John McCall8d69a212010-11-15 23:31:06 +00001876
Tanya Lattner55808c12011-06-04 00:47:47 +00001877void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1878 OS << "__builtin_astype(";
1879 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001880 OS << ", ";
1881 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00001882 OS << ")";
1883}
1884
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001885//===----------------------------------------------------------------------===//
1886// Stmt method implementations
1887//===----------------------------------------------------------------------===//
1888
Richard Smith235341b2012-08-16 03:56:14 +00001889void Stmt::dumpPretty(ASTContext &Context) const {
1890 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001891}
1892
Richard Smith235341b2012-08-16 03:56:14 +00001893void Stmt::printPretty(raw_ostream &OS,
1894 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001895 const PrintingPolicy &Policy,
1896 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001897 if (this == 0) {
1898 OS << "<NULL>";
1899 return;
1900 }
1901
Richard Smith235341b2012-08-16 03:56:14 +00001902 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001903 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001904}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001905
1906//===----------------------------------------------------------------------===//
1907// PrinterHelper
1908//===----------------------------------------------------------------------===//
1909
1910// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001911PrinterHelper::~PrinterHelper() {}