blob: b300940824de8cfa6a91ab36dc4a9e95d5342e20 [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 Kremenek871422e2007-11-26 22:50:46 +000020#include "llvm/Support/Streams.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000021#include "llvm/Support/Format.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtPrinter Visitor
26//===----------------------------------------------------------------------===//
27
28namespace {
Chris Lattner62249a62007-08-21 04:04:25 +000029 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremenek2d470fc2008-09-13 05:16:45 +000030 llvm::raw_ostream &OS;
Eli Friedmanef334fd2009-05-30 05:03:24 +000031 ASTContext &Context;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000032 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000033 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000034 PrintingPolicy Policy;
35
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000036 public:
Eli Friedmanef334fd2009-05-30 05:03:24 +000037 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Douglas Gregor7de59662009-05-29 20:38:28 +000038 const PrintingPolicy &Policy = PrintingPolicy(),
39 unsigned Indentation = 0)
Eli Friedmanef334fd2009-05-30 05:03:24 +000040 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
41 Policy(Policy) {}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000042
Douglas Gregor7de59662009-05-29 20:38:28 +000043 void PrintStmt(Stmt *S) {
44 PrintStmt(S, Policy.Indentation);
45 }
46
47 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000048 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000049 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000050 // If this is an expr used in a stmt context, indent and newline it.
51 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000052 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000053 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000054 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000055 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000056 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000057 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000058 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000059 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000060 }
Eli Friedman15ea8802009-05-30 00:19:54 +000061
Chris Lattner073926e2007-05-20 23:04:55 +000062 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000063 void PrintRawDecl(Decl *D);
Ted Kremenek15e6b402008-10-06 18:39:36 +000064 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000065 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000066 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000067
Chris Lattner882f7882006-11-04 18:52:07 +000068 void PrintExpr(Expr *E) {
69 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000070 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000071 else
Chris Lattner882f7882006-11-04 18:52:07 +000072 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000073 }
74
Mike Stump74a76472009-02-10 20:16:46 +000075 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000076 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
77 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000078 return OS;
79 }
80
Chris Lattner98dbf0a2007-08-30 17:59:59 +000081 bool PrintOffsetOfDesignator(Expr *E);
82 void VisitUnaryOffsetOf(UnaryOperator *Node);
83
Ted Kremenek04f3cee2007-08-31 21:30:12 +000084 void Visit(Stmt* S) {
85 if (Helper && Helper->handledStmt(S,OS))
86 return;
87 else StmtVisitor<StmtPrinter>::Visit(S);
88 }
89
Chris Lattner62249a62007-08-21 04:04:25 +000090 void VisitStmt(Stmt *Node);
Douglas Gregorbe35ce92008-11-14 12:46:07 +000091#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000092 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000093#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000094 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000095}
96
Chris Lattner71e23ce2006-11-04 20:18:38 +000097//===----------------------------------------------------------------------===//
98// Stmt printing methods.
99//===----------------------------------------------------------------------===//
100
Chris Lattner882f7882006-11-04 18:52:07 +0000101void StmtPrinter::VisitStmt(Stmt *Node) {
102 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000103}
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);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000112
Chris Lattner073926e2007-05-20 23:04:55 +0000113 Indent() << "}";
114}
115
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000116void StmtPrinter::PrintRawDecl(Decl *D) {
Eli Friedmanef334fd2009-05-30 05:03:24 +0000117 D->print(OS, Context, 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;
123 for ( ; Begin != End; ++Begin)
124 Decls.push_back(*Begin);
Eli Friedman15ea8802009-05-30 00:19:54 +0000125
Eli Friedmanef334fd2009-05-30 05:03:24 +0000126 Decl::printGroup(Decls.data(), Decls.size(), OS, Context, Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000127 IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000128}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000129
130void StmtPrinter::VisitNullStmt(NullStmt *Node) {
131 Indent() << ";\n";
132}
133
134void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000135 Indent();
136 PrintRawDeclStmt(Node);
137 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000138}
139
Chris Lattner073926e2007-05-20 23:04:55 +0000140void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
141 Indent();
142 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000143 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000144}
145
Chris Lattner6c0ff132006-11-05 00:19:50 +0000146void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000147 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000148 PrintExpr(Node->getLHS());
149 if (Node->getRHS()) {
150 OS << " ... ";
151 PrintExpr(Node->getRHS());
152 }
153 OS << ":\n";
154
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000155 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000156}
157
158void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000159 Indent(-1) << "default:\n";
160 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000161}
162
163void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000164 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000165 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000166}
167
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000168void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000169 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000170 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000171 OS << ')';
Chris Lattner073926e2007-05-20 23:04:55 +0000172
173 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
174 OS << ' ';
175 PrintRawCompoundStmt(CS);
176 OS << (If->getElse() ? ' ' : '\n');
177 } else {
178 OS << '\n';
179 PrintStmt(If->getThen());
180 if (If->getElse()) Indent();
181 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000182
Chris Lattner073926e2007-05-20 23:04:55 +0000183 if (Stmt *Else = If->getElse()) {
184 OS << "else";
185
186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000190 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
191 OS << ' ';
192 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000193 } else {
194 OS << '\n';
195 PrintStmt(If->getElse());
196 }
Chris Lattner882f7882006-11-04 18:52:07 +0000197 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000198}
199
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000200void StmtPrinter::VisitIfStmt(IfStmt *If) {
201 Indent();
202 PrintRawIfStmt(If);
203}
204
Chris Lattnerf2174b62006-11-04 20:59:27 +0000205void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
206 Indent() << "switch (";
207 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000208 OS << ")";
209
210 // Pretty print compoundstmt bodies (very common).
211 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
212 OS << " ";
213 PrintRawCompoundStmt(CS);
214 OS << "\n";
215 } else {
216 OS << "\n";
217 PrintStmt(Node->getBody());
218 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000219}
220
Anders Carlsson51873c22007-07-22 07:07:56 +0000221void StmtPrinter::VisitSwitchCase(SwitchCase*) {
222 assert(0 && "SwitchCase is an abstract class");
223}
224
Chris Lattner85ed8732006-11-04 20:40:44 +0000225void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
226 Indent() << "while (";
227 PrintExpr(Node->getCond());
228 OS << ")\n";
229 PrintStmt(Node->getBody());
230}
231
232void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000233 Indent() << "do ";
234 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
235 PrintRawCompoundStmt(CS);
236 OS << " ";
237 } else {
238 OS << "\n";
239 PrintStmt(Node->getBody());
240 Indent();
241 }
242
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000243 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000244 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000245 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000246}
247
Chris Lattner71e23ce2006-11-04 20:18:38 +0000248void StmtPrinter::VisitForStmt(ForStmt *Node) {
249 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000250 if (Node->getInit()) {
251 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000252 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000253 else
254 PrintExpr(cast<Expr>(Node->getInit()));
255 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000256 OS << ";";
257 if (Node->getCond()) {
258 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000259 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000260 }
261 OS << ";";
262 if (Node->getInc()) {
263 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000264 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000265 }
266 OS << ") ";
267
268 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
269 PrintRawCompoundStmt(CS);
270 OS << "\n";
271 } else {
272 OS << "\n";
273 PrintStmt(Node->getBody());
274 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000275}
276
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000277void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000278 Indent() << "for (";
279 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000280 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000281 else
282 PrintExpr(cast<Expr>(Node->getElement()));
283 OS << " in ";
284 PrintExpr(Node->getCollection());
285 OS << ") ";
286
287 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
288 PrintRawCompoundStmt(CS);
289 OS << "\n";
290 } else {
291 OS << "\n";
292 PrintStmt(Node->getBody());
293 }
294}
295
Chris Lattner16976d32006-11-05 01:46:01 +0000296void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000297 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000298}
299
300void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000301 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000302 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000303 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000304}
305
306void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000307 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000308}
309
310void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000311 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000312}
313
314
Chris Lattner882f7882006-11-04 18:52:07 +0000315void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
316 Indent() << "return";
317 if (Node->getRetValue()) {
318 OS << " ";
319 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000320 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000321 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000322}
323
Chris Lattner73c56c02007-10-29 04:04:16 +0000324
325void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000326 Indent() << "asm ";
327
328 if (Node->isVolatile())
329 OS << "volatile ";
330
331 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000332 VisitStringLiteral(Node->getAsmString());
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000333
334 // Outputs
335 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
336 Node->getNumClobbers() != 0)
337 OS << " : ";
338
339 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
340 if (i != 0)
341 OS << ", ";
342
343 if (!Node->getOutputName(i).empty()) {
344 OS << '[';
345 OS << Node->getOutputName(i);
346 OS << "] ";
347 }
348
Chris Lattner72bbf172009-03-10 04:59:06 +0000349 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000350 OS << " ";
351 Visit(Node->getOutputExpr(i));
352 }
353
354 // Inputs
355 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
356 OS << " : ";
357
358 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
359 if (i != 0)
360 OS << ", ";
361
362 if (!Node->getInputName(i).empty()) {
363 OS << '[';
364 OS << Node->getInputName(i);
365 OS << "] ";
366 }
367
Chris Lattner72bbf172009-03-10 04:59:06 +0000368 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000369 OS << " ";
370 Visit(Node->getInputExpr(i));
371 }
372
373 // Clobbers
374 if (Node->getNumClobbers() != 0)
375 OS << " : ";
376
377 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
378 if (i != 0)
379 OS << ", ";
380
381 VisitStringLiteral(Node->getClobber(i));
382 }
383
Anders Carlsson81a5a692007-11-20 19:21:03 +0000384 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000385}
386
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000387void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000388 Indent() << "@try";
389 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
390 PrintRawCompoundStmt(TS);
391 OS << "\n";
392 }
393
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000394 for (ObjCAtCatchStmt *catchStmt =
395 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian88157952007-11-02 18:16:07 +0000396 catchStmt;
397 catchStmt =
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000398 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000399 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000400 if (catchStmt->getCatchParamDecl()) {
401 if (Decl *DS = catchStmt->getCatchParamDecl())
402 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000403 }
404 OS << ")";
405 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
406 {
407 PrintRawCompoundStmt(CS);
408 OS << "\n";
409 }
410 }
411
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000412 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000413 Node->getFinallyStmt())) {
414 Indent() << "@finally";
415 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000416 OS << "\n";
417 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000418}
419
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000420void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000421}
422
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000423void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000424 Indent() << "@catch (...) { /* todo */ } \n";
425}
426
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000427void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000428 Indent() << "@throw";
429 if (Node->getThrowExpr()) {
430 OS << " ";
431 PrintExpr(Node->getThrowExpr());
432 }
433 OS << ";\n";
434}
435
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000436void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000437 Indent() << "@synchronized (";
438 PrintExpr(Node->getSynchExpr());
439 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000440 PrintRawCompoundStmt(Node->getSynchBody());
441 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000442}
443
Sebastian Redl9b244a82008-12-22 21:35:02 +0000444void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
445 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000446 if (Decl *ExDecl = Node->getExceptionDecl())
447 PrintRawDecl(ExDecl);
448 else
449 OS << "...";
450 OS << ") ";
451 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000452}
453
454void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
455 Indent();
456 PrintRawCXXCatchStmt(Node);
457 OS << "\n";
458}
459
460void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
461 Indent() << "try ";
462 PrintRawCompoundStmt(Node->getTryBlock());
463 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
464 OS << " ";
465 PrintRawCXXCatchStmt(Node->getHandler(i));
466 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000467 OS << "\n";
468}
469
Chris Lattner71e23ce2006-11-04 20:18:38 +0000470//===----------------------------------------------------------------------===//
471// Expr printing methods.
472//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000473
Chris Lattner882f7882006-11-04 18:52:07 +0000474void StmtPrinter::VisitExpr(Expr *Node) {
475 OS << "<<unknown expr type>>";
476}
477
478void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000479 OS << Node->getDecl()->getNameAsString();
Chris Lattner882f7882006-11-04 18:52:07 +0000480}
481
Douglas Gregor18353912009-03-19 03:51:16 +0000482void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000483 NamedDecl *D = Node->getDecl();
484
Douglas Gregor7de59662009-05-29 20:38:28 +0000485 Node->getQualifier()->print(OS, Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000486 OS << D->getNameAsString();
487}
488
Douglas Gregor90a1a652009-03-19 17:26:29 +0000489void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {
Douglas Gregor7de59662009-05-29 20:38:28 +0000490 Node->getQualifier()->print(OS, Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000491 OS << Node->getDeclName().getAsString();
492}
493
Steve Naroffe46504b2007-11-12 14:29:37 +0000494void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000495 if (Node->getBase()) {
496 PrintExpr(Node->getBase());
497 OS << (Node->isArrow() ? "->" : ".");
498 }
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000499 OS << Node->getDecl()->getNameAsString();
Steve Naroffe46504b2007-11-12 14:29:37 +0000500}
501
Steve Naroffec944032008-05-30 00:40:33 +0000502void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
503 if (Node->getBase()) {
504 PrintExpr(Node->getBase());
505 OS << ".";
506 }
Steve Naroff4588d0f2008-12-04 16:24:46 +0000507 OS << Node->getProperty()->getNameAsCString();
Steve Naroffec944032008-05-30 00:40:33 +0000508}
509
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000510void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
511 if (Node->getBase()) {
512 PrintExpr(Node->getBase());
513 OS << ".";
514 }
515 // FIXME: Setter/Getter names
516}
517
Chris Lattner6307f192008-08-10 01:53:14 +0000518void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000519 switch (Node->getIdentType()) {
520 default:
521 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000522 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000523 OS << "__func__";
524 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000525 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000526 OS << "__FUNCTION__";
527 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000528 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000529 OS << "__PRETTY_FUNCTION__";
530 break;
531 }
532}
533
Steve Naroffae4143e2007-04-26 20:39:23 +0000534void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000535 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000536 if (Node->isWide())
537 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000538 switch (value) {
539 case '\\':
540 OS << "'\\\\'";
541 break;
542 case '\'':
543 OS << "'\\''";
544 break;
545 case '\a':
546 // TODO: K&R: the meaning of '\\a' is different in traditional C
547 OS << "'\\a'";
548 break;
549 case '\b':
550 OS << "'\\b'";
551 break;
552 // Nonstandard escape sequence.
553 /*case '\e':
554 OS << "'\\e'";
555 break;*/
556 case '\f':
557 OS << "'\\f'";
558 break;
559 case '\n':
560 OS << "'\\n'";
561 break;
562 case '\r':
563 OS << "'\\r'";
564 break;
565 case '\t':
566 OS << "'\\t'";
567 break;
568 case '\v':
569 OS << "'\\v'";
570 break;
571 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000572 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000573 OS << "'" << (char)value << "'";
574 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000575 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000576 } else {
577 // FIXME what to really do here?
578 OS << value;
579 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000580 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000581}
582
Steve Naroffdf7855b2007-02-21 23:46:25 +0000583void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000584 bool isSigned = Node->getType()->isSignedIntegerType();
585 OS << Node->getValue().toString(10, isSigned);
586
587 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattner574dee62008-07-26 22:17:49 +0000588 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000589 default: assert(0 && "Unexpected type for integer literal!");
590 case BuiltinType::Int: break; // no suffix.
591 case BuiltinType::UInt: OS << 'U'; break;
592 case BuiltinType::Long: OS << 'L'; break;
593 case BuiltinType::ULong: OS << "UL"; break;
594 case BuiltinType::LongLong: OS << "LL"; break;
595 case BuiltinType::ULongLong: OS << "ULL"; break;
596 }
Chris Lattner882f7882006-11-04 18:52:07 +0000597}
Steve Naroffab624882007-02-21 22:05:47 +0000598void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000599 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000600 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000601}
Chris Lattner1c20a172007-08-26 03:42:43 +0000602
603void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
604 PrintExpr(Node->getSubExpr());
605 OS << "i";
606}
607
Steve Naroffdf7855b2007-02-21 23:46:25 +0000608void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000609 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000610 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000611
Chris Lattner5d8f4942006-11-04 20:29:31 +0000612 // FIXME: this doesn't print wstrings right.
613 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000614 unsigned char Char = Str->getStrData()[i];
615
616 switch (Char) {
617 default:
618 if (isprint(Char))
619 OS << (char)Char;
620 else // Output anything hard as an octal escape.
621 OS << '\\'
622 << (char)('0'+ ((Char >> 6) & 7))
623 << (char)('0'+ ((Char >> 3) & 7))
624 << (char)('0'+ ((Char >> 0) & 7));
625 break;
626 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000627 case '\\': OS << "\\\\"; break;
628 case '"': OS << "\\\""; break;
629 case '\n': OS << "\\n"; break;
630 case '\t': OS << "\\t"; break;
631 case '\a': OS << "\\a"; break;
632 case '\b': OS << "\\b"; break;
633 }
634 }
635 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000636}
637void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
638 OS << "(";
639 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000640 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000641}
642void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000643 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000644 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000645
Eli Friedman1cf25362009-06-14 22:39:26 +0000646 // Print a space if this is an "identifier operator" like __real, or if
647 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000648 switch (Node->getOpcode()) {
649 default: break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000650 case UnaryOperator::Real:
651 case UnaryOperator::Imag:
652 case UnaryOperator::Extension:
653 OS << ' ';
654 break;
Eli Friedman1cf25362009-06-14 22:39:26 +0000655 case UnaryOperator::Plus:
656 case UnaryOperator::Minus:
657 if (isa<UnaryOperator>(Node->getSubExpr()))
658 OS << ' ';
659 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000660 }
661 }
Chris Lattner882f7882006-11-04 18:52:07 +0000662 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000663
664 if (Node->isPostfix())
665 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000666}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000667
668bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman988a16b2009-02-27 06:44:11 +0000669 if (isa<UnaryOperator>(E)) {
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000670 // Base case, print the type and comma.
671 OS << E->getType().getAsString() << ", ";
672 return true;
673 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
674 PrintOffsetOfDesignator(ASE->getLHS());
675 OS << "[";
676 PrintExpr(ASE->getRHS());
677 OS << "]";
678 return false;
679 } else {
680 MemberExpr *ME = cast<MemberExpr>(E);
681 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000682 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000683 return false;
684 }
685}
686
687void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
688 OS << "__builtin_offsetof(";
689 PrintOffsetOfDesignator(Node->getSubExpr());
690 OS << ")";
691}
692
Sebastian Redl6f282892008-11-11 17:56:53 +0000693void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
694 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
695 if (Node->isArgumentType())
696 OS << "(" << Node->getArgumentType().getAsString() << ")";
697 else {
698 OS << " ";
699 PrintExpr(Node->getArgumentExpr());
700 }
Chris Lattner882f7882006-11-04 18:52:07 +0000701}
702void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000703 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000704 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000705 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000706 OS << "]";
707}
708
709void StmtPrinter::VisitCallExpr(CallExpr *Call) {
710 PrintExpr(Call->getCallee());
711 OS << "(";
712 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000713 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
714 // Don't print any defaulted arguments
715 break;
716 }
717
Chris Lattner882f7882006-11-04 18:52:07 +0000718 if (i) OS << ", ";
719 PrintExpr(Call->getArg(i));
720 }
721 OS << ")";
722}
723void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000724 // FIXME: Suppress printing implicit bases (like "this")
725 PrintExpr(Node->getBase());
726 OS << (Node->isArrow() ? "->" : ".");
727 // FIXME: Suppress printing references to unnamed objects
728 // representing anonymous unions/structs
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000729 OS << Node->getMemberDecl()->getNameAsString();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000730}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000731void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000732 PrintExpr(Node->getBase());
733 OS << ".";
734 OS << Node->getAccessor().getName();
735}
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000736void StmtPrinter::VisitCastExpr(CastExpr *) {
737 assert(0 && "CastExpr is an abstract class");
738}
Douglas Gregore200adc2008-10-27 19:41:14 +0000739void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
740 assert(0 && "ExplicitCastExpr is an abstract class");
741}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000742void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000743 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000744 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000745}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000746void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
747 OS << "(" << Node->getType().getAsString() << ")";
748 PrintExpr(Node->getInitializer());
749}
Steve Naroff7a5af782007-07-13 16:58:59 +0000750void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000751 // No need to print anything, simply forward to the sub expression.
752 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000753}
Chris Lattner882f7882006-11-04 18:52:07 +0000754void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
755 PrintExpr(Node->getLHS());
756 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
757 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000758}
Chris Lattner86928112007-08-25 02:00:02 +0000759void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
760 PrintExpr(Node->getLHS());
761 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
762 PrintExpr(Node->getRHS());
763}
Chris Lattner882f7882006-11-04 18:52:07 +0000764void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
765 PrintExpr(Node->getCond());
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000766
767 if (Node->getLHS()) {
768 OS << " ? ";
769 PrintExpr(Node->getLHS());
770 OS << " : ";
771 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000772 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000773 OS << " ?: ";
774 }
775
Chris Lattner882f7882006-11-04 18:52:07 +0000776 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000777}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000778
Chris Lattnereefa10e2007-05-28 06:56:27 +0000779// GNU extensions.
780
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000781void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000782 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000783}
784
Chris Lattner366727f2007-07-24 16:58:17 +0000785void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
786 OS << "(";
787 PrintRawCompoundStmt(E->getSubStmt());
788 OS << ")";
789}
790
Steve Naroff78864672007-08-01 22:05:33 +0000791void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
792 OS << "__builtin_types_compatible_p(";
793 OS << Node->getArgType1().getAsString() << ",";
794 OS << Node->getArgType2().getAsString() << ")";
795}
796
Steve Naroff9efdabc2007-08-03 21:21:27 +0000797void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
798 OS << "__builtin_choose_expr(";
799 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000800 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000801 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000802 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000803 PrintExpr(Node->getRHS());
804 OS << ")";
805}
Chris Lattner366727f2007-07-24 16:58:17 +0000806
Douglas Gregor3be4b122008-11-29 04:51:27 +0000807void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
808 OS << "__null";
809}
810
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000811void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
812 OS << "__builtin_shufflevector(";
813 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
814 if (i) OS << ", ";
815 PrintExpr(Node->getExpr(i));
816 }
817 OS << ")";
818}
819
Anders Carlsson4692db02007-08-31 04:56:16 +0000820void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000821 if (Node->getSyntacticForm()) {
822 Visit(Node->getSyntacticForm());
823 return;
824 }
825
Anders Carlsson4692db02007-08-31 04:56:16 +0000826 OS << "{ ";
827 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
828 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000829 if (Node->getInit(i))
830 PrintExpr(Node->getInit(i));
831 else
832 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000833 }
834 OS << " }";
835}
836
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000837void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000838 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
839 DEnd = Node->designators_end();
840 D != DEnd; ++D) {
841 if (D->isFieldDesignator()) {
842 if (D->getDotLoc().isInvalid())
843 OS << D->getFieldName()->getName() << ":";
844 else
845 OS << "." << D->getFieldName()->getName();
846 } else {
847 OS << "[";
848 if (D->isArrayDesignator()) {
849 PrintExpr(Node->getArrayIndex(*D));
850 } else {
851 PrintExpr(Node->getArrayRangeStart(*D));
852 OS << " ... ";
853 PrintExpr(Node->getArrayRangeEnd(*D));
854 }
855 OS << "]";
856 }
857 }
858
859 OS << " = ";
860 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000861}
862
Douglas Gregor0202cb42009-01-29 17:44:32 +0000863void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000864 if (Policy.CPlusPlus)
865 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
866 else {
867 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
868 if (Node->getType()->isRecordType())
869 OS << "{}";
870 else
871 OS << 0;
872 }
Douglas Gregor0202cb42009-01-29 17:44:32 +0000873}
874
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000875void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +0000876 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000877 PrintExpr(Node->getSubExpr());
878 OS << ", ";
879 OS << Node->getType().getAsString();
880 OS << ")";
881}
882
Chris Lattnereefa10e2007-05-28 06:56:27 +0000883// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000884void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
885 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
886 "",
887#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
888 Spelling,
889#include "clang/Basic/OperatorKinds.def"
890 };
891
892 OverloadedOperatorKind Kind = Node->getOperator();
893 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
894 if (Node->getNumArgs() == 1) {
895 OS << OpStrings[Kind] << ' ';
896 PrintExpr(Node->getArg(0));
897 } else {
898 PrintExpr(Node->getArg(0));
899 OS << ' ' << OpStrings[Kind];
900 }
901 } else if (Kind == OO_Call) {
902 PrintExpr(Node->getArg(0));
903 OS << '(';
904 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
905 if (ArgIdx > 1)
906 OS << ", ";
907 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
908 PrintExpr(Node->getArg(ArgIdx));
909 }
910 OS << ')';
911 } else if (Kind == OO_Subscript) {
912 PrintExpr(Node->getArg(0));
913 OS << '[';
914 PrintExpr(Node->getArg(1));
915 OS << ']';
916 } else if (Node->getNumArgs() == 1) {
917 OS << OpStrings[Kind] << ' ';
918 PrintExpr(Node->getArg(0));
919 } else if (Node->getNumArgs() == 2) {
920 PrintExpr(Node->getArg(0));
921 OS << ' ' << OpStrings[Kind] << ' ';
922 PrintExpr(Node->getArg(1));
923 } else {
924 assert(false && "unknown overloaded operator");
925 }
926}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000927
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000928void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
929 VisitCallExpr(cast<CallExpr>(Node));
930}
931
Douglas Gregore200adc2008-10-27 19:41:14 +0000932void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
933 OS << Node->getCastName() << '<';
934 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000935 PrintExpr(Node->getSubExpr());
936 OS << ")";
937}
938
Douglas Gregore200adc2008-10-27 19:41:14 +0000939void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
940 VisitCXXNamedCastExpr(Node);
941}
942
943void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
944 VisitCXXNamedCastExpr(Node);
945}
946
947void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
948 VisitCXXNamedCastExpr(Node);
949}
950
951void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
952 VisitCXXNamedCastExpr(Node);
953}
954
Sebastian Redlc4704762008-11-11 11:37:55 +0000955void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
956 OS << "typeid(";
957 if (Node->isTypeOperand()) {
958 OS << Node->getTypeOperand().getAsString();
959 } else {
960 PrintExpr(Node->getExprOperand());
961 }
962 OS << ")";
963}
964
Chris Lattnereefa10e2007-05-28 06:56:27 +0000965void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
966 OS << (Node->getValue() ? "true" : "false");
967}
968
Sebastian Redl576fd422009-05-10 18:38:11 +0000969void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
970 OS << "nullptr";
971}
972
Douglas Gregor97a9c812008-11-04 14:32:21 +0000973void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
974 OS << "this";
975}
976
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000977void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
978 if (Node->getSubExpr() == 0)
979 OS << "throw";
980 else {
981 OS << "throw ";
982 PrintExpr(Node->getSubExpr());
983 }
984}
985
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000986void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
987 // Nothing to print: we picked up the default argument
988}
989
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000990void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
991 OS << Node->getType().getAsString();
992 OS << "(";
993 PrintExpr(Node->getSubExpr());
994 OS << ")";
995}
996
Anders Carlsson993a4b32009-05-30 20:03:25 +0000997void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
998 PrintExpr(Node->getSubExpr());
999}
1000
Douglas Gregordd04d332009-01-16 18:33:17 +00001001void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1002 OS << Node->getType().getAsString();
1003 OS << "(";
1004 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1005 ArgEnd = Node->arg_end();
1006 Arg != ArgEnd; ++Arg) {
1007 if (Arg != Node->arg_begin())
1008 OS << ", ";
1009 PrintExpr(*Arg);
1010 }
1011 OS << ")";
1012}
1013
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001014void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1015 OS << Node->getType().getAsString() << "()";
1016}
1017
Argyrios Kyrtzidisaa479132008-09-09 23:47:53 +00001018void
1019StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1020 PrintRawDecl(E->getVarDecl());
1021}
1022
Sebastian Redlbd150f42008-11-21 19:14:01 +00001023void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1024 if (E->isGlobalNew())
1025 OS << "::";
1026 OS << "new ";
1027 unsigned NumPlace = E->getNumPlacementArgs();
1028 if (NumPlace > 0) {
1029 OS << "(";
1030 PrintExpr(E->getPlacementArg(0));
1031 for (unsigned i = 1; i < NumPlace; ++i) {
1032 OS << ", ";
1033 PrintExpr(E->getPlacementArg(i));
1034 }
1035 OS << ") ";
1036 }
1037 if (E->isParenTypeId())
1038 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001039 std::string TypeS;
1040 if (Expr *Size = E->getArraySize()) {
1041 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001042 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001043 s.flush();
1044 TypeS = "[" + TypeS + "]";
1045 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001046 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001047 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001048 if (E->isParenTypeId())
1049 OS << ")";
1050
1051 if (E->hasInitializer()) {
1052 OS << "(";
1053 unsigned NumCons = E->getNumConstructorArgs();
1054 if (NumCons > 0) {
1055 PrintExpr(E->getConstructorArg(0));
1056 for (unsigned i = 1; i < NumCons; ++i) {
1057 OS << ", ";
1058 PrintExpr(E->getConstructorArg(i));
1059 }
1060 }
1061 OS << ")";
1062 }
1063}
1064
1065void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1066 if (E->isGlobalDelete())
1067 OS << "::";
1068 OS << "delete ";
1069 if (E->isArrayForm())
1070 OS << "[] ";
1071 PrintExpr(E->getArgument());
1072}
1073
Douglas Gregorb8a9a412009-02-04 15:01:18 +00001074void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1075 OS << E->getName().getAsString();
Douglas Gregorb0846b02008-12-06 00:22:45 +00001076}
1077
Anders Carlsson0781ce72009-04-23 02:32:43 +00001078void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1079 // Nothing to print.
1080}
1081
Anders Carlssonf58c2432009-05-01 22:18:43 +00001082void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001083 // Just forward to the sub expression.
1084 PrintExpr(E->getSubExpr());
1085}
1086
Douglas Gregorce934142009-05-20 18:46:25 +00001087void
1088StmtPrinter::VisitCXXUnresolvedConstructExpr(
1089 CXXUnresolvedConstructExpr *Node) {
1090 OS << Node->getTypeAsWritten().getAsString();
1091 OS << "(";
1092 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1093 ArgEnd = Node->arg_end();
1094 Arg != ArgEnd; ++Arg) {
1095 if (Arg != Node->arg_begin())
1096 OS << ", ";
1097 PrintExpr(*Arg);
1098 }
1099 OS << ")";
1100}
1101
Douglas Gregora8db9542009-05-22 21:13:27 +00001102void StmtPrinter::VisitCXXUnresolvedMemberExpr(CXXUnresolvedMemberExpr *Node) {
1103 PrintExpr(Node->getBase());
1104 OS << (Node->isArrow() ? "->" : ".");
1105 OS << Node->getMember().getAsString();
1106}
1107
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001108static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1109 switch (UTT) {
1110 default: assert(false && "Unknown type trait");
1111 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1112 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1113 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1114 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1115 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1116 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1117 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1118 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1119 case UTT_IsAbstract: return "__is_abstract";
1120 case UTT_IsClass: return "__is_class";
1121 case UTT_IsEmpty: return "__is_empty";
1122 case UTT_IsEnum: return "__is_enum";
1123 case UTT_IsPOD: return "__is_pod";
1124 case UTT_IsPolymorphic: return "__is_polymorphic";
1125 case UTT_IsUnion: return "__is_union";
1126 }
1127}
1128
1129void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1130 OS << getTypeTraitName(E->getTrait()) << "("
1131 << E->getQueriedType().getAsString() << ")";
1132}
1133
Anders Carlsson76f4a902007-08-21 17:43:55 +00001134// Obj-C
1135
1136void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1137 OS << "@";
1138 VisitStringLiteral(Node->getString());
1139}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001140
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001141void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001142 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001143}
1144
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001145void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001146 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001147}
1148
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001149void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001150 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001151}
1152
Steve Naroffd54978b2007-09-18 23:55:05 +00001153void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1154 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +00001155 Expr *receiver = Mess->getReceiver();
1156 if (receiver) PrintExpr(receiver);
1157 else OS << Mess->getClassName()->getName();
Ted Kremeneka06e7122008-05-02 17:32:38 +00001158 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001159 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001160 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001161 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001162 } else {
1163 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001164 if (i < selector.getNumArgs()) {
1165 if (i > 0) OS << ' ';
1166 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001167 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001168 else
1169 OS << ":";
1170 }
1171 else OS << ", "; // Handle variadic methods.
1172
Steve Naroffc6814ea2007-10-02 20:01:56 +00001173 PrintExpr(Mess->getArg(i));
1174 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001175 }
1176 OS << "]";
1177}
1178
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001179void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1180 OS << "super";
1181}
1182
Steve Naroffc540d662008-09-03 18:15:37 +00001183void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001184 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001185 OS << "^";
1186
1187 const FunctionType *AFT = Node->getFunctionType();
1188
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001189 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001190 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001191 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001192 OS << '(';
1193 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001194 for (BlockDecl::param_iterator AI = BD->param_begin(),
1195 E = BD->param_end(); AI != E; ++AI) {
1196 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001197 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001198 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001199 OS << ParamStr;
1200 }
1201
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001202 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001203 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001204 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001205 OS << "...";
1206 }
1207 OS << ')';
1208 }
1209}
1210
Steve Naroffc540d662008-09-03 18:15:37 +00001211void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001212 OS << Node->getDecl()->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001213}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001214//===----------------------------------------------------------------------===//
1215// Stmt method implementations
1216//===----------------------------------------------------------------------===//
1217
Eli Friedmanef334fd2009-05-30 05:03:24 +00001218void Stmt::dumpPretty(ASTContext& Context) const {
1219 printPretty(llvm::errs(), Context, 0, PrintingPolicy());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001220}
1221
Eli Friedmanef334fd2009-05-30 05:03:24 +00001222void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1223 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001224 const PrintingPolicy &Policy,
1225 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001226 if (this == 0) {
1227 OS << "<NULL>";
1228 return;
1229 }
1230
Douglas Gregor278f52e2009-05-30 00:08:05 +00001231 if (Policy.Dump) {
1232 dump();
1233 return;
1234 }
1235
Eli Friedmanef334fd2009-05-30 05:03:24 +00001236 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001237 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001238}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001239
1240//===----------------------------------------------------------------------===//
1241// PrinterHelper
1242//===----------------------------------------------------------------------===//
1243
1244// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001245PrinterHelper::~PrinterHelper() {}