blob: ad3091016c6a05901c59d9e0654e2cb036b4cd14 [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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 } else {
137 // FIXME: "struct x;"
138 assert(0 && "Unexpected decl");
139 }
140}
141
142
143void StmtPrinter::VisitNullStmt(NullStmt *Node) {
144 Indent() << ";\n";
145}
146
147void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Steve Naroff94745042007-09-13 23:52:58 +0000148 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 Indent();
150 PrintRawDecl(D);
151 OS << ";\n";
152 }
153}
154
155void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
156 Indent();
157 PrintRawCompoundStmt(Node);
158 OS << "\n";
159}
160
161void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
162 Indent(-1) << "case ";
163 PrintExpr(Node->getLHS());
164 if (Node->getRHS()) {
165 OS << " ... ";
166 PrintExpr(Node->getRHS());
167 }
168 OS << ":\n";
169
170 PrintStmt(Node->getSubStmt(), 0);
171}
172
173void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
174 Indent(-1) << "default:\n";
175 PrintStmt(Node->getSubStmt(), 0);
176}
177
178void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
179 Indent(-1) << Node->getName() << ":\n";
180 PrintStmt(Node->getSubStmt(), 0);
181}
182
183void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
184 OS << "if ";
185 PrintExpr(If->getCond());
186
187 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
188 OS << ' ';
189 PrintRawCompoundStmt(CS);
190 OS << (If->getElse() ? ' ' : '\n');
191 } else {
192 OS << '\n';
193 PrintStmt(If->getThen());
194 if (If->getElse()) Indent();
195 }
196
197 if (Stmt *Else = If->getElse()) {
198 OS << "else";
199
200 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
201 OS << ' ';
202 PrintRawCompoundStmt(CS);
203 OS << '\n';
204 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
205 OS << ' ';
206 PrintRawIfStmt(ElseIf);
207 } else {
208 OS << '\n';
209 PrintStmt(If->getElse());
210 }
211 }
212}
213
214void StmtPrinter::VisitIfStmt(IfStmt *If) {
215 Indent();
216 PrintRawIfStmt(If);
217}
218
219void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
220 Indent() << "switch (";
221 PrintExpr(Node->getCond());
222 OS << ")";
223
224 // Pretty print compoundstmt bodies (very common).
225 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
226 OS << " ";
227 PrintRawCompoundStmt(CS);
228 OS << "\n";
229 } else {
230 OS << "\n";
231 PrintStmt(Node->getBody());
232 }
233}
234
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000235void StmtPrinter::VisitSwitchCase(SwitchCase*) {
236 assert(0 && "SwitchCase is an abstract class");
237}
238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
240 Indent() << "while (";
241 PrintExpr(Node->getCond());
242 OS << ")\n";
243 PrintStmt(Node->getBody());
244}
245
246void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000247 Indent() << "do ";
248 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
249 PrintRawCompoundStmt(CS);
250 OS << " ";
251 } else {
252 OS << "\n";
253 PrintStmt(Node->getBody());
254 Indent();
255 }
256
257 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 PrintExpr(Node->getCond());
259 OS << ";\n";
260}
261
262void StmtPrinter::VisitForStmt(ForStmt *Node) {
263 Indent() << "for (";
264 if (Node->getInit()) {
265 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
266 PrintRawDecl(DS->getDecl());
267 else
268 PrintExpr(cast<Expr>(Node->getInit()));
269 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000270 OS << ";";
271 if (Node->getCond()) {
272 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000274 }
275 OS << ";";
276 if (Node->getInc()) {
277 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000279 }
280 OS << ") ";
281
282 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
283 PrintRawCompoundStmt(CS);
284 OS << "\n";
285 } else {
286 OS << "\n";
287 PrintStmt(Node->getBody());
288 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000289}
290
291void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
292 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
293}
294
295void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
296 Indent() << "goto *";
297 PrintExpr(Node->getTarget());
298 OS << ";\n";
299}
300
301void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
302 Indent() << "continue;\n";
303}
304
305void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
306 Indent() << "break;\n";
307}
308
309
310void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
311 Indent() << "return";
312 if (Node->getRetValue()) {
313 OS << " ";
314 PrintExpr(Node->getRetValue());
315 }
316 OS << ";\n";
317}
318
Chris Lattnerfe795952007-10-29 04:04:16 +0000319
320void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
321 Indent() << "asm (/*todo*/);\n";
322}
323
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000324void StmtPrinter::VisitObjcAtTryStmt(ObjcAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000325 Indent() << "@try";
326 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
327 PrintRawCompoundStmt(TS);
328 OS << "\n";
329 }
330
331 for (ObjcAtCatchStmt *catchStmt =
332 static_cast<ObjcAtCatchStmt *>(Node->getCatchStmts());
333 catchStmt;
334 catchStmt =
335 static_cast<ObjcAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
336 Indent() << "@catch(";
337 if (catchStmt->getCatchParamStmt()) {
338 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
339 PrintRawDecl(DS->getDecl());
340 }
341 OS << ")";
342 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
343 {
344 PrintRawCompoundStmt(CS);
345 OS << "\n";
346 }
347 }
348
349 Indent() << "@finally";
350 if (CompoundStmt *FS = dyn_cast<CompoundStmt>(
351 static_cast<ObjcAtFinallyStmt *>(
352 Node->getFinallyStmt())->getFinallyBody())) {
353 PrintRawCompoundStmt(FS);
354 OS << "\n";
355 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000356}
357
358void StmtPrinter::VisitObjcAtFinallyStmt(ObjcAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000359}
360
361void StmtPrinter::VisitObjcAtCatchStmt (ObjcAtCatchStmt *Node) {
362 Indent() << "@catch (...) { /* todo */ } \n";
363}
364
Reid Spencer5f016e22007-07-11 17:01:13 +0000365//===----------------------------------------------------------------------===//
366// Expr printing methods.
367//===----------------------------------------------------------------------===//
368
369void StmtPrinter::VisitExpr(Expr *Node) {
370 OS << "<<unknown expr type>>";
371}
372
373void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
374 OS << Node->getDecl()->getName();
375}
376
Anders Carlsson22742662007-07-21 05:21:51 +0000377void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
378 switch (Node->getIdentType()) {
379 default:
380 assert(0 && "unknown case");
381 case PreDefinedExpr::Func:
382 OS << "__func__";
383 break;
384 case PreDefinedExpr::Function:
385 OS << "__FUNCTION__";
386 break;
387 case PreDefinedExpr::PrettyFunction:
388 OS << "__PRETTY_FUNCTION__";
389 break;
390 }
391}
392
Reid Spencer5f016e22007-07-11 17:01:13 +0000393void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000394 // FIXME should print an L for wchar_t constants
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000395 unsigned value = Node->getValue();
Chris Lattner8bf9f072007-07-13 23:58:20 +0000396 switch (value) {
397 case '\\':
398 OS << "'\\\\'";
399 break;
400 case '\'':
401 OS << "'\\''";
402 break;
403 case '\a':
404 // TODO: K&R: the meaning of '\\a' is different in traditional C
405 OS << "'\\a'";
406 break;
407 case '\b':
408 OS << "'\\b'";
409 break;
410 // Nonstandard escape sequence.
411 /*case '\e':
412 OS << "'\\e'";
413 break;*/
414 case '\f':
415 OS << "'\\f'";
416 break;
417 case '\n':
418 OS << "'\\n'";
419 break;
420 case '\r':
421 OS << "'\\r'";
422 break;
423 case '\t':
424 OS << "'\\t'";
425 break;
426 case '\v':
427 OS << "'\\v'";
428 break;
429 default:
430 if (isprint(value) && value < 256) {
431 OS << "'" << (char)value << "'";
432 } else if (value < 256) {
433 OS << "'\\x" << std::hex << value << std::dec << "'";
434 } else {
435 // FIXME what to really do here?
436 OS << value;
437 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000438 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000439}
440
441void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
442 bool isSigned = Node->getType()->isSignedIntegerType();
443 OS << Node->getValue().toString(10, isSigned);
444
445 // Emit suffixes. Integer literals are always a builtin integer type.
446 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
447 default: assert(0 && "Unexpected type for integer literal!");
448 case BuiltinType::Int: break; // no suffix.
449 case BuiltinType::UInt: OS << 'U'; break;
450 case BuiltinType::Long: OS << 'L'; break;
451 case BuiltinType::ULong: OS << "UL"; break;
452 case BuiltinType::LongLong: OS << "LL"; break;
453 case BuiltinType::ULongLong: OS << "ULL"; break;
454 }
455}
456void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000457 // FIXME: print value more precisely.
Chris Lattnerc9bec4b2007-09-22 18:47:25 +0000458 OS << Node->getValueAsDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000459}
Chris Lattner5d661452007-08-26 03:42:43 +0000460
461void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
462 PrintExpr(Node->getSubExpr());
463 OS << "i";
464}
465
Reid Spencer5f016e22007-07-11 17:01:13 +0000466void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
467 if (Str->isWide()) OS << 'L';
468 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000469
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 // FIXME: this doesn't print wstrings right.
471 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
472 switch (Str->getStrData()[i]) {
473 default: OS << Str->getStrData()[i]; break;
474 // Handle some common ones to make dumps prettier.
475 case '\\': OS << "\\\\"; break;
476 case '"': OS << "\\\""; break;
477 case '\n': OS << "\\n"; break;
478 case '\t': OS << "\\t"; break;
479 case '\a': OS << "\\a"; break;
480 case '\b': OS << "\\b"; break;
481 }
482 }
483 OS << '"';
484}
485void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
486 OS << "(";
487 PrintExpr(Node->getSubExpr());
488 OS << ")";
489}
490void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000491 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000493
494 // Print a space if this is an "identifier operator" like sizeof or __real.
495 switch (Node->getOpcode()) {
496 default: break;
497 case UnaryOperator::SizeOf:
498 case UnaryOperator::AlignOf:
499 case UnaryOperator::Real:
500 case UnaryOperator::Imag:
501 case UnaryOperator::Extension:
502 OS << ' ';
503 break;
504 }
505 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 PrintExpr(Node->getSubExpr());
507
508 if (Node->isPostfix())
509 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000510}
Chris Lattner704fe352007-08-30 17:59:59 +0000511
512bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
513 if (isa<CompoundLiteralExpr>(E)) {
514 // Base case, print the type and comma.
515 OS << E->getType().getAsString() << ", ";
516 return true;
517 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
518 PrintOffsetOfDesignator(ASE->getLHS());
519 OS << "[";
520 PrintExpr(ASE->getRHS());
521 OS << "]";
522 return false;
523 } else {
524 MemberExpr *ME = cast<MemberExpr>(E);
525 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
526 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
527 return false;
528 }
529}
530
531void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
532 OS << "__builtin_offsetof(";
533 PrintOffsetOfDesignator(Node->getSubExpr());
534 OS << ")";
535}
536
Reid Spencer5f016e22007-07-11 17:01:13 +0000537void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
538 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
539 OS << Node->getArgumentType().getAsString() << ")";
540}
541void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000542 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000544 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 OS << "]";
546}
547
548void StmtPrinter::VisitCallExpr(CallExpr *Call) {
549 PrintExpr(Call->getCallee());
550 OS << "(";
551 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
552 if (i) OS << ", ";
553 PrintExpr(Call->getArg(i));
554 }
555 OS << ")";
556}
557void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
558 PrintExpr(Node->getBase());
559 OS << (Node->isArrow() ? "->" : ".");
560
561 FieldDecl *Field = Node->getMemberDecl();
562 assert(Field && "MemberExpr should alway reference a field!");
563 OS << Field->getName();
564}
Chris Lattner6481a572007-08-03 17:31:20 +0000565void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000566 PrintExpr(Node->getBase());
567 OS << ".";
568 OS << Node->getAccessor().getName();
569}
Reid Spencer5f016e22007-07-11 17:01:13 +0000570void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000571 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 PrintExpr(Node->getSubExpr());
573}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000574void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
575 OS << "(" << Node->getType().getAsString() << ")";
576 PrintExpr(Node->getInitializer());
577}
Steve Naroff49b45262007-07-13 16:58:59 +0000578void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000579 // No need to print anything, simply forward to the sub expression.
580 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000581}
Reid Spencer5f016e22007-07-11 17:01:13 +0000582void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
583 PrintExpr(Node->getLHS());
584 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
585 PrintExpr(Node->getRHS());
586}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000587void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
588 PrintExpr(Node->getLHS());
589 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
590 PrintExpr(Node->getRHS());
591}
Reid Spencer5f016e22007-07-11 17:01:13 +0000592void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
593 PrintExpr(Node->getCond());
594 OS << " ? ";
595 PrintExpr(Node->getLHS());
596 OS << " : ";
597 PrintExpr(Node->getRHS());
598}
599
600// GNU extensions.
601
Chris Lattner6481a572007-08-03 17:31:20 +0000602void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000604}
605
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000606void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
607 OS << "(";
608 PrintRawCompoundStmt(E->getSubStmt());
609 OS << ")";
610}
611
Steve Naroffd34e9152007-08-01 22:05:33 +0000612void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
613 OS << "__builtin_types_compatible_p(";
614 OS << Node->getArgType1().getAsString() << ",";
615 OS << Node->getArgType2().getAsString() << ")";
616}
617
Steve Naroffd04fdd52007-08-03 21:21:27 +0000618void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
619 OS << "__builtin_choose_expr(";
620 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000621 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000622 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000623 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000624 PrintExpr(Node->getRHS());
625 OS << ")";
626}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000627
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000628void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
629 OS << "{ ";
630 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
631 if (i) OS << ", ";
632 PrintExpr(Node->getInit(i));
633 }
634 OS << " }";
635}
636
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000637void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
638 OS << "va_arg(";
639 PrintExpr(Node->getSubExpr());
640 OS << ", ";
641 OS << Node->getType().getAsString();
642 OS << ")";
643}
644
Reid Spencer5f016e22007-07-11 17:01:13 +0000645// C++
646
647void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner36460ee2007-08-09 17:34:19 +0000648 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 OS << Node->getDestType().getAsString() << ">(";
650 PrintExpr(Node->getSubExpr());
651 OS << ")";
652}
653
654void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
655 OS << (Node->getValue() ? "true" : "false");
656}
657
Anders Carlsson55085182007-08-21 17:43:55 +0000658// Obj-C
659
660void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
661 OS << "@";
662 VisitStringLiteral(Node->getString());
663}
Reid Spencer5f016e22007-07-11 17:01:13 +0000664
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000665void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000666 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000667}
668
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000669void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000670 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000671}
672
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000673void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000674 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000675}
676
Steve Naroff563477d2007-09-18 23:55:05 +0000677void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
678 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000679 Expr *receiver = Mess->getReceiver();
680 if (receiver) PrintExpr(receiver);
681 else OS << Mess->getClassName()->getName();
682 Selector &selector = Mess->getSelector();
683 if (selector.isUnarySelector()) {
684 OS << " " << selector.getIdentifierInfoForSlot(0)->getName();
685 } else {
686 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Fariborz Jahanianf24d95a2007-10-16 20:52:13 +0000687 if (selector.getIdentifierInfoForSlot(i))
688 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
689 else
690 OS << ":";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000691 PrintExpr(Mess->getArg(i));
692 }
Steve Naroff563477d2007-09-18 23:55:05 +0000693 }
694 OS << "]";
695}
696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697//===----------------------------------------------------------------------===//
698// Stmt method implementations
699//===----------------------------------------------------------------------===//
700
Chris Lattner6000dac2007-08-08 22:51:59 +0000701void Stmt::dumpPretty() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 // FIXME: eliminate use of <iostream>
Chris Lattner6000dac2007-08-08 22:51:59 +0000703 printPretty(std::cerr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704}
705
Ted Kremenek42a509f2007-08-31 21:30:12 +0000706void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 if (this == 0) {
708 OS << "<NULL>";
709 return;
710 }
711
Ted Kremenek42a509f2007-08-31 21:30:12 +0000712 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000713 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +0000714}
Ted Kremenek42a509f2007-08-31 21:30:12 +0000715
716//===----------------------------------------------------------------------===//
717// PrinterHelper
718//===----------------------------------------------------------------------===//
719
720// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +0000721PrinterHelper::~PrinterHelper() {}