blob: 34ca79857980fc29cd6a7fe1c9a10f56adcca150 [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"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000019#include "llvm/Support/Compiler.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Chris Lattner62249a62007-08-21 04:04:25 +000028 class VISIBILITY_HIDDEN 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
Chris Lattner98dbf0a2007-08-30 17:59:59 +000080 bool PrintOffsetOfDesignator(Expr *E);
81 void VisitUnaryOffsetOf(UnaryOperator *Node);
Mike Stump11289f42009-09-09 15:08:12 +000082
83 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000084 if (Helper && Helper->handledStmt(S,OS))
85 return;
86 else StmtVisitor<StmtPrinter>::Visit(S);
87 }
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattner62249a62007-08-21 04:04:25 +000089 void VisitStmt(Stmt *Node);
Douglas Gregorbe35ce92008-11-14 12:46:07 +000090#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000091 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000092#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000093 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000094}
95
Chris Lattner71e23ce2006-11-04 20:18:38 +000096//===----------------------------------------------------------------------===//
97// Stmt printing methods.
98//===----------------------------------------------------------------------===//
99
Chris Lattner882f7882006-11-04 18:52:07 +0000100void StmtPrinter::VisitStmt(Stmt *Node) {
101 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000102}
103
Chris Lattner073926e2007-05-20 23:04:55 +0000104/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
105/// with no newline after the }.
106void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
107 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000108 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000109 I != E; ++I)
110 PrintStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000111
Chris Lattner073926e2007-05-20 23:04:55 +0000112 Indent() << "}";
113}
114
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000115void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000116 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000117}
118
Ted Kremenek15e6b402008-10-06 18:39:36 +0000119void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000120 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedman79635842009-05-30 04:20:30 +0000121 llvm::SmallVector<Decl*, 2> Decls;
Mike Stump11289f42009-09-09 15:08:12 +0000122 for ( ; Begin != End; ++Begin)
Eli Friedman79635842009-05-30 04:20:30 +0000123 Decls.push_back(*Begin);
Eli Friedman15ea8802009-05-30 00:19:54 +0000124
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000125 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000126}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000127
128void StmtPrinter::VisitNullStmt(NullStmt *Node) {
129 Indent() << ";\n";
130}
131
132void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000133 Indent();
134 PrintRawDeclStmt(Node);
135 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000136}
137
Chris Lattner073926e2007-05-20 23:04:55 +0000138void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
139 Indent();
140 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000141 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000142}
143
Chris Lattner6c0ff132006-11-05 00:19:50 +0000144void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000145 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000146 PrintExpr(Node->getLHS());
147 if (Node->getRHS()) {
148 OS << " ... ";
149 PrintExpr(Node->getRHS());
150 }
151 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000152
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000153 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000154}
155
156void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000157 Indent(-1) << "default:\n";
158 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000159}
160
161void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000162 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000163 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000164}
165
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000166void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000167 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000168 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000169 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000170
Chris Lattner073926e2007-05-20 23:04:55 +0000171 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
172 OS << ' ';
173 PrintRawCompoundStmt(CS);
174 OS << (If->getElse() ? ' ' : '\n');
175 } else {
176 OS << '\n';
177 PrintStmt(If->getThen());
178 if (If->getElse()) Indent();
179 }
Mike Stump11289f42009-09-09 15:08:12 +0000180
Chris Lattner073926e2007-05-20 23:04:55 +0000181 if (Stmt *Else = If->getElse()) {
182 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattner073926e2007-05-20 23:04:55 +0000184 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
185 OS << ' ';
186 PrintRawCompoundStmt(CS);
187 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000188 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
189 OS << ' ';
190 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000191 } else {
192 OS << '\n';
193 PrintStmt(If->getElse());
194 }
Chris Lattner882f7882006-11-04 18:52:07 +0000195 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000196}
197
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000198void StmtPrinter::VisitIfStmt(IfStmt *If) {
199 Indent();
200 PrintRawIfStmt(If);
201}
202
Chris Lattnerf2174b62006-11-04 20:59:27 +0000203void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
204 Indent() << "switch (";
205 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000206 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000207
Chris Lattner073926e2007-05-20 23:04:55 +0000208 // Pretty print compoundstmt bodies (very common).
209 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
210 OS << " ";
211 PrintRawCompoundStmt(CS);
212 OS << "\n";
213 } else {
214 OS << "\n";
215 PrintStmt(Node->getBody());
216 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000217}
218
Anders Carlsson51873c22007-07-22 07:07:56 +0000219void StmtPrinter::VisitSwitchCase(SwitchCase*) {
220 assert(0 && "SwitchCase is an abstract class");
221}
222
Chris Lattner85ed8732006-11-04 20:40:44 +0000223void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
224 Indent() << "while (";
225 PrintExpr(Node->getCond());
226 OS << ")\n";
227 PrintStmt(Node->getBody());
228}
229
230void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000231 Indent() << "do ";
232 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
233 PrintRawCompoundStmt(CS);
234 OS << " ";
235 } else {
236 OS << "\n";
237 PrintStmt(Node->getBody());
238 Indent();
239 }
Mike Stump11289f42009-09-09 15:08:12 +0000240
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000241 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000242 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000243 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000244}
245
Chris Lattner71e23ce2006-11-04 20:18:38 +0000246void StmtPrinter::VisitForStmt(ForStmt *Node) {
247 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000248 if (Node->getInit()) {
249 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000250 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000251 else
252 PrintExpr(cast<Expr>(Node->getInit()));
253 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000254 OS << ";";
255 if (Node->getCond()) {
256 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000257 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000258 }
259 OS << ";";
260 if (Node->getInc()) {
261 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000262 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000263 }
264 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000265
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000266 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
267 PrintRawCompoundStmt(CS);
268 OS << "\n";
269 } else {
270 OS << "\n";
271 PrintStmt(Node->getBody());
272 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000273}
274
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000275void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000276 Indent() << "for (";
277 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000278 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000279 else
280 PrintExpr(cast<Expr>(Node->getElement()));
281 OS << " in ";
282 PrintExpr(Node->getCollection());
283 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000284
Fariborz Jahanian83615522008-01-02 22:54:34 +0000285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
287 OS << "\n";
288 } else {
289 OS << "\n";
290 PrintStmt(Node->getBody());
291 }
292}
293
Chris Lattner16976d32006-11-05 01:46:01 +0000294void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000295 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000296}
297
298void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000299 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000300 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000301 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000302}
303
304void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000305 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000306}
307
308void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000309 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000310}
311
312
Chris Lattner882f7882006-11-04 18:52:07 +0000313void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
314 Indent() << "return";
315 if (Node->getRetValue()) {
316 OS << " ";
317 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000318 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000319 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000320}
321
Chris Lattner73c56c02007-10-29 04:04:16 +0000322
323void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000324 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000325
Anders Carlsson660bdd12007-11-23 23:12:25 +0000326 if (Node->isVolatile())
327 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000328
Anders Carlsson660bdd12007-11-23 23:12:25 +0000329 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000330 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000331
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000332 // Outputs
333 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
334 Node->getNumClobbers() != 0)
335 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000336
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000337 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
338 if (i != 0)
339 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000340
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000341 if (!Node->getOutputName(i).empty()) {
342 OS << '[';
343 OS << Node->getOutputName(i);
344 OS << "] ";
345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattner72bbf172009-03-10 04:59:06 +0000347 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000348 OS << " ";
349 Visit(Node->getOutputExpr(i));
350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000352 // Inputs
353 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
354 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000355
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000356 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
357 if (i != 0)
358 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000359
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000360 if (!Node->getInputName(i).empty()) {
361 OS << '[';
362 OS << Node->getInputName(i);
363 OS << "] ";
364 }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattner72bbf172009-03-10 04:59:06 +0000366 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000367 OS << " ";
368 Visit(Node->getInputExpr(i));
369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000371 // Clobbers
372 if (Node->getNumClobbers() != 0)
373 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000374
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000375 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
376 if (i != 0)
377 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000378
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000379 VisitStringLiteral(Node->getClobber(i));
380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Anders Carlsson81a5a692007-11-20 19:21:03 +0000382 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000383}
384
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000385void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000386 Indent() << "@try";
387 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
388 PrintRawCompoundStmt(TS);
389 OS << "\n";
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
392 for (ObjCAtCatchStmt *catchStmt =
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000393 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Mike Stump11289f42009-09-09 15:08:12 +0000394 catchStmt;
395 catchStmt =
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000396 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000397 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000398 if (catchStmt->getCatchParamDecl()) {
399 if (Decl *DS = catchStmt->getCatchParamDecl())
400 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000401 }
402 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000403 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
404 PrintRawCompoundStmt(CS);
405 OS << "\n";
406 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000407 }
Mike Stump11289f42009-09-09 15:08:12 +0000408
409 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
410 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000411 Indent() << "@finally";
412 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000413 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000414 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000415}
416
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000417void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000418}
419
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000420void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000421 Indent() << "@catch (...) { /* todo */ } \n";
422}
423
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000424void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000425 Indent() << "@throw";
426 if (Node->getThrowExpr()) {
427 OS << " ";
428 PrintExpr(Node->getThrowExpr());
429 }
430 OS << ";\n";
431}
432
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000433void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000434 Indent() << "@synchronized (";
435 PrintExpr(Node->getSynchExpr());
436 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000437 PrintRawCompoundStmt(Node->getSynchBody());
438 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000439}
440
Sebastian Redl9b244a82008-12-22 21:35:02 +0000441void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
442 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000443 if (Decl *ExDecl = Node->getExceptionDecl())
444 PrintRawDecl(ExDecl);
445 else
446 OS << "...";
447 OS << ") ";
448 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000449}
450
451void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
452 Indent();
453 PrintRawCXXCatchStmt(Node);
454 OS << "\n";
455}
456
457void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
458 Indent() << "try ";
459 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000460 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000461 OS << " ";
462 PrintRawCXXCatchStmt(Node->getHandler(i));
463 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000464 OS << "\n";
465}
466
Chris Lattner71e23ce2006-11-04 20:18:38 +0000467//===----------------------------------------------------------------------===//
468// Expr printing methods.
469//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000470
Chris Lattner882f7882006-11-04 18:52:07 +0000471void StmtPrinter::VisitExpr(Expr *Node) {
472 OS << "<<unknown expr type>>";
473}
474
475void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000476 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
477 Qualifier->print(OS, Policy);
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000478 OS << Node->getDecl()->getNameAsString();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000479 if (Node->hasExplicitTemplateArgumentList())
480 OS << TemplateSpecializationType::PrintTemplateArgumentList(
481 Node->getTemplateArgs(),
482 Node->getNumTemplateArgs(),
483 Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000484}
485
John McCall8cd78132009-11-19 22:55:06 +0000486void StmtPrinter::VisitDependentScopeDeclRefExpr(
487 DependentScopeDeclRefExpr *Node) {
Douglas Gregor7de59662009-05-29 20:38:28 +0000488 Node->getQualifier()->print(OS, Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000489 OS << Node->getDeclName().getAsString();
490}
491
John McCalld14a8642009-11-21 08:51:07 +0000492void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
493 OS << Node->getName().getAsString();
494}
495
Douglas Gregora727cb92009-06-30 22:34:41 +0000496void StmtPrinter::VisitTemplateIdRefExpr(TemplateIdRefExpr *Node) {
497 if (Node->getQualifier())
498 Node->getQualifier()->print(OS, Policy);
499 Node->getTemplateName().print(OS, Policy, true);
Douglas Gregora727cb92009-06-30 22:34:41 +0000500 OS << TemplateSpecializationType::PrintTemplateArgumentList(
501 Node->getTemplateArgs(),
502 Node->getNumTemplateArgs(),
503 Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000504}
505
Steve Naroffe46504b2007-11-12 14:29:37 +0000506void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000507 if (Node->getBase()) {
508 PrintExpr(Node->getBase());
509 OS << (Node->isArrow() ? "->" : ".");
510 }
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000511 OS << Node->getDecl()->getNameAsString();
Steve Naroffe46504b2007-11-12 14:29:37 +0000512}
513
Steve Naroffec944032008-05-30 00:40:33 +0000514void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
515 if (Node->getBase()) {
516 PrintExpr(Node->getBase());
517 OS << ".";
518 }
Steve Naroff4588d0f2008-12-04 16:24:46 +0000519 OS << Node->getProperty()->getNameAsCString();
Steve Naroffec944032008-05-30 00:40:33 +0000520}
521
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000522void StmtPrinter::VisitObjCImplicitSetterGetterRefExpr(
523 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000524 if (Node->getBase()) {
525 PrintExpr(Node->getBase());
526 OS << ".";
527 }
Fariborz Jahanian88cc2342009-08-18 20:50:23 +0000528 if (Node->getGetterMethod())
529 OS << Node->getGetterMethod()->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +0000530
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000531}
532
Chris Lattner6307f192008-08-10 01:53:14 +0000533void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000534 switch (Node->getIdentType()) {
535 default:
536 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000537 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000538 OS << "__func__";
539 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000540 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000541 OS << "__FUNCTION__";
542 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000543 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000544 OS << "__PRETTY_FUNCTION__";
545 break;
546 }
547}
548
Steve Naroffae4143e2007-04-26 20:39:23 +0000549void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000550 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000551 if (Node->isWide())
552 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000553 switch (value) {
554 case '\\':
555 OS << "'\\\\'";
556 break;
557 case '\'':
558 OS << "'\\''";
559 break;
560 case '\a':
561 // TODO: K&R: the meaning of '\\a' is different in traditional C
562 OS << "'\\a'";
563 break;
564 case '\b':
565 OS << "'\\b'";
566 break;
567 // Nonstandard escape sequence.
568 /*case '\e':
569 OS << "'\\e'";
570 break;*/
571 case '\f':
572 OS << "'\\f'";
573 break;
574 case '\n':
575 OS << "'\\n'";
576 break;
577 case '\r':
578 OS << "'\\r'";
579 break;
580 case '\t':
581 OS << "'\\t'";
582 break;
583 case '\v':
584 OS << "'\\v'";
585 break;
586 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000587 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000588 OS << "'" << (char)value << "'";
589 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000590 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000591 } else {
592 // FIXME what to really do here?
593 OS << value;
594 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000595 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000596}
597
Steve Naroffdf7855b2007-02-21 23:46:25 +0000598void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000599 bool isSigned = Node->getType()->isSignedIntegerType();
600 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000601
Chris Lattner06430412007-05-21 05:45:03 +0000602 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000603 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000604 default: assert(0 && "Unexpected type for integer literal!");
605 case BuiltinType::Int: break; // no suffix.
606 case BuiltinType::UInt: OS << 'U'; break;
607 case BuiltinType::Long: OS << 'L'; break;
608 case BuiltinType::ULong: OS << "UL"; break;
609 case BuiltinType::LongLong: OS << "LL"; break;
610 case BuiltinType::ULongLong: OS << "ULL"; break;
611 }
Chris Lattner882f7882006-11-04 18:52:07 +0000612}
Steve Naroffab624882007-02-21 22:05:47 +0000613void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000614 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000615 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000616}
Chris Lattner1c20a172007-08-26 03:42:43 +0000617
618void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
619 PrintExpr(Node->getSubExpr());
620 OS << "i";
621}
622
Steve Naroffdf7855b2007-02-21 23:46:25 +0000623void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000624 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000625 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000626
Chris Lattner5d8f4942006-11-04 20:29:31 +0000627 // FIXME: this doesn't print wstrings right.
628 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000629 unsigned char Char = Str->getStrData()[i];
Mike Stump11289f42009-09-09 15:08:12 +0000630
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000631 switch (Char) {
632 default:
633 if (isprint(Char))
634 OS << (char)Char;
635 else // Output anything hard as an octal escape.
636 OS << '\\'
637 << (char)('0'+ ((Char >> 6) & 7))
638 << (char)('0'+ ((Char >> 3) & 7))
639 << (char)('0'+ ((Char >> 0) & 7));
640 break;
641 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000642 case '\\': OS << "\\\\"; break;
643 case '"': OS << "\\\""; break;
644 case '\n': OS << "\\n"; break;
645 case '\t': OS << "\\t"; break;
646 case '\a': OS << "\\a"; break;
647 case '\b': OS << "\\b"; break;
648 }
649 }
650 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000651}
652void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
653 OS << "(";
654 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000655 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000656}
657void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000658 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000659 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000660
Eli Friedman1cf25362009-06-14 22:39:26 +0000661 // Print a space if this is an "identifier operator" like __real, or if
662 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000663 switch (Node->getOpcode()) {
664 default: break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000665 case UnaryOperator::Real:
666 case UnaryOperator::Imag:
667 case UnaryOperator::Extension:
668 OS << ' ';
669 break;
Eli Friedman1cf25362009-06-14 22:39:26 +0000670 case UnaryOperator::Plus:
671 case UnaryOperator::Minus:
672 if (isa<UnaryOperator>(Node->getSubExpr()))
673 OS << ' ';
674 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000675 }
676 }
Chris Lattner882f7882006-11-04 18:52:07 +0000677 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000678
Chris Lattner15768702006-11-05 23:54:51 +0000679 if (Node->isPostfix())
680 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000681}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000682
683bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman988a16b2009-02-27 06:44:11 +0000684 if (isa<UnaryOperator>(E)) {
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000685 // Base case, print the type and comma.
686 OS << E->getType().getAsString() << ", ";
687 return true;
688 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
689 PrintOffsetOfDesignator(ASE->getLHS());
690 OS << "[";
691 PrintExpr(ASE->getRHS());
692 OS << "]";
693 return false;
694 } else {
695 MemberExpr *ME = cast<MemberExpr>(E);
696 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000697 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000698 return false;
699 }
700}
701
702void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
703 OS << "__builtin_offsetof(";
704 PrintOffsetOfDesignator(Node->getSubExpr());
705 OS << ")";
706}
707
Sebastian Redl6f282892008-11-11 17:56:53 +0000708void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
709 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
710 if (Node->isArgumentType())
711 OS << "(" << Node->getArgumentType().getAsString() << ")";
712 else {
713 OS << " ";
714 PrintExpr(Node->getArgumentExpr());
715 }
Chris Lattner882f7882006-11-04 18:52:07 +0000716}
717void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000718 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000719 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000720 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000721 OS << "]";
722}
723
724void StmtPrinter::VisitCallExpr(CallExpr *Call) {
725 PrintExpr(Call->getCallee());
726 OS << "(";
727 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000728 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
729 // Don't print any defaulted arguments
730 break;
731 }
732
Chris Lattner882f7882006-11-04 18:52:07 +0000733 if (i) OS << ", ";
734 PrintExpr(Call->getArg(i));
735 }
736 OS << ")";
737}
738void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000739 // FIXME: Suppress printing implicit bases (like "this")
740 PrintExpr(Node->getBase());
741 OS << (Node->isArrow() ? "->" : ".");
742 // FIXME: Suppress printing references to unnamed objects
743 // representing anonymous unions/structs
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000744 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
745 Qualifier->print(OS, Policy);
746
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000747 OS << Node->getMemberDecl()->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +0000748
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000749 if (Node->hasExplicitTemplateArgumentList())
750 OS << TemplateSpecializationType::PrintTemplateArgumentList(
751 Node->getTemplateArgs(),
752 Node->getNumTemplateArgs(),
753 Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000754}
Steve Naroffe87026a2009-07-24 17:54:45 +0000755void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
756 PrintExpr(Node->getBase());
757 OS << (Node->isArrow() ? "->isa" : ".isa");
758}
759
Nate Begemance4d7fc2008-04-18 23:10:10 +0000760void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000761 PrintExpr(Node->getBase());
762 OS << ".";
763 OS << Node->getAccessor().getName();
764}
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000765void StmtPrinter::VisitCastExpr(CastExpr *) {
766 assert(0 && "CastExpr is an abstract class");
767}
Douglas Gregore200adc2008-10-27 19:41:14 +0000768void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
769 assert(0 && "ExplicitCastExpr is an abstract class");
770}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000771void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000772 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000773 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000774}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000775void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
776 OS << "(" << Node->getType().getAsString() << ")";
777 PrintExpr(Node->getInitializer());
778}
Steve Naroff7a5af782007-07-13 16:58:59 +0000779void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000780 // No need to print anything, simply forward to the sub expression.
781 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000782}
Chris Lattner882f7882006-11-04 18:52:07 +0000783void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
784 PrintExpr(Node->getLHS());
785 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
786 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000787}
Chris Lattner86928112007-08-25 02:00:02 +0000788void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
789 PrintExpr(Node->getLHS());
790 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
791 PrintExpr(Node->getRHS());
792}
Chris Lattner882f7882006-11-04 18:52:07 +0000793void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
794 PrintExpr(Node->getCond());
Mike Stump11289f42009-09-09 15:08:12 +0000795
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000796 if (Node->getLHS()) {
797 OS << " ? ";
798 PrintExpr(Node->getLHS());
799 OS << " : ";
800 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000801 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000802 OS << " ?: ";
803 }
Mike Stump11289f42009-09-09 15:08:12 +0000804
Chris Lattner882f7882006-11-04 18:52:07 +0000805 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000806}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000807
Chris Lattnereefa10e2007-05-28 06:56:27 +0000808// GNU extensions.
809
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000810void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000811 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000812}
813
Chris Lattner366727f2007-07-24 16:58:17 +0000814void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
815 OS << "(";
816 PrintRawCompoundStmt(E->getSubStmt());
817 OS << ")";
818}
819
Steve Naroff78864672007-08-01 22:05:33 +0000820void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
821 OS << "__builtin_types_compatible_p(";
822 OS << Node->getArgType1().getAsString() << ",";
823 OS << Node->getArgType2().getAsString() << ")";
824}
825
Steve Naroff9efdabc2007-08-03 21:21:27 +0000826void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
827 OS << "__builtin_choose_expr(";
828 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000829 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000830 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000831 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000832 PrintExpr(Node->getRHS());
833 OS << ")";
834}
Chris Lattner366727f2007-07-24 16:58:17 +0000835
Douglas Gregor3be4b122008-11-29 04:51:27 +0000836void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
837 OS << "__null";
838}
839
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000840void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
841 OS << "__builtin_shufflevector(";
842 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
843 if (i) OS << ", ";
844 PrintExpr(Node->getExpr(i));
845 }
846 OS << ")";
847}
848
Anders Carlsson4692db02007-08-31 04:56:16 +0000849void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000850 if (Node->getSyntacticForm()) {
851 Visit(Node->getSyntacticForm());
852 return;
853 }
854
Anders Carlsson4692db02007-08-31 04:56:16 +0000855 OS << "{ ";
856 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
857 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000858 if (Node->getInit(i))
859 PrintExpr(Node->getInit(i));
860 else
861 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000862 }
863 OS << " }";
864}
865
Nate Begeman5ec4b312009-08-10 23:49:36 +0000866void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
867 OS << "( ";
868 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
869 if (i) OS << ", ";
870 PrintExpr(Node->getExpr(i));
871 }
872 OS << " )";
873}
874
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000875void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000876 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
877 DEnd = Node->designators_end();
878 D != DEnd; ++D) {
879 if (D->isFieldDesignator()) {
880 if (D->getDotLoc().isInvalid())
881 OS << D->getFieldName()->getName() << ":";
882 else
883 OS << "." << D->getFieldName()->getName();
884 } else {
885 OS << "[";
886 if (D->isArrayDesignator()) {
887 PrintExpr(Node->getArrayIndex(*D));
888 } else {
889 PrintExpr(Node->getArrayRangeStart(*D));
890 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +0000891 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000892 }
893 OS << "]";
894 }
895 }
896
897 OS << " = ";
898 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000899}
900
Douglas Gregor0202cb42009-01-29 17:44:32 +0000901void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnerc61089a2009-06-30 01:26:17 +0000902 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000903 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
904 else {
905 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
906 if (Node->getType()->isRecordType())
907 OS << "{}";
908 else
909 OS << 0;
910 }
Douglas Gregor0202cb42009-01-29 17:44:32 +0000911}
912
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000913void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +0000914 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000915 PrintExpr(Node->getSubExpr());
916 OS << ", ";
917 OS << Node->getType().getAsString();
918 OS << ")";
919}
920
Chris Lattnereefa10e2007-05-28 06:56:27 +0000921// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000922void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
923 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
924 "",
925#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
926 Spelling,
927#include "clang/Basic/OperatorKinds.def"
928 };
929
930 OverloadedOperatorKind Kind = Node->getOperator();
931 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
932 if (Node->getNumArgs() == 1) {
933 OS << OpStrings[Kind] << ' ';
934 PrintExpr(Node->getArg(0));
935 } else {
936 PrintExpr(Node->getArg(0));
937 OS << ' ' << OpStrings[Kind];
938 }
939 } else if (Kind == OO_Call) {
940 PrintExpr(Node->getArg(0));
941 OS << '(';
942 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
943 if (ArgIdx > 1)
944 OS << ", ";
945 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
946 PrintExpr(Node->getArg(ArgIdx));
947 }
948 OS << ')';
949 } else if (Kind == OO_Subscript) {
950 PrintExpr(Node->getArg(0));
951 OS << '[';
952 PrintExpr(Node->getArg(1));
953 OS << ']';
954 } else if (Node->getNumArgs() == 1) {
955 OS << OpStrings[Kind] << ' ';
956 PrintExpr(Node->getArg(0));
957 } else if (Node->getNumArgs() == 2) {
958 PrintExpr(Node->getArg(0));
959 OS << ' ' << OpStrings[Kind] << ' ';
960 PrintExpr(Node->getArg(1));
961 } else {
962 assert(false && "unknown overloaded operator");
963 }
964}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000965
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000966void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
967 VisitCallExpr(cast<CallExpr>(Node));
968}
969
Douglas Gregore200adc2008-10-27 19:41:14 +0000970void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
971 OS << Node->getCastName() << '<';
972 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000973 PrintExpr(Node->getSubExpr());
974 OS << ")";
975}
976
Douglas Gregore200adc2008-10-27 19:41:14 +0000977void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
978 VisitCXXNamedCastExpr(Node);
979}
980
981void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
982 VisitCXXNamedCastExpr(Node);
983}
984
985void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
986 VisitCXXNamedCastExpr(Node);
987}
988
989void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
990 VisitCXXNamedCastExpr(Node);
991}
992
Sebastian Redlc4704762008-11-11 11:37:55 +0000993void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
994 OS << "typeid(";
995 if (Node->isTypeOperand()) {
996 OS << Node->getTypeOperand().getAsString();
997 } else {
998 PrintExpr(Node->getExprOperand());
999 }
1000 OS << ")";
1001}
1002
Chris Lattnereefa10e2007-05-28 06:56:27 +00001003void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1004 OS << (Node->getValue() ? "true" : "false");
1005}
1006
Sebastian Redl576fd422009-05-10 18:38:11 +00001007void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1008 OS << "nullptr";
1009}
1010
Douglas Gregor97a9c812008-11-04 14:32:21 +00001011void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1012 OS << "this";
1013}
1014
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001015void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1016 if (Node->getSubExpr() == 0)
1017 OS << "throw";
1018 else {
1019 OS << "throw ";
1020 PrintExpr(Node->getSubExpr());
1021 }
1022}
1023
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001024void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1025 // Nothing to print: we picked up the default argument
1026}
1027
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001028void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1029 OS << Node->getType().getAsString();
1030 OS << "(";
1031 PrintExpr(Node->getSubExpr());
1032 OS << ")";
1033}
1034
Anders Carlsson993a4b32009-05-30 20:03:25 +00001035void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1036 PrintExpr(Node->getSubExpr());
1037}
1038
Douglas Gregordd04d332009-01-16 18:33:17 +00001039void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1040 OS << Node->getType().getAsString();
1041 OS << "(";
1042 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001043 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001044 Arg != ArgEnd; ++Arg) {
1045 if (Arg != Node->arg_begin())
1046 OS << ", ";
1047 PrintExpr(*Arg);
1048 }
1049 OS << ")";
1050}
1051
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001052void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1053 OS << Node->getType().getAsString() << "()";
1054}
1055
Argyrios Kyrtzidisaa479132008-09-09 23:47:53 +00001056void
1057StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1058 PrintRawDecl(E->getVarDecl());
1059}
1060
Sebastian Redlbd150f42008-11-21 19:14:01 +00001061void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1062 if (E->isGlobalNew())
1063 OS << "::";
1064 OS << "new ";
1065 unsigned NumPlace = E->getNumPlacementArgs();
1066 if (NumPlace > 0) {
1067 OS << "(";
1068 PrintExpr(E->getPlacementArg(0));
1069 for (unsigned i = 1; i < NumPlace; ++i) {
1070 OS << ", ";
1071 PrintExpr(E->getPlacementArg(i));
1072 }
1073 OS << ") ";
1074 }
1075 if (E->isParenTypeId())
1076 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001077 std::string TypeS;
1078 if (Expr *Size = E->getArraySize()) {
1079 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001080 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001081 s.flush();
1082 TypeS = "[" + TypeS + "]";
1083 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001084 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001085 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001086 if (E->isParenTypeId())
1087 OS << ")";
1088
1089 if (E->hasInitializer()) {
1090 OS << "(";
1091 unsigned NumCons = E->getNumConstructorArgs();
1092 if (NumCons > 0) {
1093 PrintExpr(E->getConstructorArg(0));
1094 for (unsigned i = 1; i < NumCons; ++i) {
1095 OS << ", ";
1096 PrintExpr(E->getConstructorArg(i));
1097 }
1098 }
1099 OS << ")";
1100 }
1101}
1102
1103void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1104 if (E->isGlobalDelete())
1105 OS << "::";
1106 OS << "delete ";
1107 if (E->isArrayForm())
1108 OS << "[] ";
1109 PrintExpr(E->getArgument());
1110}
1111
Douglas Gregorad8a3362009-09-04 17:36:40 +00001112void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1113 PrintExpr(E->getBase());
1114 if (E->isArrow())
1115 OS << "->";
1116 else
1117 OS << '.';
1118 if (E->getQualifier())
1119 E->getQualifier()->print(OS, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001120
Douglas Gregorad8a3362009-09-04 17:36:40 +00001121 std::string TypeS;
1122 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
1123 OS << TypeS;
1124}
1125
Anders Carlsson0781ce72009-04-23 02:32:43 +00001126void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1127 // Nothing to print.
1128}
1129
Anders Carlssonf58c2432009-05-01 22:18:43 +00001130void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001131 // Just forward to the sub expression.
1132 PrintExpr(E->getSubExpr());
1133}
1134
Mike Stump11289f42009-09-09 15:08:12 +00001135void
Douglas Gregorce934142009-05-20 18:46:25 +00001136StmtPrinter::VisitCXXUnresolvedConstructExpr(
1137 CXXUnresolvedConstructExpr *Node) {
1138 OS << Node->getTypeAsWritten().getAsString();
1139 OS << "(";
1140 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001141 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001142 Arg != ArgEnd; ++Arg) {
1143 if (Arg != Node->arg_begin())
1144 OS << ", ";
1145 PrintExpr(*Arg);
1146 }
1147 OS << ")";
1148}
1149
John McCall8cd78132009-11-19 22:55:06 +00001150void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1151 CXXDependentScopeMemberExpr *Node) {
Douglas Gregora8db9542009-05-22 21:13:27 +00001152 PrintExpr(Node->getBase());
1153 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001154 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1155 Qualifier->print(OS, Policy);
Douglas Gregor308047d2009-09-09 00:23:06 +00001156 else if (Node->hasExplicitTemplateArgumentList())
1157 // FIXME: Track use of "template" keyword explicitly?
1158 OS << "template ";
Mike Stump11289f42009-09-09 15:08:12 +00001159
Douglas Gregora8db9542009-05-22 21:13:27 +00001160 OS << Node->getMember().getAsString();
Mike Stump11289f42009-09-09 15:08:12 +00001161
Douglas Gregor308047d2009-09-09 00:23:06 +00001162 if (Node->hasExplicitTemplateArgumentList()) {
1163 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1164 Node->getTemplateArgs(),
1165 Node->getNumTemplateArgs(),
1166 Policy);
1167 }
Douglas Gregora8db9542009-05-22 21:13:27 +00001168}
1169
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001170static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1171 switch (UTT) {
1172 default: assert(false && "Unknown type trait");
1173 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1174 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1175 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1176 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1177 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1178 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1179 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1180 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1181 case UTT_IsAbstract: return "__is_abstract";
1182 case UTT_IsClass: return "__is_class";
1183 case UTT_IsEmpty: return "__is_empty";
1184 case UTT_IsEnum: return "__is_enum";
1185 case UTT_IsPOD: return "__is_pod";
1186 case UTT_IsPolymorphic: return "__is_polymorphic";
1187 case UTT_IsUnion: return "__is_union";
1188 }
1189}
1190
1191void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1192 OS << getTypeTraitName(E->getTrait()) << "("
1193 << E->getQueriedType().getAsString() << ")";
1194}
1195
Mike Stump11289f42009-09-09 15:08:12 +00001196// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001197
1198void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1199 OS << "@";
1200 VisitStringLiteral(Node->getString());
1201}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001202
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001203void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001204 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001205}
1206
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001207void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001208 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001209}
1210
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001211void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001212 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001213}
1214
Steve Naroffd54978b2007-09-18 23:55:05 +00001215void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1216 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +00001217 Expr *receiver = Mess->getReceiver();
1218 if (receiver) PrintExpr(receiver);
1219 else OS << Mess->getClassName()->getName();
Ted Kremeneka06e7122008-05-02 17:32:38 +00001220 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001221 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001222 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001223 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001224 } else {
1225 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001226 if (i < selector.getNumArgs()) {
1227 if (i > 0) OS << ' ';
1228 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001229 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001230 else
1231 OS << ":";
1232 }
1233 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001234
Steve Naroffc6814ea2007-10-02 20:01:56 +00001235 PrintExpr(Mess->getArg(i));
1236 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001237 }
1238 OS << "]";
1239}
1240
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001241void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1242 OS << "super";
1243}
1244
Steve Naroffc540d662008-09-03 18:15:37 +00001245void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001246 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001247 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001248
Steve Naroffc540d662008-09-03 18:15:37 +00001249 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001250
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001251 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001252 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001253 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001254 OS << '(';
1255 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001256 for (BlockDecl::param_iterator AI = BD->param_begin(),
1257 E = BD->param_end(); AI != E; ++AI) {
1258 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001259 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001260 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001261 OS << ParamStr;
1262 }
Mike Stump11289f42009-09-09 15:08:12 +00001263
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001264 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001265 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001266 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001267 OS << "...";
1268 }
1269 OS << ')';
1270 }
1271}
1272
Steve Naroffc540d662008-09-03 18:15:37 +00001273void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001274 OS << Node->getDecl()->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001275}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001276//===----------------------------------------------------------------------===//
1277// Stmt method implementations
1278//===----------------------------------------------------------------------===//
1279
Eli Friedmanef334fd2009-05-30 05:03:24 +00001280void Stmt::dumpPretty(ASTContext& Context) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001281 printPretty(llvm::errs(), Context, 0,
1282 PrintingPolicy(Context.getLangOptions()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001283}
1284
Eli Friedmanef334fd2009-05-30 05:03:24 +00001285void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1286 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001287 const PrintingPolicy &Policy,
1288 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001289 if (this == 0) {
1290 OS << "<NULL>";
1291 return;
1292 }
1293
Douglas Gregor8655e882009-10-16 22:46:09 +00001294 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisb0c86d42009-07-14 03:20:38 +00001295 dump(Context.getSourceManager());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001296 return;
1297 }
Mike Stump11289f42009-09-09 15:08:12 +00001298
Eli Friedmanef334fd2009-05-30 05:03:24 +00001299 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001300 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001301}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001302
1303//===----------------------------------------------------------------------===//
1304// PrinterHelper
1305//===----------------------------------------------------------------------===//
1306
1307// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001308PrinterHelper::~PrinterHelper() {}