blob: fa3eb84b529700d86cac11eb413e0c43807e1692 [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) {
329 Indent() << "asm (/*todo*/);\n";
330}
331
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000332void StmtPrinter::VisitObjcAtTryStmt(ObjcAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000333 Indent() << "@try";
334 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
335 PrintRawCompoundStmt(TS);
336 OS << "\n";
337 }
338
339 for (ObjcAtCatchStmt *catchStmt =
340 static_cast<ObjcAtCatchStmt *>(Node->getCatchStmts());
341 catchStmt;
342 catchStmt =
343 static_cast<ObjcAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
344 Indent() << "@catch(";
345 if (catchStmt->getCatchParamStmt()) {
346 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
347 PrintRawDecl(DS->getDecl());
348 }
349 OS << ")";
350 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
351 {
352 PrintRawCompoundStmt(CS);
353 OS << "\n";
354 }
355 }
356
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000357 if (ObjcAtFinallyStmt *FS =static_cast<ObjcAtFinallyStmt *>(
358 Node->getFinallyStmt())) {
359 Indent() << "@finally";
360 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000361 OS << "\n";
362 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000363}
364
365void StmtPrinter::VisitObjcAtFinallyStmt(ObjcAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000366}
367
368void StmtPrinter::VisitObjcAtCatchStmt (ObjcAtCatchStmt *Node) {
369 Indent() << "@catch (...) { /* todo */ } \n";
370}
371
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000372void StmtPrinter::VisitObjcAtThrowStmt (ObjcAtThrowStmt *Node) {
373 Indent() << "@throw";
374 if (Node->getThrowExpr()) {
375 OS << " ";
376 PrintExpr(Node->getThrowExpr());
377 }
378 OS << ";\n";
379}
380
Reid Spencer5f016e22007-07-11 17:01:13 +0000381//===----------------------------------------------------------------------===//
382// Expr printing methods.
383//===----------------------------------------------------------------------===//
384
385void StmtPrinter::VisitExpr(Expr *Node) {
386 OS << "<<unknown expr type>>";
387}
388
389void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
390 OS << Node->getDecl()->getName();
391}
392
Steve Naroff7779db42007-11-12 14:29:37 +0000393void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000394 if (Node->getBase()) {
395 PrintExpr(Node->getBase());
396 OS << (Node->isArrow() ? "->" : ".");
397 }
Steve Naroff7779db42007-11-12 14:29:37 +0000398 OS << Node->getDecl()->getName();
399}
400
Anders Carlsson22742662007-07-21 05:21:51 +0000401void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
402 switch (Node->getIdentType()) {
403 default:
404 assert(0 && "unknown case");
405 case PreDefinedExpr::Func:
406 OS << "__func__";
407 break;
408 case PreDefinedExpr::Function:
409 OS << "__FUNCTION__";
410 break;
411 case PreDefinedExpr::PrettyFunction:
412 OS << "__PRETTY_FUNCTION__";
413 break;
414 }
415}
416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000418 // FIXME should print an L for wchar_t constants
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000419 unsigned value = Node->getValue();
Chris Lattner8bf9f072007-07-13 23:58:20 +0000420 switch (value) {
421 case '\\':
422 OS << "'\\\\'";
423 break;
424 case '\'':
425 OS << "'\\''";
426 break;
427 case '\a':
428 // TODO: K&R: the meaning of '\\a' is different in traditional C
429 OS << "'\\a'";
430 break;
431 case '\b':
432 OS << "'\\b'";
433 break;
434 // Nonstandard escape sequence.
435 /*case '\e':
436 OS << "'\\e'";
437 break;*/
438 case '\f':
439 OS << "'\\f'";
440 break;
441 case '\n':
442 OS << "'\\n'";
443 break;
444 case '\r':
445 OS << "'\\r'";
446 break;
447 case '\t':
448 OS << "'\\t'";
449 break;
450 case '\v':
451 OS << "'\\v'";
452 break;
453 default:
454 if (isprint(value) && value < 256) {
455 OS << "'" << (char)value << "'";
456 } else if (value < 256) {
457 OS << "'\\x" << std::hex << value << std::dec << "'";
458 } else {
459 // FIXME what to really do here?
460 OS << value;
461 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000462 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000463}
464
465void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
466 bool isSigned = Node->getType()->isSignedIntegerType();
467 OS << Node->getValue().toString(10, isSigned);
468
469 // Emit suffixes. Integer literals are always a builtin integer type.
470 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
471 default: assert(0 && "Unexpected type for integer literal!");
472 case BuiltinType::Int: break; // no suffix.
473 case BuiltinType::UInt: OS << 'U'; break;
474 case BuiltinType::Long: OS << 'L'; break;
475 case BuiltinType::ULong: OS << "UL"; break;
476 case BuiltinType::LongLong: OS << "LL"; break;
477 case BuiltinType::ULongLong: OS << "ULL"; break;
478 }
479}
480void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000481 // FIXME: print value more precisely.
Chris Lattnerc9bec4b2007-09-22 18:47:25 +0000482 OS << Node->getValueAsDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000483}
Chris Lattner5d661452007-08-26 03:42:43 +0000484
485void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
486 PrintExpr(Node->getSubExpr());
487 OS << "i";
488}
489
Reid Spencer5f016e22007-07-11 17:01:13 +0000490void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
491 if (Str->isWide()) OS << 'L';
492 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000493
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 // FIXME: this doesn't print wstrings right.
495 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
496 switch (Str->getStrData()[i]) {
497 default: OS << Str->getStrData()[i]; break;
498 // Handle some common ones to make dumps prettier.
499 case '\\': OS << "\\\\"; break;
500 case '"': OS << "\\\""; break;
501 case '\n': OS << "\\n"; break;
502 case '\t': OS << "\\t"; break;
503 case '\a': OS << "\\a"; break;
504 case '\b': OS << "\\b"; break;
505 }
506 }
507 OS << '"';
508}
509void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
510 OS << "(";
511 PrintExpr(Node->getSubExpr());
512 OS << ")";
513}
514void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000515 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000517
518 // Print a space if this is an "identifier operator" like sizeof or __real.
519 switch (Node->getOpcode()) {
520 default: break;
521 case UnaryOperator::SizeOf:
522 case UnaryOperator::AlignOf:
523 case UnaryOperator::Real:
524 case UnaryOperator::Imag:
525 case UnaryOperator::Extension:
526 OS << ' ';
527 break;
528 }
529 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 PrintExpr(Node->getSubExpr());
531
532 if (Node->isPostfix())
533 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000534}
Chris Lattner704fe352007-08-30 17:59:59 +0000535
536bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
537 if (isa<CompoundLiteralExpr>(E)) {
538 // Base case, print the type and comma.
539 OS << E->getType().getAsString() << ", ";
540 return true;
541 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
542 PrintOffsetOfDesignator(ASE->getLHS());
543 OS << "[";
544 PrintExpr(ASE->getRHS());
545 OS << "]";
546 return false;
547 } else {
548 MemberExpr *ME = cast<MemberExpr>(E);
549 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
550 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
551 return false;
552 }
553}
554
555void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
556 OS << "__builtin_offsetof(";
557 PrintOffsetOfDesignator(Node->getSubExpr());
558 OS << ")";
559}
560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
562 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
563 OS << Node->getArgumentType().getAsString() << ")";
564}
565void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000566 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000568 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 OS << "]";
570}
571
572void StmtPrinter::VisitCallExpr(CallExpr *Call) {
573 PrintExpr(Call->getCallee());
574 OS << "(";
575 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
576 if (i) OS << ", ";
577 PrintExpr(Call->getArg(i));
578 }
579 OS << ")";
580}
581void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
582 PrintExpr(Node->getBase());
583 OS << (Node->isArrow() ? "->" : ".");
584
585 FieldDecl *Field = Node->getMemberDecl();
586 assert(Field && "MemberExpr should alway reference a field!");
587 OS << Field->getName();
588}
Chris Lattner6481a572007-08-03 17:31:20 +0000589void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000590 PrintExpr(Node->getBase());
591 OS << ".";
592 OS << Node->getAccessor().getName();
593}
Reid Spencer5f016e22007-07-11 17:01:13 +0000594void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000595 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 PrintExpr(Node->getSubExpr());
597}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000598void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
599 OS << "(" << Node->getType().getAsString() << ")";
600 PrintExpr(Node->getInitializer());
601}
Steve Naroff49b45262007-07-13 16:58:59 +0000602void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000603 // No need to print anything, simply forward to the sub expression.
604 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000605}
Reid Spencer5f016e22007-07-11 17:01:13 +0000606void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
607 PrintExpr(Node->getLHS());
608 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
609 PrintExpr(Node->getRHS());
610}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000611void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
612 PrintExpr(Node->getLHS());
613 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
614 PrintExpr(Node->getRHS());
615}
Reid Spencer5f016e22007-07-11 17:01:13 +0000616void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
617 PrintExpr(Node->getCond());
618 OS << " ? ";
619 PrintExpr(Node->getLHS());
620 OS << " : ";
621 PrintExpr(Node->getRHS());
622}
623
624// GNU extensions.
625
Chris Lattner6481a572007-08-03 17:31:20 +0000626void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000628}
629
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000630void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
631 OS << "(";
632 PrintRawCompoundStmt(E->getSubStmt());
633 OS << ")";
634}
635
Steve Naroffd34e9152007-08-01 22:05:33 +0000636void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
637 OS << "__builtin_types_compatible_p(";
638 OS << Node->getArgType1().getAsString() << ",";
639 OS << Node->getArgType2().getAsString() << ")";
640}
641
Steve Naroffd04fdd52007-08-03 21:21:27 +0000642void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
643 OS << "__builtin_choose_expr(";
644 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000645 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000646 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000647 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000648 PrintExpr(Node->getRHS());
649 OS << ")";
650}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000651
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000652void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
653 OS << "{ ";
654 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
655 if (i) OS << ", ";
656 PrintExpr(Node->getInit(i));
657 }
658 OS << " }";
659}
660
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000661void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
662 OS << "va_arg(";
663 PrintExpr(Node->getSubExpr());
664 OS << ", ";
665 OS << Node->getType().getAsString();
666 OS << ")";
667}
668
Reid Spencer5f016e22007-07-11 17:01:13 +0000669// C++
670
671void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner36460ee2007-08-09 17:34:19 +0000672 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 OS << Node->getDestType().getAsString() << ">(";
674 PrintExpr(Node->getSubExpr());
675 OS << ")";
676}
677
678void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
679 OS << (Node->getValue() ? "true" : "false");
680}
681
Anders Carlsson55085182007-08-21 17:43:55 +0000682// Obj-C
683
684void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
685 OS << "@";
686 VisitStringLiteral(Node->getString());
687}
Reid Spencer5f016e22007-07-11 17:01:13 +0000688
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000689void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000690 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000691}
692
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000693void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000694 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000695}
696
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000697void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000698 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000699}
700
Steve Naroff563477d2007-09-18 23:55:05 +0000701void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
702 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000703 Expr *receiver = Mess->getReceiver();
704 if (receiver) PrintExpr(receiver);
705 else OS << Mess->getClassName()->getName();
706 Selector &selector = Mess->getSelector();
707 if (selector.isUnarySelector()) {
708 OS << " " << selector.getIdentifierInfoForSlot(0)->getName();
709 } else {
710 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Fariborz Jahanianf24d95a2007-10-16 20:52:13 +0000711 if (selector.getIdentifierInfoForSlot(i))
712 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
713 else
714 OS << ":";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000715 PrintExpr(Mess->getArg(i));
716 }
Steve Naroff563477d2007-09-18 23:55:05 +0000717 }
718 OS << "]";
719}
720
Reid Spencer5f016e22007-07-11 17:01:13 +0000721//===----------------------------------------------------------------------===//
722// Stmt method implementations
723//===----------------------------------------------------------------------===//
724
Chris Lattner6000dac2007-08-08 22:51:59 +0000725void Stmt::dumpPretty() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 // FIXME: eliminate use of <iostream>
Chris Lattner6000dac2007-08-08 22:51:59 +0000727 printPretty(std::cerr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000728}
729
Ted Kremenek42a509f2007-08-31 21:30:12 +0000730void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 if (this == 0) {
732 OS << "<NULL>";
733 return;
734 }
735
Ted Kremenek42a509f2007-08-31 21:30:12 +0000736 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000737 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +0000738}
Ted Kremenek42a509f2007-08-31 21:30:12 +0000739
740//===----------------------------------------------------------------------===//
741// PrinterHelper
742//===----------------------------------------------------------------------===//
743
744// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +0000745PrinterHelper::~PrinterHelper() {}