blob: 90c5ef2159809290973d6a82b0ca44fac36a9a21 [file] [log] [blame]
Chris Lattnera3bcb7a2006-11-04 07:16:25 +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 Lattnercbe4f772007-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.
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Chris Lattner5efbb332006-11-20 05:01:40 +000016#include "clang/AST/Decl.h"
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +000017#include "clang/AST/DeclObjc.h"
Chris Lattner29375652006-12-04 18:06:35 +000018#include "clang/AST/ExprCXX.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000020#include "clang/Basic/IdentifierTable.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000021#include "llvm/Support/Compiler.h"
22#include <iostream>
Chris Lattner6e9d9b32007-07-13 05:18:11 +000023#include <iomanip>
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// StmtPrinter Visitor
28//===----------------------------------------------------------------------===//
29
30namespace {
Chris Lattner62249a62007-08-21 04:04:25 +000031 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000032 std::ostream &OS;
33 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000034 clang::PrinterHelper* Helper;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000035 public:
Ted Kremenek04f3cee2007-08-31 21:30:12 +000036 StmtPrinter(std::ostream &os, PrinterHelper* helper) :
37 OS(os), IndentLevel(0), Helper(helper) {}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000038
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000039 void PrintStmt(Stmt *S, int SubIndent = 1) {
40 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000041 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000042 // If this is an expr used in a stmt context, indent and newline it.
43 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000044 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000045 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000046 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000047 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000048 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000049 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000050 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000051 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000052 }
Chris Lattner073926e2007-05-20 23:04:55 +000053
54 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000055 void PrintRawDecl(Decl *D);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000056 void PrintRawIfStmt(IfStmt *If);
57
Chris Lattner882f7882006-11-04 18:52:07 +000058 void PrintExpr(Expr *E) {
59 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000060 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000061 else
Chris Lattner882f7882006-11-04 18:52:07 +000062 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000063 }
64
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000065 std::ostream &Indent(int Delta = 0) const {
Chris Lattnerd5322cd2007-05-29 23:49:07 +000066 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000067 OS << " ";
68 return OS;
69 }
70
Chris Lattner98dbf0a2007-08-30 17:59:59 +000071 bool PrintOffsetOfDesignator(Expr *E);
72 void VisitUnaryOffsetOf(UnaryOperator *Node);
73
Ted Kremenek04f3cee2007-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 Lattner62249a62007-08-21 04:04:25 +000080 void VisitStmt(Stmt *Node);
Steve Naroff7f890eb2007-02-27 02:53:10 +000081#define STMT(N, CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000082 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000083#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000084 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000085}
86
Chris Lattner71e23ce2006-11-04 20:18:38 +000087//===----------------------------------------------------------------------===//
88// Stmt printing methods.
89//===----------------------------------------------------------------------===//
90
Chris Lattner882f7882006-11-04 18:52:07 +000091void StmtPrinter::VisitStmt(Stmt *Node) {
92 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000093}
94
Chris Lattner073926e2007-05-20 23:04:55 +000095/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
96/// with no newline after the }.
97void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
98 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000099 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000100 I != E; ++I)
101 PrintStmt(*I);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000102
Chris Lattner073926e2007-05-20 23:04:55 +0000103 Indent() << "}";
104}
105
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000106void StmtPrinter::PrintRawDecl(Decl *D) {
107 // FIXME: Need to complete/beautify this... this code simply shows the
Steve Naroff2a8ad182007-05-29 22:59:26 +0000108 // nodes are where they need to be.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000109 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)) {
Chris Lattnerb4522b42007-06-02 05:27:01 +0000113 // Emit storage class for vardecls.
114 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
115 switch (V->getStorageClass()) {
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000116 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;
Chris Lattnerb4522b42007-06-02 05:27:01 +0000122 }
123 }
124
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000125 std::string Name = VD->getName();
126 VD->getType().getAsStringInternal(Name);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000127 OS << Name;
128
Chris Lattner79c57592007-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 }
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000136 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000137 // FIXME: "struct x;"
138 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000139 }
140}
141
142
143void StmtPrinter::VisitNullStmt(NullStmt *Node) {
144 Indent() << ";\n";
145}
146
147void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Steve Naroffa23cc792007-09-13 23:52:58 +0000148 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattner776fac82007-06-09 00:53:06 +0000149 Indent();
150 PrintRawDecl(D);
151 OS << ";\n";
152 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000153}
154
Chris Lattner073926e2007-05-20 23:04:55 +0000155void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
156 Indent();
157 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000158 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000159}
160
Chris Lattner6c0ff132006-11-05 00:19:50 +0000161void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000162 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000163 PrintExpr(Node->getLHS());
164 if (Node->getRHS()) {
165 OS << " ... ";
166 PrintExpr(Node->getRHS());
167 }
168 OS << ":\n";
169
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000170 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000171}
172
173void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000174 Indent(-1) << "default:\n";
175 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000176}
177
178void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000179 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000180 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000181}
182
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000183void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
184 OS << "if ";
Chris Lattner882f7882006-11-04 18:52:07 +0000185 PrintExpr(If->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000186
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 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000196
Chris Lattner073926e2007-05-20 23:04:55 +0000197 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';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000204 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
205 OS << ' ';
206 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000207 } else {
208 OS << '\n';
209 PrintStmt(If->getElse());
210 }
Chris Lattner882f7882006-11-04 18:52:07 +0000211 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000212}
213
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000214void StmtPrinter::VisitIfStmt(IfStmt *If) {
215 Indent();
216 PrintRawIfStmt(If);
217}
218
Chris Lattnerf2174b62006-11-04 20:59:27 +0000219void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
220 Indent() << "switch (";
221 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000222 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 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000233}
234
Anders Carlsson51873c22007-07-22 07:07:56 +0000235void StmtPrinter::VisitSwitchCase(SwitchCase*) {
236 assert(0 && "SwitchCase is an abstract class");
237}
238
Chris Lattner85ed8732006-11-04 20:40:44 +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 Lattner5a4e9d22007-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 ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000258 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000259 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000260}
261
Chris Lattner71e23ce2006-11-04 20:18:38 +0000262void StmtPrinter::VisitForStmt(ForStmt *Node) {
263 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000264 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 Lattner5a4e9d22007-09-15 21:49:37 +0000270 OS << ";";
271 if (Node->getCond()) {
272 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000273 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000274 }
275 OS << ";";
276 if (Node->getInc()) {
277 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000278 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-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 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000289}
290
Chris Lattner16976d32006-11-05 01:46:01 +0000291void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000292 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000293}
294
295void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000296 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000297 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000298 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000299}
300
301void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000302 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000303}
304
305void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000306 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000307}
308
309
Chris Lattner882f7882006-11-04 18:52:07 +0000310void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
311 Indent() << "return";
312 if (Node->getRetValue()) {
313 OS << " ";
314 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000315 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000316 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000317}
318
Chris Lattner71e23ce2006-11-04 20:18:38 +0000319//===----------------------------------------------------------------------===//
320// Expr printing methods.
321//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000322
Chris Lattner882f7882006-11-04 18:52:07 +0000323void StmtPrinter::VisitExpr(Expr *Node) {
324 OS << "<<unknown expr type>>";
325}
326
327void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner5efbb332006-11-20 05:01:40 +0000328 OS << Node->getDecl()->getName();
Chris Lattner882f7882006-11-04 18:52:07 +0000329}
330
Anders Carlsson625bfc82007-07-21 05:21:51 +0000331void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
332 switch (Node->getIdentType()) {
333 default:
334 assert(0 && "unknown case");
335 case PreDefinedExpr::Func:
336 OS << "__func__";
337 break;
338 case PreDefinedExpr::Function:
339 OS << "__FUNCTION__";
340 break;
341 case PreDefinedExpr::PrettyFunction:
342 OS << "__PRETTY_FUNCTION__";
343 break;
344 }
345}
346
Steve Naroffae4143e2007-04-26 20:39:23 +0000347void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner666115c2007-07-13 23:58:20 +0000348 // FIXME should print an L for wchar_t constants
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000349 unsigned value = Node->getValue();
Chris Lattner666115c2007-07-13 23:58:20 +0000350 switch (value) {
351 case '\\':
352 OS << "'\\\\'";
353 break;
354 case '\'':
355 OS << "'\\''";
356 break;
357 case '\a':
358 // TODO: K&R: the meaning of '\\a' is different in traditional C
359 OS << "'\\a'";
360 break;
361 case '\b':
362 OS << "'\\b'";
363 break;
364 // Nonstandard escape sequence.
365 /*case '\e':
366 OS << "'\\e'";
367 break;*/
368 case '\f':
369 OS << "'\\f'";
370 break;
371 case '\n':
372 OS << "'\\n'";
373 break;
374 case '\r':
375 OS << "'\\r'";
376 break;
377 case '\t':
378 OS << "'\\t'";
379 break;
380 case '\v':
381 OS << "'\\v'";
382 break;
383 default:
384 if (isprint(value) && value < 256) {
385 OS << "'" << (char)value << "'";
386 } else if (value < 256) {
387 OS << "'\\x" << std::hex << value << std::dec << "'";
388 } else {
389 // FIXME what to really do here?
390 OS << value;
391 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000392 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000393}
394
Steve Naroffdf7855b2007-02-21 23:46:25 +0000395void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000396 bool isSigned = Node->getType()->isSignedIntegerType();
397 OS << Node->getValue().toString(10, isSigned);
398
399 // Emit suffixes. Integer literals are always a builtin integer type.
400 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
401 default: assert(0 && "Unexpected type for integer literal!");
402 case BuiltinType::Int: break; // no suffix.
403 case BuiltinType::UInt: OS << 'U'; break;
404 case BuiltinType::Long: OS << 'L'; break;
405 case BuiltinType::ULong: OS << "UL"; break;
406 case BuiltinType::LongLong: OS << "LL"; break;
407 case BuiltinType::ULongLong: OS << "ULL"; break;
408 }
Chris Lattner882f7882006-11-04 18:52:07 +0000409}
Steve Naroffab624882007-02-21 22:05:47 +0000410void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000411 // FIXME: print value more precisely.
Chris Lattner2dd003e2007-09-22 18:47:25 +0000412 OS << Node->getValueAsDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000413}
Chris Lattner1c20a172007-08-26 03:42:43 +0000414
415void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
416 PrintExpr(Node->getSubExpr());
417 OS << "i";
418}
419
Steve Naroffdf7855b2007-02-21 23:46:25 +0000420void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000421 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000422 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000423
Chris Lattner5d8f4942006-11-04 20:29:31 +0000424 // FIXME: this doesn't print wstrings right.
425 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
426 switch (Str->getStrData()[i]) {
427 default: OS << Str->getStrData()[i]; break;
428 // Handle some common ones to make dumps prettier.
429 case '\\': OS << "\\\\"; break;
430 case '"': OS << "\\\""; break;
431 case '\n': OS << "\\n"; break;
432 case '\t': OS << "\\t"; break;
433 case '\a': OS << "\\a"; break;
434 case '\b': OS << "\\b"; break;
435 }
436 }
437 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000438}
439void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
440 OS << "(";
441 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000442 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000443}
444void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000445 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000446 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000447
448 // Print a space if this is an "identifier operator" like sizeof or __real.
449 switch (Node->getOpcode()) {
450 default: break;
451 case UnaryOperator::SizeOf:
452 case UnaryOperator::AlignOf:
453 case UnaryOperator::Real:
454 case UnaryOperator::Imag:
455 case UnaryOperator::Extension:
456 OS << ' ';
457 break;
458 }
459 }
Chris Lattner882f7882006-11-04 18:52:07 +0000460 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000461
462 if (Node->isPostfix())
463 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000464}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000465
466bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
467 if (isa<CompoundLiteralExpr>(E)) {
468 // Base case, print the type and comma.
469 OS << E->getType().getAsString() << ", ";
470 return true;
471 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
472 PrintOffsetOfDesignator(ASE->getLHS());
473 OS << "[";
474 PrintExpr(ASE->getRHS());
475 OS << "]";
476 return false;
477 } else {
478 MemberExpr *ME = cast<MemberExpr>(E);
479 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
480 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
481 return false;
482 }
483}
484
485void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
486 OS << "__builtin_offsetof(";
487 PrintOffsetOfDesignator(Node->getSubExpr());
488 OS << ")";
489}
490
Chris Lattner882f7882006-11-04 18:52:07 +0000491void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner33e8a552006-11-19 01:48:02 +0000492 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
Chris Lattner3dc3d772007-05-16 18:07:12 +0000493 OS << Node->getArgumentType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000494}
495void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000496 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000497 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000498 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000499 OS << "]";
500}
501
502void StmtPrinter::VisitCallExpr(CallExpr *Call) {
503 PrintExpr(Call->getCallee());
504 OS << "(";
505 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
506 if (i) OS << ", ";
507 PrintExpr(Call->getArg(i));
508 }
509 OS << ")";
510}
511void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
512 PrintExpr(Node->getBase());
513 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000514
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000515 FieldDecl *Field = Node->getMemberDecl();
516 assert(Field && "MemberExpr should alway reference a field!");
517 OS << Field->getName();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000518}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000519void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000520 PrintExpr(Node->getBase());
521 OS << ".";
522 OS << Node->getAccessor().getName();
523}
Chris Lattner882f7882006-11-04 18:52:07 +0000524void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000525 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000526 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000527}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000528void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
529 OS << "(" << Node->getType().getAsString() << ")";
530 PrintExpr(Node->getInitializer());
531}
Steve Naroff7a5af782007-07-13 16:58:59 +0000532void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000533 // No need to print anything, simply forward to the sub expression.
534 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000535}
Chris Lattner882f7882006-11-04 18:52:07 +0000536void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
537 PrintExpr(Node->getLHS());
538 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
539 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000540}
Chris Lattner86928112007-08-25 02:00:02 +0000541void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
542 PrintExpr(Node->getLHS());
543 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
544 PrintExpr(Node->getRHS());
545}
Chris Lattner882f7882006-11-04 18:52:07 +0000546void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
547 PrintExpr(Node->getCond());
548 OS << " ? ";
549 PrintExpr(Node->getLHS());
Bill Wendling48fbdd02007-05-23 08:04:21 +0000550 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000551 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000552}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000553
Chris Lattnereefa10e2007-05-28 06:56:27 +0000554// GNU extensions.
555
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000556void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000557 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000558}
559
Chris Lattner366727f2007-07-24 16:58:17 +0000560void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
561 OS << "(";
562 PrintRawCompoundStmt(E->getSubStmt());
563 OS << ")";
564}
565
Steve Naroff78864672007-08-01 22:05:33 +0000566void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
567 OS << "__builtin_types_compatible_p(";
568 OS << Node->getArgType1().getAsString() << ",";
569 OS << Node->getArgType2().getAsString() << ")";
570}
571
Steve Naroff9efdabc2007-08-03 21:21:27 +0000572void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
573 OS << "__builtin_choose_expr(";
574 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000575 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000576 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000577 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000578 PrintExpr(Node->getRHS());
579 OS << ")";
580}
Chris Lattner366727f2007-07-24 16:58:17 +0000581
Anders Carlsson4692db02007-08-31 04:56:16 +0000582void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
583 OS << "{ ";
584 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
585 if (i) OS << ", ";
586 PrintExpr(Node->getInit(i));
587 }
588 OS << " }";
589}
590
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000591void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
592 OS << "va_arg(";
593 PrintExpr(Node->getSubExpr());
594 OS << ", ";
595 OS << Node->getType().getAsString();
596 OS << ")";
597}
598
Chris Lattnereefa10e2007-05-28 06:56:27 +0000599// C++
600
601void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner3f5e4682007-08-09 17:34:19 +0000602 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Chris Lattnereefa10e2007-05-28 06:56:27 +0000603 OS << Node->getDestType().getAsString() << ">(";
604 PrintExpr(Node->getSubExpr());
605 OS << ")";
606}
607
608void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
609 OS << (Node->getValue() ? "true" : "false");
610}
611
Anders Carlsson76f4a902007-08-21 17:43:55 +0000612// Obj-C
613
614void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
615 OS << "@";
616 VisitStringLiteral(Node->getString());
617}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000618
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000619void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
620 OS << "@encode(";
621 OS << Node->getEncodedType().getAsString() << ")";
622}
623
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000624void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
625 OS << "@selector(";
626 Selector &selector = Node->getSelector();
Fariborz Jahanian923aebe2007-10-16 21:07:53 +0000627 OS << " " << selector.getName();
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000628 OS << ")";
629}
630
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000631void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
632 OS << "@protocol(";
633 OS << Node->getProtocol()->getName();
634 OS << ")";
635}
636
Steve Naroffd54978b2007-09-18 23:55:05 +0000637void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
638 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +0000639 Expr *receiver = Mess->getReceiver();
640 if (receiver) PrintExpr(receiver);
641 else OS << Mess->getClassName()->getName();
642 Selector &selector = Mess->getSelector();
643 if (selector.isUnarySelector()) {
644 OS << " " << selector.getIdentifierInfoForSlot(0)->getName();
645 } else {
646 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Fariborz Jahanian81ccd882007-10-16 20:52:13 +0000647 if (selector.getIdentifierInfoForSlot(i))
648 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
649 else
650 OS << ":";
Steve Naroffc6814ea2007-10-02 20:01:56 +0000651 PrintExpr(Mess->getArg(i));
652 }
Steve Naroffd54978b2007-09-18 23:55:05 +0000653 }
654 OS << "]";
655}
656
657
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000658//===----------------------------------------------------------------------===//
659// Stmt method implementations
660//===----------------------------------------------------------------------===//
661
Chris Lattnercbe4f772007-08-08 22:51:59 +0000662void Stmt::dumpPretty() const {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000663 // FIXME: eliminate use of <iostream>
Chris Lattnercbe4f772007-08-08 22:51:59 +0000664 printPretty(std::cerr);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000665}
666
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000667void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Chris Lattner882f7882006-11-04 18:52:07 +0000668 if (this == 0) {
669 OS << "<NULL>";
670 return;
671 }
672
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000673 StmtPrinter P(OS, Helper);
Chris Lattner62249a62007-08-21 04:04:25 +0000674 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000675}
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000676
677//===----------------------------------------------------------------------===//
678// PrinterHelper
679//===----------------------------------------------------------------------===//
680
681// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +0000682PrinterHelper::~PrinterHelper() {}