blob: 566ab717ea2ff1b55bdf988bdc57cdb71a06872d [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);
53 void PrintRawIfStmt(IfStmt *If);
54
55 void PrintExpr(Expr *E) {
56 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000057 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 else
59 OS << "<null expr>";
60 }
61
Ted Kremeneka95d3752008-09-13 05:16:45 +000062 llvm::raw_ostream &Indent(int Delta = 0) const {
Reid Spencer5f016e22007-07-11 17:01:13 +000063 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
64 OS << " ";
65 return OS;
66 }
67
Chris Lattner704fe352007-08-30 17:59:59 +000068 bool PrintOffsetOfDesignator(Expr *E);
69 void VisitUnaryOffsetOf(UnaryOperator *Node);
70
Ted Kremenek42a509f2007-08-31 21:30:12 +000071 void Visit(Stmt* S) {
72 if (Helper && Helper->handledStmt(S,OS))
73 return;
74 else StmtVisitor<StmtPrinter>::Visit(S);
75 }
76
Chris Lattnerc5598cb2007-08-21 04:04:25 +000077 void VisitStmt(Stmt *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000078#define STMT(N, CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000079 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000080#include "clang/AST/StmtNodes.def"
81 };
82}
83
84//===----------------------------------------------------------------------===//
85// Stmt printing methods.
86//===----------------------------------------------------------------------===//
87
88void StmtPrinter::VisitStmt(Stmt *Node) {
89 Indent() << "<<unknown stmt type>>\n";
90}
91
92/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
93/// with no newline after the }.
94void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
95 OS << "{\n";
96 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
97 I != E; ++I)
98 PrintStmt(*I);
99
100 Indent() << "}";
101}
102
103void StmtPrinter::PrintRawDecl(Decl *D) {
104 // FIXME: Need to complete/beautify this... this code simply shows the
105 // nodes are where they need to be.
106 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
107 OS << "typedef " << localType->getUnderlyingType().getAsString();
108 OS << " " << localType->getName();
109 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
110 // Emit storage class for vardecls.
111 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
112 switch (V->getStorageClass()) {
113 default: assert(0 && "Unknown storage class!");
114 case VarDecl::None: break;
115 case VarDecl::Extern: OS << "extern "; break;
116 case VarDecl::Static: OS << "static "; break;
117 case VarDecl::Auto: OS << "auto "; break;
118 case VarDecl::Register: OS << "register "; break;
119 }
120 }
121
122 std::string Name = VD->getName();
123 VD->getType().getAsStringInternal(Name);
124 OS << Name;
125
Chris Lattner24c39902007-07-12 00:36:32 +0000126 // If this is a vardecl with an initializer, emit it.
127 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
128 if (V->getInit()) {
129 OS << " = ";
130 PrintExpr(V->getInit());
131 }
132 }
Steve Naroff91578f32007-11-17 21:21:01 +0000133 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
134 // print a free standing tag decl (e.g. "struct x;").
135 OS << TD->getKindName();
136 OS << " ";
137 if (const IdentifierInfo *II = TD->getIdentifier())
138 OS << II->getName();
139 else
140 OS << "<anonymous>";
141 // FIXME: print tag bodies.
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 assert(0 && "Unexpected decl");
144 }
145}
146
147
148void StmtPrinter::VisitNullStmt(NullStmt *Node) {
149 Indent() << ";\n";
150}
151
152void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Steve Naroff94745042007-09-13 23:52:58 +0000153 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 Indent();
155 PrintRawDecl(D);
156 OS << ";\n";
157 }
158}
159
160void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
161 Indent();
162 PrintRawCompoundStmt(Node);
163 OS << "\n";
164}
165
166void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
167 Indent(-1) << "case ";
168 PrintExpr(Node->getLHS());
169 if (Node->getRHS()) {
170 OS << " ... ";
171 PrintExpr(Node->getRHS());
172 }
173 OS << ":\n";
174
175 PrintStmt(Node->getSubStmt(), 0);
176}
177
178void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
179 Indent(-1) << "default:\n";
180 PrintStmt(Node->getSubStmt(), 0);
181}
182
183void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
184 Indent(-1) << Node->getName() << ":\n";
185 PrintStmt(Node->getSubStmt(), 0);
186}
187
188void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
189 OS << "if ";
190 PrintExpr(If->getCond());
191
192 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
193 OS << ' ';
194 PrintRawCompoundStmt(CS);
195 OS << (If->getElse() ? ' ' : '\n');
196 } else {
197 OS << '\n';
198 PrintStmt(If->getThen());
199 if (If->getElse()) Indent();
200 }
201
202 if (Stmt *Else = If->getElse()) {
203 OS << "else";
204
205 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
206 OS << ' ';
207 PrintRawCompoundStmt(CS);
208 OS << '\n';
209 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
210 OS << ' ';
211 PrintRawIfStmt(ElseIf);
212 } else {
213 OS << '\n';
214 PrintStmt(If->getElse());
215 }
216 }
217}
218
219void StmtPrinter::VisitIfStmt(IfStmt *If) {
220 Indent();
221 PrintRawIfStmt(If);
222}
223
224void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
225 Indent() << "switch (";
226 PrintExpr(Node->getCond());
227 OS << ")";
228
229 // Pretty print compoundstmt bodies (very common).
230 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
231 OS << " ";
232 PrintRawCompoundStmt(CS);
233 OS << "\n";
234 } else {
235 OS << "\n";
236 PrintStmt(Node->getBody());
237 }
238}
239
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000240void StmtPrinter::VisitSwitchCase(SwitchCase*) {
241 assert(0 && "SwitchCase is an abstract class");
242}
243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
245 Indent() << "while (";
246 PrintExpr(Node->getCond());
247 OS << ")\n";
248 PrintStmt(Node->getBody());
249}
250
251void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000252 Indent() << "do ";
253 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
254 PrintRawCompoundStmt(CS);
255 OS << " ";
256 } else {
257 OS << "\n";
258 PrintStmt(Node->getBody());
259 Indent();
260 }
261
262 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 PrintExpr(Node->getCond());
264 OS << ";\n";
265}
266
267void StmtPrinter::VisitForStmt(ForStmt *Node) {
268 Indent() << "for (";
269 if (Node->getInit()) {
270 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
271 PrintRawDecl(DS->getDecl());
272 else
273 PrintExpr(cast<Expr>(Node->getInit()));
274 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000275 OS << ";";
276 if (Node->getCond()) {
277 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000279 }
280 OS << ";";
281 if (Node->getInc()) {
282 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000284 }
285 OS << ") ";
286
287 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
288 PrintRawCompoundStmt(CS);
289 OS << "\n";
290 } else {
291 OS << "\n";
292 PrintStmt(Node->getBody());
293 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000294}
295
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000296void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000297 Indent() << "for (";
298 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
299 PrintRawDecl(DS->getDecl());
300 else
301 PrintExpr(cast<Expr>(Node->getElement()));
302 OS << " in ";
303 PrintExpr(Node->getCollection());
304 OS << ") ";
305
306 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
307 PrintRawCompoundStmt(CS);
308 OS << "\n";
309 } else {
310 OS << "\n";
311 PrintStmt(Node->getBody());
312 }
313}
314
Reid Spencer5f016e22007-07-11 17:01:13 +0000315void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
316 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
317}
318
319void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
320 Indent() << "goto *";
321 PrintExpr(Node->getTarget());
322 OS << ";\n";
323}
324
325void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
326 Indent() << "continue;\n";
327}
328
329void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
330 Indent() << "break;\n";
331}
332
333
334void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
335 Indent() << "return";
336 if (Node->getRetValue()) {
337 OS << " ";
338 PrintExpr(Node->getRetValue());
339 }
340 OS << ";\n";
341}
342
Chris Lattnerfe795952007-10-29 04:04:16 +0000343
344void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000345 Indent() << "asm ";
346
347 if (Node->isVolatile())
348 OS << "volatile ";
349
350 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000351 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000352
353 // Outputs
354 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
355 Node->getNumClobbers() != 0)
356 OS << " : ";
357
358 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
359 if (i != 0)
360 OS << ", ";
361
362 if (!Node->getOutputName(i).empty()) {
363 OS << '[';
364 OS << Node->getOutputName(i);
365 OS << "] ";
366 }
367
368 VisitStringLiteral(Node->getOutputConstraint(i));
369 OS << " ";
370 Visit(Node->getOutputExpr(i));
371 }
372
373 // Inputs
374 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
375 OS << " : ";
376
377 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
378 if (i != 0)
379 OS << ", ";
380
381 if (!Node->getInputName(i).empty()) {
382 OS << '[';
383 OS << Node->getInputName(i);
384 OS << "] ";
385 }
386
387 VisitStringLiteral(Node->getInputConstraint(i));
388 OS << " ";
389 Visit(Node->getInputExpr(i));
390 }
391
392 // Clobbers
393 if (Node->getNumClobbers() != 0)
394 OS << " : ";
395
396 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
397 if (i != 0)
398 OS << ", ";
399
400 VisitStringLiteral(Node->getClobber(i));
401 }
402
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000403 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000404}
405
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000406void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000407 Indent() << "@try";
408 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
409 PrintRawCompoundStmt(TS);
410 OS << "\n";
411 }
412
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000413 for (ObjCAtCatchStmt *catchStmt =
414 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000415 catchStmt;
416 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000417 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000418 Indent() << "@catch(";
419 if (catchStmt->getCatchParamStmt()) {
420 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
421 PrintRawDecl(DS->getDecl());
422 }
423 OS << ")";
424 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
425 {
426 PrintRawCompoundStmt(CS);
427 OS << "\n";
428 }
429 }
430
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000431 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000432 Node->getFinallyStmt())) {
433 Indent() << "@finally";
434 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000435 OS << "\n";
436 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000437}
438
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000439void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000440}
441
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000442void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000443 Indent() << "@catch (...) { /* todo */ } \n";
444}
445
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000446void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000447 Indent() << "@throw";
448 if (Node->getThrowExpr()) {
449 OS << " ";
450 PrintExpr(Node->getThrowExpr());
451 }
452 OS << ";\n";
453}
454
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000455void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000456 Indent() << "@synchronized (";
457 PrintExpr(Node->getSynchExpr());
458 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000459 PrintRawCompoundStmt(Node->getSynchBody());
460 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000461}
462
Reid Spencer5f016e22007-07-11 17:01:13 +0000463//===----------------------------------------------------------------------===//
464// Expr printing methods.
465//===----------------------------------------------------------------------===//
466
467void StmtPrinter::VisitExpr(Expr *Node) {
468 OS << "<<unknown expr type>>";
469}
470
471void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
472 OS << Node->getDecl()->getName();
473}
474
Steve Naroff7779db42007-11-12 14:29:37 +0000475void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000476 if (Node->getBase()) {
477 PrintExpr(Node->getBase());
478 OS << (Node->isArrow() ? "->" : ".");
479 }
Steve Naroff7779db42007-11-12 14:29:37 +0000480 OS << Node->getDecl()->getName();
481}
482
Steve Naroffae784072008-05-30 00:40:33 +0000483void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
484 if (Node->getBase()) {
485 PrintExpr(Node->getBase());
486 OS << ".";
487 }
488 // FIXME: OS << Node->getDecl()->getName();
489}
490
Chris Lattnerd9f69102008-08-10 01:53:14 +0000491void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000492 switch (Node->getIdentType()) {
493 default:
494 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000495 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000496 OS << "__func__";
497 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000498 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000499 OS << "__FUNCTION__";
500 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000501 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000502 OS << "__PRETTY_FUNCTION__";
503 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000504 case PredefinedExpr::ObjCSuper:
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000505 OS << "super";
506 break;
Anders Carlsson22742662007-07-21 05:21:51 +0000507 }
508}
509
Reid Spencer5f016e22007-07-11 17:01:13 +0000510void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000511 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000512 if (Node->isWide())
513 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000514 switch (value) {
515 case '\\':
516 OS << "'\\\\'";
517 break;
518 case '\'':
519 OS << "'\\''";
520 break;
521 case '\a':
522 // TODO: K&R: the meaning of '\\a' is different in traditional C
523 OS << "'\\a'";
524 break;
525 case '\b':
526 OS << "'\\b'";
527 break;
528 // Nonstandard escape sequence.
529 /*case '\e':
530 OS << "'\\e'";
531 break;*/
532 case '\f':
533 OS << "'\\f'";
534 break;
535 case '\n':
536 OS << "'\\n'";
537 break;
538 case '\r':
539 OS << "'\\r'";
540 break;
541 case '\t':
542 OS << "'\\t'";
543 break;
544 case '\v':
545 OS << "'\\v'";
546 break;
547 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000548 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000549 OS << "'" << (char)value << "'";
550 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000551 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000552 } else {
553 // FIXME what to really do here?
554 OS << value;
555 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000556 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000557}
558
559void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
560 bool isSigned = Node->getType()->isSignedIntegerType();
561 OS << Node->getValue().toString(10, isSigned);
562
563 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000564 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 default: assert(0 && "Unexpected type for integer literal!");
566 case BuiltinType::Int: break; // no suffix.
567 case BuiltinType::UInt: OS << 'U'; break;
568 case BuiltinType::Long: OS << 'L'; break;
569 case BuiltinType::ULong: OS << "UL"; break;
570 case BuiltinType::LongLong: OS << "LL"; break;
571 case BuiltinType::ULongLong: OS << "ULL"; break;
572 }
573}
574void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000575 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000576 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000577}
Chris Lattner5d661452007-08-26 03:42:43 +0000578
579void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
580 PrintExpr(Node->getSubExpr());
581 OS << "i";
582}
583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
585 if (Str->isWide()) OS << 'L';
586 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000587
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 // FIXME: this doesn't print wstrings right.
589 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
590 switch (Str->getStrData()[i]) {
591 default: OS << Str->getStrData()[i]; break;
592 // Handle some common ones to make dumps prettier.
593 case '\\': OS << "\\\\"; break;
594 case '"': OS << "\\\""; break;
595 case '\n': OS << "\\n"; break;
596 case '\t': OS << "\\t"; break;
597 case '\a': OS << "\\a"; break;
598 case '\b': OS << "\\b"; break;
599 }
600 }
601 OS << '"';
602}
603void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
604 OS << "(";
605 PrintExpr(Node->getSubExpr());
606 OS << ")";
607}
608void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000609 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000611
612 // Print a space if this is an "identifier operator" like sizeof or __real.
613 switch (Node->getOpcode()) {
614 default: break;
615 case UnaryOperator::SizeOf:
616 case UnaryOperator::AlignOf:
617 case UnaryOperator::Real:
618 case UnaryOperator::Imag:
619 case UnaryOperator::Extension:
620 OS << ' ';
621 break;
622 }
623 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 PrintExpr(Node->getSubExpr());
625
626 if (Node->isPostfix())
627 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000628}
Chris Lattner704fe352007-08-30 17:59:59 +0000629
630bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
631 if (isa<CompoundLiteralExpr>(E)) {
632 // Base case, print the type and comma.
633 OS << E->getType().getAsString() << ", ";
634 return true;
635 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
636 PrintOffsetOfDesignator(ASE->getLHS());
637 OS << "[";
638 PrintExpr(ASE->getRHS());
639 OS << "]";
640 return false;
641 } else {
642 MemberExpr *ME = cast<MemberExpr>(E);
643 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
644 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
645 return false;
646 }
647}
648
649void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
650 OS << "__builtin_offsetof(";
651 PrintOffsetOfDesignator(Node->getSubExpr());
652 OS << ")";
653}
654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
656 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
657 OS << Node->getArgumentType().getAsString() << ")";
658}
659void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000660 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000662 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 OS << "]";
664}
665
666void StmtPrinter::VisitCallExpr(CallExpr *Call) {
667 PrintExpr(Call->getCallee());
668 OS << "(";
669 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000670 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
671 // Don't print any defaulted arguments
672 break;
673 }
674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 if (i) OS << ", ";
676 PrintExpr(Call->getArg(i));
677 }
678 OS << ")";
679}
680void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
681 PrintExpr(Node->getBase());
682 OS << (Node->isArrow() ? "->" : ".");
683
684 FieldDecl *Field = Node->getMemberDecl();
685 assert(Field && "MemberExpr should alway reference a field!");
686 OS << Field->getName();
687}
Nate Begeman213541a2008-04-18 23:10:10 +0000688void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000689 PrintExpr(Node->getBase());
690 OS << ".";
691 OS << Node->getAccessor().getName();
692}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000693void StmtPrinter::VisitCastExpr(CastExpr *) {
694 assert(0 && "CastExpr is an abstract class");
695}
696void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000697 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 PrintExpr(Node->getSubExpr());
699}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000700void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
701 OS << "(" << Node->getType().getAsString() << ")";
702 PrintExpr(Node->getInitializer());
703}
Steve Naroff49b45262007-07-13 16:58:59 +0000704void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000705 // No need to print anything, simply forward to the sub expression.
706 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000707}
Reid Spencer5f016e22007-07-11 17:01:13 +0000708void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
709 PrintExpr(Node->getLHS());
710 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
711 PrintExpr(Node->getRHS());
712}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000713void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
714 PrintExpr(Node->getLHS());
715 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
716 PrintExpr(Node->getRHS());
717}
Reid Spencer5f016e22007-07-11 17:01:13 +0000718void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
719 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000720
721 if (Node->getLHS()) {
722 OS << " ? ";
723 PrintExpr(Node->getLHS());
724 OS << " : ";
725 }
726 else { // Handle GCC extention where LHS can be NULL.
727 OS << " ?: ";
728 }
729
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 PrintExpr(Node->getRHS());
731}
732
733// GNU extensions.
734
Chris Lattner6481a572007-08-03 17:31:20 +0000735void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000737}
738
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000739void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
740 OS << "(";
741 PrintRawCompoundStmt(E->getSubStmt());
742 OS << ")";
743}
744
Steve Naroffd34e9152007-08-01 22:05:33 +0000745void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
746 OS << "__builtin_types_compatible_p(";
747 OS << Node->getArgType1().getAsString() << ",";
748 OS << Node->getArgType2().getAsString() << ")";
749}
750
Steve Naroffd04fdd52007-08-03 21:21:27 +0000751void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
752 OS << "__builtin_choose_expr(";
753 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000754 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000755 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000756 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000757 PrintExpr(Node->getRHS());
758 OS << ")";
759}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000760
Nate Begemane2ce1d92008-01-17 17:46:27 +0000761void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
762 OS << "__builtin_overload(";
Nate Begeman67295d02008-01-30 20:50:20 +0000763 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begemane2ce1d92008-01-17 17:46:27 +0000764 if (i) OS << ", ";
Nate Begeman67295d02008-01-30 20:50:20 +0000765 PrintExpr(Node->getExpr(i));
Nate Begemane2ce1d92008-01-17 17:46:27 +0000766 }
767 OS << ")";
768}
769
Eli Friedmand38617c2008-05-14 19:38:39 +0000770void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
771 OS << "__builtin_shufflevector(";
772 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
773 if (i) OS << ", ";
774 PrintExpr(Node->getExpr(i));
775 }
776 OS << ")";
777}
778
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000779void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
780 OS << "{ ";
781 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
782 if (i) OS << ", ";
783 PrintExpr(Node->getInit(i));
784 }
785 OS << " }";
786}
787
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000788void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
789 OS << "va_arg(";
790 PrintExpr(Node->getSubExpr());
791 OS << ", ";
792 OS << Node->getType().getAsString();
793 OS << ")";
794}
795
Reid Spencer5f016e22007-07-11 17:01:13 +0000796// C++
797
798void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner36460ee2007-08-09 17:34:19 +0000799 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 OS << Node->getDestType().getAsString() << ">(";
801 PrintExpr(Node->getSubExpr());
802 OS << ")";
803}
804
805void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
806 OS << (Node->getValue() ? "true" : "false");
807}
808
Chris Lattner50dd2892008-02-26 00:51:44 +0000809void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
810 if (Node->getSubExpr() == 0)
811 OS << "throw";
812 else {
813 OS << "throw ";
814 PrintExpr(Node->getSubExpr());
815 }
816}
817
Chris Lattner04421082008-04-08 04:40:51 +0000818void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
819 // Nothing to print: we picked up the default argument
820}
821
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000822void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
823 OS << Node->getType().getAsString();
824 OS << "(";
825 PrintExpr(Node->getSubExpr());
826 OS << ")";
827}
828
829void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
830 OS << Node->getType().getAsString() << "()";
831}
832
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +0000833void
834StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
835 PrintRawDecl(E->getVarDecl());
836}
837
Anders Carlsson55085182007-08-21 17:43:55 +0000838// Obj-C
839
840void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
841 OS << "@";
842 VisitStringLiteral(Node->getString());
843}
Reid Spencer5f016e22007-07-11 17:01:13 +0000844
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000845void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000846 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000847}
848
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000849void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000850 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000851}
852
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000853void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000854 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000855}
856
Steve Naroff563477d2007-09-18 23:55:05 +0000857void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
858 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000859 Expr *receiver = Mess->getReceiver();
860 if (receiver) PrintExpr(receiver);
861 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +0000862 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +0000863 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000864 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +0000865 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000866 } else {
867 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +0000868 if (i < selector.getNumArgs()) {
869 if (i > 0) OS << ' ';
870 if (selector.getIdentifierInfoForSlot(i))
871 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
872 else
873 OS << ":";
874 }
875 else OS << ", "; // Handle variadic methods.
876
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000877 PrintExpr(Mess->getArg(i));
878 }
Steve Naroff563477d2007-09-18 23:55:05 +0000879 }
880 OS << "]";
881}
882
Steve Naroff4eb206b2008-09-03 18:15:37 +0000883void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
884 OS << "^";
885
886 const FunctionType *AFT = Node->getFunctionType();
887
888 if (isa<FunctionTypeNoProto>(AFT)) {
889 OS << "()";
890 } else if (!Node->arg_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
891 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
892 OS << '(';
893 std::string ParamStr;
894 for (BlockStmtExpr::arg_iterator AI = Node->arg_begin(),
895 E = Node->arg_end(); AI != E; ++AI) {
896 if (AI != Node->arg_begin()) OS << ", ";
897 ParamStr = (*AI)->getName();
898 (*AI)->getType().getAsStringInternal(ParamStr);
899 OS << ParamStr;
900 }
901
902 if (FT->isVariadic()) {
903 if (!Node->arg_empty()) OS << ", ";
904 OS << "...";
905 }
906 OS << ')';
907 }
908}
909
910void StmtPrinter::VisitBlockStmtExpr(BlockStmtExpr *Node) {
911 VisitBlockExpr(Node);
912 PrintRawCompoundStmt(Node->getBody());
913}
914
915void StmtPrinter::VisitBlockExprExpr(BlockExprExpr *Node) {
916 VisitBlockExpr(Node);
917 PrintExpr(Node->getExpr());
918}
919
920void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
921 OS << Node->getDecl()->getName();
922}
Reid Spencer5f016e22007-07-11 17:01:13 +0000923//===----------------------------------------------------------------------===//
924// Stmt method implementations
925//===----------------------------------------------------------------------===//
926
Chris Lattner6000dac2007-08-08 22:51:59 +0000927void Stmt::dumpPretty() const {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000928 printPretty(llvm::errs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000929}
930
Ted Kremeneka95d3752008-09-13 05:16:45 +0000931void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 if (this == 0) {
933 OS << "<NULL>";
934 return;
935 }
936
Ted Kremenek42a509f2007-08-31 21:30:12 +0000937 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000938 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +0000939}
Ted Kremenek42a509f2007-08-31 21:30:12 +0000940
941//===----------------------------------------------------------------------===//
942// PrinterHelper
943//===----------------------------------------------------------------------===//
944
945// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +0000946PrinterHelper::~PrinterHelper() {}