blob: d42a16640f28681d6305b3ec2e0d25ea1e5020a3 [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"
Ted Kremenek5c84c012007-10-17 18:36:42 +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 Lattner73c56c02007-10-29 04:04:16 +0000319
320void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
321 Indent() << "asm (/*todo*/);\n";
322}
323
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000324void StmtPrinter::VisitObjcAtTryStmt(ObjcAtTryStmt *Node) {
Fariborz Jahanian88157952007-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
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000349 if (ObjcAtFinallyStmt *FS =static_cast<ObjcAtFinallyStmt *>(
350 Node->getFinallyStmt())) {
351 Indent() << "@finally";
352 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000353 OS << "\n";
354 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000355}
356
357void StmtPrinter::VisitObjcAtFinallyStmt(ObjcAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000358}
359
360void StmtPrinter::VisitObjcAtCatchStmt (ObjcAtCatchStmt *Node) {
361 Indent() << "@catch (...) { /* todo */ } \n";
362}
363
Chris Lattner71e23ce2006-11-04 20:18:38 +0000364//===----------------------------------------------------------------------===//
365// Expr printing methods.
366//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000367
Chris Lattner882f7882006-11-04 18:52:07 +0000368void StmtPrinter::VisitExpr(Expr *Node) {
369 OS << "<<unknown expr type>>";
370}
371
372void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner5efbb332006-11-20 05:01:40 +0000373 OS << Node->getDecl()->getName();
Chris Lattner882f7882006-11-04 18:52:07 +0000374}
375
Anders Carlsson625bfc82007-07-21 05:21:51 +0000376void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
377 switch (Node->getIdentType()) {
378 default:
379 assert(0 && "unknown case");
380 case PreDefinedExpr::Func:
381 OS << "__func__";
382 break;
383 case PreDefinedExpr::Function:
384 OS << "__FUNCTION__";
385 break;
386 case PreDefinedExpr::PrettyFunction:
387 OS << "__PRETTY_FUNCTION__";
388 break;
389 }
390}
391
Steve Naroffae4143e2007-04-26 20:39:23 +0000392void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner666115c2007-07-13 23:58:20 +0000393 // FIXME should print an L for wchar_t constants
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000394 unsigned value = Node->getValue();
Chris Lattner666115c2007-07-13 23:58:20 +0000395 switch (value) {
396 case '\\':
397 OS << "'\\\\'";
398 break;
399 case '\'':
400 OS << "'\\''";
401 break;
402 case '\a':
403 // TODO: K&R: the meaning of '\\a' is different in traditional C
404 OS << "'\\a'";
405 break;
406 case '\b':
407 OS << "'\\b'";
408 break;
409 // Nonstandard escape sequence.
410 /*case '\e':
411 OS << "'\\e'";
412 break;*/
413 case '\f':
414 OS << "'\\f'";
415 break;
416 case '\n':
417 OS << "'\\n'";
418 break;
419 case '\r':
420 OS << "'\\r'";
421 break;
422 case '\t':
423 OS << "'\\t'";
424 break;
425 case '\v':
426 OS << "'\\v'";
427 break;
428 default:
429 if (isprint(value) && value < 256) {
430 OS << "'" << (char)value << "'";
431 } else if (value < 256) {
432 OS << "'\\x" << std::hex << value << std::dec << "'";
433 } else {
434 // FIXME what to really do here?
435 OS << value;
436 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000437 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000438}
439
Steve Naroffdf7855b2007-02-21 23:46:25 +0000440void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000441 bool isSigned = Node->getType()->isSignedIntegerType();
442 OS << Node->getValue().toString(10, isSigned);
443
444 // Emit suffixes. Integer literals are always a builtin integer type.
445 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
446 default: assert(0 && "Unexpected type for integer literal!");
447 case BuiltinType::Int: break; // no suffix.
448 case BuiltinType::UInt: OS << 'U'; break;
449 case BuiltinType::Long: OS << 'L'; break;
450 case BuiltinType::ULong: OS << "UL"; break;
451 case BuiltinType::LongLong: OS << "LL"; break;
452 case BuiltinType::ULongLong: OS << "ULL"; break;
453 }
Chris Lattner882f7882006-11-04 18:52:07 +0000454}
Steve Naroffab624882007-02-21 22:05:47 +0000455void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000456 // FIXME: print value more precisely.
Chris Lattner2dd003e2007-09-22 18:47:25 +0000457 OS << Node->getValueAsDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000458}
Chris Lattner1c20a172007-08-26 03:42:43 +0000459
460void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
461 PrintExpr(Node->getSubExpr());
462 OS << "i";
463}
464
Steve Naroffdf7855b2007-02-21 23:46:25 +0000465void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000466 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000467 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000468
Chris Lattner5d8f4942006-11-04 20:29:31 +0000469 // FIXME: this doesn't print wstrings right.
470 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
471 switch (Str->getStrData()[i]) {
472 default: OS << Str->getStrData()[i]; break;
473 // Handle some common ones to make dumps prettier.
474 case '\\': OS << "\\\\"; break;
475 case '"': OS << "\\\""; break;
476 case '\n': OS << "\\n"; break;
477 case '\t': OS << "\\t"; break;
478 case '\a': OS << "\\a"; break;
479 case '\b': OS << "\\b"; break;
480 }
481 }
482 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000483}
484void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
485 OS << "(";
486 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000487 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000488}
489void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000490 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000491 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000492
493 // Print a space if this is an "identifier operator" like sizeof or __real.
494 switch (Node->getOpcode()) {
495 default: break;
496 case UnaryOperator::SizeOf:
497 case UnaryOperator::AlignOf:
498 case UnaryOperator::Real:
499 case UnaryOperator::Imag:
500 case UnaryOperator::Extension:
501 OS << ' ';
502 break;
503 }
504 }
Chris Lattner882f7882006-11-04 18:52:07 +0000505 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000506
507 if (Node->isPostfix())
508 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000509}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000510
511bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
512 if (isa<CompoundLiteralExpr>(E)) {
513 // Base case, print the type and comma.
514 OS << E->getType().getAsString() << ", ";
515 return true;
516 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
517 PrintOffsetOfDesignator(ASE->getLHS());
518 OS << "[";
519 PrintExpr(ASE->getRHS());
520 OS << "]";
521 return false;
522 } else {
523 MemberExpr *ME = cast<MemberExpr>(E);
524 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
525 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
526 return false;
527 }
528}
529
530void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
531 OS << "__builtin_offsetof(";
532 PrintOffsetOfDesignator(Node->getSubExpr());
533 OS << ")";
534}
535
Chris Lattner882f7882006-11-04 18:52:07 +0000536void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner33e8a552006-11-19 01:48:02 +0000537 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
Chris Lattner3dc3d772007-05-16 18:07:12 +0000538 OS << Node->getArgumentType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000539}
540void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000541 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000542 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000543 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000544 OS << "]";
545}
546
547void StmtPrinter::VisitCallExpr(CallExpr *Call) {
548 PrintExpr(Call->getCallee());
549 OS << "(";
550 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
551 if (i) OS << ", ";
552 PrintExpr(Call->getArg(i));
553 }
554 OS << ")";
555}
556void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
557 PrintExpr(Node->getBase());
558 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000559
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000560 FieldDecl *Field = Node->getMemberDecl();
561 assert(Field && "MemberExpr should alway reference a field!");
562 OS << Field->getName();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000563}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000564void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000565 PrintExpr(Node->getBase());
566 OS << ".";
567 OS << Node->getAccessor().getName();
568}
Chris Lattner882f7882006-11-04 18:52:07 +0000569void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000570 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000571 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000572}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000573void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
574 OS << "(" << Node->getType().getAsString() << ")";
575 PrintExpr(Node->getInitializer());
576}
Steve Naroff7a5af782007-07-13 16:58:59 +0000577void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000578 // No need to print anything, simply forward to the sub expression.
579 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000580}
Chris Lattner882f7882006-11-04 18:52:07 +0000581void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
582 PrintExpr(Node->getLHS());
583 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
584 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000585}
Chris Lattner86928112007-08-25 02:00:02 +0000586void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
587 PrintExpr(Node->getLHS());
588 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
589 PrintExpr(Node->getRHS());
590}
Chris Lattner882f7882006-11-04 18:52:07 +0000591void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
592 PrintExpr(Node->getCond());
593 OS << " ? ";
594 PrintExpr(Node->getLHS());
Bill Wendling48fbdd02007-05-23 08:04:21 +0000595 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000596 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000597}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000598
Chris Lattnereefa10e2007-05-28 06:56:27 +0000599// GNU extensions.
600
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000601void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000602 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000603}
604
Chris Lattner366727f2007-07-24 16:58:17 +0000605void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
606 OS << "(";
607 PrintRawCompoundStmt(E->getSubStmt());
608 OS << ")";
609}
610
Steve Naroff78864672007-08-01 22:05:33 +0000611void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
612 OS << "__builtin_types_compatible_p(";
613 OS << Node->getArgType1().getAsString() << ",";
614 OS << Node->getArgType2().getAsString() << ")";
615}
616
Steve Naroff9efdabc2007-08-03 21:21:27 +0000617void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
618 OS << "__builtin_choose_expr(";
619 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000620 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000621 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000622 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000623 PrintExpr(Node->getRHS());
624 OS << ")";
625}
Chris Lattner366727f2007-07-24 16:58:17 +0000626
Anders Carlsson4692db02007-08-31 04:56:16 +0000627void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
628 OS << "{ ";
629 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
630 if (i) OS << ", ";
631 PrintExpr(Node->getInit(i));
632 }
633 OS << " }";
634}
635
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000636void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
637 OS << "va_arg(";
638 PrintExpr(Node->getSubExpr());
639 OS << ", ";
640 OS << Node->getType().getAsString();
641 OS << ")";
642}
643
Chris Lattnereefa10e2007-05-28 06:56:27 +0000644// C++
645
646void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner3f5e4682007-08-09 17:34:19 +0000647 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Chris Lattnereefa10e2007-05-28 06:56:27 +0000648 OS << Node->getDestType().getAsString() << ">(";
649 PrintExpr(Node->getSubExpr());
650 OS << ")";
651}
652
653void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
654 OS << (Node->getValue() ? "true" : "false");
655}
656
Anders Carlsson76f4a902007-08-21 17:43:55 +0000657// Obj-C
658
659void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
660 OS << "@";
661 VisitStringLiteral(Node->getString());
662}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000663
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000664void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattnerc7f39812007-10-18 00:39:29 +0000665 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000666}
667
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000668void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattnerc7f39812007-10-18 00:39:29 +0000669 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000670}
671
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000672void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattnerc7f39812007-10-18 00:39:29 +0000673 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000674}
675
Steve Naroffd54978b2007-09-18 23:55:05 +0000676void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
677 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +0000678 Expr *receiver = Mess->getReceiver();
679 if (receiver) PrintExpr(receiver);
680 else OS << Mess->getClassName()->getName();
681 Selector &selector = Mess->getSelector();
682 if (selector.isUnarySelector()) {
683 OS << " " << selector.getIdentifierInfoForSlot(0)->getName();
684 } else {
685 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Fariborz Jahanian81ccd882007-10-16 20:52:13 +0000686 if (selector.getIdentifierInfoForSlot(i))
687 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
688 else
689 OS << ":";
Steve Naroffc6814ea2007-10-02 20:01:56 +0000690 PrintExpr(Mess->getArg(i));
691 }
Steve Naroffd54978b2007-09-18 23:55:05 +0000692 }
693 OS << "]";
694}
695
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000696//===----------------------------------------------------------------------===//
697// Stmt method implementations
698//===----------------------------------------------------------------------===//
699
Chris Lattnercbe4f772007-08-08 22:51:59 +0000700void Stmt::dumpPretty() const {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000701 // FIXME: eliminate use of <iostream>
Chris Lattnercbe4f772007-08-08 22:51:59 +0000702 printPretty(std::cerr);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000703}
704
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000705void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Chris Lattner882f7882006-11-04 18:52:07 +0000706 if (this == 0) {
707 OS << "<NULL>";
708 return;
709 }
710
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000711 StmtPrinter P(OS, Helper);
Chris Lattner62249a62007-08-21 04:04:25 +0000712 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000713}
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000714
715//===----------------------------------------------------------------------===//
716// PrinterHelper
717//===----------------------------------------------------------------------===//
718
719// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +0000720PrinterHelper::~PrinterHelper() {}