blob: 61cd89f70fac17c39bcf0d980a1d5f0828c54c6d [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner95578782007-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 Lattner4b009652007-07-25 00:24:17 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Ted Kremenek10364dd2007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Ted Kremenek08176a52007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "llvm/Support/Compiler.h"
Ted Kremenekdf409eb2007-11-26 22:50:46 +000019#include "llvm/Support/Streams.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000028 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000029 llvm::raw_ostream &OS;
Chris Lattner4b009652007-07-25 00:24:17 +000030 unsigned IndentLevel;
Ted Kremenek08176a52007-08-31 21:30:12 +000031 clang::PrinterHelper* Helper;
Chris Lattner4b009652007-07-25 00:24:17 +000032 public:
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000033 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper) :
Ted Kremenek08176a52007-08-31 21:30:12 +000034 OS(os), IndentLevel(0), Helper(helper) {}
Chris Lattner4b009652007-07-25 00:24:17 +000035
36 void PrintStmt(Stmt *S, int SubIndent = 1) {
37 IndentLevel += SubIndent;
38 if (S && isa<Expr>(S)) {
39 // If this is an expr used in a stmt context, indent and newline it.
40 Indent();
Chris Lattner7ba21fa2007-08-21 04:04:25 +000041 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000042 OS << ";\n";
43 } else if (S) {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000044 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000045 } else {
46 Indent() << "<<<NULL STATEMENT>>>\n";
47 }
48 IndentLevel -= SubIndent;
49 }
50
51 void PrintRawCompoundStmt(CompoundStmt *S);
52 void PrintRawDecl(Decl *D);
Ted Kremenek388b2842008-10-06 18:39:36 +000053 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattner4b009652007-07-25 00:24:17 +000054 void PrintRawIfStmt(IfStmt *If);
55
56 void PrintExpr(Expr *E) {
57 if (E)
Chris Lattner7ba21fa2007-08-21 04:04:25 +000058 Visit(E);
Chris Lattner4b009652007-07-25 00:24:17 +000059 else
60 OS << "<null expr>";
61 }
62
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000063 llvm::raw_ostream &Indent(int Delta = 0) const {
Chris Lattner4b009652007-07-25 00:24:17 +000064 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
65 OS << " ";
66 return OS;
67 }
68
Chris Lattner2af6a802007-08-30 17:59:59 +000069 bool PrintOffsetOfDesignator(Expr *E);
70 void VisitUnaryOffsetOf(UnaryOperator *Node);
71
Ted Kremenek08176a52007-08-31 21:30:12 +000072 void Visit(Stmt* S) {
73 if (Helper && Helper->handledStmt(S,OS))
74 return;
75 else StmtVisitor<StmtPrinter>::Visit(S);
76 }
77
Chris Lattner7ba21fa2007-08-21 04:04:25 +000078 void VisitStmt(Stmt *Node);
Douglas Gregor19330152008-11-14 12:46:07 +000079#define STMT(CLASS, PARENT) \
Chris Lattner7ba21fa2007-08-21 04:04:25 +000080 void Visit##CLASS(CLASS *Node);
Chris Lattner4b009652007-07-25 00:24:17 +000081#include "clang/AST/StmtNodes.def"
82 };
83}
84
85//===----------------------------------------------------------------------===//
86// Stmt printing methods.
87//===----------------------------------------------------------------------===//
88
89void StmtPrinter::VisitStmt(Stmt *Node) {
90 Indent() << "<<unknown stmt type>>\n";
91}
92
93/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
94/// with no newline after the }.
95void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
96 OS << "{\n";
97 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
98 I != E; ++I)
99 PrintStmt(*I);
100
101 Indent() << "}";
102}
103
104void StmtPrinter::PrintRawDecl(Decl *D) {
105 // FIXME: Need to complete/beautify this... this code simply shows the
106 // nodes are where they need to be.
107 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
108 OS << "typedef " << localType->getUnderlyingType().getAsString();
Chris Lattner6c5ec622008-11-24 04:00:27 +0000109 OS << " " << localType->getNameAsString();
Chris Lattner4b009652007-07-25 00:24:17 +0000110 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
111 // Emit storage class for vardecls.
112 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
113 switch (V->getStorageClass()) {
114 default: assert(0 && "Unknown storage class!");
115 case VarDecl::None: break;
116 case VarDecl::Extern: OS << "extern "; break;
117 case VarDecl::Static: OS << "static "; break;
118 case VarDecl::Auto: OS << "auto "; break;
119 case VarDecl::Register: OS << "register "; break;
120 }
121 }
122
Chris Lattner6c5ec622008-11-24 04:00:27 +0000123 std::string Name = VD->getNameAsString();
Chris Lattner4b009652007-07-25 00:24:17 +0000124 VD->getType().getAsStringInternal(Name);
125 OS << Name;
126
127 // If this is a vardecl with an initializer, emit it.
128 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
129 if (V->getInit()) {
130 OS << " = ";
131 PrintExpr(V->getInit());
132 }
133 }
Steve Naroff3340c232007-11-17 21:21:01 +0000134 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
135 // print a free standing tag decl (e.g. "struct x;").
136 OS << TD->getKindName();
137 OS << " ";
138 if (const IdentifierInfo *II = TD->getIdentifier())
139 OS << II->getName();
140 else
141 OS << "<anonymous>";
142 // FIXME: print tag bodies.
Chris Lattner4b009652007-07-25 00:24:17 +0000143 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000144 assert(0 && "Unexpected decl");
145 }
146}
147
Ted Kremenek388b2842008-10-06 18:39:36 +0000148void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
149 bool isFirst = false;
150
151 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
152 I != E; ++I) {
153
154 if (!isFirst) OS << ", ";
155 else isFirst = false;
156
157 PrintRawDecl(*I);
158 }
159}
Chris Lattner4b009652007-07-25 00:24:17 +0000160
161void StmtPrinter::VisitNullStmt(NullStmt *Node) {
162 Indent() << ";\n";
163}
164
165void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenek388b2842008-10-06 18:39:36 +0000166 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
167 I!=E; ++I) {
Chris Lattner4b009652007-07-25 00:24:17 +0000168 Indent();
Ted Kremenek388b2842008-10-06 18:39:36 +0000169 PrintRawDecl(*I);
Chris Lattner4b009652007-07-25 00:24:17 +0000170 OS << ";\n";
171 }
172}
173
174void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
175 Indent();
176 PrintRawCompoundStmt(Node);
177 OS << "\n";
178}
179
180void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
181 Indent(-1) << "case ";
182 PrintExpr(Node->getLHS());
183 if (Node->getRHS()) {
184 OS << " ... ";
185 PrintExpr(Node->getRHS());
186 }
187 OS << ":\n";
188
189 PrintStmt(Node->getSubStmt(), 0);
190}
191
192void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
193 Indent(-1) << "default:\n";
194 PrintStmt(Node->getSubStmt(), 0);
195}
196
197void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
198 Indent(-1) << Node->getName() << ":\n";
199 PrintStmt(Node->getSubStmt(), 0);
200}
201
202void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
203 OS << "if ";
204 PrintExpr(If->getCond());
205
206 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
207 OS << ' ';
208 PrintRawCompoundStmt(CS);
209 OS << (If->getElse() ? ' ' : '\n');
210 } else {
211 OS << '\n';
212 PrintStmt(If->getThen());
213 if (If->getElse()) Indent();
214 }
215
216 if (Stmt *Else = If->getElse()) {
217 OS << "else";
218
219 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
220 OS << ' ';
221 PrintRawCompoundStmt(CS);
222 OS << '\n';
223 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
224 OS << ' ';
225 PrintRawIfStmt(ElseIf);
226 } else {
227 OS << '\n';
228 PrintStmt(If->getElse());
229 }
230 }
231}
232
233void StmtPrinter::VisitIfStmt(IfStmt *If) {
234 Indent();
235 PrintRawIfStmt(If);
236}
237
238void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
239 Indent() << "switch (";
240 PrintExpr(Node->getCond());
241 OS << ")";
242
243 // Pretty print compoundstmt bodies (very common).
244 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
245 OS << " ";
246 PrintRawCompoundStmt(CS);
247 OS << "\n";
248 } else {
249 OS << "\n";
250 PrintStmt(Node->getBody());
251 }
252}
253
254void StmtPrinter::VisitSwitchCase(SwitchCase*) {
255 assert(0 && "SwitchCase is an abstract class");
256}
257
258void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
259 Indent() << "while (";
260 PrintExpr(Node->getCond());
261 OS << ")\n";
262 PrintStmt(Node->getBody());
263}
264
265void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000266 Indent() << "do ";
267 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
268 PrintRawCompoundStmt(CS);
269 OS << " ";
270 } else {
271 OS << "\n";
272 PrintStmt(Node->getBody());
273 Indent();
274 }
275
276 OS << "while ";
Chris Lattner4b009652007-07-25 00:24:17 +0000277 PrintExpr(Node->getCond());
278 OS << ";\n";
279}
280
281void StmtPrinter::VisitForStmt(ForStmt *Node) {
282 Indent() << "for (";
283 if (Node->getInit()) {
284 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek388b2842008-10-06 18:39:36 +0000285 PrintRawDeclStmt(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000286 else
287 PrintExpr(cast<Expr>(Node->getInit()));
288 }
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000289 OS << ";";
290 if (Node->getCond()) {
291 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000292 PrintExpr(Node->getCond());
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000293 }
294 OS << ";";
295 if (Node->getInc()) {
296 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000297 PrintExpr(Node->getInc());
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000298 }
299 OS << ") ";
300
301 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
302 PrintRawCompoundStmt(CS);
303 OS << "\n";
304 } else {
305 OS << "\n";
306 PrintStmt(Node->getBody());
307 }
Chris Lattner4b009652007-07-25 00:24:17 +0000308}
309
Ted Kremenek42730c52008-01-07 19:49:32 +0000310void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000311 Indent() << "for (";
312 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek388b2842008-10-06 18:39:36 +0000313 PrintRawDeclStmt(DS);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000314 else
315 PrintExpr(cast<Expr>(Node->getElement()));
316 OS << " in ";
317 PrintExpr(Node->getCollection());
318 OS << ") ";
319
320 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
321 PrintRawCompoundStmt(CS);
322 OS << "\n";
323 } else {
324 OS << "\n";
325 PrintStmt(Node->getBody());
326 }
327}
328
Chris Lattner4b009652007-07-25 00:24:17 +0000329void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
330 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
331}
332
333void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
334 Indent() << "goto *";
335 PrintExpr(Node->getTarget());
336 OS << ";\n";
337}
338
339void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
340 Indent() << "continue;\n";
341}
342
343void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
344 Indent() << "break;\n";
345}
346
347
348void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
349 Indent() << "return";
350 if (Node->getRetValue()) {
351 OS << " ";
352 PrintExpr(Node->getRetValue());
353 }
354 OS << ";\n";
355}
356
Chris Lattner8a40a832007-10-29 04:04:16 +0000357
358void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson759f45d2007-11-23 23:12:25 +0000359 Indent() << "asm ";
360
361 if (Node->isVolatile())
362 OS << "volatile ";
363
364 OS << "(";
Anders Carlsson076c1112007-11-20 19:21:03 +0000365 VisitStringLiteral(Node->getAsmString());
Anders Carlsson965d5202007-11-22 01:36:19 +0000366
367 // Outputs
368 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
369 Node->getNumClobbers() != 0)
370 OS << " : ";
371
372 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
373 if (i != 0)
374 OS << ", ";
375
376 if (!Node->getOutputName(i).empty()) {
377 OS << '[';
378 OS << Node->getOutputName(i);
379 OS << "] ";
380 }
381
382 VisitStringLiteral(Node->getOutputConstraint(i));
383 OS << " ";
384 Visit(Node->getOutputExpr(i));
385 }
386
387 // Inputs
388 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
389 OS << " : ";
390
391 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
392 if (i != 0)
393 OS << ", ";
394
395 if (!Node->getInputName(i).empty()) {
396 OS << '[';
397 OS << Node->getInputName(i);
398 OS << "] ";
399 }
400
401 VisitStringLiteral(Node->getInputConstraint(i));
402 OS << " ";
403 Visit(Node->getInputExpr(i));
404 }
405
406 // Clobbers
407 if (Node->getNumClobbers() != 0)
408 OS << " : ";
409
410 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
411 if (i != 0)
412 OS << ", ";
413
414 VisitStringLiteral(Node->getClobber(i));
415 }
416
Anders Carlsson076c1112007-11-20 19:21:03 +0000417 OS << ");\n";
Chris Lattner8a40a832007-10-29 04:04:16 +0000418}
419
Ted Kremenek42730c52008-01-07 19:49:32 +0000420void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000421 Indent() << "@try";
422 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
423 PrintRawCompoundStmt(TS);
424 OS << "\n";
425 }
426
Ted Kremenek42730c52008-01-07 19:49:32 +0000427 for (ObjCAtCatchStmt *catchStmt =
428 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000429 catchStmt;
430 catchStmt =
Ted Kremenek42730c52008-01-07 19:49:32 +0000431 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000432 Indent() << "@catch(";
433 if (catchStmt->getCatchParamStmt()) {
434 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenek388b2842008-10-06 18:39:36 +0000435 PrintRawDeclStmt(DS);
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000436 }
437 OS << ")";
438 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
439 {
440 PrintRawCompoundStmt(CS);
441 OS << "\n";
442 }
443 }
444
Ted Kremenek42730c52008-01-07 19:49:32 +0000445 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian50b47922007-11-07 00:46:42 +0000446 Node->getFinallyStmt())) {
447 Indent() << "@finally";
448 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000449 OS << "\n";
450 }
Fariborz Jahanian70952482007-11-01 21:12:44 +0000451}
452
Ted Kremenek42730c52008-01-07 19:49:32 +0000453void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian70952482007-11-01 21:12:44 +0000454}
455
Ted Kremenek42730c52008-01-07 19:49:32 +0000456void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian70952482007-11-01 21:12:44 +0000457 Indent() << "@catch (...) { /* todo */ } \n";
458}
459
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +0000460void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +0000461 Indent() << "@throw";
462 if (Node->getThrowExpr()) {
463 OS << " ";
464 PrintExpr(Node->getThrowExpr());
465 }
466 OS << ";\n";
467}
468
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +0000469void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanian993360a2008-01-29 18:21:32 +0000470 Indent() << "@synchronized (";
471 PrintExpr(Node->getSynchExpr());
472 OS << ")";
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +0000473 PrintRawCompoundStmt(Node->getSynchBody());
474 OS << "\n";
Fariborz Jahanian993360a2008-01-29 18:21:32 +0000475}
476
Sebastian Redl743c8162008-12-22 19:15:10 +0000477void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
478 Indent() << "catch (";
479 if (Decl *ExDecl = Node->getExceptionDecl())
480 PrintRawDecl(ExDecl);
481 else
482 OS << "...";
483 OS << ") ";
484 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
485 OS << "\n";
486}
487
Chris Lattner4b009652007-07-25 00:24:17 +0000488//===----------------------------------------------------------------------===//
489// Expr printing methods.
490//===----------------------------------------------------------------------===//
491
492void StmtPrinter::VisitExpr(Expr *Node) {
493 OS << "<<unknown expr type>>";
494}
495
496void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +0000497 OS << Node->getDecl()->getNameAsString();
Chris Lattner4b009652007-07-25 00:24:17 +0000498}
499
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000500void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000501 if (Node->getBase()) {
502 PrintExpr(Node->getBase());
503 OS << (Node->isArrow() ? "->" : ".");
504 }
Chris Lattner6c5ec622008-11-24 04:00:27 +0000505 OS << Node->getDecl()->getNameAsString();
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000506}
507
Steve Naroff05391d22008-05-30 00:40:33 +0000508void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
509 if (Node->getBase()) {
510 PrintExpr(Node->getBase());
511 OS << ".";
512 }
Steve Naroff0e948412008-12-04 16:24:46 +0000513 OS << Node->getProperty()->getNameAsCString();
Steve Naroff05391d22008-05-30 00:40:33 +0000514}
515
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000516void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
517 if (Node->getBase()) {
518 PrintExpr(Node->getBase());
519 OS << ".";
520 }
521 // FIXME: Setter/Getter names
522}
523
Chris Lattner69909292008-08-10 01:53:14 +0000524void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000525 switch (Node->getIdentType()) {
526 default:
527 assert(0 && "unknown case");
Chris Lattner69909292008-08-10 01:53:14 +0000528 case PredefinedExpr::Func:
Chris Lattner4b009652007-07-25 00:24:17 +0000529 OS << "__func__";
530 break;
Chris Lattner69909292008-08-10 01:53:14 +0000531 case PredefinedExpr::Function:
Chris Lattner4b009652007-07-25 00:24:17 +0000532 OS << "__FUNCTION__";
533 break;
Chris Lattner69909292008-08-10 01:53:14 +0000534 case PredefinedExpr::PrettyFunction:
Chris Lattner4b009652007-07-25 00:24:17 +0000535 OS << "__PRETTY_FUNCTION__";
536 break;
537 }
538}
539
540void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000541 unsigned value = Node->getValue();
Chris Lattner1aaf71c2008-06-07 22:35:38 +0000542 if (Node->isWide())
543 OS << "L";
Chris Lattner4b009652007-07-25 00:24:17 +0000544 switch (value) {
545 case '\\':
546 OS << "'\\\\'";
547 break;
548 case '\'':
549 OS << "'\\''";
550 break;
551 case '\a':
552 // TODO: K&R: the meaning of '\\a' is different in traditional C
553 OS << "'\\a'";
554 break;
555 case '\b':
556 OS << "'\\b'";
557 break;
558 // Nonstandard escape sequence.
559 /*case '\e':
560 OS << "'\\e'";
561 break;*/
562 case '\f':
563 OS << "'\\f'";
564 break;
565 case '\n':
566 OS << "'\\n'";
567 break;
568 case '\r':
569 OS << "'\\r'";
570 break;
571 case '\t':
572 OS << "'\\t'";
573 break;
574 case '\v':
575 OS << "'\\v'";
576 break;
577 default:
Ted Kremenekf91bc1d2008-02-23 00:52:04 +0000578 if (value < 256 && isprint(value)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000579 OS << "'" << (char)value << "'";
580 } else if (value < 256) {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000581 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner4b009652007-07-25 00:24:17 +0000582 } else {
583 // FIXME what to really do here?
584 OS << value;
585 }
586 }
587}
588
589void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
590 bool isSigned = Node->getType()->isSignedIntegerType();
591 OS << Node->getValue().toString(10, isSigned);
592
593 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000594 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000595 default: assert(0 && "Unexpected type for integer literal!");
596 case BuiltinType::Int: break; // no suffix.
597 case BuiltinType::UInt: OS << 'U'; break;
598 case BuiltinType::Long: OS << 'L'; break;
599 case BuiltinType::ULong: OS << "UL"; break;
600 case BuiltinType::LongLong: OS << "LL"; break;
601 case BuiltinType::ULongLong: OS << "ULL"; break;
602 }
603}
604void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner6a390402007-08-01 00:23:58 +0000605 // FIXME: print value more precisely.
Chris Lattnere0391b22008-06-07 22:13:43 +0000606 OS << Node->getValueAsApproximateDouble();
Chris Lattner4b009652007-07-25 00:24:17 +0000607}
Chris Lattner1de66eb2007-08-26 03:42:43 +0000608
609void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
610 PrintExpr(Node->getSubExpr());
611 OS << "i";
612}
613
Chris Lattner4b009652007-07-25 00:24:17 +0000614void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
615 if (Str->isWide()) OS << 'L';
616 OS << '"';
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000617
Chris Lattner4b009652007-07-25 00:24:17 +0000618 // FIXME: this doesn't print wstrings right.
619 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
620 switch (Str->getStrData()[i]) {
621 default: OS << Str->getStrData()[i]; break;
622 // Handle some common ones to make dumps prettier.
623 case '\\': OS << "\\\\"; break;
624 case '"': OS << "\\\""; break;
625 case '\n': OS << "\\n"; break;
626 case '\t': OS << "\\t"; break;
627 case '\a': OS << "\\a"; break;
628 case '\b': OS << "\\b"; break;
629 }
630 }
631 OS << '"';
632}
633void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
634 OS << "(";
635 PrintExpr(Node->getSubExpr());
636 OS << ")";
637}
638void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner2b97b092007-08-23 21:46:40 +0000639 if (!Node->isPostfix()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000640 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner2b97b092007-08-23 21:46:40 +0000641
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000642 // Print a space if this is an "identifier operator" like __real.
Chris Lattner2b97b092007-08-23 21:46:40 +0000643 switch (Node->getOpcode()) {
644 default: break;
Chris Lattner2b97b092007-08-23 21:46:40 +0000645 case UnaryOperator::Real:
646 case UnaryOperator::Imag:
647 case UnaryOperator::Extension:
648 OS << ' ';
649 break;
650 }
651 }
Chris Lattner4b009652007-07-25 00:24:17 +0000652 PrintExpr(Node->getSubExpr());
653
654 if (Node->isPostfix())
655 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner4b009652007-07-25 00:24:17 +0000656}
Chris Lattner2af6a802007-08-30 17:59:59 +0000657
658bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
659 if (isa<CompoundLiteralExpr>(E)) {
660 // Base case, print the type and comma.
661 OS << E->getType().getAsString() << ", ";
662 return true;
663 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
664 PrintOffsetOfDesignator(ASE->getLHS());
665 OS << "[";
666 PrintExpr(ASE->getRHS());
667 OS << "]";
668 return false;
669 } else {
670 MemberExpr *ME = cast<MemberExpr>(E);
671 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner6c5ec622008-11-24 04:00:27 +0000672 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner2af6a802007-08-30 17:59:59 +0000673 return false;
674 }
675}
676
677void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
678 OS << "__builtin_offsetof(";
679 PrintOffsetOfDesignator(Node->getSubExpr());
680 OS << ")";
681}
682
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000683void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
684 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
685 if (Node->isArgumentType())
686 OS << "(" << Node->getArgumentType().getAsString() << ")";
687 else {
688 OS << " ";
689 PrintExpr(Node->getArgumentExpr());
690 }
Chris Lattner4b009652007-07-25 00:24:17 +0000691}
692void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000693 PrintExpr(Node->getLHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000694 OS << "[";
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000695 PrintExpr(Node->getRHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000696 OS << "]";
697}
698
699void StmtPrinter::VisitCallExpr(CallExpr *Call) {
700 PrintExpr(Call->getCallee());
701 OS << "(";
702 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000703 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
704 // Don't print any defaulted arguments
705 break;
706 }
707
Chris Lattner4b009652007-07-25 00:24:17 +0000708 if (i) OS << ", ";
709 PrintExpr(Call->getArg(i));
710 }
711 OS << ")";
712}
713void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
714 PrintExpr(Node->getBase());
715 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor82d44772008-12-20 23:49:58 +0000716 OS << Node->getMemberDecl()->getNameAsString();
Chris Lattner4b009652007-07-25 00:24:17 +0000717}
Nate Begemanaf6ed502008-04-18 23:10:10 +0000718void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroffc11705f2007-07-28 23:10:27 +0000719 PrintExpr(Node->getBase());
720 OS << ".";
721 OS << Node->getAccessor().getName();
722}
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000723void StmtPrinter::VisitCastExpr(CastExpr *) {
724 assert(0 && "CastExpr is an abstract class");
725}
Douglas Gregor21a04f32008-10-27 19:41:14 +0000726void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
727 assert(0 && "ExplicitCastExpr is an abstract class");
728}
Douglas Gregor035d0882008-10-28 15:36:24 +0000729void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000730 OS << "(" << Node->getType().getAsString() << ")";
731 PrintExpr(Node->getSubExpr());
732}
733void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
734 OS << "(" << Node->getType().getAsString() << ")";
735 PrintExpr(Node->getInitializer());
736}
737void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
738 // No need to print anything, simply forward to the sub expression.
739 PrintExpr(Node->getSubExpr());
740}
741void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
742 PrintExpr(Node->getLHS());
743 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
744 PrintExpr(Node->getRHS());
745}
Chris Lattner06078d22007-08-25 02:00:02 +0000746void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
747 PrintExpr(Node->getLHS());
748 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
749 PrintExpr(Node->getRHS());
750}
Chris Lattner4b009652007-07-25 00:24:17 +0000751void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
752 PrintExpr(Node->getCond());
Ted Kremenekeecd9e82007-11-26 18:27:54 +0000753
754 if (Node->getLHS()) {
755 OS << " ? ";
756 PrintExpr(Node->getLHS());
757 OS << " : ";
758 }
759 else { // Handle GCC extention where LHS can be NULL.
760 OS << " ?: ";
761 }
762
Chris Lattner4b009652007-07-25 00:24:17 +0000763 PrintExpr(Node->getRHS());
764}
765
766// GNU extensions.
767
Chris Lattnera0d03a72007-08-03 17:31:20 +0000768void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000769 OS << "&&" << Node->getLabel()->getName();
770}
771
772void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
773 OS << "(";
774 PrintRawCompoundStmt(E->getSubStmt());
775 OS << ")";
776}
777
Steve Naroff63bad2d2007-08-01 22:05:33 +0000778void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
779 OS << "__builtin_types_compatible_p(";
780 OS << Node->getArgType1().getAsString() << ",";
781 OS << Node->getArgType2().getAsString() << ")";
782}
783
Steve Naroff93c53012007-08-03 21:21:27 +0000784void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
785 OS << "__builtin_choose_expr(";
786 PrintExpr(Node->getCond());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000787 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000788 PrintExpr(Node->getLHS());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000789 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000790 PrintExpr(Node->getRHS());
791 OS << ")";
792}
Chris Lattner4b009652007-07-25 00:24:17 +0000793
Douglas Gregorad4b3792008-11-29 04:51:27 +0000794void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
795 OS << "__null";
796}
797
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000798void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
799 OS << "__builtin_overload(";
Nate Begemanbd881ef2008-01-30 20:50:20 +0000800 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000801 if (i) OS << ", ";
Nate Begemanbd881ef2008-01-30 20:50:20 +0000802 PrintExpr(Node->getExpr(i));
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000803 }
804 OS << ")";
805}
806
Eli Friedmand0e9d092008-05-14 19:38:39 +0000807void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
808 OS << "__builtin_shufflevector(";
809 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
810 if (i) OS << ", ";
811 PrintExpr(Node->getExpr(i));
812 }
813 OS << ")";
814}
815
Anders Carlsson762b7c72007-08-31 04:56:16 +0000816void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
817 OS << "{ ";
818 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
819 if (i) OS << ", ";
820 PrintExpr(Node->getInit(i));
821 }
822 OS << " }";
823}
824
Anders Carlsson36760332007-10-15 20:28:48 +0000825void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
826 OS << "va_arg(";
827 PrintExpr(Node->getSubExpr());
828 OS << ", ";
829 OS << Node->getType().getAsString();
830 OS << ")";
831}
832
Chris Lattner4b009652007-07-25 00:24:17 +0000833// C++
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000834void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
835 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
836 "",
837#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
838 Spelling,
839#include "clang/Basic/OperatorKinds.def"
840 };
841
842 OverloadedOperatorKind Kind = Node->getOperator();
843 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
844 if (Node->getNumArgs() == 1) {
845 OS << OpStrings[Kind] << ' ';
846 PrintExpr(Node->getArg(0));
847 } else {
848 PrintExpr(Node->getArg(0));
849 OS << ' ' << OpStrings[Kind];
850 }
851 } else if (Kind == OO_Call) {
852 PrintExpr(Node->getArg(0));
853 OS << '(';
854 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
855 if (ArgIdx > 1)
856 OS << ", ";
857 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
858 PrintExpr(Node->getArg(ArgIdx));
859 }
860 OS << ')';
861 } else if (Kind == OO_Subscript) {
862 PrintExpr(Node->getArg(0));
863 OS << '[';
864 PrintExpr(Node->getArg(1));
865 OS << ']';
866 } else if (Node->getNumArgs() == 1) {
867 OS << OpStrings[Kind] << ' ';
868 PrintExpr(Node->getArg(0));
869 } else if (Node->getNumArgs() == 2) {
870 PrintExpr(Node->getArg(0));
871 OS << ' ' << OpStrings[Kind] << ' ';
872 PrintExpr(Node->getArg(1));
873 } else {
874 assert(false && "unknown overloaded operator");
875 }
876}
Chris Lattner4b009652007-07-25 00:24:17 +0000877
Douglas Gregor3257fb52008-12-22 05:46:06 +0000878void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
879 VisitCallExpr(cast<CallExpr>(Node));
880}
881
Douglas Gregor21a04f32008-10-27 19:41:14 +0000882void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
883 OS << Node->getCastName() << '<';
884 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattner4b009652007-07-25 00:24:17 +0000885 PrintExpr(Node->getSubExpr());
886 OS << ")";
887}
888
Douglas Gregor21a04f32008-10-27 19:41:14 +0000889void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
890 VisitCXXNamedCastExpr(Node);
891}
892
893void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
894 VisitCXXNamedCastExpr(Node);
895}
896
897void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
898 VisitCXXNamedCastExpr(Node);
899}
900
901void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
902 VisitCXXNamedCastExpr(Node);
903}
904
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000905void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
906 OS << "typeid(";
907 if (Node->isTypeOperand()) {
908 OS << Node->getTypeOperand().getAsString();
909 } else {
910 PrintExpr(Node->getExprOperand());
911 }
912 OS << ")";
913}
914
Chris Lattner4b009652007-07-25 00:24:17 +0000915void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
916 OS << (Node->getValue() ? "true" : "false");
917}
918
Douglas Gregora5b022a2008-11-04 14:32:21 +0000919void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
920 OS << "this";
921}
922
Chris Lattnera7447ba2008-02-26 00:51:44 +0000923void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
924 if (Node->getSubExpr() == 0)
925 OS << "throw";
926 else {
927 OS << "throw ";
928 PrintExpr(Node->getSubExpr());
929 }
930}
931
Chris Lattner3e254fb2008-04-08 04:40:51 +0000932void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
933 // Nothing to print: we picked up the default argument
934}
935
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000936void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
937 OS << Node->getType().getAsString();
938 OS << "(";
939 PrintExpr(Node->getSubExpr());
940 OS << ")";
941}
942
943void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
944 OS << Node->getType().getAsString() << "()";
945}
946
Argiris Kirtzidisdbce6c12008-09-09 23:47:53 +0000947void
948StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
949 PrintRawDecl(E->getVarDecl());
950}
951
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000952void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
953 if (E->isGlobalNew())
954 OS << "::";
955 OS << "new ";
956 unsigned NumPlace = E->getNumPlacementArgs();
957 if (NumPlace > 0) {
958 OS << "(";
959 PrintExpr(E->getPlacementArg(0));
960 for (unsigned i = 1; i < NumPlace; ++i) {
961 OS << ", ";
962 PrintExpr(E->getPlacementArg(i));
963 }
964 OS << ") ";
965 }
966 if (E->isParenTypeId())
967 OS << "(";
Sebastian Redl516432a2008-12-02 22:08:59 +0000968 std::string TypeS;
969 if (Expr *Size = E->getArraySize()) {
970 llvm::raw_string_ostream s(TypeS);
971 Size->printPretty(s);
972 s.flush();
973 TypeS = "[" + TypeS + "]";
974 }
975 E->getAllocatedType().getAsStringInternal(TypeS);
976 OS << TypeS;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000977 if (E->isParenTypeId())
978 OS << ")";
979
980 if (E->hasInitializer()) {
981 OS << "(";
982 unsigned NumCons = E->getNumConstructorArgs();
983 if (NumCons > 0) {
984 PrintExpr(E->getConstructorArg(0));
985 for (unsigned i = 1; i < NumCons; ++i) {
986 OS << ", ";
987 PrintExpr(E->getConstructorArg(i));
988 }
989 }
990 OS << ")";
991 }
992}
993
994void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
995 if (E->isGlobalDelete())
996 OS << "::";
997 OS << "delete ";
998 if (E->isArrayForm())
999 OS << "[] ";
1000 PrintExpr(E->getArgument());
1001}
1002
Douglas Gregora133e262008-12-06 00:22:45 +00001003void StmtPrinter::VisitCXXDependentNameExpr(CXXDependentNameExpr *E) {
1004 OS << E->getName()->getName();
1005}
1006
Anders Carlssona66cad42007-08-21 17:43:55 +00001007// Obj-C
1008
1009void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1010 OS << "@";
1011 VisitStringLiteral(Node->getString());
1012}
Chris Lattner4b009652007-07-25 00:24:17 +00001013
Anders Carlsson8be1d402007-08-22 15:14:15 +00001014void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001015 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlsson8be1d402007-08-22 15:14:15 +00001016}
1017
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001018void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001019 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001020}
1021
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001022void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001023 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001024}
1025
Steve Naroffc39ca262007-09-18 23:55:05 +00001026void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1027 OS << "[";
Steve Narofffa465d12007-10-02 20:01:56 +00001028 Expr *receiver = Mess->getReceiver();
1029 if (receiver) PrintExpr(receiver);
1030 else OS << Mess->getClassName()->getName();
Ted Kremenekbfec9492008-05-02 17:32:38 +00001031 OS << ' ';
Ted Kremenek2287cee2008-04-16 04:30:16 +00001032 Selector selector = Mess->getSelector();
Steve Narofffa465d12007-10-02 20:01:56 +00001033 if (selector.isUnarySelector()) {
Ted Kremenekbfec9492008-05-02 17:32:38 +00001034 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Narofffa465d12007-10-02 20:01:56 +00001035 } else {
1036 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekbfec9492008-05-02 17:32:38 +00001037 if (i < selector.getNumArgs()) {
1038 if (i > 0) OS << ' ';
1039 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner6c5ec622008-11-24 04:00:27 +00001040 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekbfec9492008-05-02 17:32:38 +00001041 else
1042 OS << ":";
1043 }
1044 else OS << ", "; // Handle variadic methods.
1045
Steve Narofffa465d12007-10-02 20:01:56 +00001046 PrintExpr(Mess->getArg(i));
1047 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001048 }
1049 OS << "]";
1050}
1051
Douglas Gregord8606632008-11-04 14:56:14 +00001052void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1053 OS << "super";
1054}
1055
Steve Naroff52a81c02008-09-03 18:15:37 +00001056void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff9ac456d2008-10-08 17:01:13 +00001057 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff52a81c02008-09-03 18:15:37 +00001058 OS << "^";
1059
1060 const FunctionType *AFT = Node->getFunctionType();
1061
1062 if (isa<FunctionTypeNoProto>(AFT)) {
1063 OS << "()";
Steve Naroff9ac456d2008-10-08 17:01:13 +00001064 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroff52a81c02008-09-03 18:15:37 +00001065 OS << '(';
1066 std::string ParamStr;
Steve Naroff9ac456d2008-10-08 17:01:13 +00001067 for (BlockDecl::param_iterator AI = BD->param_begin(),
1068 E = BD->param_end(); AI != E; ++AI) {
1069 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001070 ParamStr = (*AI)->getNameAsString();
Steve Naroff52a81c02008-09-03 18:15:37 +00001071 (*AI)->getType().getAsStringInternal(ParamStr);
1072 OS << ParamStr;
1073 }
1074
Steve Naroff9ac456d2008-10-08 17:01:13 +00001075 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroff52a81c02008-09-03 18:15:37 +00001076 if (FT->isVariadic()) {
Steve Naroff9ac456d2008-10-08 17:01:13 +00001077 if (!BD->param_empty()) OS << ", ";
Steve Naroff52a81c02008-09-03 18:15:37 +00001078 OS << "...";
1079 }
1080 OS << ')';
1081 }
1082}
1083
Steve Naroff52a81c02008-09-03 18:15:37 +00001084void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001085 OS << Node->getDecl()->getNameAsString();
Steve Naroff52a81c02008-09-03 18:15:37 +00001086}
Chris Lattner4b009652007-07-25 00:24:17 +00001087//===----------------------------------------------------------------------===//
1088// Stmt method implementations
1089//===----------------------------------------------------------------------===//
1090
Chris Lattner95578782007-08-08 22:51:59 +00001091void Stmt::dumpPretty() const {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001092 printPretty(llvm::errs());
Chris Lattner4b009652007-07-25 00:24:17 +00001093}
1094
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001095void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Chris Lattner4b009652007-07-25 00:24:17 +00001096 if (this == 0) {
1097 OS << "<NULL>";
1098 return;
1099 }
1100
Ted Kremenek08176a52007-08-31 21:30:12 +00001101 StmtPrinter P(OS, Helper);
Chris Lattner7ba21fa2007-08-21 04:04:25 +00001102 P.Visit(const_cast<Stmt*>(this));
Chris Lattner4b009652007-07-25 00:24:17 +00001103}
Ted Kremenek08176a52007-08-31 21:30:12 +00001104
1105//===----------------------------------------------------------------------===//
1106// PrinterHelper
1107//===----------------------------------------------------------------------===//
1108
1109// Implement virtual destructor.
Gabor Greif61ce98c2007-09-11 15:32:40 +00001110PrinterHelper::~PrinterHelper() {}