blob: 306bfcb18e641530bdee839853d97a8d4acdafd9 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000020#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/Support/Compiler.h"
22#include <iostream>
Chris Lattnerb0a721a2007-07-13 05:18:11 +000023#include <iomanip>
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// StmtPrinter Visitor
28//===----------------------------------------------------------------------===//
29
30namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000031 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 std::ostream &OS;
33 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000034 clang::PrinterHelper* Helper;
Reid Spencer5f016e22007-07-11 17:01:13 +000035 public:
Ted Kremenek42a509f2007-08-31 21:30:12 +000036 StmtPrinter(std::ostream &os, PrinterHelper* helper) :
37 OS(os), IndentLevel(0), Helper(helper) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000038
39 void PrintStmt(Stmt *S, int SubIndent = 1) {
40 IndentLevel += SubIndent;
41 if (S && isa<Expr>(S)) {
42 // If this is an expr used in a stmt context, indent and newline it.
43 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000044 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000045 OS << ";\n";
46 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000047 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 } else {
49 Indent() << "<<<NULL STATEMENT>>>\n";
50 }
51 IndentLevel -= SubIndent;
52 }
53
54 void PrintRawCompoundStmt(CompoundStmt *S);
55 void PrintRawDecl(Decl *D);
56 void PrintRawIfStmt(IfStmt *If);
57
58 void PrintExpr(Expr *E) {
59 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000060 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000061 else
62 OS << "<null expr>";
63 }
64
65 std::ostream &Indent(int Delta = 0) const {
66 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
67 OS << " ";
68 return OS;
69 }
70
Chris Lattner704fe352007-08-30 17:59:59 +000071 bool PrintOffsetOfDesignator(Expr *E);
72 void VisitUnaryOffsetOf(UnaryOperator *Node);
73
Ted Kremenek42a509f2007-08-31 21:30:12 +000074 void Visit(Stmt* S) {
75 if (Helper && Helper->handledStmt(S,OS))
76 return;
77 else StmtVisitor<StmtPrinter>::Visit(S);
78 }
79
Chris Lattnerc5598cb2007-08-21 04:04:25 +000080 void VisitStmt(Stmt *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000081#define STMT(N, CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000082 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000083#include "clang/AST/StmtNodes.def"
84 };
85}
86
87//===----------------------------------------------------------------------===//
88// Stmt printing methods.
89//===----------------------------------------------------------------------===//
90
91void StmtPrinter::VisitStmt(Stmt *Node) {
92 Indent() << "<<unknown stmt type>>\n";
93}
94
95/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
96/// with no newline after the }.
97void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
98 OS << "{\n";
99 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
100 I != E; ++I)
101 PrintStmt(*I);
102
103 Indent() << "}";
104}
105
106void StmtPrinter::PrintRawDecl(Decl *D) {
107 // FIXME: Need to complete/beautify this... this code simply shows the
108 // nodes are where they need to be.
109 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
110 OS << "typedef " << localType->getUnderlyingType().getAsString();
111 OS << " " << localType->getName();
112 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
113 // Emit storage class for vardecls.
114 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
115 switch (V->getStorageClass()) {
116 default: assert(0 && "Unknown storage class!");
117 case VarDecl::None: break;
118 case VarDecl::Extern: OS << "extern "; break;
119 case VarDecl::Static: OS << "static "; break;
120 case VarDecl::Auto: OS << "auto "; break;
121 case VarDecl::Register: OS << "register "; break;
122 }
123 }
124
125 std::string Name = VD->getName();
126 VD->getType().getAsStringInternal(Name);
127 OS << Name;
128
Chris Lattner24c39902007-07-12 00:36:32 +0000129 // If this is a vardecl with an initializer, emit it.
130 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
131 if (V->getInit()) {
132 OS << " = ";
133 PrintExpr(V->getInit());
134 }
135 }
Steve Naroff91578f32007-11-17 21:21:01 +0000136 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
137 // print a free standing tag decl (e.g. "struct x;").
138 OS << TD->getKindName();
139 OS << " ";
140 if (const IdentifierInfo *II = TD->getIdentifier())
141 OS << II->getName();
142 else
143 OS << "<anonymous>";
144 // FIXME: print tag bodies.
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 assert(0 && "Unexpected decl");
147 }
148}
149
150
151void StmtPrinter::VisitNullStmt(NullStmt *Node) {
152 Indent() << ";\n";
153}
154
155void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Steve Naroff94745042007-09-13 23:52:58 +0000156 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 Indent();
158 PrintRawDecl(D);
159 OS << ";\n";
160 }
161}
162
163void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
164 Indent();
165 PrintRawCompoundStmt(Node);
166 OS << "\n";
167}
168
169void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
170 Indent(-1) << "case ";
171 PrintExpr(Node->getLHS());
172 if (Node->getRHS()) {
173 OS << " ... ";
174 PrintExpr(Node->getRHS());
175 }
176 OS << ":\n";
177
178 PrintStmt(Node->getSubStmt(), 0);
179}
180
181void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
182 Indent(-1) << "default:\n";
183 PrintStmt(Node->getSubStmt(), 0);
184}
185
186void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
187 Indent(-1) << Node->getName() << ":\n";
188 PrintStmt(Node->getSubStmt(), 0);
189}
190
191void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
192 OS << "if ";
193 PrintExpr(If->getCond());
194
195 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
196 OS << ' ';
197 PrintRawCompoundStmt(CS);
198 OS << (If->getElse() ? ' ' : '\n');
199 } else {
200 OS << '\n';
201 PrintStmt(If->getThen());
202 if (If->getElse()) Indent();
203 }
204
205 if (Stmt *Else = If->getElse()) {
206 OS << "else";
207
208 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
209 OS << ' ';
210 PrintRawCompoundStmt(CS);
211 OS << '\n';
212 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
213 OS << ' ';
214 PrintRawIfStmt(ElseIf);
215 } else {
216 OS << '\n';
217 PrintStmt(If->getElse());
218 }
219 }
220}
221
222void StmtPrinter::VisitIfStmt(IfStmt *If) {
223 Indent();
224 PrintRawIfStmt(If);
225}
226
227void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
228 Indent() << "switch (";
229 PrintExpr(Node->getCond());
230 OS << ")";
231
232 // Pretty print compoundstmt bodies (very common).
233 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
234 OS << " ";
235 PrintRawCompoundStmt(CS);
236 OS << "\n";
237 } else {
238 OS << "\n";
239 PrintStmt(Node->getBody());
240 }
241}
242
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000243void StmtPrinter::VisitSwitchCase(SwitchCase*) {
244 assert(0 && "SwitchCase is an abstract class");
245}
246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
248 Indent() << "while (";
249 PrintExpr(Node->getCond());
250 OS << ")\n";
251 PrintStmt(Node->getBody());
252}
253
254void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000255 Indent() << "do ";
256 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
257 PrintRawCompoundStmt(CS);
258 OS << " ";
259 } else {
260 OS << "\n";
261 PrintStmt(Node->getBody());
262 Indent();
263 }
264
265 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 PrintExpr(Node->getCond());
267 OS << ";\n";
268}
269
270void StmtPrinter::VisitForStmt(ForStmt *Node) {
271 Indent() << "for (";
272 if (Node->getInit()) {
273 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
274 PrintRawDecl(DS->getDecl());
275 else
276 PrintExpr(cast<Expr>(Node->getInit()));
277 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000278 OS << ";";
279 if (Node->getCond()) {
280 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000282 }
283 OS << ";";
284 if (Node->getInc()) {
285 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000287 }
288 OS << ") ";
289
290 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
291 PrintRawCompoundStmt(CS);
292 OS << "\n";
293 } else {
294 OS << "\n";
295 PrintStmt(Node->getBody());
296 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000297}
298
299void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
300 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
301}
302
303void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
304 Indent() << "goto *";
305 PrintExpr(Node->getTarget());
306 OS << ";\n";
307}
308
309void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
310 Indent() << "continue;\n";
311}
312
313void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
314 Indent() << "break;\n";
315}
316
317
318void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
319 Indent() << "return";
320 if (Node->getRetValue()) {
321 OS << " ";
322 PrintExpr(Node->getRetValue());
323 }
324 OS << ";\n";
325}
326
Chris Lattnerfe795952007-10-29 04:04:16 +0000327
328void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000329 Indent() << "asm (";
330 VisitStringLiteral(Node->getAsmString());
331 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000332}
333
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000334void StmtPrinter::VisitObjcAtTryStmt(ObjcAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000335 Indent() << "@try";
336 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
337 PrintRawCompoundStmt(TS);
338 OS << "\n";
339 }
340
341 for (ObjcAtCatchStmt *catchStmt =
342 static_cast<ObjcAtCatchStmt *>(Node->getCatchStmts());
343 catchStmt;
344 catchStmt =
345 static_cast<ObjcAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
346 Indent() << "@catch(";
347 if (catchStmt->getCatchParamStmt()) {
348 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
349 PrintRawDecl(DS->getDecl());
350 }
351 OS << ")";
352 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
353 {
354 PrintRawCompoundStmt(CS);
355 OS << "\n";
356 }
357 }
358
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000359 if (ObjcAtFinallyStmt *FS =static_cast<ObjcAtFinallyStmt *>(
360 Node->getFinallyStmt())) {
361 Indent() << "@finally";
362 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000363 OS << "\n";
364 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000365}
366
367void StmtPrinter::VisitObjcAtFinallyStmt(ObjcAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000368}
369
370void StmtPrinter::VisitObjcAtCatchStmt (ObjcAtCatchStmt *Node) {
371 Indent() << "@catch (...) { /* todo */ } \n";
372}
373
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000374void StmtPrinter::VisitObjcAtThrowStmt (ObjcAtThrowStmt *Node) {
375 Indent() << "@throw";
376 if (Node->getThrowExpr()) {
377 OS << " ";
378 PrintExpr(Node->getThrowExpr());
379 }
380 OS << ";\n";
381}
382
Reid Spencer5f016e22007-07-11 17:01:13 +0000383//===----------------------------------------------------------------------===//
384// Expr printing methods.
385//===----------------------------------------------------------------------===//
386
387void StmtPrinter::VisitExpr(Expr *Node) {
388 OS << "<<unknown expr type>>";
389}
390
391void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
392 OS << Node->getDecl()->getName();
393}
394
Steve Naroff7779db42007-11-12 14:29:37 +0000395void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000396 if (Node->getBase()) {
397 PrintExpr(Node->getBase());
398 OS << (Node->isArrow() ? "->" : ".");
399 }
Steve Naroff7779db42007-11-12 14:29:37 +0000400 OS << Node->getDecl()->getName();
401}
402
Anders Carlsson22742662007-07-21 05:21:51 +0000403void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
404 switch (Node->getIdentType()) {
405 default:
406 assert(0 && "unknown case");
407 case PreDefinedExpr::Func:
408 OS << "__func__";
409 break;
410 case PreDefinedExpr::Function:
411 OS << "__FUNCTION__";
412 break;
413 case PreDefinedExpr::PrettyFunction:
414 OS << "__PRETTY_FUNCTION__";
415 break;
416 }
417}
418
Reid Spencer5f016e22007-07-11 17:01:13 +0000419void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000420 // FIXME should print an L for wchar_t constants
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000421 unsigned value = Node->getValue();
Chris Lattner8bf9f072007-07-13 23:58:20 +0000422 switch (value) {
423 case '\\':
424 OS << "'\\\\'";
425 break;
426 case '\'':
427 OS << "'\\''";
428 break;
429 case '\a':
430 // TODO: K&R: the meaning of '\\a' is different in traditional C
431 OS << "'\\a'";
432 break;
433 case '\b':
434 OS << "'\\b'";
435 break;
436 // Nonstandard escape sequence.
437 /*case '\e':
438 OS << "'\\e'";
439 break;*/
440 case '\f':
441 OS << "'\\f'";
442 break;
443 case '\n':
444 OS << "'\\n'";
445 break;
446 case '\r':
447 OS << "'\\r'";
448 break;
449 case '\t':
450 OS << "'\\t'";
451 break;
452 case '\v':
453 OS << "'\\v'";
454 break;
455 default:
456 if (isprint(value) && value < 256) {
457 OS << "'" << (char)value << "'";
458 } else if (value < 256) {
459 OS << "'\\x" << std::hex << value << std::dec << "'";
460 } else {
461 // FIXME what to really do here?
462 OS << value;
463 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000464 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000465}
466
467void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
468 bool isSigned = Node->getType()->isSignedIntegerType();
469 OS << Node->getValue().toString(10, isSigned);
470
471 // Emit suffixes. Integer literals are always a builtin integer type.
472 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
473 default: assert(0 && "Unexpected type for integer literal!");
474 case BuiltinType::Int: break; // no suffix.
475 case BuiltinType::UInt: OS << 'U'; break;
476 case BuiltinType::Long: OS << 'L'; break;
477 case BuiltinType::ULong: OS << "UL"; break;
478 case BuiltinType::LongLong: OS << "LL"; break;
479 case BuiltinType::ULongLong: OS << "ULL"; break;
480 }
481}
482void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000483 // FIXME: print value more precisely.
Chris Lattnerc9bec4b2007-09-22 18:47:25 +0000484 OS << Node->getValueAsDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000485}
Chris Lattner5d661452007-08-26 03:42:43 +0000486
487void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
488 PrintExpr(Node->getSubExpr());
489 OS << "i";
490}
491
Reid Spencer5f016e22007-07-11 17:01:13 +0000492void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
493 if (Str->isWide()) OS << 'L';
494 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000495
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 // FIXME: this doesn't print wstrings right.
497 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
498 switch (Str->getStrData()[i]) {
499 default: OS << Str->getStrData()[i]; break;
500 // Handle some common ones to make dumps prettier.
501 case '\\': OS << "\\\\"; break;
502 case '"': OS << "\\\""; break;
503 case '\n': OS << "\\n"; break;
504 case '\t': OS << "\\t"; break;
505 case '\a': OS << "\\a"; break;
506 case '\b': OS << "\\b"; break;
507 }
508 }
509 OS << '"';
510}
511void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
512 OS << "(";
513 PrintExpr(Node->getSubExpr());
514 OS << ")";
515}
516void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000517 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000519
520 // Print a space if this is an "identifier operator" like sizeof or __real.
521 switch (Node->getOpcode()) {
522 default: break;
523 case UnaryOperator::SizeOf:
524 case UnaryOperator::AlignOf:
525 case UnaryOperator::Real:
526 case UnaryOperator::Imag:
527 case UnaryOperator::Extension:
528 OS << ' ';
529 break;
530 }
531 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 PrintExpr(Node->getSubExpr());
533
534 if (Node->isPostfix())
535 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000536}
Chris Lattner704fe352007-08-30 17:59:59 +0000537
538bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
539 if (isa<CompoundLiteralExpr>(E)) {
540 // Base case, print the type and comma.
541 OS << E->getType().getAsString() << ", ";
542 return true;
543 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
544 PrintOffsetOfDesignator(ASE->getLHS());
545 OS << "[";
546 PrintExpr(ASE->getRHS());
547 OS << "]";
548 return false;
549 } else {
550 MemberExpr *ME = cast<MemberExpr>(E);
551 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
552 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
553 return false;
554 }
555}
556
557void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
558 OS << "__builtin_offsetof(";
559 PrintOffsetOfDesignator(Node->getSubExpr());
560 OS << ")";
561}
562
Reid Spencer5f016e22007-07-11 17:01:13 +0000563void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
564 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
565 OS << Node->getArgumentType().getAsString() << ")";
566}
567void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000568 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000570 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 OS << "]";
572}
573
574void StmtPrinter::VisitCallExpr(CallExpr *Call) {
575 PrintExpr(Call->getCallee());
576 OS << "(";
577 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
578 if (i) OS << ", ";
579 PrintExpr(Call->getArg(i));
580 }
581 OS << ")";
582}
583void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
584 PrintExpr(Node->getBase());
585 OS << (Node->isArrow() ? "->" : ".");
586
587 FieldDecl *Field = Node->getMemberDecl();
588 assert(Field && "MemberExpr should alway reference a field!");
589 OS << Field->getName();
590}
Chris Lattner6481a572007-08-03 17:31:20 +0000591void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000592 PrintExpr(Node->getBase());
593 OS << ".";
594 OS << Node->getAccessor().getName();
595}
Reid Spencer5f016e22007-07-11 17:01:13 +0000596void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000597 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 PrintExpr(Node->getSubExpr());
599}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000600void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
601 OS << "(" << Node->getType().getAsString() << ")";
602 PrintExpr(Node->getInitializer());
603}
Steve Naroff49b45262007-07-13 16:58:59 +0000604void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000605 // No need to print anything, simply forward to the sub expression.
606 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000607}
Reid Spencer5f016e22007-07-11 17:01:13 +0000608void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
609 PrintExpr(Node->getLHS());
610 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
611 PrintExpr(Node->getRHS());
612}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000613void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
614 PrintExpr(Node->getLHS());
615 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
616 PrintExpr(Node->getRHS());
617}
Reid Spencer5f016e22007-07-11 17:01:13 +0000618void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
619 PrintExpr(Node->getCond());
620 OS << " ? ";
621 PrintExpr(Node->getLHS());
622 OS << " : ";
623 PrintExpr(Node->getRHS());
624}
625
626// GNU extensions.
627
Chris Lattner6481a572007-08-03 17:31:20 +0000628void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000630}
631
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000632void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
633 OS << "(";
634 PrintRawCompoundStmt(E->getSubStmt());
635 OS << ")";
636}
637
Steve Naroffd34e9152007-08-01 22:05:33 +0000638void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
639 OS << "__builtin_types_compatible_p(";
640 OS << Node->getArgType1().getAsString() << ",";
641 OS << Node->getArgType2().getAsString() << ")";
642}
643
Steve Naroffd04fdd52007-08-03 21:21:27 +0000644void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
645 OS << "__builtin_choose_expr(";
646 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000647 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000648 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000649 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000650 PrintExpr(Node->getRHS());
651 OS << ")";
652}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000653
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000654void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
655 OS << "{ ";
656 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
657 if (i) OS << ", ";
658 PrintExpr(Node->getInit(i));
659 }
660 OS << " }";
661}
662
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000663void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
664 OS << "va_arg(";
665 PrintExpr(Node->getSubExpr());
666 OS << ", ";
667 OS << Node->getType().getAsString();
668 OS << ")";
669}
670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671// C++
672
673void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner36460ee2007-08-09 17:34:19 +0000674 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 OS << Node->getDestType().getAsString() << ">(";
676 PrintExpr(Node->getSubExpr());
677 OS << ")";
678}
679
680void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
681 OS << (Node->getValue() ? "true" : "false");
682}
683
Anders Carlsson55085182007-08-21 17:43:55 +0000684// Obj-C
685
686void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
687 OS << "@";
688 VisitStringLiteral(Node->getString());
689}
Reid Spencer5f016e22007-07-11 17:01:13 +0000690
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000691void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000692 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000693}
694
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000695void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000696 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000697}
698
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000699void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000700 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000701}
702
Steve Naroff563477d2007-09-18 23:55:05 +0000703void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
704 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000705 Expr *receiver = Mess->getReceiver();
706 if (receiver) PrintExpr(receiver);
707 else OS << Mess->getClassName()->getName();
708 Selector &selector = Mess->getSelector();
709 if (selector.isUnarySelector()) {
710 OS << " " << selector.getIdentifierInfoForSlot(0)->getName();
711 } else {
712 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Fariborz Jahanianf24d95a2007-10-16 20:52:13 +0000713 if (selector.getIdentifierInfoForSlot(i))
714 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
715 else
716 OS << ":";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000717 PrintExpr(Mess->getArg(i));
718 }
Steve Naroff563477d2007-09-18 23:55:05 +0000719 }
720 OS << "]";
721}
722
Reid Spencer5f016e22007-07-11 17:01:13 +0000723//===----------------------------------------------------------------------===//
724// Stmt method implementations
725//===----------------------------------------------------------------------===//
726
Chris Lattner6000dac2007-08-08 22:51:59 +0000727void Stmt::dumpPretty() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 // FIXME: eliminate use of <iostream>
Chris Lattner6000dac2007-08-08 22:51:59 +0000729 printPretty(std::cerr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000730}
731
Ted Kremenek42a509f2007-08-31 21:30:12 +0000732void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 if (this == 0) {
734 OS << "<NULL>";
735 return;
736 }
737
Ted Kremenek42a509f2007-08-31 21:30:12 +0000738 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000739 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +0000740}
Ted Kremenek42a509f2007-08-31 21:30:12 +0000741
742//===----------------------------------------------------------------------===//
743// PrinterHelper
744//===----------------------------------------------------------------------===//
745
746// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +0000747PrinterHelper::~PrinterHelper() {}