blob: 1b2285c7948b3670c31bcd4a267513db1bc467b8 [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";
448 OS << *(Node->getAsmString()) << "\n";
449 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());
1116 OS << ", ";
Richard Smithfeea8832012-04-12 05:08:17 +00001117 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1118 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001119 PrintExpr(Node->getVal1());
1120 OS << ", ";
1121 }
Richard Smithfeea8832012-04-12 05:08:17 +00001122 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1123 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001124 PrintExpr(Node->getVal2());
1125 OS << ", ";
1126 }
Richard Smithfeea8832012-04-12 05:08:17 +00001127 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1128 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1129 PrintExpr(Node->getWeak());
1130 OS << ", ";
1131 }
1132 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init)
David Chisnallfa35df62012-01-16 17:27:18 +00001133 PrintExpr(Node->getOrder());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001134 if (Node->isCmpXChg()) {
1135 OS << ", ";
1136 PrintExpr(Node->getOrderFail());
1137 }
1138 OS << ")";
1139}
1140
Chris Lattnereefa10e2007-05-28 06:56:27 +00001141// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001142void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1143 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1144 "",
1145#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1146 Spelling,
1147#include "clang/Basic/OperatorKinds.def"
1148 };
1149
1150 OverloadedOperatorKind Kind = Node->getOperator();
1151 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1152 if (Node->getNumArgs() == 1) {
1153 OS << OpStrings[Kind] << ' ';
1154 PrintExpr(Node->getArg(0));
1155 } else {
1156 PrintExpr(Node->getArg(0));
1157 OS << ' ' << OpStrings[Kind];
1158 }
Eli Friedman8e236422012-10-12 22:45:14 +00001159 } else if (Kind == OO_Arrow) {
1160 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001161 } else if (Kind == OO_Call) {
1162 PrintExpr(Node->getArg(0));
1163 OS << '(';
1164 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1165 if (ArgIdx > 1)
1166 OS << ", ";
1167 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1168 PrintExpr(Node->getArg(ArgIdx));
1169 }
1170 OS << ')';
1171 } else if (Kind == OO_Subscript) {
1172 PrintExpr(Node->getArg(0));
1173 OS << '[';
1174 PrintExpr(Node->getArg(1));
1175 OS << ']';
1176 } else if (Node->getNumArgs() == 1) {
1177 OS << OpStrings[Kind] << ' ';
1178 PrintExpr(Node->getArg(0));
1179 } else if (Node->getNumArgs() == 2) {
1180 PrintExpr(Node->getArg(0));
1181 OS << ' ' << OpStrings[Kind] << ' ';
1182 PrintExpr(Node->getArg(1));
1183 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001184 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001185 }
1186}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001187
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001188void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1189 VisitCallExpr(cast<CallExpr>(Node));
1190}
1191
Peter Collingbourne41f85462011-02-09 21:07:24 +00001192void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1193 PrintExpr(Node->getCallee());
1194 OS << "<<<";
1195 PrintCallArgs(Node->getConfig());
1196 OS << ">>>(";
1197 PrintCallArgs(Node);
1198 OS << ")";
1199}
1200
Douglas Gregore200adc2008-10-27 19:41:14 +00001201void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1202 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001203 Node->getTypeAsWritten().print(OS, Policy);
1204 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001205 PrintExpr(Node->getSubExpr());
1206 OS << ")";
1207}
1208
Douglas Gregore200adc2008-10-27 19:41:14 +00001209void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1210 VisitCXXNamedCastExpr(Node);
1211}
1212
1213void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1214 VisitCXXNamedCastExpr(Node);
1215}
1216
1217void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1218 VisitCXXNamedCastExpr(Node);
1219}
1220
1221void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1222 VisitCXXNamedCastExpr(Node);
1223}
1224
Sebastian Redlc4704762008-11-11 11:37:55 +00001225void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1226 OS << "typeid(";
1227 if (Node->isTypeOperand()) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001228 Node->getTypeOperand().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001229 } else {
1230 PrintExpr(Node->getExprOperand());
1231 }
1232 OS << ")";
1233}
1234
Francois Pichet9f4f2072010-09-08 12:20:18 +00001235void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1236 OS << "__uuidof(";
1237 if (Node->isTypeOperand()) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001238 Node->getTypeOperand().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001239 } else {
1240 PrintExpr(Node->getExprOperand());
1241 }
1242 OS << ")";
1243}
1244
John McCall5e77d762013-04-16 07:28:30 +00001245void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1246 PrintExpr(Node->getBaseExpr());
1247 if (Node->isArrow())
1248 OS << "->";
1249 else
1250 OS << ".";
1251 if (NestedNameSpecifier *Qualifier =
1252 Node->getQualifierLoc().getNestedNameSpecifier())
1253 Qualifier->print(OS, Policy);
1254 OS << Node->getPropertyDecl()->getDeclName();
1255}
1256
Richard Smithc67fdd42012-03-07 08:35:16 +00001257void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1258 switch (Node->getLiteralOperatorKind()) {
1259 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001260 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001261 break;
1262 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001263 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1264 const TemplateArgumentList *Args =
1265 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1266 assert(Args);
1267 const TemplateArgument &Pack = Args->get(0);
1268 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1269 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001270 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001271 OS << C;
1272 }
1273 break;
1274 }
Richard Smith75025ba2012-03-08 09:02:38 +00001275 case UserDefinedLiteral::LOK_Integer: {
1276 // Print integer literal without suffix.
1277 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1278 OS << Int->getValue().toString(10, /*isSigned*/false);
1279 break;
1280 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001281 case UserDefinedLiteral::LOK_Floating: {
1282 // Print floating literal without suffix.
1283 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1284 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1285 break;
1286 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001287 case UserDefinedLiteral::LOK_String:
1288 case UserDefinedLiteral::LOK_Character:
1289 PrintExpr(Node->getCookedLiteral());
1290 break;
1291 }
1292 OS << Node->getUDSuffix()->getName();
1293}
1294
Chris Lattnereefa10e2007-05-28 06:56:27 +00001295void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1296 OS << (Node->getValue() ? "true" : "false");
1297}
1298
Sebastian Redl576fd422009-05-10 18:38:11 +00001299void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1300 OS << "nullptr";
1301}
1302
Douglas Gregor97a9c812008-11-04 14:32:21 +00001303void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1304 OS << "this";
1305}
1306
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001307void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1308 if (Node->getSubExpr() == 0)
1309 OS << "throw";
1310 else {
1311 OS << "throw ";
1312 PrintExpr(Node->getSubExpr());
1313 }
1314}
1315
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001316void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1317 // Nothing to print: we picked up the default argument
1318}
1319
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001320void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001321 Node->getType().print(OS, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001322 OS << "(";
1323 PrintExpr(Node->getSubExpr());
1324 OS << ")";
1325}
1326
Anders Carlsson993a4b32009-05-30 20:03:25 +00001327void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1328 PrintExpr(Node->getSubExpr());
1329}
1330
Douglas Gregordd04d332009-01-16 18:33:17 +00001331void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001332 Node->getType().print(OS, Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001333 OS << "(";
1334 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001335 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001336 Arg != ArgEnd; ++Arg) {
1337 if (Arg != Node->arg_begin())
1338 OS << ", ";
1339 PrintExpr(*Arg);
1340 }
1341 OS << ")";
1342}
1343
Douglas Gregore31e6062012-02-07 10:09:13 +00001344void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1345 OS << '[';
1346 bool NeedComma = false;
1347 switch (Node->getCaptureDefault()) {
1348 case LCD_None:
1349 break;
1350
1351 case LCD_ByCopy:
1352 OS << '=';
1353 NeedComma = true;
1354 break;
1355
1356 case LCD_ByRef:
1357 OS << '&';
1358 NeedComma = true;
1359 break;
1360 }
1361 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1362 CEnd = Node->explicit_capture_end();
1363 C != CEnd;
1364 ++C) {
1365 if (NeedComma)
1366 OS << ", ";
1367 NeedComma = true;
1368
1369 switch (C->getCaptureKind()) {
1370 case LCK_This:
1371 OS << "this";
1372 break;
1373
1374 case LCK_ByRef:
1375 if (Node->getCaptureDefault() != LCD_ByRef)
1376 OS << '&';
1377 OS << C->getCapturedVar()->getName();
1378 break;
1379
1380 case LCK_ByCopy:
1381 if (Node->getCaptureDefault() != LCD_ByCopy)
1382 OS << '=';
1383 OS << C->getCapturedVar()->getName();
1384 break;
1385 }
1386 }
1387 OS << ']';
1388
1389 if (Node->hasExplicitParameters()) {
1390 OS << " (";
1391 CXXMethodDecl *Method = Node->getCallOperator();
1392 NeedComma = false;
1393 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1394 PEnd = Method->param_end();
1395 P != PEnd; ++P) {
1396 if (NeedComma) {
1397 OS << ", ";
1398 } else {
1399 NeedComma = true;
1400 }
1401 std::string ParamStr = (*P)->getNameAsString();
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001402 (*P)->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001403 }
1404 if (Method->isVariadic()) {
1405 if (NeedComma)
1406 OS << ", ";
1407 OS << "...";
1408 }
1409 OS << ')';
1410
1411 if (Node->isMutable())
1412 OS << " mutable";
1413
1414 const FunctionProtoType *Proto
1415 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001416 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001417
Bill Wendling44426052012-12-20 19:22:21 +00001418 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00001419
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001420 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001421 if (Node->hasExplicitResultType()) {
1422 OS << " -> ";
1423 Proto->getResultType().print(OS, Policy);
1424 }
Douglas Gregore31e6062012-02-07 10:09:13 +00001425 }
1426
1427 // Print the body.
1428 CompoundStmt *Body = Node->getBody();
1429 OS << ' ';
1430 PrintStmt(Body);
1431}
1432
Douglas Gregor747eb782010-07-08 06:14:04 +00001433void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001434 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001435 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001436 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001437 Node->getType().print(OS, Policy);
1438 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001439}
1440
Sebastian Redlbd150f42008-11-21 19:14:01 +00001441void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1442 if (E->isGlobalNew())
1443 OS << "::";
1444 OS << "new ";
1445 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001446 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001447 OS << "(";
1448 PrintExpr(E->getPlacementArg(0));
1449 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001450 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1451 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001452 OS << ", ";
1453 PrintExpr(E->getPlacementArg(i));
1454 }
1455 OS << ") ";
1456 }
1457 if (E->isParenTypeId())
1458 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001459 std::string TypeS;
1460 if (Expr *Size = E->getArraySize()) {
1461 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001462 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00001463 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001464 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001465 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001466 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001467 if (E->isParenTypeId())
1468 OS << ")";
1469
Sebastian Redl6047f072012-02-16 12:22:20 +00001470 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1471 if (InitStyle) {
1472 if (InitStyle == CXXNewExpr::CallInit)
1473 OS << "(";
1474 PrintExpr(E->getInitializer());
1475 if (InitStyle == CXXNewExpr::CallInit)
1476 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001477 }
1478}
1479
1480void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1481 if (E->isGlobalDelete())
1482 OS << "::";
1483 OS << "delete ";
1484 if (E->isArrayForm())
1485 OS << "[] ";
1486 PrintExpr(E->getArgument());
1487}
1488
Douglas Gregorad8a3362009-09-04 17:36:40 +00001489void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1490 PrintExpr(E->getBase());
1491 if (E->isArrow())
1492 OS << "->";
1493 else
1494 OS << '.';
1495 if (E->getQualifier())
1496 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00001497 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00001498
Douglas Gregor678f90d2010-02-25 01:56:36 +00001499 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1500 OS << II->getName();
1501 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001502 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001503}
1504
Anders Carlsson0781ce72009-04-23 02:32:43 +00001505void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithd59b8322012-12-19 01:39:02 +00001506 if (E->isListInitialization())
1507 OS << "{ ";
1508
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001509 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1510 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1511 // Don't print any defaulted arguments
1512 break;
1513 }
1514
1515 if (i) OS << ", ";
1516 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001517 }
Richard Smithd59b8322012-12-19 01:39:02 +00001518
1519 if (E->isListInitialization())
1520 OS << " }";
Anders Carlsson0781ce72009-04-23 02:32:43 +00001521}
1522
John McCall5d413782010-12-06 08:20:24 +00001523void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001524 // Just forward to the sub expression.
1525 PrintExpr(E->getSubExpr());
1526}
1527
Mike Stump11289f42009-09-09 15:08:12 +00001528void
Douglas Gregorce934142009-05-20 18:46:25 +00001529StmtPrinter::VisitCXXUnresolvedConstructExpr(
1530 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001531 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001532 OS << "(";
1533 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001534 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001535 Arg != ArgEnd; ++Arg) {
1536 if (Arg != Node->arg_begin())
1537 OS << ", ";
1538 PrintExpr(*Arg);
1539 }
1540 OS << ")";
1541}
1542
John McCall8cd78132009-11-19 22:55:06 +00001543void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1544 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001545 if (!Node->isImplicitAccess()) {
1546 PrintExpr(Node->getBase());
1547 OS << (Node->isArrow() ? "->" : ".");
1548 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001549 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1550 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001551 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001552 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001553 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001554 if (Node->hasExplicitTemplateArgs())
1555 TemplateSpecializationType::PrintTemplateArgumentList(
1556 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00001557}
1558
John McCall10eae182009-11-30 22:42:35 +00001559void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001560 if (!Node->isImplicitAccess()) {
1561 PrintExpr(Node->getBase());
1562 OS << (Node->isArrow() ? "->" : ".");
1563 }
John McCall10eae182009-11-30 22:42:35 +00001564 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1565 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001566 if (Node->hasTemplateKeyword())
1567 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001568 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001569 if (Node->hasExplicitTemplateArgs())
1570 TemplateSpecializationType::PrintTemplateArgumentList(
1571 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00001572}
1573
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001574static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1575 switch (UTT) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001576 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
Joao Matosc9523d42013-03-27 01:34:16 +00001577 case UTT_HasNothrowMoveAssign: return "__has_nothrow_move_assign";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001578 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001579 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001580 case UTT_HasTrivialAssign: return "__has_trivial_assign";
Joao Matosc9523d42013-03-27 01:34:16 +00001581 case UTT_HasTrivialMoveAssign: return "__has_trivial_move_assign";
1582 case UTT_HasTrivialMoveConstructor: return "__has_trivial_move_constructor";
Alexis Huntf479f1b2011-05-09 18:22:59 +00001583 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001584 case UTT_HasTrivialCopy: return "__has_trivial_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001585 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1586 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1587 case UTT_IsAbstract: return "__is_abstract";
John Wiegley65497cc2011-04-27 23:09:49 +00001588 case UTT_IsArithmetic: return "__is_arithmetic";
1589 case UTT_IsArray: return "__is_array";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001590 case UTT_IsClass: return "__is_class";
John Wiegley65497cc2011-04-27 23:09:49 +00001591 case UTT_IsCompleteType: return "__is_complete_type";
1592 case UTT_IsCompound: return "__is_compound";
1593 case UTT_IsConst: return "__is_const";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001594 case UTT_IsEmpty: return "__is_empty";
1595 case UTT_IsEnum: return "__is_enum";
Douglas Gregordca70af2011-12-03 18:14:24 +00001596 case UTT_IsFinal: return "__is_final";
John Wiegley65497cc2011-04-27 23:09:49 +00001597 case UTT_IsFloatingPoint: return "__is_floating_point";
1598 case UTT_IsFunction: return "__is_function";
1599 case UTT_IsFundamental: return "__is_fundamental";
1600 case UTT_IsIntegral: return "__is_integral";
John McCallbf4a7d72012-09-25 07:32:49 +00001601 case UTT_IsInterfaceClass: return "__is_interface_class";
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001602 case UTT_IsLiteral: return "__is_literal";
John Wiegley65497cc2011-04-27 23:09:49 +00001603 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1604 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1605 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1606 case UTT_IsMemberPointer: return "__is_member_pointer";
1607 case UTT_IsObject: return "__is_object";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001608 case UTT_IsPOD: return "__is_pod";
John Wiegley65497cc2011-04-27 23:09:49 +00001609 case UTT_IsPointer: return "__is_pointer";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001610 case UTT_IsPolymorphic: return "__is_polymorphic";
John Wiegley65497cc2011-04-27 23:09:49 +00001611 case UTT_IsReference: return "__is_reference";
John Wiegley65497cc2011-04-27 23:09:49 +00001612 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1613 case UTT_IsScalar: return "__is_scalar";
1614 case UTT_IsSigned: return "__is_signed";
1615 case UTT_IsStandardLayout: return "__is_standard_layout";
1616 case UTT_IsTrivial: return "__is_trivial";
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001617 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001618 case UTT_IsUnion: return "__is_union";
John Wiegley65497cc2011-04-27 23:09:49 +00001619 case UTT_IsUnsigned: return "__is_unsigned";
1620 case UTT_IsVoid: return "__is_void";
1621 case UTT_IsVolatile: return "__is_volatile";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001622 }
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001623 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001624}
1625
1626static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1627 switch (BTT) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00001628 case BTT_IsBaseOf: return "__is_base_of";
1629 case BTT_IsConvertible: return "__is_convertible";
1630 case BTT_IsSame: return "__is_same";
1631 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
1632 case BTT_IsConvertibleTo: return "__is_convertible_to";
1633 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001634 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001635 llvm_unreachable("Binary type trait not covered by switch");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001636}
1637
Douglas Gregor29c42f22012-02-24 07:38:34 +00001638static const char *getTypeTraitName(TypeTrait TT) {
1639 switch (TT) {
1640 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1641 }
1642 llvm_unreachable("Type trait not covered by switch");
1643}
1644
John Wiegley6242b6a2011-04-28 00:16:57 +00001645static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1646 switch (ATT) {
1647 case ATT_ArrayRank: return "__array_rank";
1648 case ATT_ArrayExtent: return "__array_extent";
1649 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001650 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001651}
1652
John Wiegleyf9f65842011-04-25 06:54:41 +00001653static const char *getExpressionTraitName(ExpressionTrait ET) {
1654 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001655 case ET_IsLValueExpr: return "__is_lvalue_expr";
1656 case ET_IsRValueExpr: return "__is_rvalue_expr";
1657 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001658 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001659}
1660
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001661void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001662 OS << getTypeTraitName(E->getTrait()) << '(';
1663 E->getQueriedType().print(OS, Policy);
1664 OS << ')';
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001665}
1666
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001667void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001668 OS << getTypeTraitName(E->getTrait()) << '(';
1669 E->getLhsType().print(OS, Policy);
1670 OS << ',';
1671 E->getRhsType().print(OS, Policy);
1672 OS << ')';
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001673}
1674
Douglas Gregor29c42f22012-02-24 07:38:34 +00001675void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1676 OS << getTypeTraitName(E->getTrait()) << "(";
1677 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1678 if (I > 0)
1679 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001680 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00001681 }
1682 OS << ")";
1683}
1684
John Wiegley6242b6a2011-04-28 00:16:57 +00001685void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001686 OS << getTypeTraitName(E->getTrait()) << '(';
1687 E->getQueriedType().print(OS, Policy);
1688 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00001689}
1690
John Wiegleyf9f65842011-04-25 06:54:41 +00001691void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001692 OS << getExpressionTraitName(E->getTrait()) << '(';
1693 PrintExpr(E->getQueriedExpression());
1694 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00001695}
1696
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001697void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1698 OS << "noexcept(";
1699 PrintExpr(E->getOperand());
1700 OS << ")";
1701}
1702
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001703void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001704 PrintExpr(E->getPattern());
1705 OS << "...";
1706}
1707
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001708void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001709 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001710}
1711
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001712void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1713 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001714 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001715}
1716
John McCall7c454bb2011-07-15 05:09:51 +00001717void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1718 SubstNonTypeTemplateParmExpr *Node) {
1719 Visit(Node->getReplacement());
1720}
1721
Richard Smithb15fe3a2012-09-12 00:56:43 +00001722void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1723 OS << *E->getParameterPack();
1724}
1725
Douglas Gregorfe314812011-06-21 17:03:29 +00001726void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1727 PrintExpr(Node->GetTemporaryExpr());
1728}
1729
Mike Stump11289f42009-09-09 15:08:12 +00001730// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001731
1732void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1733 OS << "@";
1734 VisitStringLiteral(Node->getString());
1735}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001736
Patrick Beard0caa3942012-04-19 00:25:12 +00001737void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001738 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00001739 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001740}
1741
1742void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1743 OS << "@[ ";
1744 StmtRange ch = E->children();
1745 if (ch.first != ch.second) {
1746 while (1) {
1747 Visit(*ch.first);
1748 ++ch.first;
1749 if (ch.first == ch.second) break;
1750 OS << ", ";
1751 }
1752 }
1753 OS << " ]";
1754}
1755
1756void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1757 OS << "@{ ";
1758 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1759 if (I > 0)
1760 OS << ", ";
1761
1762 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1763 Visit(Element.Key);
1764 OS << " : ";
1765 Visit(Element.Value);
1766 if (Element.isPackExpansion())
1767 OS << "...";
1768 }
1769 OS << " }";
1770}
1771
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001772void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001773 OS << "@encode(";
1774 Node->getEncodedType().print(OS, Policy);
1775 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001776}
1777
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001778void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001779 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001780}
1781
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001782void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001783 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001784}
1785
Steve Naroffd54978b2007-09-18 23:55:05 +00001786void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1787 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001788 switch (Mess->getReceiverKind()) {
1789 case ObjCMessageExpr::Instance:
1790 PrintExpr(Mess->getInstanceReceiver());
1791 break;
1792
1793 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001794 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00001795 break;
1796
1797 case ObjCMessageExpr::SuperInstance:
1798 case ObjCMessageExpr::SuperClass:
1799 OS << "Super";
1800 break;
1801 }
1802
Ted Kremeneka06e7122008-05-02 17:32:38 +00001803 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001804 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001805 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001806 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001807 } else {
1808 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001809 if (i < selector.getNumArgs()) {
1810 if (i > 0) OS << ' ';
1811 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001812 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001813 else
1814 OS << ":";
1815 }
1816 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001817
Steve Naroffc6814ea2007-10-02 20:01:56 +00001818 PrintExpr(Mess->getArg(i));
1819 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001820 }
1821 OS << "]";
1822}
1823
Ted Kremeneke65b0862012-03-06 20:05:56 +00001824void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1825 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1826}
1827
John McCall31168b02011-06-15 23:02:42 +00001828void
1829StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1830 PrintExpr(E->getSubExpr());
1831}
1832
1833void
1834StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001835 OS << '(' << E->getBridgeKindName();
1836 E->getType().print(OS, Policy);
1837 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00001838 PrintExpr(E->getSubExpr());
1839}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001840
Steve Naroffc540d662008-09-03 18:15:37 +00001841void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001842 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001843 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001844
Steve Naroffc540d662008-09-03 18:15:37 +00001845 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001846
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001847 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001848 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001849 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001850 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00001851 for (BlockDecl::param_iterator AI = BD->param_begin(),
1852 E = BD->param_end(); AI != E; ++AI) {
1853 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001854 std::string ParamStr = (*AI)->getNameAsString();
1855 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00001856 }
Mike Stump11289f42009-09-09 15:08:12 +00001857
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001858 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001859 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001860 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001861 OS << "...";
1862 }
1863 OS << ')';
1864 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00001865 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00001866}
1867
Ted Kremenekf551f322011-11-30 22:08:08 +00001868void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1869 PrintExpr(Node->getSourceExpr());
1870}
John McCall8d69a212010-11-15 23:31:06 +00001871
Tanya Lattner55808c12011-06-04 00:47:47 +00001872void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1873 OS << "__builtin_astype(";
1874 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001875 OS << ", ";
1876 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00001877 OS << ")";
1878}
1879
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001880//===----------------------------------------------------------------------===//
1881// Stmt method implementations
1882//===----------------------------------------------------------------------===//
1883
Richard Smith235341b2012-08-16 03:56:14 +00001884void Stmt::dumpPretty(ASTContext &Context) const {
1885 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001886}
1887
Richard Smith235341b2012-08-16 03:56:14 +00001888void Stmt::printPretty(raw_ostream &OS,
1889 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001890 const PrintingPolicy &Policy,
1891 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001892 if (this == 0) {
1893 OS << "<NULL>";
1894 return;
1895 }
1896
Richard Smith235341b2012-08-16 03:56:14 +00001897 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001898 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001899}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001900
1901//===----------------------------------------------------------------------===//
1902// PrinterHelper
1903//===----------------------------------------------------------------------===//
1904
1905// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001906PrinterHelper::~PrinterHelper() {}