blob: 2db46f4e0cb2a695a92afc69211e3f98e593ca17 [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"
Chris Lattner29375652006-12-04 18:06:35 +000017#include "clang/AST/ExprCXX.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000019#include "clang/Basic/IdentifierTable.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000020#include "llvm/Support/Compiler.h"
21#include <iostream>
Chris Lattner6e9d9b32007-07-13 05:18:11 +000022#include <iomanip>
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// StmtPrinter Visitor
27//===----------------------------------------------------------------------===//
28
29namespace {
Chris Lattner62249a62007-08-21 04:04:25 +000030 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000031 std::ostream &OS;
32 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000033 clang::PrinterHelper* Helper;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000034 public:
Ted Kremenek04f3cee2007-08-31 21:30:12 +000035 StmtPrinter(std::ostream &os, PrinterHelper* helper) :
36 OS(os), IndentLevel(0), Helper(helper) {}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000037
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000038 void PrintStmt(Stmt *S, int SubIndent = 1) {
39 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000040 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000041 // If this is an expr used in a stmt context, indent and newline it.
42 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000043 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000044 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000045 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000046 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000047 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000048 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000049 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000050 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000051 }
Chris Lattner073926e2007-05-20 23:04:55 +000052
53 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000054 void PrintRawDecl(Decl *D);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000055 void PrintRawIfStmt(IfStmt *If);
56
Chris Lattner882f7882006-11-04 18:52:07 +000057 void PrintExpr(Expr *E) {
58 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000059 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000060 else
Chris Lattner882f7882006-11-04 18:52:07 +000061 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000062 }
63
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000064 std::ostream &Indent(int Delta = 0) const {
Chris Lattnerd5322cd2007-05-29 23:49:07 +000065 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000066 OS << " ";
67 return OS;
68 }
69
Chris Lattner98dbf0a2007-08-30 17:59:59 +000070 bool PrintOffsetOfDesignator(Expr *E);
71 void VisitUnaryOffsetOf(UnaryOperator *Node);
72
Ted Kremenek04f3cee2007-08-31 21:30:12 +000073 void Visit(Stmt* S) {
74 if (Helper && Helper->handledStmt(S,OS))
75 return;
76 else StmtVisitor<StmtPrinter>::Visit(S);
77 }
78
Chris Lattner62249a62007-08-21 04:04:25 +000079 void VisitStmt(Stmt *Node);
Steve Naroff7f890eb2007-02-27 02:53:10 +000080#define STMT(N, CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000081 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000082#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000083 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000084}
85
Chris Lattner71e23ce2006-11-04 20:18:38 +000086//===----------------------------------------------------------------------===//
87// Stmt printing methods.
88//===----------------------------------------------------------------------===//
89
Chris Lattner882f7882006-11-04 18:52:07 +000090void StmtPrinter::VisitStmt(Stmt *Node) {
91 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000092}
93
Chris Lattner073926e2007-05-20 23:04:55 +000094/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
95/// with no newline after the }.
96void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
97 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000098 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +000099 I != E; ++I)
100 PrintStmt(*I);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000101
Chris Lattner073926e2007-05-20 23:04:55 +0000102 Indent() << "}";
103}
104
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000105void StmtPrinter::PrintRawDecl(Decl *D) {
106 // FIXME: Need to complete/beautify this... this code simply shows the
Steve Naroff2a8ad182007-05-29 22:59:26 +0000107 // nodes are where they need to be.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000108 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
109 OS << "typedef " << localType->getUnderlyingType().getAsString();
110 OS << " " << localType->getName();
111 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerb4522b42007-06-02 05:27:01 +0000112 // Emit storage class for vardecls.
113 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
114 switch (V->getStorageClass()) {
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000115 default: assert(0 && "Unknown storage class!");
116 case VarDecl::None: break;
117 case VarDecl::Extern: OS << "extern "; break;
118 case VarDecl::Static: OS << "static "; break;
119 case VarDecl::Auto: OS << "auto "; break;
120 case VarDecl::Register: OS << "register "; break;
Chris Lattnerb4522b42007-06-02 05:27:01 +0000121 }
122 }
123
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000124 std::string Name = VD->getName();
125 VD->getType().getAsStringInternal(Name);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000126 OS << Name;
127
Chris Lattner79c57592007-07-12 00:36:32 +0000128 // If this is a vardecl with an initializer, emit it.
129 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
130 if (V->getInit()) {
131 OS << " = ";
132 PrintExpr(V->getInit());
133 }
134 }
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000135 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000136 // FIXME: "struct x;"
137 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000138 }
139}
140
141
142void StmtPrinter::VisitNullStmt(NullStmt *Node) {
143 Indent() << ";\n";
144}
145
146void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Steve Naroffa23cc792007-09-13 23:52:58 +0000147 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattner776fac82007-06-09 00:53:06 +0000148 Indent();
149 PrintRawDecl(D);
150 OS << ";\n";
151 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000152}
153
Chris Lattner073926e2007-05-20 23:04:55 +0000154void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
155 Indent();
156 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000157 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000158}
159
Chris Lattner6c0ff132006-11-05 00:19:50 +0000160void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000161 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000162 PrintExpr(Node->getLHS());
163 if (Node->getRHS()) {
164 OS << " ... ";
165 PrintExpr(Node->getRHS());
166 }
167 OS << ":\n";
168
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000169 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000170}
171
172void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000173 Indent(-1) << "default:\n";
174 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000175}
176
177void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000178 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000179 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000180}
181
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000182void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
183 OS << "if ";
Chris Lattner882f7882006-11-04 18:52:07 +0000184 PrintExpr(If->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000185
186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << (If->getElse() ? ' ' : '\n');
190 } else {
191 OS << '\n';
192 PrintStmt(If->getThen());
193 if (If->getElse()) Indent();
194 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000195
Chris Lattner073926e2007-05-20 23:04:55 +0000196 if (Stmt *Else = If->getElse()) {
197 OS << "else";
198
199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
200 OS << ' ';
201 PrintRawCompoundStmt(CS);
202 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000203 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
204 OS << ' ';
205 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000206 } else {
207 OS << '\n';
208 PrintStmt(If->getElse());
209 }
Chris Lattner882f7882006-11-04 18:52:07 +0000210 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000211}
212
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000213void StmtPrinter::VisitIfStmt(IfStmt *If) {
214 Indent();
215 PrintRawIfStmt(If);
216}
217
Chris Lattnerf2174b62006-11-04 20:59:27 +0000218void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
219 Indent() << "switch (";
220 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000221 OS << ")";
222
223 // Pretty print compoundstmt bodies (very common).
224 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
225 OS << " ";
226 PrintRawCompoundStmt(CS);
227 OS << "\n";
228 } else {
229 OS << "\n";
230 PrintStmt(Node->getBody());
231 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000232}
233
Anders Carlsson51873c22007-07-22 07:07:56 +0000234void StmtPrinter::VisitSwitchCase(SwitchCase*) {
235 assert(0 && "SwitchCase is an abstract class");
236}
237
Chris Lattner85ed8732006-11-04 20:40:44 +0000238void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
239 Indent() << "while (";
240 PrintExpr(Node->getCond());
241 OS << ")\n";
242 PrintStmt(Node->getBody());
243}
244
245void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000246 Indent() << "do ";
247 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
248 PrintRawCompoundStmt(CS);
249 OS << " ";
250 } else {
251 OS << "\n";
252 PrintStmt(Node->getBody());
253 Indent();
254 }
255
256 OS << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000257 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000258 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000259}
260
Chris Lattner71e23ce2006-11-04 20:18:38 +0000261void StmtPrinter::VisitForStmt(ForStmt *Node) {
262 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000263 if (Node->getInit()) {
264 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
265 PrintRawDecl(DS->getDecl());
266 else
267 PrintExpr(cast<Expr>(Node->getInit()));
268 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000269 OS << ";";
270 if (Node->getCond()) {
271 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000272 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000273 }
274 OS << ";";
275 if (Node->getInc()) {
276 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000277 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000278 }
279 OS << ") ";
280
281 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
282 PrintRawCompoundStmt(CS);
283 OS << "\n";
284 } else {
285 OS << "\n";
286 PrintStmt(Node->getBody());
287 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000288}
289
Chris Lattner16976d32006-11-05 01:46:01 +0000290void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000291 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000292}
293
294void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000295 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000296 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000297 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000298}
299
300void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000301 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000302}
303
304void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000305 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000306}
307
308
Chris Lattner882f7882006-11-04 18:52:07 +0000309void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
310 Indent() << "return";
311 if (Node->getRetValue()) {
312 OS << " ";
313 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000314 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000315 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000316}
317
Chris Lattner71e23ce2006-11-04 20:18:38 +0000318//===----------------------------------------------------------------------===//
319// Expr printing methods.
320//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000321
Chris Lattner882f7882006-11-04 18:52:07 +0000322void StmtPrinter::VisitExpr(Expr *Node) {
323 OS << "<<unknown expr type>>";
324}
325
326void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner5efbb332006-11-20 05:01:40 +0000327 OS << Node->getDecl()->getName();
Chris Lattner882f7882006-11-04 18:52:07 +0000328}
329
Anders Carlsson625bfc82007-07-21 05:21:51 +0000330void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
331 switch (Node->getIdentType()) {
332 default:
333 assert(0 && "unknown case");
334 case PreDefinedExpr::Func:
335 OS << "__func__";
336 break;
337 case PreDefinedExpr::Function:
338 OS << "__FUNCTION__";
339 break;
340 case PreDefinedExpr::PrettyFunction:
341 OS << "__PRETTY_FUNCTION__";
342 break;
343 }
344}
345
Steve Naroffae4143e2007-04-26 20:39:23 +0000346void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner666115c2007-07-13 23:58:20 +0000347 // FIXME should print an L for wchar_t constants
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000348 unsigned value = Node->getValue();
Chris Lattner666115c2007-07-13 23:58:20 +0000349 switch (value) {
350 case '\\':
351 OS << "'\\\\'";
352 break;
353 case '\'':
354 OS << "'\\''";
355 break;
356 case '\a':
357 // TODO: K&R: the meaning of '\\a' is different in traditional C
358 OS << "'\\a'";
359 break;
360 case '\b':
361 OS << "'\\b'";
362 break;
363 // Nonstandard escape sequence.
364 /*case '\e':
365 OS << "'\\e'";
366 break;*/
367 case '\f':
368 OS << "'\\f'";
369 break;
370 case '\n':
371 OS << "'\\n'";
372 break;
373 case '\r':
374 OS << "'\\r'";
375 break;
376 case '\t':
377 OS << "'\\t'";
378 break;
379 case '\v':
380 OS << "'\\v'";
381 break;
382 default:
383 if (isprint(value) && value < 256) {
384 OS << "'" << (char)value << "'";
385 } else if (value < 256) {
386 OS << "'\\x" << std::hex << value << std::dec << "'";
387 } else {
388 // FIXME what to really do here?
389 OS << value;
390 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000391 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000392}
393
Steve Naroffdf7855b2007-02-21 23:46:25 +0000394void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000395 bool isSigned = Node->getType()->isSignedIntegerType();
396 OS << Node->getValue().toString(10, isSigned);
397
398 // Emit suffixes. Integer literals are always a builtin integer type.
399 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
400 default: assert(0 && "Unexpected type for integer literal!");
401 case BuiltinType::Int: break; // no suffix.
402 case BuiltinType::UInt: OS << 'U'; break;
403 case BuiltinType::Long: OS << 'L'; break;
404 case BuiltinType::ULong: OS << "UL"; break;
405 case BuiltinType::LongLong: OS << "LL"; break;
406 case BuiltinType::ULongLong: OS << "ULL"; break;
407 }
Chris Lattner882f7882006-11-04 18:52:07 +0000408}
Steve Naroffab624882007-02-21 22:05:47 +0000409void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000410 // FIXME: print value more precisely.
Chris Lattner2dd003e2007-09-22 18:47:25 +0000411 OS << Node->getValueAsDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000412}
Chris Lattner1c20a172007-08-26 03:42:43 +0000413
414void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
415 PrintExpr(Node->getSubExpr());
416 OS << "i";
417}
418
Steve Naroffdf7855b2007-02-21 23:46:25 +0000419void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000420 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000421 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000422
Chris Lattner5d8f4942006-11-04 20:29:31 +0000423 // FIXME: this doesn't print wstrings right.
424 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
425 switch (Str->getStrData()[i]) {
426 default: OS << Str->getStrData()[i]; break;
427 // Handle some common ones to make dumps prettier.
428 case '\\': OS << "\\\\"; break;
429 case '"': OS << "\\\""; break;
430 case '\n': OS << "\\n"; break;
431 case '\t': OS << "\\t"; break;
432 case '\a': OS << "\\a"; break;
433 case '\b': OS << "\\b"; break;
434 }
435 }
436 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000437}
438void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
439 OS << "(";
440 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000441 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000442}
443void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000444 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000445 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000446
447 // Print a space if this is an "identifier operator" like sizeof or __real.
448 switch (Node->getOpcode()) {
449 default: break;
450 case UnaryOperator::SizeOf:
451 case UnaryOperator::AlignOf:
452 case UnaryOperator::Real:
453 case UnaryOperator::Imag:
454 case UnaryOperator::Extension:
455 OS << ' ';
456 break;
457 }
458 }
Chris Lattner882f7882006-11-04 18:52:07 +0000459 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000460
461 if (Node->isPostfix())
462 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000463}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000464
465bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
466 if (isa<CompoundLiteralExpr>(E)) {
467 // Base case, print the type and comma.
468 OS << E->getType().getAsString() << ", ";
469 return true;
470 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
471 PrintOffsetOfDesignator(ASE->getLHS());
472 OS << "[";
473 PrintExpr(ASE->getRHS());
474 OS << "]";
475 return false;
476 } else {
477 MemberExpr *ME = cast<MemberExpr>(E);
478 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
479 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
480 return false;
481 }
482}
483
484void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
485 OS << "__builtin_offsetof(";
486 PrintOffsetOfDesignator(Node->getSubExpr());
487 OS << ")";
488}
489
Chris Lattner882f7882006-11-04 18:52:07 +0000490void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner33e8a552006-11-19 01:48:02 +0000491 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
Chris Lattner3dc3d772007-05-16 18:07:12 +0000492 OS << Node->getArgumentType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000493}
494void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000495 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000496 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000497 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000498 OS << "]";
499}
500
501void StmtPrinter::VisitCallExpr(CallExpr *Call) {
502 PrintExpr(Call->getCallee());
503 OS << "(";
504 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
505 if (i) OS << ", ";
506 PrintExpr(Call->getArg(i));
507 }
508 OS << ")";
509}
510void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
511 PrintExpr(Node->getBase());
512 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000513
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000514 FieldDecl *Field = Node->getMemberDecl();
515 assert(Field && "MemberExpr should alway reference a field!");
516 OS << Field->getName();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000517}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000518void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000519 PrintExpr(Node->getBase());
520 OS << ".";
521 OS << Node->getAccessor().getName();
522}
Chris Lattner882f7882006-11-04 18:52:07 +0000523void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000524 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000525 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000526}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000527void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
528 OS << "(" << Node->getType().getAsString() << ")";
529 PrintExpr(Node->getInitializer());
530}
Steve Naroff7a5af782007-07-13 16:58:59 +0000531void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000532 // No need to print anything, simply forward to the sub expression.
533 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000534}
Chris Lattner882f7882006-11-04 18:52:07 +0000535void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
536 PrintExpr(Node->getLHS());
537 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
538 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000539}
Chris Lattner86928112007-08-25 02:00:02 +0000540void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
541 PrintExpr(Node->getLHS());
542 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
543 PrintExpr(Node->getRHS());
544}
Chris Lattner882f7882006-11-04 18:52:07 +0000545void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
546 PrintExpr(Node->getCond());
547 OS << " ? ";
548 PrintExpr(Node->getLHS());
Bill Wendling48fbdd02007-05-23 08:04:21 +0000549 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000550 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000551}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000552
Chris Lattnereefa10e2007-05-28 06:56:27 +0000553// GNU extensions.
554
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000555void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000556 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000557}
558
Chris Lattner366727f2007-07-24 16:58:17 +0000559void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
560 OS << "(";
561 PrintRawCompoundStmt(E->getSubStmt());
562 OS << ")";
563}
564
Steve Naroff78864672007-08-01 22:05:33 +0000565void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
566 OS << "__builtin_types_compatible_p(";
567 OS << Node->getArgType1().getAsString() << ",";
568 OS << Node->getArgType2().getAsString() << ")";
569}
570
Steve Naroff9efdabc2007-08-03 21:21:27 +0000571void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
572 OS << "__builtin_choose_expr(";
573 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000574 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000575 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000576 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000577 PrintExpr(Node->getRHS());
578 OS << ")";
579}
Chris Lattner366727f2007-07-24 16:58:17 +0000580
Anders Carlsson4692db02007-08-31 04:56:16 +0000581void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
582 OS << "{ ";
583 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
584 if (i) OS << ", ";
585 PrintExpr(Node->getInit(i));
586 }
587 OS << " }";
588}
589
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000590void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
591 OS << "va_arg(";
592 PrintExpr(Node->getSubExpr());
593 OS << ", ";
594 OS << Node->getType().getAsString();
595 OS << ")";
596}
597
Chris Lattnereefa10e2007-05-28 06:56:27 +0000598// C++
599
600void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner3f5e4682007-08-09 17:34:19 +0000601 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Chris Lattnereefa10e2007-05-28 06:56:27 +0000602 OS << Node->getDestType().getAsString() << ">(";
603 PrintExpr(Node->getSubExpr());
604 OS << ")";
605}
606
607void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
608 OS << (Node->getValue() ? "true" : "false");
609}
610
Anders Carlsson76f4a902007-08-21 17:43:55 +0000611// Obj-C
612
613void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
614 OS << "@";
615 VisitStringLiteral(Node->getString());
616}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000617
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000618void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
619 OS << "@encode(";
620 OS << Node->getEncodedType().getAsString() << ")";
621}
622
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000623void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
624 OS << "@selector(";
625 Selector &selector = Node->getSelector();
626 if (selector.isUnarySelector())
627 OS << " " << selector.getIdentifierInfoForSlot(0)->getName();
628 else {
629 for (unsigned i = 0, e = Node->getNumArgs(); i != e; ++i)
Fariborz Jahanian81ccd882007-10-16 20:52:13 +0000630 if (selector.getIdentifierInfoForSlot(i))
631 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
632 else
633 OS << ":";
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000634 }
635 OS << ")";
636}
637
Steve Naroffd54978b2007-09-18 23:55:05 +0000638void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
639 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +0000640 Expr *receiver = Mess->getReceiver();
641 if (receiver) PrintExpr(receiver);
642 else OS << Mess->getClassName()->getName();
643 Selector &selector = Mess->getSelector();
644 if (selector.isUnarySelector()) {
645 OS << " " << selector.getIdentifierInfoForSlot(0)->getName();
646 } else {
647 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Fariborz Jahanian81ccd882007-10-16 20:52:13 +0000648 if (selector.getIdentifierInfoForSlot(i))
649 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
650 else
651 OS << ":";
Steve Naroffc6814ea2007-10-02 20:01:56 +0000652 PrintExpr(Mess->getArg(i));
653 }
Steve Naroffd54978b2007-09-18 23:55:05 +0000654 }
655 OS << "]";
656}
657
658
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000659//===----------------------------------------------------------------------===//
660// Stmt method implementations
661//===----------------------------------------------------------------------===//
662
Chris Lattnercbe4f772007-08-08 22:51:59 +0000663void Stmt::dumpPretty() const {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000664 // FIXME: eliminate use of <iostream>
Chris Lattnercbe4f772007-08-08 22:51:59 +0000665 printPretty(std::cerr);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000666}
667
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000668void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Chris Lattner882f7882006-11-04 18:52:07 +0000669 if (this == 0) {
670 OS << "<NULL>";
671 return;
672 }
673
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000674 StmtPrinter P(OS, Helper);
Chris Lattner62249a62007-08-21 04:04:25 +0000675 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000676}
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000677
678//===----------------------------------------------------------------------===//
679// PrinterHelper
680//===----------------------------------------------------------------------===//
681
682// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +0000683PrinterHelper::~PrinterHelper() {}