blob: 5e347435b6339122256547245ea7f416cf2912f0 [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"
16#include "clang/AST/Decl.h"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/ExprCXX.h"
Steve Narofff494b572008-05-29 21:12:08 +000019#include "clang/AST/ExprObjC.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000020#include "clang/AST/PrettyPrinter.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000021#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/Support/Compiler.h"
Ted Kremenek51221ec2007-11-26 22:50:46 +000023#include "llvm/Support/Streams.h"
Chris Lattnerb0a721a2007-07-13 05:18:11 +000024#include <iomanip>
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// StmtPrinter Visitor
29//===----------------------------------------------------------------------===//
30
31namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000032 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 std::ostream &OS;
34 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000035 clang::PrinterHelper* Helper;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 public:
Ted Kremenek42a509f2007-08-31 21:30:12 +000037 StmtPrinter(std::ostream &os, PrinterHelper* helper) :
38 OS(os), IndentLevel(0), Helper(helper) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000039
40 void PrintStmt(Stmt *S, int SubIndent = 1) {
41 IndentLevel += SubIndent;
42 if (S && isa<Expr>(S)) {
43 // If this is an expr used in a stmt context, indent and newline it.
44 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000045 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000046 OS << ";\n";
47 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000048 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 } else {
50 Indent() << "<<<NULL STATEMENT>>>\n";
51 }
52 IndentLevel -= SubIndent;
53 }
54
55 void PrintRawCompoundStmt(CompoundStmt *S);
56 void PrintRawDecl(Decl *D);
57 void PrintRawIfStmt(IfStmt *If);
58
59 void PrintExpr(Expr *E) {
60 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000061 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000062 else
63 OS << "<null expr>";
64 }
65
66 std::ostream &Indent(int Delta = 0) const {
67 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
68 OS << " ";
69 return OS;
70 }
71
Chris Lattner704fe352007-08-30 17:59:59 +000072 bool PrintOffsetOfDesignator(Expr *E);
73 void VisitUnaryOffsetOf(UnaryOperator *Node);
74
Ted Kremenek42a509f2007-08-31 21:30:12 +000075 void Visit(Stmt* S) {
76 if (Helper && Helper->handledStmt(S,OS))
77 return;
78 else StmtVisitor<StmtPrinter>::Visit(S);
79 }
80
Chris Lattnerc5598cb2007-08-21 04:04:25 +000081 void VisitStmt(Stmt *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000082#define STMT(N, CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000083 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000084#include "clang/AST/StmtNodes.def"
85 };
86}
87
88//===----------------------------------------------------------------------===//
89// Stmt printing methods.
90//===----------------------------------------------------------------------===//
91
92void StmtPrinter::VisitStmt(Stmt *Node) {
93 Indent() << "<<unknown stmt type>>\n";
94}
95
96/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
97/// with no newline after the }.
98void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
99 OS << "{\n";
100 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
101 I != E; ++I)
102 PrintStmt(*I);
103
104 Indent() << "}";
105}
106
107void StmtPrinter::PrintRawDecl(Decl *D) {
108 // FIXME: Need to complete/beautify this... this code simply shows the
109 // nodes are where they need to be.
110 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
111 OS << "typedef " << localType->getUnderlyingType().getAsString();
112 OS << " " << localType->getName();
113 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
114 // Emit storage class for vardecls.
115 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
116 switch (V->getStorageClass()) {
117 default: assert(0 && "Unknown storage class!");
118 case VarDecl::None: break;
119 case VarDecl::Extern: OS << "extern "; break;
120 case VarDecl::Static: OS << "static "; break;
121 case VarDecl::Auto: OS << "auto "; break;
122 case VarDecl::Register: OS << "register "; break;
123 }
124 }
125
126 std::string Name = VD->getName();
127 VD->getType().getAsStringInternal(Name);
128 OS << Name;
129
Chris Lattner24c39902007-07-12 00:36:32 +0000130 // If this is a vardecl with an initializer, emit it.
131 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
132 if (V->getInit()) {
133 OS << " = ";
134 PrintExpr(V->getInit());
135 }
136 }
Steve Naroff91578f32007-11-17 21:21:01 +0000137 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
138 // print a free standing tag decl (e.g. "struct x;").
139 OS << TD->getKindName();
140 OS << " ";
141 if (const IdentifierInfo *II = TD->getIdentifier())
142 OS << II->getName();
143 else
144 OS << "<anonymous>";
145 // FIXME: print tag bodies.
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 assert(0 && "Unexpected decl");
148 }
149}
150
151
152void StmtPrinter::VisitNullStmt(NullStmt *Node) {
153 Indent() << ";\n";
154}
155
156void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Steve Naroff94745042007-09-13 23:52:58 +0000157 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 Indent();
159 PrintRawDecl(D);
160 OS << ";\n";
161 }
162}
163
164void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
165 Indent();
166 PrintRawCompoundStmt(Node);
167 OS << "\n";
168}
169
170void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
171 Indent(-1) << "case ";
172 PrintExpr(Node->getLHS());
173 if (Node->getRHS()) {
174 OS << " ... ";
175 PrintExpr(Node->getRHS());
176 }
177 OS << ":\n";
178
179 PrintStmt(Node->getSubStmt(), 0);
180}
181
182void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
183 Indent(-1) << "default:\n";
184 PrintStmt(Node->getSubStmt(), 0);
185}
186
187void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
188 Indent(-1) << Node->getName() << ":\n";
189 PrintStmt(Node->getSubStmt(), 0);
190}
191
192void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
193 OS << "if ";
194 PrintExpr(If->getCond());
195
196 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
197 OS << ' ';
198 PrintRawCompoundStmt(CS);
199 OS << (If->getElse() ? ' ' : '\n');
200 } else {
201 OS << '\n';
202 PrintStmt(If->getThen());
203 if (If->getElse()) Indent();
204 }
205
206 if (Stmt *Else = If->getElse()) {
207 OS << "else";
208
209 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
210 OS << ' ';
211 PrintRawCompoundStmt(CS);
212 OS << '\n';
213 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
214 OS << ' ';
215 PrintRawIfStmt(ElseIf);
216 } else {
217 OS << '\n';
218 PrintStmt(If->getElse());
219 }
220 }
221}
222
223void StmtPrinter::VisitIfStmt(IfStmt *If) {
224 Indent();
225 PrintRawIfStmt(If);
226}
227
228void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
229 Indent() << "switch (";
230 PrintExpr(Node->getCond());
231 OS << ")";
232
233 // Pretty print compoundstmt bodies (very common).
234 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
235 OS << " ";
236 PrintRawCompoundStmt(CS);
237 OS << "\n";
238 } else {
239 OS << "\n";
240 PrintStmt(Node->getBody());
241 }
242}
243
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000244void StmtPrinter::VisitSwitchCase(SwitchCase*) {
245 assert(0 && "SwitchCase is an abstract class");
246}
247
Reid Spencer5f016e22007-07-11 17:01:13 +0000248void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
249 Indent() << "while (";
250 PrintExpr(Node->getCond());
251 OS << ")\n";
252 PrintStmt(Node->getBody());
253}
254
255void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000256 Indent() << "do ";
257 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
258 PrintRawCompoundStmt(CS);
259 OS << " ";
260 } else {
261 OS << "\n";
262 PrintStmt(Node->getBody());
263 Indent();
264 }
265
266 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 PrintExpr(Node->getCond());
268 OS << ";\n";
269}
270
271void StmtPrinter::VisitForStmt(ForStmt *Node) {
272 Indent() << "for (";
273 if (Node->getInit()) {
274 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
275 PrintRawDecl(DS->getDecl());
276 else
277 PrintExpr(cast<Expr>(Node->getInit()));
278 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000279 OS << ";";
280 if (Node->getCond()) {
281 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000283 }
284 OS << ";";
285 if (Node->getInc()) {
286 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000288 }
289 OS << ") ";
290
291 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
292 PrintRawCompoundStmt(CS);
293 OS << "\n";
294 } else {
295 OS << "\n";
296 PrintStmt(Node->getBody());
297 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000298}
299
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000300void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000301 Indent() << "for (";
302 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
303 PrintRawDecl(DS->getDecl());
304 else
305 PrintExpr(cast<Expr>(Node->getElement()));
306 OS << " in ";
307 PrintExpr(Node->getCollection());
308 OS << ") ";
309
310 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
311 PrintRawCompoundStmt(CS);
312 OS << "\n";
313 } else {
314 OS << "\n";
315 PrintStmt(Node->getBody());
316 }
317}
318
Reid Spencer5f016e22007-07-11 17:01:13 +0000319void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
320 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
321}
322
323void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
324 Indent() << "goto *";
325 PrintExpr(Node->getTarget());
326 OS << ";\n";
327}
328
329void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
330 Indent() << "continue;\n";
331}
332
333void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
334 Indent() << "break;\n";
335}
336
337
338void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
339 Indent() << "return";
340 if (Node->getRetValue()) {
341 OS << " ";
342 PrintExpr(Node->getRetValue());
343 }
344 OS << ";\n";
345}
346
Chris Lattnerfe795952007-10-29 04:04:16 +0000347
348void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000349 Indent() << "asm ";
350
351 if (Node->isVolatile())
352 OS << "volatile ";
353
354 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000355 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000356
357 // Outputs
358 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
359 Node->getNumClobbers() != 0)
360 OS << " : ";
361
362 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
363 if (i != 0)
364 OS << ", ";
365
366 if (!Node->getOutputName(i).empty()) {
367 OS << '[';
368 OS << Node->getOutputName(i);
369 OS << "] ";
370 }
371
372 VisitStringLiteral(Node->getOutputConstraint(i));
373 OS << " ";
374 Visit(Node->getOutputExpr(i));
375 }
376
377 // Inputs
378 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
379 OS << " : ";
380
381 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
382 if (i != 0)
383 OS << ", ";
384
385 if (!Node->getInputName(i).empty()) {
386 OS << '[';
387 OS << Node->getInputName(i);
388 OS << "] ";
389 }
390
391 VisitStringLiteral(Node->getInputConstraint(i));
392 OS << " ";
393 Visit(Node->getInputExpr(i));
394 }
395
396 // Clobbers
397 if (Node->getNumClobbers() != 0)
398 OS << " : ";
399
400 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
401 if (i != 0)
402 OS << ", ";
403
404 VisitStringLiteral(Node->getClobber(i));
405 }
406
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000407 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000408}
409
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000410void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000411 Indent() << "@try";
412 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
413 PrintRawCompoundStmt(TS);
414 OS << "\n";
415 }
416
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000417 for (ObjCAtCatchStmt *catchStmt =
418 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000419 catchStmt;
420 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000421 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000422 Indent() << "@catch(";
423 if (catchStmt->getCatchParamStmt()) {
424 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
425 PrintRawDecl(DS->getDecl());
426 }
427 OS << ")";
428 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
429 {
430 PrintRawCompoundStmt(CS);
431 OS << "\n";
432 }
433 }
434
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000435 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000436 Node->getFinallyStmt())) {
437 Indent() << "@finally";
438 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000439 OS << "\n";
440 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000441}
442
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000443void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000444}
445
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000446void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000447 Indent() << "@catch (...) { /* todo */ } \n";
448}
449
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000450void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000451 Indent() << "@throw";
452 if (Node->getThrowExpr()) {
453 OS << " ";
454 PrintExpr(Node->getThrowExpr());
455 }
456 OS << ";\n";
457}
458
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000459void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000460 Indent() << "@synchronized (";
461 PrintExpr(Node->getSynchExpr());
462 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000463 PrintRawCompoundStmt(Node->getSynchBody());
464 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000465}
466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467//===----------------------------------------------------------------------===//
468// Expr printing methods.
469//===----------------------------------------------------------------------===//
470
471void StmtPrinter::VisitExpr(Expr *Node) {
472 OS << "<<unknown expr type>>";
473}
474
475void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
476 OS << Node->getDecl()->getName();
477}
478
Steve Naroffe3e9add2008-06-02 23:03:37 +0000479void StmtPrinter::VisitObjCSuperRefExpr(ObjCSuperRefExpr *Node) {
480 OS << "super";
481}
482
Steve Naroff7779db42007-11-12 14:29:37 +0000483void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000484 if (Node->getBase()) {
485 PrintExpr(Node->getBase());
486 OS << (Node->isArrow() ? "->" : ".");
487 }
Steve Naroff7779db42007-11-12 14:29:37 +0000488 OS << Node->getDecl()->getName();
489}
490
Steve Naroffae784072008-05-30 00:40:33 +0000491void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
492 if (Node->getBase()) {
493 PrintExpr(Node->getBase());
494 OS << ".";
495 }
496 // FIXME: OS << Node->getDecl()->getName();
497}
498
Anders Carlsson22742662007-07-21 05:21:51 +0000499void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
500 switch (Node->getIdentType()) {
501 default:
502 assert(0 && "unknown case");
503 case PreDefinedExpr::Func:
504 OS << "__func__";
505 break;
506 case PreDefinedExpr::Function:
507 OS << "__FUNCTION__";
508 break;
509 case PreDefinedExpr::PrettyFunction:
510 OS << "__PRETTY_FUNCTION__";
511 break;
512 }
513}
514
Reid Spencer5f016e22007-07-11 17:01:13 +0000515void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000516 // FIXME should print an L for wchar_t constants
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000517 unsigned value = Node->getValue();
Chris Lattner8bf9f072007-07-13 23:58:20 +0000518 switch (value) {
519 case '\\':
520 OS << "'\\\\'";
521 break;
522 case '\'':
523 OS << "'\\''";
524 break;
525 case '\a':
526 // TODO: K&R: the meaning of '\\a' is different in traditional C
527 OS << "'\\a'";
528 break;
529 case '\b':
530 OS << "'\\b'";
531 break;
532 // Nonstandard escape sequence.
533 /*case '\e':
534 OS << "'\\e'";
535 break;*/
536 case '\f':
537 OS << "'\\f'";
538 break;
539 case '\n':
540 OS << "'\\n'";
541 break;
542 case '\r':
543 OS << "'\\r'";
544 break;
545 case '\t':
546 OS << "'\\t'";
547 break;
548 case '\v':
549 OS << "'\\v'";
550 break;
551 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000552 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000553 OS << "'" << (char)value << "'";
554 } else if (value < 256) {
555 OS << "'\\x" << std::hex << value << std::dec << "'";
556 } else {
557 // FIXME what to really do here?
558 OS << value;
559 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000560 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000561}
562
563void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
564 bool isSigned = Node->getType()->isSignedIntegerType();
565 OS << Node->getValue().toString(10, isSigned);
566
567 // Emit suffixes. Integer literals are always a builtin integer type.
568 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
569 default: assert(0 && "Unexpected type for integer literal!");
570 case BuiltinType::Int: break; // no suffix.
571 case BuiltinType::UInt: OS << 'U'; break;
572 case BuiltinType::Long: OS << 'L'; break;
573 case BuiltinType::ULong: OS << "UL"; break;
574 case BuiltinType::LongLong: OS << "LL"; break;
575 case BuiltinType::ULongLong: OS << "ULL"; break;
576 }
577}
578void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000579 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000580 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000581}
Chris Lattner5d661452007-08-26 03:42:43 +0000582
583void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
584 PrintExpr(Node->getSubExpr());
585 OS << "i";
586}
587
Reid Spencer5f016e22007-07-11 17:01:13 +0000588void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
589 if (Str->isWide()) OS << 'L';
590 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000591
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 // FIXME: this doesn't print wstrings right.
593 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
594 switch (Str->getStrData()[i]) {
595 default: OS << Str->getStrData()[i]; break;
596 // Handle some common ones to make dumps prettier.
597 case '\\': OS << "\\\\"; break;
598 case '"': OS << "\\\""; break;
599 case '\n': OS << "\\n"; break;
600 case '\t': OS << "\\t"; break;
601 case '\a': OS << "\\a"; break;
602 case '\b': OS << "\\b"; break;
603 }
604 }
605 OS << '"';
606}
607void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
608 OS << "(";
609 PrintExpr(Node->getSubExpr());
610 OS << ")";
611}
612void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000613 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000615
616 // Print a space if this is an "identifier operator" like sizeof or __real.
617 switch (Node->getOpcode()) {
618 default: break;
619 case UnaryOperator::SizeOf:
620 case UnaryOperator::AlignOf:
621 case UnaryOperator::Real:
622 case UnaryOperator::Imag:
623 case UnaryOperator::Extension:
624 OS << ' ';
625 break;
626 }
627 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 PrintExpr(Node->getSubExpr());
629
630 if (Node->isPostfix())
631 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000632}
Chris Lattner704fe352007-08-30 17:59:59 +0000633
634bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
635 if (isa<CompoundLiteralExpr>(E)) {
636 // Base case, print the type and comma.
637 OS << E->getType().getAsString() << ", ";
638 return true;
639 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
640 PrintOffsetOfDesignator(ASE->getLHS());
641 OS << "[";
642 PrintExpr(ASE->getRHS());
643 OS << "]";
644 return false;
645 } else {
646 MemberExpr *ME = cast<MemberExpr>(E);
647 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
648 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
649 return false;
650 }
651}
652
653void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
654 OS << "__builtin_offsetof(";
655 PrintOffsetOfDesignator(Node->getSubExpr());
656 OS << ")";
657}
658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
660 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
661 OS << Node->getArgumentType().getAsString() << ")";
662}
663void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000664 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000666 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 OS << "]";
668}
669
670void StmtPrinter::VisitCallExpr(CallExpr *Call) {
671 PrintExpr(Call->getCallee());
672 OS << "(";
673 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000674 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
675 // Don't print any defaulted arguments
676 break;
677 }
678
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 if (i) OS << ", ";
680 PrintExpr(Call->getArg(i));
681 }
682 OS << ")";
683}
684void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
685 PrintExpr(Node->getBase());
686 OS << (Node->isArrow() ? "->" : ".");
687
688 FieldDecl *Field = Node->getMemberDecl();
689 assert(Field && "MemberExpr should alway reference a field!");
690 OS << Field->getName();
691}
Nate Begeman213541a2008-04-18 23:10:10 +0000692void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000693 PrintExpr(Node->getBase());
694 OS << ".";
695 OS << Node->getAccessor().getName();
696}
Reid Spencer5f016e22007-07-11 17:01:13 +0000697void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000698 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 PrintExpr(Node->getSubExpr());
700}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000701void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
702 OS << "(" << Node->getType().getAsString() << ")";
703 PrintExpr(Node->getInitializer());
704}
Steve Naroff49b45262007-07-13 16:58:59 +0000705void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000706 // No need to print anything, simply forward to the sub expression.
707 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000708}
Reid Spencer5f016e22007-07-11 17:01:13 +0000709void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
710 PrintExpr(Node->getLHS());
711 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
712 PrintExpr(Node->getRHS());
713}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000714void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
715 PrintExpr(Node->getLHS());
716 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
717 PrintExpr(Node->getRHS());
718}
Reid Spencer5f016e22007-07-11 17:01:13 +0000719void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
720 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000721
722 if (Node->getLHS()) {
723 OS << " ? ";
724 PrintExpr(Node->getLHS());
725 OS << " : ";
726 }
727 else { // Handle GCC extention where LHS can be NULL.
728 OS << " ?: ";
729 }
730
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 PrintExpr(Node->getRHS());
732}
733
734// GNU extensions.
735
Chris Lattner6481a572007-08-03 17:31:20 +0000736void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000738}
739
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000740void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
741 OS << "(";
742 PrintRawCompoundStmt(E->getSubStmt());
743 OS << ")";
744}
745
Steve Naroffd34e9152007-08-01 22:05:33 +0000746void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
747 OS << "__builtin_types_compatible_p(";
748 OS << Node->getArgType1().getAsString() << ",";
749 OS << Node->getArgType2().getAsString() << ")";
750}
751
Steve Naroffd04fdd52007-08-03 21:21:27 +0000752void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
753 OS << "__builtin_choose_expr(";
754 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000755 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000756 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000757 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000758 PrintExpr(Node->getRHS());
759 OS << ")";
760}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000761
Nate Begemane2ce1d92008-01-17 17:46:27 +0000762void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
763 OS << "__builtin_overload(";
Nate Begeman67295d02008-01-30 20:50:20 +0000764 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begemane2ce1d92008-01-17 17:46:27 +0000765 if (i) OS << ", ";
Nate Begeman67295d02008-01-30 20:50:20 +0000766 PrintExpr(Node->getExpr(i));
Nate Begemane2ce1d92008-01-17 17:46:27 +0000767 }
768 OS << ")";
769}
770
Eli Friedmand38617c2008-05-14 19:38:39 +0000771void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
772 OS << "__builtin_shufflevector(";
773 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
774 if (i) OS << ", ";
775 PrintExpr(Node->getExpr(i));
776 }
777 OS << ")";
778}
779
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000780void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
781 OS << "{ ";
782 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
783 if (i) OS << ", ";
784 PrintExpr(Node->getInit(i));
785 }
786 OS << " }";
787}
788
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000789void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
790 OS << "va_arg(";
791 PrintExpr(Node->getSubExpr());
792 OS << ", ";
793 OS << Node->getType().getAsString();
794 OS << ")";
795}
796
Reid Spencer5f016e22007-07-11 17:01:13 +0000797// C++
798
799void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner36460ee2007-08-09 17:34:19 +0000800 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 OS << Node->getDestType().getAsString() << ">(";
802 PrintExpr(Node->getSubExpr());
803 OS << ")";
804}
805
806void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
807 OS << (Node->getValue() ? "true" : "false");
808}
809
Chris Lattner50dd2892008-02-26 00:51:44 +0000810void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
811 if (Node->getSubExpr() == 0)
812 OS << "throw";
813 else {
814 OS << "throw ";
815 PrintExpr(Node->getSubExpr());
816 }
817}
818
Chris Lattner04421082008-04-08 04:40:51 +0000819void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
820 // Nothing to print: we picked up the default argument
821}
822
Anders Carlsson55085182007-08-21 17:43:55 +0000823// Obj-C
824
825void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
826 OS << "@";
827 VisitStringLiteral(Node->getString());
828}
Reid Spencer5f016e22007-07-11 17:01:13 +0000829
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000830void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000831 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000832}
833
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000834void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000835 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000836}
837
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000838void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000839 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000840}
841
Steve Naroff563477d2007-09-18 23:55:05 +0000842void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
843 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000844 Expr *receiver = Mess->getReceiver();
845 if (receiver) PrintExpr(receiver);
846 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +0000847 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +0000848 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000849 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +0000850 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000851 } else {
852 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +0000853 if (i < selector.getNumArgs()) {
854 if (i > 0) OS << ' ';
855 if (selector.getIdentifierInfoForSlot(i))
856 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
857 else
858 OS << ":";
859 }
860 else OS << ", "; // Handle variadic methods.
861
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000862 PrintExpr(Mess->getArg(i));
863 }
Steve Naroff563477d2007-09-18 23:55:05 +0000864 }
865 OS << "]";
866}
867
Reid Spencer5f016e22007-07-11 17:01:13 +0000868//===----------------------------------------------------------------------===//
869// Stmt method implementations
870//===----------------------------------------------------------------------===//
871
Chris Lattner6000dac2007-08-08 22:51:59 +0000872void Stmt::dumpPretty() const {
Ted Kremenek51221ec2007-11-26 22:50:46 +0000873 printPretty(*llvm::cerr.stream());
Reid Spencer5f016e22007-07-11 17:01:13 +0000874}
875
Ted Kremenek42a509f2007-08-31 21:30:12 +0000876void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 if (this == 0) {
878 OS << "<NULL>";
879 return;
880 }
881
Ted Kremenek42a509f2007-08-31 21:30:12 +0000882 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000883 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +0000884}
Ted Kremenek42a509f2007-08-31 21:30:12 +0000885
886//===----------------------------------------------------------------------===//
887// PrinterHelper
888//===----------------------------------------------------------------------===//
889
890// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +0000891PrinterHelper::~PrinterHelper() {}