blob: cd0e8822a8ab00bdeb1bf89e41d987e2c439e2d9 [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;
151 for (RecordDecl::field_iterator i = RD->field_begin(); i != RD->field_end(); ++i) {
152 PrintFieldDecl(*i);
153 IndentLevel -= 1;
154 }
155 }
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000156 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000157 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000158 }
159}
160
Mike Stump74a76472009-02-10 20:16:46 +0000161void StmtPrinter::PrintFieldDecl(FieldDecl *FD) {
162 Indent() << FD->getNameAsString() << "\n";
163}
164
Ted Kremenek15e6b402008-10-06 18:39:36 +0000165void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Mike Stump74a76472009-02-10 20:16:46 +0000166 bool isFirst = true;
Ted Kremenek15e6b402008-10-06 18:39:36 +0000167
168 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
169 I != E; ++I) {
170
171 if (!isFirst) OS << ", ";
172 else isFirst = false;
173
174 PrintRawDecl(*I);
175 }
176}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000177
178void StmtPrinter::VisitNullStmt(NullStmt *Node) {
179 Indent() << ";\n";
180}
181
182void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenek15e6b402008-10-06 18:39:36 +0000183 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
184 I!=E; ++I) {
Chris Lattner776fac82007-06-09 00:53:06 +0000185 Indent();
Ted Kremenek15e6b402008-10-06 18:39:36 +0000186 PrintRawDecl(*I);
Chris Lattner776fac82007-06-09 00:53:06 +0000187 OS << ";\n";
188 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000189}
190
Chris Lattner073926e2007-05-20 23:04:55 +0000191void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
192 Indent();
193 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000194 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000195}
196
Chris Lattner6c0ff132006-11-05 00:19:50 +0000197void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000198 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000199 PrintExpr(Node->getLHS());
200 if (Node->getRHS()) {
201 OS << " ... ";
202 PrintExpr(Node->getRHS());
203 }
204 OS << ":\n";
205
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000206 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000207}
208
209void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000210 Indent(-1) << "default:\n";
211 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000212}
213
214void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000215 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000216 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000217}
218
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000219void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000220 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000221 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000222 OS << ')';
Chris Lattner073926e2007-05-20 23:04:55 +0000223
224 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
225 OS << ' ';
226 PrintRawCompoundStmt(CS);
227 OS << (If->getElse() ? ' ' : '\n');
228 } else {
229 OS << '\n';
230 PrintStmt(If->getThen());
231 if (If->getElse()) Indent();
232 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000233
Chris Lattner073926e2007-05-20 23:04:55 +0000234 if (Stmt *Else = If->getElse()) {
235 OS << "else";
236
237 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
238 OS << ' ';
239 PrintRawCompoundStmt(CS);
240 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000241 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
242 OS << ' ';
243 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000244 } else {
245 OS << '\n';
246 PrintStmt(If->getElse());
247 }
Chris Lattner882f7882006-11-04 18:52:07 +0000248 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000249}
250
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000251void StmtPrinter::VisitIfStmt(IfStmt *If) {
252 Indent();
253 PrintRawIfStmt(If);
254}
255
Chris Lattnerf2174b62006-11-04 20:59:27 +0000256void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
257 Indent() << "switch (";
258 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000259 OS << ")";
260
261 // Pretty print compoundstmt bodies (very common).
262 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
263 OS << " ";
264 PrintRawCompoundStmt(CS);
265 OS << "\n";
266 } else {
267 OS << "\n";
268 PrintStmt(Node->getBody());
269 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000270}
271
Anders Carlsson51873c22007-07-22 07:07:56 +0000272void StmtPrinter::VisitSwitchCase(SwitchCase*) {
273 assert(0 && "SwitchCase is an abstract class");
274}
275
Chris Lattner85ed8732006-11-04 20:40:44 +0000276void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
277 Indent() << "while (";
278 PrintExpr(Node->getCond());
279 OS << ")\n";
280 PrintStmt(Node->getBody());
281}
282
283void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000284 Indent() << "do ";
285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
287 OS << " ";
288 } else {
289 OS << "\n";
290 PrintStmt(Node->getBody());
291 Indent();
292 }
293
294 OS << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000295 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000296 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000297}
298
Chris Lattner71e23ce2006-11-04 20:18:38 +0000299void StmtPrinter::VisitForStmt(ForStmt *Node) {
300 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000301 if (Node->getInit()) {
302 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000303 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000304 else
305 PrintExpr(cast<Expr>(Node->getInit()));
306 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000307 OS << ";";
308 if (Node->getCond()) {
309 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000310 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000311 }
312 OS << ";";
313 if (Node->getInc()) {
314 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000315 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000316 }
317 OS << ") ";
318
319 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
320 PrintRawCompoundStmt(CS);
321 OS << "\n";
322 } else {
323 OS << "\n";
324 PrintStmt(Node->getBody());
325 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000326}
327
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000328void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000329 Indent() << "for (";
330 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000331 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000332 else
333 PrintExpr(cast<Expr>(Node->getElement()));
334 OS << " in ";
335 PrintExpr(Node->getCollection());
336 OS << ") ";
337
338 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
339 PrintRawCompoundStmt(CS);
340 OS << "\n";
341 } else {
342 OS << "\n";
343 PrintStmt(Node->getBody());
344 }
345}
346
Chris Lattner16976d32006-11-05 01:46:01 +0000347void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000348 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000349}
350
351void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000352 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000353 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000354 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000355}
356
357void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000358 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000359}
360
361void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000362 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000363}
364
365
Chris Lattner882f7882006-11-04 18:52:07 +0000366void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
367 Indent() << "return";
368 if (Node->getRetValue()) {
369 OS << " ";
370 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000371 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000372 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000373}
374
Chris Lattner73c56c02007-10-29 04:04:16 +0000375
376void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000377 Indent() << "asm ";
378
379 if (Node->isVolatile())
380 OS << "volatile ";
381
382 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000383 VisitStringLiteral(Node->getAsmString());
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000384
385 // Outputs
386 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
387 Node->getNumClobbers() != 0)
388 OS << " : ";
389
390 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
391 if (i != 0)
392 OS << ", ";
393
394 if (!Node->getOutputName(i).empty()) {
395 OS << '[';
396 OS << Node->getOutputName(i);
397 OS << "] ";
398 }
399
Chris Lattner72bbf172009-03-10 04:59:06 +0000400 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000401 OS << " ";
402 Visit(Node->getOutputExpr(i));
403 }
404
405 // Inputs
406 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
407 OS << " : ";
408
409 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
410 if (i != 0)
411 OS << ", ";
412
413 if (!Node->getInputName(i).empty()) {
414 OS << '[';
415 OS << Node->getInputName(i);
416 OS << "] ";
417 }
418
Chris Lattner72bbf172009-03-10 04:59:06 +0000419 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000420 OS << " ";
421 Visit(Node->getInputExpr(i));
422 }
423
424 // Clobbers
425 if (Node->getNumClobbers() != 0)
426 OS << " : ";
427
428 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
429 if (i != 0)
430 OS << ", ";
431
432 VisitStringLiteral(Node->getClobber(i));
433 }
434
Anders Carlsson81a5a692007-11-20 19:21:03 +0000435 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000436}
437
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000438void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000439 Indent() << "@try";
440 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
441 PrintRawCompoundStmt(TS);
442 OS << "\n";
443 }
444
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000445 for (ObjCAtCatchStmt *catchStmt =
446 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian88157952007-11-02 18:16:07 +0000447 catchStmt;
448 catchStmt =
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000449 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000450 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000451 if (catchStmt->getCatchParamDecl()) {
452 if (Decl *DS = catchStmt->getCatchParamDecl())
453 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000454 }
455 OS << ")";
456 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
457 {
458 PrintRawCompoundStmt(CS);
459 OS << "\n";
460 }
461 }
462
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000463 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000464 Node->getFinallyStmt())) {
465 Indent() << "@finally";
466 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000467 OS << "\n";
468 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000469}
470
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000471void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000472}
473
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000474void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000475 Indent() << "@catch (...) { /* todo */ } \n";
476}
477
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000478void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000479 Indent() << "@throw";
480 if (Node->getThrowExpr()) {
481 OS << " ";
482 PrintExpr(Node->getThrowExpr());
483 }
484 OS << ";\n";
485}
486
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000487void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000488 Indent() << "@synchronized (";
489 PrintExpr(Node->getSynchExpr());
490 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000491 PrintRawCompoundStmt(Node->getSynchBody());
492 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000493}
494
Sebastian Redl9b244a82008-12-22 21:35:02 +0000495void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
496 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000497 if (Decl *ExDecl = Node->getExceptionDecl())
498 PrintRawDecl(ExDecl);
499 else
500 OS << "...";
501 OS << ") ";
502 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000503}
504
505void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
506 Indent();
507 PrintRawCXXCatchStmt(Node);
508 OS << "\n";
509}
510
511void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
512 Indent() << "try ";
513 PrintRawCompoundStmt(Node->getTryBlock());
514 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
515 OS << " ";
516 PrintRawCXXCatchStmt(Node->getHandler(i));
517 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000518 OS << "\n";
519}
520
Chris Lattner71e23ce2006-11-04 20:18:38 +0000521//===----------------------------------------------------------------------===//
522// Expr printing methods.
523//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000524
Chris Lattner882f7882006-11-04 18:52:07 +0000525void StmtPrinter::VisitExpr(Expr *Node) {
526 OS << "<<unknown expr type>>";
527}
528
529void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000530 OS << Node->getDecl()->getNameAsString();
Chris Lattner882f7882006-11-04 18:52:07 +0000531}
532
Douglas Gregor18353912009-03-19 03:51:16 +0000533void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000534 NamedDecl *D = Node->getDecl();
535
Douglas Gregorb046ffb2009-03-31 20:22:05 +0000536 Node->getQualifier()->print(OS);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000537 OS << D->getNameAsString();
538}
539
Douglas Gregor90a1a652009-03-19 17:26:29 +0000540void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {
Douglas Gregorb046ffb2009-03-31 20:22:05 +0000541 Node->getQualifier()->print(OS);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000542 OS << Node->getDeclName().getAsString();
543}
544
Steve Naroffe46504b2007-11-12 14:29:37 +0000545void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000546 if (Node->getBase()) {
547 PrintExpr(Node->getBase());
548 OS << (Node->isArrow() ? "->" : ".");
549 }
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000550 OS << Node->getDecl()->getNameAsString();
Steve Naroffe46504b2007-11-12 14:29:37 +0000551}
552
Steve Naroffec944032008-05-30 00:40:33 +0000553void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
554 if (Node->getBase()) {
555 PrintExpr(Node->getBase());
556 OS << ".";
557 }
Steve Naroff4588d0f2008-12-04 16:24:46 +0000558 OS << Node->getProperty()->getNameAsCString();
Steve Naroffec944032008-05-30 00:40:33 +0000559}
560
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000561void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
562 if (Node->getBase()) {
563 PrintExpr(Node->getBase());
564 OS << ".";
565 }
566 // FIXME: Setter/Getter names
567}
568
Chris Lattner6307f192008-08-10 01:53:14 +0000569void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000570 switch (Node->getIdentType()) {
571 default:
572 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000573 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000574 OS << "__func__";
575 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000576 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000577 OS << "__FUNCTION__";
578 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000579 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000580 OS << "__PRETTY_FUNCTION__";
581 break;
582 }
583}
584
Steve Naroffae4143e2007-04-26 20:39:23 +0000585void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000586 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000587 if (Node->isWide())
588 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000589 switch (value) {
590 case '\\':
591 OS << "'\\\\'";
592 break;
593 case '\'':
594 OS << "'\\''";
595 break;
596 case '\a':
597 // TODO: K&R: the meaning of '\\a' is different in traditional C
598 OS << "'\\a'";
599 break;
600 case '\b':
601 OS << "'\\b'";
602 break;
603 // Nonstandard escape sequence.
604 /*case '\e':
605 OS << "'\\e'";
606 break;*/
607 case '\f':
608 OS << "'\\f'";
609 break;
610 case '\n':
611 OS << "'\\n'";
612 break;
613 case '\r':
614 OS << "'\\r'";
615 break;
616 case '\t':
617 OS << "'\\t'";
618 break;
619 case '\v':
620 OS << "'\\v'";
621 break;
622 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000623 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000624 OS << "'" << (char)value << "'";
625 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000626 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000627 } else {
628 // FIXME what to really do here?
629 OS << value;
630 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000631 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000632}
633
Steve Naroffdf7855b2007-02-21 23:46:25 +0000634void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000635 bool isSigned = Node->getType()->isSignedIntegerType();
636 OS << Node->getValue().toString(10, isSigned);
637
638 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattner574dee62008-07-26 22:17:49 +0000639 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000640 default: assert(0 && "Unexpected type for integer literal!");
641 case BuiltinType::Int: break; // no suffix.
642 case BuiltinType::UInt: OS << 'U'; break;
643 case BuiltinType::Long: OS << 'L'; break;
644 case BuiltinType::ULong: OS << "UL"; break;
645 case BuiltinType::LongLong: OS << "LL"; break;
646 case BuiltinType::ULongLong: OS << "ULL"; break;
647 }
Chris Lattner882f7882006-11-04 18:52:07 +0000648}
Steve Naroffab624882007-02-21 22:05:47 +0000649void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000650 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000651 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000652}
Chris Lattner1c20a172007-08-26 03:42:43 +0000653
654void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
655 PrintExpr(Node->getSubExpr());
656 OS << "i";
657}
658
Steve Naroffdf7855b2007-02-21 23:46:25 +0000659void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000660 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000661 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000662
Chris Lattner5d8f4942006-11-04 20:29:31 +0000663 // FIXME: this doesn't print wstrings right.
664 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000665 unsigned char Char = Str->getStrData()[i];
666
667 switch (Char) {
668 default:
669 if (isprint(Char))
670 OS << (char)Char;
671 else // Output anything hard as an octal escape.
672 OS << '\\'
673 << (char)('0'+ ((Char >> 6) & 7))
674 << (char)('0'+ ((Char >> 3) & 7))
675 << (char)('0'+ ((Char >> 0) & 7));
676 break;
677 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000678 case '\\': OS << "\\\\"; break;
679 case '"': OS << "\\\""; break;
680 case '\n': OS << "\\n"; break;
681 case '\t': OS << "\\t"; break;
682 case '\a': OS << "\\a"; break;
683 case '\b': OS << "\\b"; break;
684 }
685 }
686 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000687}
688void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
689 OS << "(";
690 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000691 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000692}
693void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000694 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000695 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000696
Sebastian Redl6f282892008-11-11 17:56:53 +0000697 // Print a space if this is an "identifier operator" like __real.
Chris Lattnere5b60442007-08-23 21:46:40 +0000698 switch (Node->getOpcode()) {
699 default: break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000700 case UnaryOperator::Real:
701 case UnaryOperator::Imag:
702 case UnaryOperator::Extension:
703 OS << ' ';
704 break;
705 }
706 }
Chris Lattner882f7882006-11-04 18:52:07 +0000707 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000708
709 if (Node->isPostfix())
710 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000711}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000712
713bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman988a16b2009-02-27 06:44:11 +0000714 if (isa<UnaryOperator>(E)) {
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000715 // Base case, print the type and comma.
716 OS << E->getType().getAsString() << ", ";
717 return true;
718 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
719 PrintOffsetOfDesignator(ASE->getLHS());
720 OS << "[";
721 PrintExpr(ASE->getRHS());
722 OS << "]";
723 return false;
724 } else {
725 MemberExpr *ME = cast<MemberExpr>(E);
726 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000727 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000728 return false;
729 }
730}
731
732void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
733 OS << "__builtin_offsetof(";
734 PrintOffsetOfDesignator(Node->getSubExpr());
735 OS << ")";
736}
737
Sebastian Redl6f282892008-11-11 17:56:53 +0000738void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
739 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
740 if (Node->isArgumentType())
741 OS << "(" << Node->getArgumentType().getAsString() << ")";
742 else {
743 OS << " ";
744 PrintExpr(Node->getArgumentExpr());
745 }
Chris Lattner882f7882006-11-04 18:52:07 +0000746}
747void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000748 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000749 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000750 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000751 OS << "]";
752}
753
754void StmtPrinter::VisitCallExpr(CallExpr *Call) {
755 PrintExpr(Call->getCallee());
756 OS << "(";
757 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000758 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
759 // Don't print any defaulted arguments
760 break;
761 }
762
Chris Lattner882f7882006-11-04 18:52:07 +0000763 if (i) OS << ", ";
764 PrintExpr(Call->getArg(i));
765 }
766 OS << ")";
767}
768void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000769 // FIXME: Suppress printing implicit bases (like "this")
770 PrintExpr(Node->getBase());
771 OS << (Node->isArrow() ? "->" : ".");
772 // FIXME: Suppress printing references to unnamed objects
773 // representing anonymous unions/structs
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000774 OS << Node->getMemberDecl()->getNameAsString();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000775}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000776void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000777 PrintExpr(Node->getBase());
778 OS << ".";
779 OS << Node->getAccessor().getName();
780}
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000781void StmtPrinter::VisitCastExpr(CastExpr *) {
782 assert(0 && "CastExpr is an abstract class");
783}
Douglas Gregore200adc2008-10-27 19:41:14 +0000784void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
785 assert(0 && "ExplicitCastExpr is an abstract class");
786}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000787void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000788 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000789 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000790}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000791void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
792 OS << "(" << Node->getType().getAsString() << ")";
793 PrintExpr(Node->getInitializer());
794}
Steve Naroff7a5af782007-07-13 16:58:59 +0000795void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000796 // No need to print anything, simply forward to the sub expression.
797 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000798}
Chris Lattner882f7882006-11-04 18:52:07 +0000799void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
800 PrintExpr(Node->getLHS());
801 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
802 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000803}
Chris Lattner86928112007-08-25 02:00:02 +0000804void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
805 PrintExpr(Node->getLHS());
806 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
807 PrintExpr(Node->getRHS());
808}
Chris Lattner882f7882006-11-04 18:52:07 +0000809void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
810 PrintExpr(Node->getCond());
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000811
812 if (Node->getLHS()) {
813 OS << " ? ";
814 PrintExpr(Node->getLHS());
815 OS << " : ";
816 }
817 else { // Handle GCC extention where LHS can be NULL.
818 OS << " ?: ";
819 }
820
Chris Lattner882f7882006-11-04 18:52:07 +0000821 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000822}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000823
Chris Lattnereefa10e2007-05-28 06:56:27 +0000824// GNU extensions.
825
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000826void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000827 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000828}
829
Chris Lattner366727f2007-07-24 16:58:17 +0000830void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
831 OS << "(";
832 PrintRawCompoundStmt(E->getSubStmt());
833 OS << ")";
834}
835
Steve Naroff78864672007-08-01 22:05:33 +0000836void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
837 OS << "__builtin_types_compatible_p(";
838 OS << Node->getArgType1().getAsString() << ",";
839 OS << Node->getArgType2().getAsString() << ")";
840}
841
Steve Naroff9efdabc2007-08-03 21:21:27 +0000842void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
843 OS << "__builtin_choose_expr(";
844 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000845 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000846 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000847 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000848 PrintExpr(Node->getRHS());
849 OS << ")";
850}
Chris Lattner366727f2007-07-24 16:58:17 +0000851
Douglas Gregor3be4b122008-11-29 04:51:27 +0000852void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
853 OS << "__null";
854}
855
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000856void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
857 OS << "__builtin_shufflevector(";
858 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
859 if (i) OS << ", ";
860 PrintExpr(Node->getExpr(i));
861 }
862 OS << ")";
863}
864
Anders Carlsson4692db02007-08-31 04:56:16 +0000865void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
866 OS << "{ ";
867 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
868 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000869 if (Node->getInit(i))
870 PrintExpr(Node->getInit(i));
871 else
872 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000873 }
874 OS << " }";
875}
876
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000877void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000878 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
879 DEnd = Node->designators_end();
880 D != DEnd; ++D) {
881 if (D->isFieldDesignator()) {
882 if (D->getDotLoc().isInvalid())
883 OS << D->getFieldName()->getName() << ":";
884 else
885 OS << "." << D->getFieldName()->getName();
886 } else {
887 OS << "[";
888 if (D->isArrayDesignator()) {
889 PrintExpr(Node->getArrayIndex(*D));
890 } else {
891 PrintExpr(Node->getArrayRangeStart(*D));
892 OS << " ... ";
893 PrintExpr(Node->getArrayRangeEnd(*D));
894 }
895 OS << "]";
896 }
897 }
898
899 OS << " = ";
900 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000901}
902
Douglas Gregor0202cb42009-01-29 17:44:32 +0000903void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
904 OS << "/*implicit*/" << Node->getType().getAsString() << "()";
905}
906
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000907void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
908 OS << "va_arg(";
909 PrintExpr(Node->getSubExpr());
910 OS << ", ";
911 OS << Node->getType().getAsString();
912 OS << ")";
913}
914
Chris Lattnereefa10e2007-05-28 06:56:27 +0000915// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000916void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
917 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
918 "",
919#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
920 Spelling,
921#include "clang/Basic/OperatorKinds.def"
922 };
923
924 OverloadedOperatorKind Kind = Node->getOperator();
925 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
926 if (Node->getNumArgs() == 1) {
927 OS << OpStrings[Kind] << ' ';
928 PrintExpr(Node->getArg(0));
929 } else {
930 PrintExpr(Node->getArg(0));
931 OS << ' ' << OpStrings[Kind];
932 }
933 } else if (Kind == OO_Call) {
934 PrintExpr(Node->getArg(0));
935 OS << '(';
936 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
937 if (ArgIdx > 1)
938 OS << ", ";
939 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
940 PrintExpr(Node->getArg(ArgIdx));
941 }
942 OS << ')';
943 } else if (Kind == OO_Subscript) {
944 PrintExpr(Node->getArg(0));
945 OS << '[';
946 PrintExpr(Node->getArg(1));
947 OS << ']';
948 } else if (Node->getNumArgs() == 1) {
949 OS << OpStrings[Kind] << ' ';
950 PrintExpr(Node->getArg(0));
951 } else if (Node->getNumArgs() == 2) {
952 PrintExpr(Node->getArg(0));
953 OS << ' ' << OpStrings[Kind] << ' ';
954 PrintExpr(Node->getArg(1));
955 } else {
956 assert(false && "unknown overloaded operator");
957 }
958}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000959
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000960void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
961 VisitCallExpr(cast<CallExpr>(Node));
962}
963
Douglas Gregore200adc2008-10-27 19:41:14 +0000964void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
965 OS << Node->getCastName() << '<';
966 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000967 PrintExpr(Node->getSubExpr());
968 OS << ")";
969}
970
Douglas Gregore200adc2008-10-27 19:41:14 +0000971void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
972 VisitCXXNamedCastExpr(Node);
973}
974
975void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
976 VisitCXXNamedCastExpr(Node);
977}
978
979void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
980 VisitCXXNamedCastExpr(Node);
981}
982
983void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
984 VisitCXXNamedCastExpr(Node);
985}
986
Sebastian Redlc4704762008-11-11 11:37:55 +0000987void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
988 OS << "typeid(";
989 if (Node->isTypeOperand()) {
990 OS << Node->getTypeOperand().getAsString();
991 } else {
992 PrintExpr(Node->getExprOperand());
993 }
994 OS << ")";
995}
996
Chris Lattnereefa10e2007-05-28 06:56:27 +0000997void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
998 OS << (Node->getValue() ? "true" : "false");
999}
1000
Douglas Gregor97a9c812008-11-04 14:32:21 +00001001void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1002 OS << "this";
1003}
1004
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001005void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1006 if (Node->getSubExpr() == 0)
1007 OS << "throw";
1008 else {
1009 OS << "throw ";
1010 PrintExpr(Node->getSubExpr());
1011 }
1012}
1013
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001014void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1015 // Nothing to print: we picked up the default argument
1016}
1017
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001018void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1019 OS << Node->getType().getAsString();
1020 OS << "(";
1021 PrintExpr(Node->getSubExpr());
1022 OS << ")";
1023}
1024
Douglas Gregordd04d332009-01-16 18:33:17 +00001025void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1026 OS << Node->getType().getAsString();
1027 OS << "(";
1028 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1029 ArgEnd = Node->arg_end();
1030 Arg != ArgEnd; ++Arg) {
1031 if (Arg != Node->arg_begin())
1032 OS << ", ";
1033 PrintExpr(*Arg);
1034 }
1035 OS << ")";
1036}
1037
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001038void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1039 OS << Node->getType().getAsString() << "()";
1040}
1041
Argyrios Kyrtzidisaa479132008-09-09 23:47:53 +00001042void
1043StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1044 PrintRawDecl(E->getVarDecl());
1045}
1046
Sebastian Redlbd150f42008-11-21 19:14:01 +00001047void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1048 if (E->isGlobalNew())
1049 OS << "::";
1050 OS << "new ";
1051 unsigned NumPlace = E->getNumPlacementArgs();
1052 if (NumPlace > 0) {
1053 OS << "(";
1054 PrintExpr(E->getPlacementArg(0));
1055 for (unsigned i = 1; i < NumPlace; ++i) {
1056 OS << ", ";
1057 PrintExpr(E->getPlacementArg(i));
1058 }
1059 OS << ") ";
1060 }
1061 if (E->isParenTypeId())
1062 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001063 std::string TypeS;
1064 if (Expr *Size = E->getArraySize()) {
1065 llvm::raw_string_ostream s(TypeS);
1066 Size->printPretty(s);
1067 s.flush();
1068 TypeS = "[" + TypeS + "]";
1069 }
1070 E->getAllocatedType().getAsStringInternal(TypeS);
1071 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001072 if (E->isParenTypeId())
1073 OS << ")";
1074
1075 if (E->hasInitializer()) {
1076 OS << "(";
1077 unsigned NumCons = E->getNumConstructorArgs();
1078 if (NumCons > 0) {
1079 PrintExpr(E->getConstructorArg(0));
1080 for (unsigned i = 1; i < NumCons; ++i) {
1081 OS << ", ";
1082 PrintExpr(E->getConstructorArg(i));
1083 }
1084 }
1085 OS << ")";
1086 }
1087}
1088
1089void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1090 if (E->isGlobalDelete())
1091 OS << "::";
1092 OS << "delete ";
1093 if (E->isArrayForm())
1094 OS << "[] ";
1095 PrintExpr(E->getArgument());
1096}
1097
Douglas Gregorb8a9a412009-02-04 15:01:18 +00001098void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1099 OS << E->getName().getAsString();
Douglas Gregorb0846b02008-12-06 00:22:45 +00001100}
1101
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001102static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1103 switch (UTT) {
1104 default: assert(false && "Unknown type trait");
1105 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1106 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1107 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1108 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1109 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1110 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1111 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1112 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1113 case UTT_IsAbstract: return "__is_abstract";
1114 case UTT_IsClass: return "__is_class";
1115 case UTT_IsEmpty: return "__is_empty";
1116 case UTT_IsEnum: return "__is_enum";
1117 case UTT_IsPOD: return "__is_pod";
1118 case UTT_IsPolymorphic: return "__is_polymorphic";
1119 case UTT_IsUnion: return "__is_union";
1120 }
1121}
1122
1123void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1124 OS << getTypeTraitName(E->getTrait()) << "("
1125 << E->getQueriedType().getAsString() << ")";
1126}
1127
Anders Carlsson76f4a902007-08-21 17:43:55 +00001128// Obj-C
1129
1130void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1131 OS << "@";
1132 VisitStringLiteral(Node->getString());
1133}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001134
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001135void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001136 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001137}
1138
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001139void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001140 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001141}
1142
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001143void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001144 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001145}
1146
Steve Naroffd54978b2007-09-18 23:55:05 +00001147void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1148 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +00001149 Expr *receiver = Mess->getReceiver();
1150 if (receiver) PrintExpr(receiver);
1151 else OS << Mess->getClassName()->getName();
Ted Kremeneka06e7122008-05-02 17:32:38 +00001152 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001153 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001154 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001155 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001156 } else {
1157 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001158 if (i < selector.getNumArgs()) {
1159 if (i > 0) OS << ' ';
1160 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001161 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001162 else
1163 OS << ":";
1164 }
1165 else OS << ", "; // Handle variadic methods.
1166
Steve Naroffc6814ea2007-10-02 20:01:56 +00001167 PrintExpr(Mess->getArg(i));
1168 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001169 }
1170 OS << "]";
1171}
1172
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001173void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1174 OS << "super";
1175}
1176
Steve Naroffc540d662008-09-03 18:15:37 +00001177void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001178 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001179 OS << "^";
1180
1181 const FunctionType *AFT = Node->getFunctionType();
1182
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001183 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001184 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001185 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001186 OS << '(';
1187 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001188 for (BlockDecl::param_iterator AI = BD->param_begin(),
1189 E = BD->param_end(); AI != E; ++AI) {
1190 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001191 ParamStr = (*AI)->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001192 (*AI)->getType().getAsStringInternal(ParamStr);
1193 OS << ParamStr;
1194 }
1195
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001196 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001197 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001198 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001199 OS << "...";
1200 }
1201 OS << ')';
1202 }
1203}
1204
Steve Naroffc540d662008-09-03 18:15:37 +00001205void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001206 OS << Node->getDecl()->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001207}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001208//===----------------------------------------------------------------------===//
1209// Stmt method implementations
1210//===----------------------------------------------------------------------===//
1211
Chris Lattnercbe4f772007-08-08 22:51:59 +00001212void Stmt::dumpPretty() const {
Daniel Dunbar4095d892009-03-10 18:00:19 +00001213 printPretty(llvm::errs());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001214}
1215
Mike Stump74a76472009-02-10 20:16:46 +00001216void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper,
1217 unsigned I, bool NoIndent) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001218 if (this == 0) {
1219 OS << "<NULL>";
1220 return;
1221 }
1222
Mike Stump74a76472009-02-10 20:16:46 +00001223 StmtPrinter P(OS, Helper, I, NoIndent);
Chris Lattner62249a62007-08-21 04:04:25 +00001224 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001225}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001226
1227//===----------------------------------------------------------------------===//
1228// PrinterHelper
1229//===----------------------------------------------------------------------===//
1230
1231// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001232PrinterHelper::~PrinterHelper() {}