blob: b55bddfed09d74b90abe70ef3dad7de1aaeedbac [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;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000031 unsigned IndentLevel;
Mike Stump74a76472009-02-10 20:16:46 +000032 bool NoIndent;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000033 clang::PrinterHelper* Helper;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000034 public:
Mike Stump74a76472009-02-10 20:16:46 +000035 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper, unsigned I=0,
36 bool noIndent=false) :
37 OS(os), IndentLevel(I), NoIndent(noIndent), Helper(helper) {}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000038
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000039 void PrintStmt(Stmt *S, int SubIndent = 1) {
40 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000041 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000042 // If this is an expr used in a stmt context, indent and newline it.
43 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000044 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000045 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000046 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000047 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000048 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000049 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000050 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000051 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000052 }
Chris Lattner073926e2007-05-20 23:04:55 +000053
54 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000055 void PrintRawDecl(Decl *D);
Ted Kremenek15e6b402008-10-06 18:39:36 +000056 void PrintRawDeclStmt(DeclStmt *S);
Mike Stump74a76472009-02-10 20:16:46 +000057 void PrintFieldDecl(FieldDecl *FD);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000058 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000059 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000060
Chris Lattner882f7882006-11-04 18:52:07 +000061 void PrintExpr(Expr *E) {
62 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000063 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000064 else
Chris Lattner882f7882006-11-04 18:52:07 +000065 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000066 }
67
Mike Stump74a76472009-02-10 20:16:46 +000068 llvm::raw_ostream &Indent(int Delta = 0) {
69 if (!NoIndent) {
70 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
71 OS << " ";
72 } else NoIndent = false;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000073 return OS;
74 }
75
Chris Lattner98dbf0a2007-08-30 17:59:59 +000076 bool PrintOffsetOfDesignator(Expr *E);
77 void VisitUnaryOffsetOf(UnaryOperator *Node);
78
Ted Kremenek04f3cee2007-08-31 21:30:12 +000079 void Visit(Stmt* S) {
80 if (Helper && Helper->handledStmt(S,OS))
81 return;
82 else StmtVisitor<StmtPrinter>::Visit(S);
83 }
84
Chris Lattner62249a62007-08-21 04:04:25 +000085 void VisitStmt(Stmt *Node);
Douglas Gregorbe35ce92008-11-14 12:46:07 +000086#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000087 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000088#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000089 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000090}
91
Chris Lattner71e23ce2006-11-04 20:18:38 +000092//===----------------------------------------------------------------------===//
93// Stmt printing methods.
94//===----------------------------------------------------------------------===//
95
Chris Lattner882f7882006-11-04 18:52:07 +000096void StmtPrinter::VisitStmt(Stmt *Node) {
97 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000098}
99
Chris Lattner073926e2007-05-20 23:04:55 +0000100/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
101/// with no newline after the }.
102void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
103 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000104 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000105 I != E; ++I)
106 PrintStmt(*I);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000107
Chris Lattner073926e2007-05-20 23:04:55 +0000108 Indent() << "}";
109}
110
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000111void StmtPrinter::PrintRawDecl(Decl *D) {
112 // FIXME: Need to complete/beautify this... this code simply shows the
Steve Naroff2a8ad182007-05-29 22:59:26 +0000113 // nodes are where they need to be.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000114 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
115 OS << "typedef " << localType->getUnderlyingType().getAsString();
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000116 OS << " " << localType->getNameAsString();
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000117 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerb4522b42007-06-02 05:27:01 +0000118 // Emit storage class for vardecls.
119 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
120 switch (V->getStorageClass()) {
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000121 default: assert(0 && "Unknown storage class!");
Mike Stump6abe6922009-02-10 23:49:50 +0000122 case VarDecl::None: break;
123 case VarDecl::Extern: OS << "extern "; break;
124 case VarDecl::Static: OS << "static "; break;
125 case VarDecl::Auto: OS << "auto "; break;
126 case VarDecl::Register: OS << "register "; break;
127 case VarDecl::PrivateExtern: OS << "__private_extern "; break;
Chris Lattnerb4522b42007-06-02 05:27:01 +0000128 }
129 }
130
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000131 std::string Name = VD->getNameAsString();
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000132 VD->getType().getAsStringInternal(Name);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000133 OS << Name;
134
Chris Lattner79c57592007-07-12 00:36:32 +0000135 // If this is a vardecl with an initializer, emit it.
136 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
137 if (V->getInit()) {
138 OS << " = ";
139 PrintExpr(V->getInit());
140 }
141 }
Steve Naroffc1e6d602007-11-17 21:21:01 +0000142 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
143 // print a free standing tag decl (e.g. "struct x;").
144 OS << TD->getKindName();
145 OS << " ";
146 if (const IdentifierInfo *II = TD->getIdentifier())
147 OS << II->getName();
Mike Stump74a76472009-02-10 20:16:46 +0000148 if (RecordDecl *RD = dyn_cast<RecordDecl>(TD)) {
149 OS << "{\n";
150 IndentLevel += 1;
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000151 // FIXME: The context passed to field_begin/field_end should
152 // never be NULL!
153 ASTContext *Context = 0;
154 for (RecordDecl::field_iterator i = RD->field_begin(*Context);
155 i != RD->field_end(*Context); ++i) {
Mike Stump74a76472009-02-10 20:16:46 +0000156 PrintFieldDecl(*i);
157 IndentLevel -= 1;
158 }
159 }
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000160 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000161 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000162 }
163}
164
Mike Stump74a76472009-02-10 20:16:46 +0000165void StmtPrinter::PrintFieldDecl(FieldDecl *FD) {
166 Indent() << FD->getNameAsString() << "\n";
167}
168
Ted Kremenek15e6b402008-10-06 18:39:36 +0000169void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Mike Stump74a76472009-02-10 20:16:46 +0000170 bool isFirst = true;
Ted Kremenek15e6b402008-10-06 18:39:36 +0000171
172 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
173 I != E; ++I) {
174
175 if (!isFirst) OS << ", ";
176 else isFirst = false;
177
178 PrintRawDecl(*I);
179 }
180}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000181
182void StmtPrinter::VisitNullStmt(NullStmt *Node) {
183 Indent() << ";\n";
184}
185
186void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenek15e6b402008-10-06 18:39:36 +0000187 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
188 I!=E; ++I) {
Chris Lattner776fac82007-06-09 00:53:06 +0000189 Indent();
Ted Kremenek15e6b402008-10-06 18:39:36 +0000190 PrintRawDecl(*I);
Chris Lattner776fac82007-06-09 00:53:06 +0000191 OS << ";\n";
192 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000193}
194
Chris Lattner073926e2007-05-20 23:04:55 +0000195void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
196 Indent();
197 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000198 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000199}
200
Chris Lattner6c0ff132006-11-05 00:19:50 +0000201void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000202 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000203 PrintExpr(Node->getLHS());
204 if (Node->getRHS()) {
205 OS << " ... ";
206 PrintExpr(Node->getRHS());
207 }
208 OS << ":\n";
209
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000210 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000211}
212
213void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000214 Indent(-1) << "default:\n";
215 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000216}
217
218void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000219 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000220 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000221}
222
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000223void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000224 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000225 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000226 OS << ')';
Chris Lattner073926e2007-05-20 23:04:55 +0000227
228 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
229 OS << ' ';
230 PrintRawCompoundStmt(CS);
231 OS << (If->getElse() ? ' ' : '\n');
232 } else {
233 OS << '\n';
234 PrintStmt(If->getThen());
235 if (If->getElse()) Indent();
236 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000237
Chris Lattner073926e2007-05-20 23:04:55 +0000238 if (Stmt *Else = If->getElse()) {
239 OS << "else";
240
241 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
242 OS << ' ';
243 PrintRawCompoundStmt(CS);
244 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000245 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
246 OS << ' ';
247 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000248 } else {
249 OS << '\n';
250 PrintStmt(If->getElse());
251 }
Chris Lattner882f7882006-11-04 18:52:07 +0000252 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000253}
254
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000255void StmtPrinter::VisitIfStmt(IfStmt *If) {
256 Indent();
257 PrintRawIfStmt(If);
258}
259
Chris Lattnerf2174b62006-11-04 20:59:27 +0000260void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
261 Indent() << "switch (";
262 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000263 OS << ")";
264
265 // Pretty print compoundstmt bodies (very common).
266 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
267 OS << " ";
268 PrintRawCompoundStmt(CS);
269 OS << "\n";
270 } else {
271 OS << "\n";
272 PrintStmt(Node->getBody());
273 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000274}
275
Anders Carlsson51873c22007-07-22 07:07:56 +0000276void StmtPrinter::VisitSwitchCase(SwitchCase*) {
277 assert(0 && "SwitchCase is an abstract class");
278}
279
Chris Lattner85ed8732006-11-04 20:40:44 +0000280void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
281 Indent() << "while (";
282 PrintExpr(Node->getCond());
283 OS << ")\n";
284 PrintStmt(Node->getBody());
285}
286
287void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000288 Indent() << "do ";
289 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
290 PrintRawCompoundStmt(CS);
291 OS << " ";
292 } else {
293 OS << "\n";
294 PrintStmt(Node->getBody());
295 Indent();
296 }
297
298 OS << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000299 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000300 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000301}
302
Chris Lattner71e23ce2006-11-04 20:18:38 +0000303void StmtPrinter::VisitForStmt(ForStmt *Node) {
304 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000305 if (Node->getInit()) {
306 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000307 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000308 else
309 PrintExpr(cast<Expr>(Node->getInit()));
310 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000311 OS << ";";
312 if (Node->getCond()) {
313 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000314 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000315 }
316 OS << ";";
317 if (Node->getInc()) {
318 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000319 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000320 }
321 OS << ") ";
322
323 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
324 PrintRawCompoundStmt(CS);
325 OS << "\n";
326 } else {
327 OS << "\n";
328 PrintStmt(Node->getBody());
329 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000330}
331
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000332void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000333 Indent() << "for (";
334 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000335 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000336 else
337 PrintExpr(cast<Expr>(Node->getElement()));
338 OS << " in ";
339 PrintExpr(Node->getCollection());
340 OS << ") ";
341
342 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
343 PrintRawCompoundStmt(CS);
344 OS << "\n";
345 } else {
346 OS << "\n";
347 PrintStmt(Node->getBody());
348 }
349}
350
Chris Lattner16976d32006-11-05 01:46:01 +0000351void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000352 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000353}
354
355void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000356 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000357 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000358 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000359}
360
361void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000362 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000363}
364
365void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000366 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000367}
368
369
Chris Lattner882f7882006-11-04 18:52:07 +0000370void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
371 Indent() << "return";
372 if (Node->getRetValue()) {
373 OS << " ";
374 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000375 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000376 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000377}
378
Chris Lattner73c56c02007-10-29 04:04:16 +0000379
380void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000381 Indent() << "asm ";
382
383 if (Node->isVolatile())
384 OS << "volatile ";
385
386 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000387 VisitStringLiteral(Node->getAsmString());
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000388
389 // Outputs
390 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
391 Node->getNumClobbers() != 0)
392 OS << " : ";
393
394 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
395 if (i != 0)
396 OS << ", ";
397
398 if (!Node->getOutputName(i).empty()) {
399 OS << '[';
400 OS << Node->getOutputName(i);
401 OS << "] ";
402 }
403
Chris Lattner72bbf172009-03-10 04:59:06 +0000404 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000405 OS << " ";
406 Visit(Node->getOutputExpr(i));
407 }
408
409 // Inputs
410 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
411 OS << " : ";
412
413 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
414 if (i != 0)
415 OS << ", ";
416
417 if (!Node->getInputName(i).empty()) {
418 OS << '[';
419 OS << Node->getInputName(i);
420 OS << "] ";
421 }
422
Chris Lattner72bbf172009-03-10 04:59:06 +0000423 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000424 OS << " ";
425 Visit(Node->getInputExpr(i));
426 }
427
428 // Clobbers
429 if (Node->getNumClobbers() != 0)
430 OS << " : ";
431
432 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
433 if (i != 0)
434 OS << ", ";
435
436 VisitStringLiteral(Node->getClobber(i));
437 }
438
Anders Carlsson81a5a692007-11-20 19:21:03 +0000439 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000440}
441
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000442void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000443 Indent() << "@try";
444 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
445 PrintRawCompoundStmt(TS);
446 OS << "\n";
447 }
448
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000449 for (ObjCAtCatchStmt *catchStmt =
450 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian88157952007-11-02 18:16:07 +0000451 catchStmt;
452 catchStmt =
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000453 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000454 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000455 if (catchStmt->getCatchParamDecl()) {
456 if (Decl *DS = catchStmt->getCatchParamDecl())
457 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000458 }
459 OS << ")";
460 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
461 {
462 PrintRawCompoundStmt(CS);
463 OS << "\n";
464 }
465 }
466
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000467 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000468 Node->getFinallyStmt())) {
469 Indent() << "@finally";
470 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000471 OS << "\n";
472 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000473}
474
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000475void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000476}
477
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000478void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000479 Indent() << "@catch (...) { /* todo */ } \n";
480}
481
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000482void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000483 Indent() << "@throw";
484 if (Node->getThrowExpr()) {
485 OS << " ";
486 PrintExpr(Node->getThrowExpr());
487 }
488 OS << ";\n";
489}
490
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000491void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000492 Indent() << "@synchronized (";
493 PrintExpr(Node->getSynchExpr());
494 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000495 PrintRawCompoundStmt(Node->getSynchBody());
496 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000497}
498
Sebastian Redl9b244a82008-12-22 21:35:02 +0000499void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
500 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000501 if (Decl *ExDecl = Node->getExceptionDecl())
502 PrintRawDecl(ExDecl);
503 else
504 OS << "...";
505 OS << ") ";
506 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000507}
508
509void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
510 Indent();
511 PrintRawCXXCatchStmt(Node);
512 OS << "\n";
513}
514
515void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
516 Indent() << "try ";
517 PrintRawCompoundStmt(Node->getTryBlock());
518 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
519 OS << " ";
520 PrintRawCXXCatchStmt(Node->getHandler(i));
521 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000522 OS << "\n";
523}
524
Chris Lattner71e23ce2006-11-04 20:18:38 +0000525//===----------------------------------------------------------------------===//
526// Expr printing methods.
527//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000528
Chris Lattner882f7882006-11-04 18:52:07 +0000529void StmtPrinter::VisitExpr(Expr *Node) {
530 OS << "<<unknown expr type>>";
531}
532
533void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000534 OS << Node->getDecl()->getNameAsString();
Chris Lattner882f7882006-11-04 18:52:07 +0000535}
536
Douglas Gregor18353912009-03-19 03:51:16 +0000537void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000538 NamedDecl *D = Node->getDecl();
539
Douglas Gregorb046ffb2009-03-31 20:22:05 +0000540 Node->getQualifier()->print(OS);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000541 OS << D->getNameAsString();
542}
543
Douglas Gregor90a1a652009-03-19 17:26:29 +0000544void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {
Douglas Gregorb046ffb2009-03-31 20:22:05 +0000545 Node->getQualifier()->print(OS);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000546 OS << Node->getDeclName().getAsString();
547}
548
Steve Naroffe46504b2007-11-12 14:29:37 +0000549void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000550 if (Node->getBase()) {
551 PrintExpr(Node->getBase());
552 OS << (Node->isArrow() ? "->" : ".");
553 }
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000554 OS << Node->getDecl()->getNameAsString();
Steve Naroffe46504b2007-11-12 14:29:37 +0000555}
556
Steve Naroffec944032008-05-30 00:40:33 +0000557void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
558 if (Node->getBase()) {
559 PrintExpr(Node->getBase());
560 OS << ".";
561 }
Steve Naroff4588d0f2008-12-04 16:24:46 +0000562 OS << Node->getProperty()->getNameAsCString();
Steve Naroffec944032008-05-30 00:40:33 +0000563}
564
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000565void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
566 if (Node->getBase()) {
567 PrintExpr(Node->getBase());
568 OS << ".";
569 }
570 // FIXME: Setter/Getter names
571}
572
Chris Lattner6307f192008-08-10 01:53:14 +0000573void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000574 switch (Node->getIdentType()) {
575 default:
576 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000577 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000578 OS << "__func__";
579 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000580 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000581 OS << "__FUNCTION__";
582 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000583 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000584 OS << "__PRETTY_FUNCTION__";
585 break;
586 }
587}
588
Steve Naroffae4143e2007-04-26 20:39:23 +0000589void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000590 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000591 if (Node->isWide())
592 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000593 switch (value) {
594 case '\\':
595 OS << "'\\\\'";
596 break;
597 case '\'':
598 OS << "'\\''";
599 break;
600 case '\a':
601 // TODO: K&R: the meaning of '\\a' is different in traditional C
602 OS << "'\\a'";
603 break;
604 case '\b':
605 OS << "'\\b'";
606 break;
607 // Nonstandard escape sequence.
608 /*case '\e':
609 OS << "'\\e'";
610 break;*/
611 case '\f':
612 OS << "'\\f'";
613 break;
614 case '\n':
615 OS << "'\\n'";
616 break;
617 case '\r':
618 OS << "'\\r'";
619 break;
620 case '\t':
621 OS << "'\\t'";
622 break;
623 case '\v':
624 OS << "'\\v'";
625 break;
626 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000627 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000628 OS << "'" << (char)value << "'";
629 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000630 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000631 } else {
632 // FIXME what to really do here?
633 OS << value;
634 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000635 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000636}
637
Steve Naroffdf7855b2007-02-21 23:46:25 +0000638void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000639 bool isSigned = Node->getType()->isSignedIntegerType();
640 OS << Node->getValue().toString(10, isSigned);
641
642 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattner574dee62008-07-26 22:17:49 +0000643 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000644 default: assert(0 && "Unexpected type for integer literal!");
645 case BuiltinType::Int: break; // no suffix.
646 case BuiltinType::UInt: OS << 'U'; break;
647 case BuiltinType::Long: OS << 'L'; break;
648 case BuiltinType::ULong: OS << "UL"; break;
649 case BuiltinType::LongLong: OS << "LL"; break;
650 case BuiltinType::ULongLong: OS << "ULL"; break;
651 }
Chris Lattner882f7882006-11-04 18:52:07 +0000652}
Steve Naroffab624882007-02-21 22:05:47 +0000653void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000654 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000655 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000656}
Chris Lattner1c20a172007-08-26 03:42:43 +0000657
658void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
659 PrintExpr(Node->getSubExpr());
660 OS << "i";
661}
662
Steve Naroffdf7855b2007-02-21 23:46:25 +0000663void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000664 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000665 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000666
Chris Lattner5d8f4942006-11-04 20:29:31 +0000667 // FIXME: this doesn't print wstrings right.
668 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000669 unsigned char Char = Str->getStrData()[i];
670
671 switch (Char) {
672 default:
673 if (isprint(Char))
674 OS << (char)Char;
675 else // Output anything hard as an octal escape.
676 OS << '\\'
677 << (char)('0'+ ((Char >> 6) & 7))
678 << (char)('0'+ ((Char >> 3) & 7))
679 << (char)('0'+ ((Char >> 0) & 7));
680 break;
681 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000682 case '\\': OS << "\\\\"; break;
683 case '"': OS << "\\\""; break;
684 case '\n': OS << "\\n"; break;
685 case '\t': OS << "\\t"; break;
686 case '\a': OS << "\\a"; break;
687 case '\b': OS << "\\b"; break;
688 }
689 }
690 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000691}
692void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
693 OS << "(";
694 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000695 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000696}
697void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000698 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000699 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000700
Sebastian Redl6f282892008-11-11 17:56:53 +0000701 // Print a space if this is an "identifier operator" like __real.
Chris Lattnere5b60442007-08-23 21:46:40 +0000702 switch (Node->getOpcode()) {
703 default: break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000704 case UnaryOperator::Real:
705 case UnaryOperator::Imag:
706 case UnaryOperator::Extension:
707 OS << ' ';
708 break;
709 }
710 }
Chris Lattner882f7882006-11-04 18:52:07 +0000711 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000712
713 if (Node->isPostfix())
714 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000715}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000716
717bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman988a16b2009-02-27 06:44:11 +0000718 if (isa<UnaryOperator>(E)) {
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000719 // Base case, print the type and comma.
720 OS << E->getType().getAsString() << ", ";
721 return true;
722 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
723 PrintOffsetOfDesignator(ASE->getLHS());
724 OS << "[";
725 PrintExpr(ASE->getRHS());
726 OS << "]";
727 return false;
728 } else {
729 MemberExpr *ME = cast<MemberExpr>(E);
730 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000731 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000732 return false;
733 }
734}
735
736void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
737 OS << "__builtin_offsetof(";
738 PrintOffsetOfDesignator(Node->getSubExpr());
739 OS << ")";
740}
741
Sebastian Redl6f282892008-11-11 17:56:53 +0000742void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
743 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
744 if (Node->isArgumentType())
745 OS << "(" << Node->getArgumentType().getAsString() << ")";
746 else {
747 OS << " ";
748 PrintExpr(Node->getArgumentExpr());
749 }
Chris Lattner882f7882006-11-04 18:52:07 +0000750}
751void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000752 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000753 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000754 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000755 OS << "]";
756}
757
758void StmtPrinter::VisitCallExpr(CallExpr *Call) {
759 PrintExpr(Call->getCallee());
760 OS << "(";
761 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000762 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
763 // Don't print any defaulted arguments
764 break;
765 }
766
Chris Lattner882f7882006-11-04 18:52:07 +0000767 if (i) OS << ", ";
768 PrintExpr(Call->getArg(i));
769 }
770 OS << ")";
771}
772void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000773 // FIXME: Suppress printing implicit bases (like "this")
774 PrintExpr(Node->getBase());
775 OS << (Node->isArrow() ? "->" : ".");
776 // FIXME: Suppress printing references to unnamed objects
777 // representing anonymous unions/structs
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000778 OS << Node->getMemberDecl()->getNameAsString();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000779}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000780void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000781 PrintExpr(Node->getBase());
782 OS << ".";
783 OS << Node->getAccessor().getName();
784}
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000785void StmtPrinter::VisitCastExpr(CastExpr *) {
786 assert(0 && "CastExpr is an abstract class");
787}
Douglas Gregore200adc2008-10-27 19:41:14 +0000788void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
789 assert(0 && "ExplicitCastExpr is an abstract class");
790}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000791void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000792 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000793 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000794}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000795void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
796 OS << "(" << Node->getType().getAsString() << ")";
797 PrintExpr(Node->getInitializer());
798}
Steve Naroff7a5af782007-07-13 16:58:59 +0000799void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000800 // No need to print anything, simply forward to the sub expression.
801 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000802}
Chris Lattner882f7882006-11-04 18:52:07 +0000803void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
804 PrintExpr(Node->getLHS());
805 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
806 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000807}
Chris Lattner86928112007-08-25 02:00:02 +0000808void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
809 PrintExpr(Node->getLHS());
810 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
811 PrintExpr(Node->getRHS());
812}
Chris Lattner882f7882006-11-04 18:52:07 +0000813void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
814 PrintExpr(Node->getCond());
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000815
816 if (Node->getLHS()) {
817 OS << " ? ";
818 PrintExpr(Node->getLHS());
819 OS << " : ";
820 }
821 else { // Handle GCC extention where LHS can be NULL.
822 OS << " ?: ";
823 }
824
Chris Lattner882f7882006-11-04 18:52:07 +0000825 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000826}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000827
Chris Lattnereefa10e2007-05-28 06:56:27 +0000828// GNU extensions.
829
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000830void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000831 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000832}
833
Chris Lattner366727f2007-07-24 16:58:17 +0000834void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
835 OS << "(";
836 PrintRawCompoundStmt(E->getSubStmt());
837 OS << ")";
838}
839
Steve Naroff78864672007-08-01 22:05:33 +0000840void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
841 OS << "__builtin_types_compatible_p(";
842 OS << Node->getArgType1().getAsString() << ",";
843 OS << Node->getArgType2().getAsString() << ")";
844}
845
Steve Naroff9efdabc2007-08-03 21:21:27 +0000846void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
847 OS << "__builtin_choose_expr(";
848 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000849 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000850 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000851 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000852 PrintExpr(Node->getRHS());
853 OS << ")";
854}
Chris Lattner366727f2007-07-24 16:58:17 +0000855
Douglas Gregor3be4b122008-11-29 04:51:27 +0000856void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
857 OS << "__null";
858}
859
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000860void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
861 OS << "__builtin_shufflevector(";
862 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
863 if (i) OS << ", ";
864 PrintExpr(Node->getExpr(i));
865 }
866 OS << ")";
867}
868
Anders Carlsson4692db02007-08-31 04:56:16 +0000869void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
870 OS << "{ ";
871 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
872 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000873 if (Node->getInit(i))
874 PrintExpr(Node->getInit(i));
875 else
876 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000877 }
878 OS << " }";
879}
880
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000881void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000882 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
883 DEnd = Node->designators_end();
884 D != DEnd; ++D) {
885 if (D->isFieldDesignator()) {
886 if (D->getDotLoc().isInvalid())
887 OS << D->getFieldName()->getName() << ":";
888 else
889 OS << "." << D->getFieldName()->getName();
890 } else {
891 OS << "[";
892 if (D->isArrayDesignator()) {
893 PrintExpr(Node->getArrayIndex(*D));
894 } else {
895 PrintExpr(Node->getArrayRangeStart(*D));
896 OS << " ... ";
897 PrintExpr(Node->getArrayRangeEnd(*D));
898 }
899 OS << "]";
900 }
901 }
902
903 OS << " = ";
904 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000905}
906
Douglas Gregor0202cb42009-01-29 17:44:32 +0000907void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
908 OS << "/*implicit*/" << Node->getType().getAsString() << "()";
909}
910
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000911void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
912 OS << "va_arg(";
913 PrintExpr(Node->getSubExpr());
914 OS << ", ";
915 OS << Node->getType().getAsString();
916 OS << ")";
917}
918
Chris Lattnereefa10e2007-05-28 06:56:27 +0000919// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000920void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
921 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
922 "",
923#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
924 Spelling,
925#include "clang/Basic/OperatorKinds.def"
926 };
927
928 OverloadedOperatorKind Kind = Node->getOperator();
929 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
930 if (Node->getNumArgs() == 1) {
931 OS << OpStrings[Kind] << ' ';
932 PrintExpr(Node->getArg(0));
933 } else {
934 PrintExpr(Node->getArg(0));
935 OS << ' ' << OpStrings[Kind];
936 }
937 } else if (Kind == OO_Call) {
938 PrintExpr(Node->getArg(0));
939 OS << '(';
940 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
941 if (ArgIdx > 1)
942 OS << ", ";
943 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
944 PrintExpr(Node->getArg(ArgIdx));
945 }
946 OS << ')';
947 } else if (Kind == OO_Subscript) {
948 PrintExpr(Node->getArg(0));
949 OS << '[';
950 PrintExpr(Node->getArg(1));
951 OS << ']';
952 } else if (Node->getNumArgs() == 1) {
953 OS << OpStrings[Kind] << ' ';
954 PrintExpr(Node->getArg(0));
955 } else if (Node->getNumArgs() == 2) {
956 PrintExpr(Node->getArg(0));
957 OS << ' ' << OpStrings[Kind] << ' ';
958 PrintExpr(Node->getArg(1));
959 } else {
960 assert(false && "unknown overloaded operator");
961 }
962}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000963
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000964void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
965 VisitCallExpr(cast<CallExpr>(Node));
966}
967
Douglas Gregore200adc2008-10-27 19:41:14 +0000968void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
969 OS << Node->getCastName() << '<';
970 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000971 PrintExpr(Node->getSubExpr());
972 OS << ")";
973}
974
Douglas Gregore200adc2008-10-27 19:41:14 +0000975void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
976 VisitCXXNamedCastExpr(Node);
977}
978
979void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
980 VisitCXXNamedCastExpr(Node);
981}
982
983void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
984 VisitCXXNamedCastExpr(Node);
985}
986
987void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
988 VisitCXXNamedCastExpr(Node);
989}
990
Sebastian Redlc4704762008-11-11 11:37:55 +0000991void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
992 OS << "typeid(";
993 if (Node->isTypeOperand()) {
994 OS << Node->getTypeOperand().getAsString();
995 } else {
996 PrintExpr(Node->getExprOperand());
997 }
998 OS << ")";
999}
1000
Chris Lattnereefa10e2007-05-28 06:56:27 +00001001void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1002 OS << (Node->getValue() ? "true" : "false");
1003}
1004
Douglas Gregor97a9c812008-11-04 14:32:21 +00001005void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1006 OS << "this";
1007}
1008
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001009void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1010 if (Node->getSubExpr() == 0)
1011 OS << "throw";
1012 else {
1013 OS << "throw ";
1014 PrintExpr(Node->getSubExpr());
1015 }
1016}
1017
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001018void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1019 // Nothing to print: we picked up the default argument
1020}
1021
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001022void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1023 OS << Node->getType().getAsString();
1024 OS << "(";
1025 PrintExpr(Node->getSubExpr());
1026 OS << ")";
1027}
1028
Douglas Gregordd04d332009-01-16 18:33:17 +00001029void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1030 OS << Node->getType().getAsString();
1031 OS << "(";
1032 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1033 ArgEnd = Node->arg_end();
1034 Arg != ArgEnd; ++Arg) {
1035 if (Arg != Node->arg_begin())
1036 OS << ", ";
1037 PrintExpr(*Arg);
1038 }
1039 OS << ")";
1040}
1041
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001042void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1043 OS << Node->getType().getAsString() << "()";
1044}
1045
Argyrios Kyrtzidisaa479132008-09-09 23:47:53 +00001046void
1047StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1048 PrintRawDecl(E->getVarDecl());
1049}
1050
Sebastian Redlbd150f42008-11-21 19:14:01 +00001051void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1052 if (E->isGlobalNew())
1053 OS << "::";
1054 OS << "new ";
1055 unsigned NumPlace = E->getNumPlacementArgs();
1056 if (NumPlace > 0) {
1057 OS << "(";
1058 PrintExpr(E->getPlacementArg(0));
1059 for (unsigned i = 1; i < NumPlace; ++i) {
1060 OS << ", ";
1061 PrintExpr(E->getPlacementArg(i));
1062 }
1063 OS << ") ";
1064 }
1065 if (E->isParenTypeId())
1066 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001067 std::string TypeS;
1068 if (Expr *Size = E->getArraySize()) {
1069 llvm::raw_string_ostream s(TypeS);
1070 Size->printPretty(s);
1071 s.flush();
1072 TypeS = "[" + TypeS + "]";
1073 }
1074 E->getAllocatedType().getAsStringInternal(TypeS);
1075 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001076 if (E->isParenTypeId())
1077 OS << ")";
1078
1079 if (E->hasInitializer()) {
1080 OS << "(";
1081 unsigned NumCons = E->getNumConstructorArgs();
1082 if (NumCons > 0) {
1083 PrintExpr(E->getConstructorArg(0));
1084 for (unsigned i = 1; i < NumCons; ++i) {
1085 OS << ", ";
1086 PrintExpr(E->getConstructorArg(i));
1087 }
1088 }
1089 OS << ")";
1090 }
1091}
1092
1093void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1094 if (E->isGlobalDelete())
1095 OS << "::";
1096 OS << "delete ";
1097 if (E->isArrayForm())
1098 OS << "[] ";
1099 PrintExpr(E->getArgument());
1100}
1101
Douglas Gregorb8a9a412009-02-04 15:01:18 +00001102void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1103 OS << E->getName().getAsString();
Douglas Gregorb0846b02008-12-06 00:22:45 +00001104}
1105
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001106static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1107 switch (UTT) {
1108 default: assert(false && "Unknown type trait");
1109 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1110 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1111 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1112 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1113 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1114 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1115 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1116 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1117 case UTT_IsAbstract: return "__is_abstract";
1118 case UTT_IsClass: return "__is_class";
1119 case UTT_IsEmpty: return "__is_empty";
1120 case UTT_IsEnum: return "__is_enum";
1121 case UTT_IsPOD: return "__is_pod";
1122 case UTT_IsPolymorphic: return "__is_polymorphic";
1123 case UTT_IsUnion: return "__is_union";
1124 }
1125}
1126
1127void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1128 OS << getTypeTraitName(E->getTrait()) << "("
1129 << E->getQueriedType().getAsString() << ")";
1130}
1131
Anders Carlsson76f4a902007-08-21 17:43:55 +00001132// Obj-C
1133
1134void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1135 OS << "@";
1136 VisitStringLiteral(Node->getString());
1137}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001138
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001139void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001140 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001141}
1142
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001143void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001144 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001145}
1146
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001147void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001148 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001149}
1150
Steve Naroffd54978b2007-09-18 23:55:05 +00001151void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1152 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +00001153 Expr *receiver = Mess->getReceiver();
1154 if (receiver) PrintExpr(receiver);
1155 else OS << Mess->getClassName()->getName();
Ted Kremeneka06e7122008-05-02 17:32:38 +00001156 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001157 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001158 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001159 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001160 } else {
1161 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001162 if (i < selector.getNumArgs()) {
1163 if (i > 0) OS << ' ';
1164 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001165 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001166 else
1167 OS << ":";
1168 }
1169 else OS << ", "; // Handle variadic methods.
1170
Steve Naroffc6814ea2007-10-02 20:01:56 +00001171 PrintExpr(Mess->getArg(i));
1172 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001173 }
1174 OS << "]";
1175}
1176
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001177void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1178 OS << "super";
1179}
1180
Steve Naroffc540d662008-09-03 18:15:37 +00001181void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001182 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001183 OS << "^";
1184
1185 const FunctionType *AFT = Node->getFunctionType();
1186
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001187 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001188 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001189 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001190 OS << '(';
1191 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001192 for (BlockDecl::param_iterator AI = BD->param_begin(),
1193 E = BD->param_end(); AI != E; ++AI) {
1194 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001195 ParamStr = (*AI)->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001196 (*AI)->getType().getAsStringInternal(ParamStr);
1197 OS << ParamStr;
1198 }
1199
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001200 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001201 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001202 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001203 OS << "...";
1204 }
1205 OS << ')';
1206 }
1207}
1208
Steve Naroffc540d662008-09-03 18:15:37 +00001209void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001210 OS << Node->getDecl()->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001211}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001212//===----------------------------------------------------------------------===//
1213// Stmt method implementations
1214//===----------------------------------------------------------------------===//
1215
Chris Lattnercbe4f772007-08-08 22:51:59 +00001216void Stmt::dumpPretty() const {
Daniel Dunbar4095d892009-03-10 18:00:19 +00001217 printPretty(llvm::errs());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001218}
1219
Mike Stump74a76472009-02-10 20:16:46 +00001220void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper,
1221 unsigned I, bool NoIndent) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001222 if (this == 0) {
1223 OS << "<NULL>";
1224 return;
1225 }
1226
Mike Stump74a76472009-02-10 20:16:46 +00001227 StmtPrinter P(OS, Helper, I, NoIndent);
Chris Lattner62249a62007-08-21 04:04:25 +00001228 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001229}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001230
1231//===----------------------------------------------------------------------===//
1232// PrinterHelper
1233//===----------------------------------------------------------------------===//
1234
1235// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001236PrinterHelper::~PrinterHelper() {}