blob: 7f497a3d60f6782a478091611a35d195043c1329 [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
15#include "clang/AST/StmtVisitor.h"
Douglas Gregorc7acfdf2009-01-06 05:10:23 +000016#include "clang/AST/DeclCXX.h"
Ted Kremenek5c84c012007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000019#include "llvm/Support/Format.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000020#include "clang/AST/Expr.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000028 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremenek2d470fc2008-09-13 05:16:45 +000029 llvm::raw_ostream &OS;
Eli Friedmanef334fd2009-05-30 05:03:24 +000030 ASTContext &Context;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000031 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000032 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000033 PrintingPolicy Policy;
34
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000035 public:
Mike Stump11289f42009-09-09 15:08:12 +000036 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +000037 const PrintingPolicy &Policy,
Douglas Gregor7de59662009-05-29 20:38:28 +000038 unsigned Indentation = 0)
Eli Friedmanef334fd2009-05-30 05:03:24 +000039 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
40 Policy(Policy) {}
Mike Stump11289f42009-09-09 15:08:12 +000041
Douglas Gregor7de59662009-05-29 20:38:28 +000042 void PrintStmt(Stmt *S) {
43 PrintStmt(S, Policy.Indentation);
44 }
45
46 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000047 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000048 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000049 // If this is an expr used in a stmt context, indent and newline it.
50 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000051 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000052 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000053 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000054 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000055 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000056 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000057 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000058 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000059 }
Eli Friedman15ea8802009-05-30 00:19:54 +000060
Chris Lattner073926e2007-05-20 23:04:55 +000061 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000062 void PrintRawDecl(Decl *D);
Ted Kremenek15e6b402008-10-06 18:39:36 +000063 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000064 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000065 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Mike Stump11289f42009-09-09 15:08:12 +000066
Chris Lattner882f7882006-11-04 18:52:07 +000067 void PrintExpr(Expr *E) {
68 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000069 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000070 else
Chris Lattner882f7882006-11-04 18:52:07 +000071 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000072 }
Mike Stump11289f42009-09-09 15:08:12 +000073
Mike Stump74a76472009-02-10 20:16:46 +000074 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000075 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
76 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000077 return OS;
78 }
Mike Stump11289f42009-09-09 15:08:12 +000079
Mike Stump11289f42009-09-09 15:08:12 +000080 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000081 if (Helper && Helper->handledStmt(S,OS))
82 return;
83 else StmtVisitor<StmtPrinter>::Visit(S);
84 }
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000085
86 void VisitStmt(Stmt *Node) ATTRIBUTE_UNUSED {
87 Indent() << "<<unknown stmt type>>\n";
88 }
89 void VisitExpr(Expr *Node) ATTRIBUTE_UNUSED {
90 OS << "<<unknown expr type>>";
91 }
92 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +000093
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000094#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +000095#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000096 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +000097#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +000098 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000099}
100
Chris Lattner71e23ce2006-11-04 20:18:38 +0000101//===----------------------------------------------------------------------===//
102// Stmt printing methods.
103//===----------------------------------------------------------------------===//
104
Chris Lattner073926e2007-05-20 23:04:55 +0000105/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
106/// with no newline after the }.
107void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
108 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000109 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000110 I != E; ++I)
111 PrintStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000112
Chris Lattner073926e2007-05-20 23:04:55 +0000113 Indent() << "}";
114}
115
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000116void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000117 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000118}
119
Ted Kremenek15e6b402008-10-06 18:39:36 +0000120void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000121 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedman79635842009-05-30 04:20:30 +0000122 llvm::SmallVector<Decl*, 2> Decls;
Mike Stump11289f42009-09-09 15:08:12 +0000123 for ( ; Begin != End; ++Begin)
Eli Friedman79635842009-05-30 04:20:30 +0000124 Decls.push_back(*Begin);
Eli Friedman15ea8802009-05-30 00:19:54 +0000125
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000126 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000127}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000128
129void StmtPrinter::VisitNullStmt(NullStmt *Node) {
130 Indent() << ";\n";
131}
132
133void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000134 Indent();
135 PrintRawDeclStmt(Node);
136 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000137}
138
Chris Lattner073926e2007-05-20 23:04:55 +0000139void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
140 Indent();
141 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000142 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000143}
144
Chris Lattner6c0ff132006-11-05 00:19:50 +0000145void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000146 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000147 PrintExpr(Node->getLHS());
148 if (Node->getRHS()) {
149 OS << " ... ";
150 PrintExpr(Node->getRHS());
151 }
152 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000153
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000154 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000155}
156
157void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000158 Indent(-1) << "default:\n";
159 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000160}
161
162void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000163 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000164 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000165}
166
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000167void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000168 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000169 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000170 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000171
Chris Lattner073926e2007-05-20 23:04:55 +0000172 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
173 OS << ' ';
174 PrintRawCompoundStmt(CS);
175 OS << (If->getElse() ? ' ' : '\n');
176 } else {
177 OS << '\n';
178 PrintStmt(If->getThen());
179 if (If->getElse()) Indent();
180 }
Mike Stump11289f42009-09-09 15:08:12 +0000181
Chris Lattner073926e2007-05-20 23:04:55 +0000182 if (Stmt *Else = If->getElse()) {
183 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000184
Chris Lattner073926e2007-05-20 23:04:55 +0000185 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
186 OS << ' ';
187 PrintRawCompoundStmt(CS);
188 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000189 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
190 OS << ' ';
191 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000192 } else {
193 OS << '\n';
194 PrintStmt(If->getElse());
195 }
Chris Lattner882f7882006-11-04 18:52:07 +0000196 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000197}
198
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000199void StmtPrinter::VisitIfStmt(IfStmt *If) {
200 Indent();
201 PrintRawIfStmt(If);
202}
203
Chris Lattnerf2174b62006-11-04 20:59:27 +0000204void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
205 Indent() << "switch (";
206 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000207 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000208
Chris Lattner073926e2007-05-20 23:04:55 +0000209 // Pretty print compoundstmt bodies (very common).
210 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
211 OS << " ";
212 PrintRawCompoundStmt(CS);
213 OS << "\n";
214 } else {
215 OS << "\n";
216 PrintStmt(Node->getBody());
217 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000218}
219
Anders Carlsson51873c22007-07-22 07:07:56 +0000220void StmtPrinter::VisitSwitchCase(SwitchCase*) {
221 assert(0 && "SwitchCase is an abstract class");
222}
223
Chris Lattner85ed8732006-11-04 20:40:44 +0000224void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
225 Indent() << "while (";
226 PrintExpr(Node->getCond());
227 OS << ")\n";
228 PrintStmt(Node->getBody());
229}
230
231void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000232 Indent() << "do ";
233 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
234 PrintRawCompoundStmt(CS);
235 OS << " ";
236 } else {
237 OS << "\n";
238 PrintStmt(Node->getBody());
239 Indent();
240 }
Mike Stump11289f42009-09-09 15:08:12 +0000241
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000242 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000243 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000244 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000245}
246
Chris Lattner71e23ce2006-11-04 20:18:38 +0000247void StmtPrinter::VisitForStmt(ForStmt *Node) {
248 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000249 if (Node->getInit()) {
250 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000251 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000252 else
253 PrintExpr(cast<Expr>(Node->getInit()));
254 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000255 OS << ";";
256 if (Node->getCond()) {
257 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000258 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000259 }
260 OS << ";";
261 if (Node->getInc()) {
262 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000263 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000264 }
265 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000266
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000267 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
268 PrintRawCompoundStmt(CS);
269 OS << "\n";
270 } else {
271 OS << "\n";
272 PrintStmt(Node->getBody());
273 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000274}
275
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000276void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000277 Indent() << "for (";
278 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000279 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000280 else
281 PrintExpr(cast<Expr>(Node->getElement()));
282 OS << " in ";
283 PrintExpr(Node->getCollection());
284 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000285
Fariborz Jahanian83615522008-01-02 22:54:34 +0000286 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
287 PrintRawCompoundStmt(CS);
288 OS << "\n";
289 } else {
290 OS << "\n";
291 PrintStmt(Node->getBody());
292 }
293}
294
Chris Lattner16976d32006-11-05 01:46:01 +0000295void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000296 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000297}
298
299void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000300 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000301 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000302 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000303}
304
305void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000306 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000307}
308
309void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000310 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000311}
312
313
Chris Lattner882f7882006-11-04 18:52:07 +0000314void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
315 Indent() << "return";
316 if (Node->getRetValue()) {
317 OS << " ";
318 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000319 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000320 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000321}
322
Chris Lattner73c56c02007-10-29 04:04:16 +0000323
324void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000325 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000326
Anders Carlsson660bdd12007-11-23 23:12:25 +0000327 if (Node->isVolatile())
328 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000329
Anders Carlsson660bdd12007-11-23 23:12:25 +0000330 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000331 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000332
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000333 // Outputs
334 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
335 Node->getNumClobbers() != 0)
336 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000337
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000338 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
339 if (i != 0)
340 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000341
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000342 if (!Node->getOutputName(i).empty()) {
343 OS << '[';
344 OS << Node->getOutputName(i);
345 OS << "] ";
346 }
Mike Stump11289f42009-09-09 15:08:12 +0000347
Chris Lattner72bbf172009-03-10 04:59:06 +0000348 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000349 OS << " ";
350 Visit(Node->getOutputExpr(i));
351 }
Mike Stump11289f42009-09-09 15:08:12 +0000352
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000353 // Inputs
354 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
355 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000356
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000357 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
358 if (i != 0)
359 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000360
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000361 if (!Node->getInputName(i).empty()) {
362 OS << '[';
363 OS << Node->getInputName(i);
364 OS << "] ";
365 }
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattner72bbf172009-03-10 04:59:06 +0000367 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000368 OS << " ";
369 Visit(Node->getInputExpr(i));
370 }
Mike Stump11289f42009-09-09 15:08:12 +0000371
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000372 // Clobbers
373 if (Node->getNumClobbers() != 0)
374 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000375
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000376 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
377 if (i != 0)
378 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000379
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000380 VisitStringLiteral(Node->getClobber(i));
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Anders Carlsson81a5a692007-11-20 19:21:03 +0000383 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000384}
385
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000386void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000387 Indent() << "@try";
388 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
389 PrintRawCompoundStmt(TS);
390 OS << "\n";
391 }
Mike Stump11289f42009-09-09 15:08:12 +0000392
Douglas Gregor96c79492010-04-23 22:50:49 +0000393 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
394 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000395 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000396 if (catchStmt->getCatchParamDecl()) {
397 if (Decl *DS = catchStmt->getCatchParamDecl())
398 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000399 }
400 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000401 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
402 PrintRawCompoundStmt(CS);
403 OS << "\n";
404 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000405 }
Mike Stump11289f42009-09-09 15:08:12 +0000406
407 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
408 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000409 Indent() << "@finally";
410 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000411 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000412 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000413}
414
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000415void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000416}
417
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000418void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000419 Indent() << "@catch (...) { /* todo */ } \n";
420}
421
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000422void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000423 Indent() << "@throw";
424 if (Node->getThrowExpr()) {
425 OS << " ";
426 PrintExpr(Node->getThrowExpr());
427 }
428 OS << ";\n";
429}
430
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000431void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000432 Indent() << "@synchronized (";
433 PrintExpr(Node->getSynchExpr());
434 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000435 PrintRawCompoundStmt(Node->getSynchBody());
436 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000437}
438
Sebastian Redl9b244a82008-12-22 21:35:02 +0000439void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
440 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000441 if (Decl *ExDecl = Node->getExceptionDecl())
442 PrintRawDecl(ExDecl);
443 else
444 OS << "...";
445 OS << ") ";
446 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000447}
448
449void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
450 Indent();
451 PrintRawCXXCatchStmt(Node);
452 OS << "\n";
453}
454
455void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
456 Indent() << "try ";
457 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000458 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000459 OS << " ";
460 PrintRawCXXCatchStmt(Node->getHandler(i));
461 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000462 OS << "\n";
463}
464
Chris Lattner71e23ce2006-11-04 20:18:38 +0000465//===----------------------------------------------------------------------===//
466// Expr printing methods.
467//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000468
Chris Lattner882f7882006-11-04 18:52:07 +0000469void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000470 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
471 Qualifier->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000472 OS << Node->getNameInfo();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000473 if (Node->hasExplicitTemplateArgumentList())
474 OS << TemplateSpecializationType::PrintTemplateArgumentList(
475 Node->getTemplateArgs(),
476 Node->getNumTemplateArgs(),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000477 Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000478}
479
John McCall8cd78132009-11-19 22:55:06 +0000480void StmtPrinter::VisitDependentScopeDeclRefExpr(
481 DependentScopeDeclRefExpr *Node) {
Douglas Gregor7de59662009-05-29 20:38:28 +0000482 Node->getQualifier()->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000483 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000484 if (Node->hasExplicitTemplateArgs())
485 OS << TemplateSpecializationType::PrintTemplateArgumentList(
486 Node->getTemplateArgs(),
487 Node->getNumTemplateArgs(),
488 Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000489}
490
John McCalld14a8642009-11-21 08:51:07 +0000491void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000492 if (Node->getQualifier())
493 Node->getQualifier()->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000494 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000495 if (Node->hasExplicitTemplateArgs())
496 OS << TemplateSpecializationType::PrintTemplateArgumentList(
497 Node->getTemplateArgs(),
Douglas Gregora727cb92009-06-30 22:34:41 +0000498 Node->getNumTemplateArgs(),
John McCalle66edc12009-11-24 19:00:30 +0000499 Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000500}
501
Steve Naroffe46504b2007-11-12 14:29:37 +0000502void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000503 if (Node->getBase()) {
504 PrintExpr(Node->getBase());
505 OS << (Node->isArrow() ? "->" : ".");
506 }
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000507 OS << Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000508}
509
Steve Naroffec944032008-05-30 00:40:33 +0000510void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
511 if (Node->getBase()) {
512 PrintExpr(Node->getBase());
513 OS << ".";
514 }
Steve Naroff4588d0f2008-12-04 16:24:46 +0000515 OS << Node->getProperty()->getNameAsCString();
Steve Naroffec944032008-05-30 00:40:33 +0000516}
517
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000518void StmtPrinter::VisitObjCImplicitSetterGetterRefExpr(
519 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000520 if (Node->getBase()) {
521 PrintExpr(Node->getBase());
522 OS << ".";
523 }
Fariborz Jahanian88cc2342009-08-18 20:50:23 +0000524 if (Node->getGetterMethod())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000525 OS << Node->getGetterMethod();
Mike Stump11289f42009-09-09 15:08:12 +0000526
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000527}
528
Chris Lattner6307f192008-08-10 01:53:14 +0000529void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000530 switch (Node->getIdentType()) {
531 default:
532 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000533 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000534 OS << "__func__";
535 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000536 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000537 OS << "__FUNCTION__";
538 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000539 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000540 OS << "__PRETTY_FUNCTION__";
541 break;
542 }
543}
544
Steve Naroffae4143e2007-04-26 20:39:23 +0000545void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000546 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000547 if (Node->isWide())
548 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000549 switch (value) {
550 case '\\':
551 OS << "'\\\\'";
552 break;
553 case '\'':
554 OS << "'\\''";
555 break;
556 case '\a':
557 // TODO: K&R: the meaning of '\\a' is different in traditional C
558 OS << "'\\a'";
559 break;
560 case '\b':
561 OS << "'\\b'";
562 break;
563 // Nonstandard escape sequence.
564 /*case '\e':
565 OS << "'\\e'";
566 break;*/
567 case '\f':
568 OS << "'\\f'";
569 break;
570 case '\n':
571 OS << "'\\n'";
572 break;
573 case '\r':
574 OS << "'\\r'";
575 break;
576 case '\t':
577 OS << "'\\t'";
578 break;
579 case '\v':
580 OS << "'\\v'";
581 break;
582 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000583 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000584 OS << "'" << (char)value << "'";
585 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000586 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000587 } else {
588 // FIXME what to really do here?
589 OS << value;
590 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000591 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000592}
593
Steve Naroffdf7855b2007-02-21 23:46:25 +0000594void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000595 bool isSigned = Node->getType()->isSignedIntegerType();
596 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000597
Chris Lattner06430412007-05-21 05:45:03 +0000598 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000599 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000600 default: assert(0 && "Unexpected type for integer literal!");
601 case BuiltinType::Int: break; // no suffix.
602 case BuiltinType::UInt: OS << 'U'; break;
603 case BuiltinType::Long: OS << 'L'; break;
604 case BuiltinType::ULong: OS << "UL"; break;
605 case BuiltinType::LongLong: OS << "LL"; break;
606 case BuiltinType::ULongLong: OS << "ULL"; break;
607 }
Chris Lattner882f7882006-11-04 18:52:07 +0000608}
Steve Naroffab624882007-02-21 22:05:47 +0000609void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000610 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000611 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000612}
Chris Lattner1c20a172007-08-26 03:42:43 +0000613
614void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
615 PrintExpr(Node->getSubExpr());
616 OS << "i";
617}
618
Steve Naroffdf7855b2007-02-21 23:46:25 +0000619void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000620 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000621 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000622
Chris Lattner5d8f4942006-11-04 20:29:31 +0000623 // FIXME: this doesn't print wstrings right.
624 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000625 unsigned char Char = Str->getStrData()[i];
Mike Stump11289f42009-09-09 15:08:12 +0000626
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000627 switch (Char) {
628 default:
629 if (isprint(Char))
630 OS << (char)Char;
631 else // Output anything hard as an octal escape.
632 OS << '\\'
633 << (char)('0'+ ((Char >> 6) & 7))
634 << (char)('0'+ ((Char >> 3) & 7))
635 << (char)('0'+ ((Char >> 0) & 7));
636 break;
637 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000638 case '\\': OS << "\\\\"; break;
639 case '"': OS << "\\\""; break;
640 case '\n': OS << "\\n"; break;
641 case '\t': OS << "\\t"; break;
642 case '\a': OS << "\\a"; break;
643 case '\b': OS << "\\b"; break;
644 }
645 }
646 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000647}
648void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
649 OS << "(";
650 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000651 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000652}
653void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000654 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000655 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000656
Eli Friedman1cf25362009-06-14 22:39:26 +0000657 // Print a space if this is an "identifier operator" like __real, or if
658 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000659 switch (Node->getOpcode()) {
660 default: break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000661 case UnaryOperator::Real:
662 case UnaryOperator::Imag:
663 case UnaryOperator::Extension:
664 OS << ' ';
665 break;
Eli Friedman1cf25362009-06-14 22:39:26 +0000666 case UnaryOperator::Plus:
667 case UnaryOperator::Minus:
668 if (isa<UnaryOperator>(Node->getSubExpr()))
669 OS << ' ';
670 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000671 }
672 }
Chris Lattner882f7882006-11-04 18:52:07 +0000673 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner15768702006-11-05 23:54:51 +0000675 if (Node->isPostfix())
676 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000677}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000678
Douglas Gregor882211c2010-04-28 22:16:22 +0000679void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
680 OS << "__builtin_offsetof(";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000681 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000682 bool PrintedSomething = false;
683 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
684 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
685 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
686 // Array node
687 OS << "[";
688 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
689 OS << "]";
690 PrintedSomething = true;
691 continue;
692 }
Douglas Gregord1702062010-04-29 00:18:15 +0000693
694 // Skip implicit base indirections.
695 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
696 continue;
697
Douglas Gregor882211c2010-04-28 22:16:22 +0000698 // Field or identifier node.
699 IdentifierInfo *Id = ON.getFieldName();
700 if (!Id)
701 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000702
Douglas Gregor882211c2010-04-28 22:16:22 +0000703 if (PrintedSomething)
704 OS << ".";
705 else
706 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000707 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000708 }
709 OS << ")";
710}
711
Sebastian Redl6f282892008-11-11 17:56:53 +0000712void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
713 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
714 if (Node->isArgumentType())
Daniel Dunbar219fa692010-06-30 19:16:48 +0000715 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl6f282892008-11-11 17:56:53 +0000716 else {
717 OS << " ";
718 PrintExpr(Node->getArgumentExpr());
719 }
Chris Lattner882f7882006-11-04 18:52:07 +0000720}
721void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000722 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000723 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000724 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000725 OS << "]";
726}
727
728void StmtPrinter::VisitCallExpr(CallExpr *Call) {
729 PrintExpr(Call->getCallee());
730 OS << "(";
731 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000732 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
733 // Don't print any defaulted arguments
734 break;
735 }
736
Chris Lattner882f7882006-11-04 18:52:07 +0000737 if (i) OS << ", ";
738 PrintExpr(Call->getArg(i));
739 }
740 OS << ")";
741}
742void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000743 // FIXME: Suppress printing implicit bases (like "this")
744 PrintExpr(Node->getBase());
Fariborz Jahanian2990c022010-01-11 21:17:32 +0000745 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
746 if (FD->isAnonymousStructOrUnion())
747 return;
Douglas Gregore4b414c2009-01-08 22:45:41 +0000748 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000749 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
750 Qualifier->print(OS, Policy);
751
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000752 OS << Node->getMemberNameInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000753
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000754 if (Node->hasExplicitTemplateArgumentList())
755 OS << TemplateSpecializationType::PrintTemplateArgumentList(
756 Node->getTemplateArgs(),
757 Node->getNumTemplateArgs(),
758 Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000759}
Steve Naroffe87026a2009-07-24 17:54:45 +0000760void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
761 PrintExpr(Node->getBase());
762 OS << (Node->isArrow() ? "->isa" : ".isa");
763}
764
Nate Begemance4d7fc2008-04-18 23:10:10 +0000765void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000766 PrintExpr(Node->getBase());
767 OS << ".";
768 OS << Node->getAccessor().getName();
769}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000770void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahaniane33c1162010-06-30 16:31:08 +0000771 OS << "(" << Node->getType().getAsString(Policy) << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000772 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000773}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000774void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000775 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroff57eb2c52007-07-19 21:32:11 +0000776 PrintExpr(Node->getInitializer());
777}
Steve Naroff7a5af782007-07-13 16:58:59 +0000778void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000779 // No need to print anything, simply forward to the sub expression.
780 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000781}
Chris Lattner882f7882006-11-04 18:52:07 +0000782void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
783 PrintExpr(Node->getLHS());
784 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
785 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000786}
Chris Lattner86928112007-08-25 02:00:02 +0000787void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
788 PrintExpr(Node->getLHS());
789 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
790 PrintExpr(Node->getRHS());
791}
Chris Lattner882f7882006-11-04 18:52:07 +0000792void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
793 PrintExpr(Node->getCond());
Mike Stump11289f42009-09-09 15:08:12 +0000794
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000795 if (Node->getLHS()) {
796 OS << " ? ";
797 PrintExpr(Node->getLHS());
798 OS << " : ";
799 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000800 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000801 OS << " ?: ";
802 }
Mike Stump11289f42009-09-09 15:08:12 +0000803
Chris Lattner882f7882006-11-04 18:52:07 +0000804 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000805}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000806
Chris Lattnereefa10e2007-05-28 06:56:27 +0000807// GNU extensions.
808
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000809void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000810 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000811}
812
Chris Lattner366727f2007-07-24 16:58:17 +0000813void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
814 OS << "(";
815 PrintRawCompoundStmt(E->getSubStmt());
816 OS << ")";
817}
818
Steve Naroff78864672007-08-01 22:05:33 +0000819void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
820 OS << "__builtin_types_compatible_p(";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000821 OS << Node->getArgType1().getAsString(Policy) << ",";
822 OS << Node->getArgType2().getAsString(Policy) << ")";
Steve Naroff78864672007-08-01 22:05:33 +0000823}
824
Steve Naroff9efdabc2007-08-03 21:21:27 +0000825void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
826 OS << "__builtin_choose_expr(";
827 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000828 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000829 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000830 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000831 PrintExpr(Node->getRHS());
832 OS << ")";
833}
Chris Lattner366727f2007-07-24 16:58:17 +0000834
Douglas Gregor3be4b122008-11-29 04:51:27 +0000835void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
836 OS << "__null";
837}
838
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000839void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
840 OS << "__builtin_shufflevector(";
841 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
842 if (i) OS << ", ";
843 PrintExpr(Node->getExpr(i));
844 }
845 OS << ")";
846}
847
Anders Carlsson4692db02007-08-31 04:56:16 +0000848void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000849 if (Node->getSyntacticForm()) {
850 Visit(Node->getSyntacticForm());
851 return;
852 }
853
Anders Carlsson4692db02007-08-31 04:56:16 +0000854 OS << "{ ";
855 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
856 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000857 if (Node->getInit(i))
858 PrintExpr(Node->getInit(i));
859 else
860 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000861 }
862 OS << " }";
863}
864
Nate Begeman5ec4b312009-08-10 23:49:36 +0000865void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
866 OS << "( ";
867 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
868 if (i) OS << ", ";
869 PrintExpr(Node->getExpr(i));
870 }
871 OS << " )";
872}
873
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000874void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000875 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
876 DEnd = Node->designators_end();
877 D != DEnd; ++D) {
878 if (D->isFieldDesignator()) {
879 if (D->getDotLoc().isInvalid())
880 OS << D->getFieldName()->getName() << ":";
881 else
882 OS << "." << D->getFieldName()->getName();
883 } else {
884 OS << "[";
885 if (D->isArrayDesignator()) {
886 PrintExpr(Node->getArrayIndex(*D));
887 } else {
888 PrintExpr(Node->getArrayRangeStart(*D));
889 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +0000890 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000891 }
892 OS << "]";
893 }
894 }
895
896 OS << " = ";
897 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000898}
899
Douglas Gregor0202cb42009-01-29 17:44:32 +0000900void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnerc61089a2009-06-30 01:26:17 +0000901 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000902 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
903 else {
904 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
905 if (Node->getType()->isRecordType())
906 OS << "{}";
907 else
908 OS << 0;
909 }
Douglas Gregor0202cb42009-01-29 17:44:32 +0000910}
911
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000912void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +0000913 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000914 PrintExpr(Node->getSubExpr());
915 OS << ", ";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000916 OS << Node->getType().getAsString(Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000917 OS << ")";
918}
919
Chris Lattnereefa10e2007-05-28 06:56:27 +0000920// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000921void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
922 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
923 "",
924#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
925 Spelling,
926#include "clang/Basic/OperatorKinds.def"
927 };
928
929 OverloadedOperatorKind Kind = Node->getOperator();
930 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
931 if (Node->getNumArgs() == 1) {
932 OS << OpStrings[Kind] << ' ';
933 PrintExpr(Node->getArg(0));
934 } else {
935 PrintExpr(Node->getArg(0));
936 OS << ' ' << OpStrings[Kind];
937 }
938 } else if (Kind == OO_Call) {
939 PrintExpr(Node->getArg(0));
940 OS << '(';
941 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
942 if (ArgIdx > 1)
943 OS << ", ";
944 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
945 PrintExpr(Node->getArg(ArgIdx));
946 }
947 OS << ')';
948 } else if (Kind == OO_Subscript) {
949 PrintExpr(Node->getArg(0));
950 OS << '[';
951 PrintExpr(Node->getArg(1));
952 OS << ']';
953 } else if (Node->getNumArgs() == 1) {
954 OS << OpStrings[Kind] << ' ';
955 PrintExpr(Node->getArg(0));
956 } else if (Node->getNumArgs() == 2) {
957 PrintExpr(Node->getArg(0));
958 OS << ' ' << OpStrings[Kind] << ' ';
959 PrintExpr(Node->getArg(1));
960 } else {
961 assert(false && "unknown overloaded operator");
962 }
963}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000964
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000965void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
966 VisitCallExpr(cast<CallExpr>(Node));
967}
968
Douglas Gregore200adc2008-10-27 19:41:14 +0000969void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
970 OS << Node->getCastName() << '<';
Daniel Dunbar219fa692010-06-30 19:16:48 +0000971 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000972 PrintExpr(Node->getSubExpr());
973 OS << ")";
974}
975
Douglas Gregore200adc2008-10-27 19:41:14 +0000976void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
977 VisitCXXNamedCastExpr(Node);
978}
979
980void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
981 VisitCXXNamedCastExpr(Node);
982}
983
984void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
985 VisitCXXNamedCastExpr(Node);
986}
987
988void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
989 VisitCXXNamedCastExpr(Node);
990}
991
Sebastian Redlc4704762008-11-11 11:37:55 +0000992void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
993 OS << "typeid(";
994 if (Node->isTypeOperand()) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000995 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +0000996 } else {
997 PrintExpr(Node->getExprOperand());
998 }
999 OS << ")";
1000}
1001
Chris Lattnereefa10e2007-05-28 06:56:27 +00001002void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1003 OS << (Node->getValue() ? "true" : "false");
1004}
1005
Sebastian Redl576fd422009-05-10 18:38:11 +00001006void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1007 OS << "nullptr";
1008}
1009
Douglas Gregor97a9c812008-11-04 14:32:21 +00001010void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1011 OS << "this";
1012}
1013
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001014void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1015 if (Node->getSubExpr() == 0)
1016 OS << "throw";
1017 else {
1018 OS << "throw ";
1019 PrintExpr(Node->getSubExpr());
1020 }
1021}
1022
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001023void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1024 // Nothing to print: we picked up the default argument
1025}
1026
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001027void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001028 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001029 OS << "(";
1030 PrintExpr(Node->getSubExpr());
1031 OS << ")";
1032}
1033
Anders Carlsson993a4b32009-05-30 20:03:25 +00001034void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1035 PrintExpr(Node->getSubExpr());
1036}
1037
Anders Carlssonba6c4372010-01-29 02:39:32 +00001038void StmtPrinter::VisitCXXBindReferenceExpr(CXXBindReferenceExpr *Node) {
1039 PrintExpr(Node->getSubExpr());
1040}
1041
Douglas Gregordd04d332009-01-16 18:33:17 +00001042void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001043 OS << Node->getType().getAsString(Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001044 OS << "(";
1045 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001046 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001047 Arg != ArgEnd; ++Arg) {
1048 if (Arg != Node->arg_begin())
1049 OS << ", ";
1050 PrintExpr(*Arg);
1051 }
1052 OS << ")";
1053}
1054
Douglas Gregor747eb782010-07-08 06:14:04 +00001055void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001056 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001057}
1058
Sebastian Redlbd150f42008-11-21 19:14:01 +00001059void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1060 if (E->isGlobalNew())
1061 OS << "::";
1062 OS << "new ";
1063 unsigned NumPlace = E->getNumPlacementArgs();
1064 if (NumPlace > 0) {
1065 OS << "(";
1066 PrintExpr(E->getPlacementArg(0));
1067 for (unsigned i = 1; i < NumPlace; ++i) {
1068 OS << ", ";
1069 PrintExpr(E->getPlacementArg(i));
1070 }
1071 OS << ") ";
1072 }
1073 if (E->isParenTypeId())
1074 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001075 std::string TypeS;
1076 if (Expr *Size = E->getArraySize()) {
1077 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001078 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001079 s.flush();
1080 TypeS = "[" + TypeS + "]";
1081 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001082 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001083 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001084 if (E->isParenTypeId())
1085 OS << ")";
1086
1087 if (E->hasInitializer()) {
1088 OS << "(";
1089 unsigned NumCons = E->getNumConstructorArgs();
1090 if (NumCons > 0) {
1091 PrintExpr(E->getConstructorArg(0));
1092 for (unsigned i = 1; i < NumCons; ++i) {
1093 OS << ", ";
1094 PrintExpr(E->getConstructorArg(i));
1095 }
1096 }
1097 OS << ")";
1098 }
1099}
1100
1101void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1102 if (E->isGlobalDelete())
1103 OS << "::";
1104 OS << "delete ";
1105 if (E->isArrayForm())
1106 OS << "[] ";
1107 PrintExpr(E->getArgument());
1108}
1109
Douglas Gregorad8a3362009-09-04 17:36:40 +00001110void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1111 PrintExpr(E->getBase());
1112 if (E->isArrow())
1113 OS << "->";
1114 else
1115 OS << '.';
1116 if (E->getQualifier())
1117 E->getQualifier()->print(OS, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001118
Douglas Gregorad8a3362009-09-04 17:36:40 +00001119 std::string TypeS;
Douglas Gregor678f90d2010-02-25 01:56:36 +00001120 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1121 OS << II->getName();
1122 else
1123 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001124 OS << TypeS;
1125}
1126
Anders Carlsson0781ce72009-04-23 02:32:43 +00001127void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001128 // FIXME. For now we just print a trivial constructor call expression,
1129 // constructing its first argument object.
1130 if (E->getNumArgs() == 1) {
1131 CXXConstructorDecl *CD = E->getConstructor();
1132 if (CD->isTrivial())
1133 PrintExpr(E->getArg(0));
1134 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001135 // Nothing to print.
1136}
1137
Anders Carlssonf58c2432009-05-01 22:18:43 +00001138void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001139 // Just forward to the sub expression.
1140 PrintExpr(E->getSubExpr());
1141}
1142
Mike Stump11289f42009-09-09 15:08:12 +00001143void
Douglas Gregorce934142009-05-20 18:46:25 +00001144StmtPrinter::VisitCXXUnresolvedConstructExpr(
1145 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001146 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001147 OS << "(";
1148 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001149 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001150 Arg != ArgEnd; ++Arg) {
1151 if (Arg != Node->arg_begin())
1152 OS << ", ";
1153 PrintExpr(*Arg);
1154 }
1155 OS << ")";
1156}
1157
John McCall8cd78132009-11-19 22:55:06 +00001158void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1159 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001160 if (!Node->isImplicitAccess()) {
1161 PrintExpr(Node->getBase());
1162 OS << (Node->isArrow() ? "->" : ".");
1163 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001164 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1165 Qualifier->print(OS, Policy);
John McCall2d74de92009-12-01 22:10:20 +00001166 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor308047d2009-09-09 00:23:06 +00001167 // FIXME: Track use of "template" keyword explicitly?
1168 OS << "template ";
Mike Stump11289f42009-09-09 15:08:12 +00001169
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001170 OS << Node->getMemberNameInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001171
John McCall2d74de92009-12-01 22:10:20 +00001172 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor308047d2009-09-09 00:23:06 +00001173 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1174 Node->getTemplateArgs(),
1175 Node->getNumTemplateArgs(),
1176 Policy);
1177 }
Douglas Gregora8db9542009-05-22 21:13:27 +00001178}
1179
John McCall10eae182009-11-30 22:42:35 +00001180void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001181 if (!Node->isImplicitAccess()) {
1182 PrintExpr(Node->getBase());
1183 OS << (Node->isArrow() ? "->" : ".");
1184 }
John McCall10eae182009-11-30 22:42:35 +00001185 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1186 Qualifier->print(OS, Policy);
1187
1188 // FIXME: this might originally have been written with 'template'
1189
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001190 OS << Node->getMemberNameInfo();
John McCall10eae182009-11-30 22:42:35 +00001191
1192 if (Node->hasExplicitTemplateArgs()) {
1193 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1194 Node->getTemplateArgs(),
1195 Node->getNumTemplateArgs(),
1196 Policy);
1197 }
1198}
1199
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001200static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1201 switch (UTT) {
1202 default: assert(false && "Unknown type trait");
1203 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1204 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1205 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1206 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1207 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1208 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1209 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1210 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1211 case UTT_IsAbstract: return "__is_abstract";
1212 case UTT_IsClass: return "__is_class";
1213 case UTT_IsEmpty: return "__is_empty";
1214 case UTT_IsEnum: return "__is_enum";
1215 case UTT_IsPOD: return "__is_pod";
1216 case UTT_IsPolymorphic: return "__is_polymorphic";
1217 case UTT_IsUnion: return "__is_union";
1218 }
1219}
1220
1221void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1222 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar219fa692010-06-30 19:16:48 +00001223 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001224}
1225
Mike Stump11289f42009-09-09 15:08:12 +00001226// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001227
1228void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1229 OS << "@";
1230 VisitStringLiteral(Node->getString());
1231}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001232
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001233void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001234 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001235}
1236
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001237void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001238 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001239}
1240
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001241void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001242 OS << "@protocol(" << Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001243}
1244
Steve Naroffd54978b2007-09-18 23:55:05 +00001245void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1246 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001247 switch (Mess->getReceiverKind()) {
1248 case ObjCMessageExpr::Instance:
1249 PrintExpr(Mess->getInstanceReceiver());
1250 break;
1251
1252 case ObjCMessageExpr::Class:
1253 OS << Mess->getClassReceiver().getAsString(Policy);
1254 break;
1255
1256 case ObjCMessageExpr::SuperInstance:
1257 case ObjCMessageExpr::SuperClass:
1258 OS << "Super";
1259 break;
1260 }
1261
Ted Kremeneka06e7122008-05-02 17:32:38 +00001262 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001263 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001264 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001265 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001266 } else {
1267 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001268 if (i < selector.getNumArgs()) {
1269 if (i > 0) OS << ' ';
1270 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001271 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001272 else
1273 OS << ":";
1274 }
1275 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001276
Steve Naroffc6814ea2007-10-02 20:01:56 +00001277 PrintExpr(Mess->getArg(i));
1278 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001279 }
1280 OS << "]";
1281}
1282
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001283void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1284 OS << "super";
1285}
1286
Steve Naroffc540d662008-09-03 18:15:37 +00001287void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001288 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001289 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001290
Steve Naroffc540d662008-09-03 18:15:37 +00001291 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001292
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001293 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001294 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001295 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001296 OS << '(';
1297 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001298 for (BlockDecl::param_iterator AI = BD->param_begin(),
1299 E = BD->param_end(); AI != E; ++AI) {
1300 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001301 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001302 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001303 OS << ParamStr;
1304 }
Mike Stump11289f42009-09-09 15:08:12 +00001305
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001306 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001307 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001308 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001309 OS << "...";
1310 }
1311 OS << ')';
1312 }
1313}
1314
Steve Naroffc540d662008-09-03 18:15:37 +00001315void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001316 OS << Node->getDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001317}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001318//===----------------------------------------------------------------------===//
1319// Stmt method implementations
1320//===----------------------------------------------------------------------===//
1321
Eli Friedmanef334fd2009-05-30 05:03:24 +00001322void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001323 printPretty(llvm::errs(), Context, 0,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001324 PrintingPolicy(Context.getLangOptions()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001325}
1326
Eli Friedmanef334fd2009-05-30 05:03:24 +00001327void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1328 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001329 const PrintingPolicy &Policy,
1330 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001331 if (this == 0) {
1332 OS << "<NULL>";
1333 return;
1334 }
1335
Douglas Gregor8655e882009-10-16 22:46:09 +00001336 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001337 dump(OS, Context.getSourceManager());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001338 return;
1339 }
Mike Stump11289f42009-09-09 15:08:12 +00001340
Eli Friedmanef334fd2009-05-30 05:03:24 +00001341 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001342 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001343}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001344
1345//===----------------------------------------------------------------------===//
1346// PrinterHelper
1347//===----------------------------------------------------------------------===//
1348
1349// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001350PrinterHelper::~PrinterHelper() {}