blob: ba67a97debc1706cefb8e62da01160fbfcffbad5 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner6000dac2007-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.
Reid Spencer5f016e22007-07-11 17:01:13 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/Support/Compiler.h"
Ted Kremenek51221ec2007-11-26 22:50:46 +000019#include "llvm/Support/Streams.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000028 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremeneka95d3752008-09-13 05:16:45 +000029 llvm::raw_ostream &OS;
Reid Spencer5f016e22007-07-11 17:01:13 +000030 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000031 clang::PrinterHelper* Helper;
Reid Spencer5f016e22007-07-11 17:01:13 +000032 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +000033 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper) :
Ted Kremenek42a509f2007-08-31 21:30:12 +000034 OS(os), IndentLevel(0), Helper(helper) {}
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerc5598cb2007-08-21 04:04:25 +000041 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000042 OS << ";\n";
43 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000044 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000045 } else {
46 Indent() << "<<<NULL STATEMENT>>>\n";
47 }
48 IndentLevel -= SubIndent;
49 }
50
51 void PrintRawCompoundStmt(CompoundStmt *S);
52 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000053 void PrintRawDeclStmt(DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 void PrintRawIfStmt(IfStmt *If);
55
56 void PrintExpr(Expr *E) {
57 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000058 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000059 else
60 OS << "<null expr>";
61 }
62
Ted Kremeneka95d3752008-09-13 05:16:45 +000063 llvm::raw_ostream &Indent(int Delta = 0) const {
Reid Spencer5f016e22007-07-11 17:01:13 +000064 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
65 OS << " ";
66 return OS;
67 }
68
Chris Lattner704fe352007-08-30 17:59:59 +000069 bool PrintOffsetOfDesignator(Expr *E);
70 void VisitUnaryOffsetOf(UnaryOperator *Node);
71
Ted Kremenek42a509f2007-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 Lattnerc5598cb2007-08-21 04:04:25 +000078 void VisitStmt(Stmt *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000079#define STMT(N, CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000080 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +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();
109 OS << " " << localType->getName();
110 } 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
123 std::string Name = VD->getName();
124 VD->getType().getAsStringInternal(Name);
125 OS << Name;
126
Chris Lattner24c39902007-07-12 00:36:32 +0000127 // 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 Naroff91578f32007-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 assert(0 && "Unexpected decl");
145 }
146}
147
Ted Kremenekecd64c52008-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}
Reid Spencer5f016e22007-07-11 17:01:13 +0000160
161void StmtPrinter::VisitNullStmt(NullStmt *Node) {
162 Indent() << ";\n";
163}
164
165void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenekecd64c52008-10-06 18:39:36 +0000166 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
167 I!=E; ++I) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 Indent();
Ted Kremenekecd64c52008-10-06 18:39:36 +0000169 PrintRawDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +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
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000254void StmtPrinter::VisitSwitchCase(SwitchCase*) {
255 assert(0 && "SwitchCase is an abstract class");
256}
257
Reid Spencer5f016e22007-07-11 17:01:13 +0000258void 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 Lattner8bdcc472007-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 ";
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenekecd64c52008-10-06 18:39:36 +0000285 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 else
287 PrintExpr(cast<Expr>(Node->getInit()));
288 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000289 OS << ";";
290 if (Node->getCond()) {
291 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000293 }
294 OS << ";";
295 if (Node->getInc()) {
296 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000308}
309
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000310void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000311 Indent() << "for (";
312 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000313 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerfe795952007-10-29 04:04:16 +0000357
358void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000359 Indent() << "asm ";
360
361 if (Node->isVolatile())
362 OS << "volatile ";
363
364 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000365 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-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 Carlsson6a0ef4b2007-11-20 19:21:03 +0000417 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000418}
419
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000420void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000427 for (ObjCAtCatchStmt *catchStmt =
428 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000429 catchStmt;
430 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000431 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000432 Indent() << "@catch(";
433 if (catchStmt->getCatchParamStmt()) {
434 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000435 PrintRawDeclStmt(DS);
Fariborz Jahanian7794cb82007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000445 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000446 Node->getFinallyStmt())) {
447 Indent() << "@finally";
448 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000449 OS << "\n";
450 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000451}
452
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000453void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000454}
455
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000456void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000457 Indent() << "@catch (...) { /* todo */ } \n";
458}
459
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000460void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-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 Jahanian78a677b2008-01-30 17:38:29 +0000469void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000470 Indent() << "@synchronized (";
471 PrintExpr(Node->getSynchExpr());
472 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000473 PrintRawCompoundStmt(Node->getSynchBody());
474 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000475}
476
Reid Spencer5f016e22007-07-11 17:01:13 +0000477//===----------------------------------------------------------------------===//
478// Expr printing methods.
479//===----------------------------------------------------------------------===//
480
481void StmtPrinter::VisitExpr(Expr *Node) {
482 OS << "<<unknown expr type>>";
483}
484
485void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
486 OS << Node->getDecl()->getName();
487}
488
Steve Naroff7779db42007-11-12 14:29:37 +0000489void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000490 if (Node->getBase()) {
491 PrintExpr(Node->getBase());
492 OS << (Node->isArrow() ? "->" : ".");
493 }
Steve Naroff7779db42007-11-12 14:29:37 +0000494 OS << Node->getDecl()->getName();
495}
496
Steve Naroffae784072008-05-30 00:40:33 +0000497void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
498 if (Node->getBase()) {
499 PrintExpr(Node->getBase());
500 OS << ".";
501 }
502 // FIXME: OS << Node->getDecl()->getName();
503}
504
Chris Lattnerd9f69102008-08-10 01:53:14 +0000505void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000506 switch (Node->getIdentType()) {
507 default:
508 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000509 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000510 OS << "__func__";
511 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000512 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000513 OS << "__FUNCTION__";
514 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000515 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000516 OS << "__PRETTY_FUNCTION__";
517 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000518 case PredefinedExpr::ObjCSuper:
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000519 OS << "super";
520 break;
Anders Carlsson22742662007-07-21 05:21:51 +0000521 }
522}
523
Reid Spencer5f016e22007-07-11 17:01:13 +0000524void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000525 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000526 if (Node->isWide())
527 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000528 switch (value) {
529 case '\\':
530 OS << "'\\\\'";
531 break;
532 case '\'':
533 OS << "'\\''";
534 break;
535 case '\a':
536 // TODO: K&R: the meaning of '\\a' is different in traditional C
537 OS << "'\\a'";
538 break;
539 case '\b':
540 OS << "'\\b'";
541 break;
542 // Nonstandard escape sequence.
543 /*case '\e':
544 OS << "'\\e'";
545 break;*/
546 case '\f':
547 OS << "'\\f'";
548 break;
549 case '\n':
550 OS << "'\\n'";
551 break;
552 case '\r':
553 OS << "'\\r'";
554 break;
555 case '\t':
556 OS << "'\\t'";
557 break;
558 case '\v':
559 OS << "'\\v'";
560 break;
561 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000562 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000563 OS << "'" << (char)value << "'";
564 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000565 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000566 } else {
567 // FIXME what to really do here?
568 OS << value;
569 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000570 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000571}
572
573void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
574 bool isSigned = Node->getType()->isSignedIntegerType();
575 OS << Node->getValue().toString(10, isSigned);
576
577 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000578 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 default: assert(0 && "Unexpected type for integer literal!");
580 case BuiltinType::Int: break; // no suffix.
581 case BuiltinType::UInt: OS << 'U'; break;
582 case BuiltinType::Long: OS << 'L'; break;
583 case BuiltinType::ULong: OS << "UL"; break;
584 case BuiltinType::LongLong: OS << "LL"; break;
585 case BuiltinType::ULongLong: OS << "ULL"; break;
586 }
587}
588void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000589 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000590 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000591}
Chris Lattner5d661452007-08-26 03:42:43 +0000592
593void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
594 PrintExpr(Node->getSubExpr());
595 OS << "i";
596}
597
Reid Spencer5f016e22007-07-11 17:01:13 +0000598void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
599 if (Str->isWide()) OS << 'L';
600 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000601
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 // FIXME: this doesn't print wstrings right.
603 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
604 switch (Str->getStrData()[i]) {
605 default: OS << Str->getStrData()[i]; break;
606 // Handle some common ones to make dumps prettier.
607 case '\\': OS << "\\\\"; break;
608 case '"': OS << "\\\""; break;
609 case '\n': OS << "\\n"; break;
610 case '\t': OS << "\\t"; break;
611 case '\a': OS << "\\a"; break;
612 case '\b': OS << "\\b"; break;
613 }
614 }
615 OS << '"';
616}
617void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
618 OS << "(";
619 PrintExpr(Node->getSubExpr());
620 OS << ")";
621}
622void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000623 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000625
626 // Print a space if this is an "identifier operator" like sizeof or __real.
627 switch (Node->getOpcode()) {
628 default: break;
629 case UnaryOperator::SizeOf:
630 case UnaryOperator::AlignOf:
631 case UnaryOperator::Real:
632 case UnaryOperator::Imag:
633 case UnaryOperator::Extension:
634 OS << ' ';
635 break;
636 }
637 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 PrintExpr(Node->getSubExpr());
639
640 if (Node->isPostfix())
641 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000642}
Chris Lattner704fe352007-08-30 17:59:59 +0000643
644bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
645 if (isa<CompoundLiteralExpr>(E)) {
646 // Base case, print the type and comma.
647 OS << E->getType().getAsString() << ", ";
648 return true;
649 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
650 PrintOffsetOfDesignator(ASE->getLHS());
651 OS << "[";
652 PrintExpr(ASE->getRHS());
653 OS << "]";
654 return false;
655 } else {
656 MemberExpr *ME = cast<MemberExpr>(E);
657 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
658 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
659 return false;
660 }
661}
662
663void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
664 OS << "__builtin_offsetof(";
665 PrintOffsetOfDesignator(Node->getSubExpr());
666 OS << ")";
667}
668
Reid Spencer5f016e22007-07-11 17:01:13 +0000669void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
670 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
671 OS << Node->getArgumentType().getAsString() << ")";
672}
673void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000674 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000676 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 OS << "]";
678}
679
680void StmtPrinter::VisitCallExpr(CallExpr *Call) {
681 PrintExpr(Call->getCallee());
682 OS << "(";
683 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000684 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
685 // Don't print any defaulted arguments
686 break;
687 }
688
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 if (i) OS << ", ";
690 PrintExpr(Call->getArg(i));
691 }
692 OS << ")";
693}
694void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
695 PrintExpr(Node->getBase());
696 OS << (Node->isArrow() ? "->" : ".");
697
698 FieldDecl *Field = Node->getMemberDecl();
699 assert(Field && "MemberExpr should alway reference a field!");
700 OS << Field->getName();
701}
Nate Begeman213541a2008-04-18 23:10:10 +0000702void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000703 PrintExpr(Node->getBase());
704 OS << ".";
705 OS << Node->getAccessor().getName();
706}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000707void StmtPrinter::VisitCastExpr(CastExpr *) {
708 assert(0 && "CastExpr is an abstract class");
709}
710void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000711 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 PrintExpr(Node->getSubExpr());
713}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000714void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
715 OS << "(" << Node->getType().getAsString() << ")";
716 PrintExpr(Node->getInitializer());
717}
Steve Naroff49b45262007-07-13 16:58:59 +0000718void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000719 // No need to print anything, simply forward to the sub expression.
720 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000721}
Reid Spencer5f016e22007-07-11 17:01:13 +0000722void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
723 PrintExpr(Node->getLHS());
724 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
725 PrintExpr(Node->getRHS());
726}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000727void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
728 PrintExpr(Node->getLHS());
729 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
730 PrintExpr(Node->getRHS());
731}
Reid Spencer5f016e22007-07-11 17:01:13 +0000732void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
733 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000734
735 if (Node->getLHS()) {
736 OS << " ? ";
737 PrintExpr(Node->getLHS());
738 OS << " : ";
739 }
740 else { // Handle GCC extention where LHS can be NULL.
741 OS << " ?: ";
742 }
743
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 PrintExpr(Node->getRHS());
745}
746
747// GNU extensions.
748
Chris Lattner6481a572007-08-03 17:31:20 +0000749void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000751}
752
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000753void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
754 OS << "(";
755 PrintRawCompoundStmt(E->getSubStmt());
756 OS << ")";
757}
758
Steve Naroffd34e9152007-08-01 22:05:33 +0000759void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
760 OS << "__builtin_types_compatible_p(";
761 OS << Node->getArgType1().getAsString() << ",";
762 OS << Node->getArgType2().getAsString() << ")";
763}
764
Steve Naroffd04fdd52007-08-03 21:21:27 +0000765void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
766 OS << "__builtin_choose_expr(";
767 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000768 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000769 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000770 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000771 PrintExpr(Node->getRHS());
772 OS << ")";
773}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000774
Nate Begemane2ce1d92008-01-17 17:46:27 +0000775void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
776 OS << "__builtin_overload(";
Nate Begeman67295d02008-01-30 20:50:20 +0000777 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begemane2ce1d92008-01-17 17:46:27 +0000778 if (i) OS << ", ";
Nate Begeman67295d02008-01-30 20:50:20 +0000779 PrintExpr(Node->getExpr(i));
Nate Begemane2ce1d92008-01-17 17:46:27 +0000780 }
781 OS << ")";
782}
783
Eli Friedmand38617c2008-05-14 19:38:39 +0000784void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
785 OS << "__builtin_shufflevector(";
786 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
787 if (i) OS << ", ";
788 PrintExpr(Node->getExpr(i));
789 }
790 OS << ")";
791}
792
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000793void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
794 OS << "{ ";
795 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
796 if (i) OS << ", ";
797 PrintExpr(Node->getInit(i));
798 }
799 OS << " }";
800}
801
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000802void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
803 OS << "va_arg(";
804 PrintExpr(Node->getSubExpr());
805 OS << ", ";
806 OS << Node->getType().getAsString();
807 OS << ")";
808}
809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810// C++
811
812void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner36460ee2007-08-09 17:34:19 +0000813 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 OS << Node->getDestType().getAsString() << ">(";
815 PrintExpr(Node->getSubExpr());
816 OS << ")";
817}
818
819void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
820 OS << (Node->getValue() ? "true" : "false");
821}
822
Chris Lattner50dd2892008-02-26 00:51:44 +0000823void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
824 if (Node->getSubExpr() == 0)
825 OS << "throw";
826 else {
827 OS << "throw ";
828 PrintExpr(Node->getSubExpr());
829 }
830}
831
Chris Lattner04421082008-04-08 04:40:51 +0000832void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
833 // Nothing to print: we picked up the default argument
834}
835
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000836void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
837 OS << Node->getType().getAsString();
838 OS << "(";
839 PrintExpr(Node->getSubExpr());
840 OS << ")";
841}
842
843void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
844 OS << Node->getType().getAsString() << "()";
845}
846
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +0000847void
848StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
849 PrintRawDecl(E->getVarDecl());
850}
851
Anders Carlsson55085182007-08-21 17:43:55 +0000852// Obj-C
853
854void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
855 OS << "@";
856 VisitStringLiteral(Node->getString());
857}
Reid Spencer5f016e22007-07-11 17:01:13 +0000858
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000859void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000860 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000861}
862
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000863void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000864 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000865}
866
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000867void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000868 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000869}
870
Steve Naroff563477d2007-09-18 23:55:05 +0000871void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
872 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000873 Expr *receiver = Mess->getReceiver();
874 if (receiver) PrintExpr(receiver);
875 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +0000876 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +0000877 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000878 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +0000879 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000880 } else {
881 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +0000882 if (i < selector.getNumArgs()) {
883 if (i > 0) OS << ' ';
884 if (selector.getIdentifierInfoForSlot(i))
885 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
886 else
887 OS << ":";
888 }
889 else OS << ", "; // Handle variadic methods.
890
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000891 PrintExpr(Mess->getArg(i));
892 }
Steve Naroff563477d2007-09-18 23:55:05 +0000893 }
894 OS << "]";
895}
896
Steve Naroff4eb206b2008-09-03 18:15:37 +0000897void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +0000898 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000899 OS << "^";
900
901 const FunctionType *AFT = Node->getFunctionType();
902
903 if (isa<FunctionTypeNoProto>(AFT)) {
904 OS << "()";
Steve Naroff56ee6892008-10-08 17:01:13 +0000905 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +0000906 OS << '(';
907 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +0000908 for (BlockDecl::param_iterator AI = BD->param_begin(),
909 E = BD->param_end(); AI != E; ++AI) {
910 if (AI != BD->param_begin()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +0000911 ParamStr = (*AI)->getName();
912 (*AI)->getType().getAsStringInternal(ParamStr);
913 OS << ParamStr;
914 }
915
Steve Naroff56ee6892008-10-08 17:01:13 +0000916 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +0000917 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +0000918 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +0000919 OS << "...";
920 }
921 OS << ')';
922 }
923}
924
Steve Naroff4eb206b2008-09-03 18:15:37 +0000925void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
926 OS << Node->getDecl()->getName();
927}
Reid Spencer5f016e22007-07-11 17:01:13 +0000928//===----------------------------------------------------------------------===//
929// Stmt method implementations
930//===----------------------------------------------------------------------===//
931
Chris Lattner6000dac2007-08-08 22:51:59 +0000932void Stmt::dumpPretty() const {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000933 printPretty(llvm::errs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000934}
935
Ted Kremeneka95d3752008-09-13 05:16:45 +0000936void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 if (this == 0) {
938 OS << "<NULL>";
939 return;
940 }
941
Ted Kremenek42a509f2007-08-31 21:30:12 +0000942 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000943 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +0000944}
Ted Kremenek42a509f2007-08-31 21:30:12 +0000945
946//===----------------------------------------------------------------------===//
947// PrinterHelper
948//===----------------------------------------------------------------------===//
949
950// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +0000951PrinterHelper::~PrinterHelper() {}