blob: 875af790201a071aecf490a3b2129fae763e0848 [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"
Chris Lattner6c0ff132006-11-05 00:19:50 +000018#include "clang/Lex/IdentifierTable.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000019#include "llvm/Support/Compiler.h"
20#include <iostream>
Chris Lattner6e9d9b32007-07-13 05:18:11 +000021#include <iomanip>
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtPrinter Visitor
26//===----------------------------------------------------------------------===//
27
28namespace {
Chris Lattner62249a62007-08-21 04:04:25 +000029 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000030 std::ostream &OS;
31 unsigned IndentLevel;
32 public:
33 StmtPrinter(std::ostream &os) : OS(os), IndentLevel(0) {}
34
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000035 void PrintStmt(Stmt *S, int SubIndent = 1) {
36 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000037 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000038 // If this is an expr used in a stmt context, indent and newline it.
39 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000040 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000041 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000042 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000043 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000044 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000045 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000046 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000047 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000048 }
Chris Lattner073926e2007-05-20 23:04:55 +000049
50 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000051 void PrintRawDecl(Decl *D);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000052 void PrintRawIfStmt(IfStmt *If);
53
Chris Lattner882f7882006-11-04 18:52:07 +000054 void PrintExpr(Expr *E) {
55 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000056 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000057 else
Chris Lattner882f7882006-11-04 18:52:07 +000058 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000059 }
60
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000061 std::ostream &Indent(int Delta = 0) const {
Chris Lattnerd5322cd2007-05-29 23:49:07 +000062 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000063 OS << " ";
64 return OS;
65 }
66
Chris Lattner62249a62007-08-21 04:04:25 +000067 void VisitStmt(Stmt *Node);
Steve Naroff7f890eb2007-02-27 02:53:10 +000068#define STMT(N, CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000069 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000070#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000071 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000072}
73
Chris Lattner71e23ce2006-11-04 20:18:38 +000074//===----------------------------------------------------------------------===//
75// Stmt printing methods.
76//===----------------------------------------------------------------------===//
77
Chris Lattner882f7882006-11-04 18:52:07 +000078void StmtPrinter::VisitStmt(Stmt *Node) {
79 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000080}
81
Chris Lattner073926e2007-05-20 23:04:55 +000082/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
83/// with no newline after the }.
84void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
85 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000086 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +000087 I != E; ++I)
88 PrintStmt(*I);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000089
Chris Lattner073926e2007-05-20 23:04:55 +000090 Indent() << "}";
91}
92
Chris Lattnerfdc195a2007-06-05 20:52:47 +000093void StmtPrinter::PrintRawDecl(Decl *D) {
94 // FIXME: Need to complete/beautify this... this code simply shows the
Steve Naroff2a8ad182007-05-29 22:59:26 +000095 // nodes are where they need to be.
Chris Lattnerfdc195a2007-06-05 20:52:47 +000096 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
97 OS << "typedef " << localType->getUnderlyingType().getAsString();
98 OS << " " << localType->getName();
99 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerb4522b42007-06-02 05:27:01 +0000100 // Emit storage class for vardecls.
101 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
102 switch (V->getStorageClass()) {
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000103 default: assert(0 && "Unknown storage class!");
104 case VarDecl::None: break;
105 case VarDecl::Extern: OS << "extern "; break;
106 case VarDecl::Static: OS << "static "; break;
107 case VarDecl::Auto: OS << "auto "; break;
108 case VarDecl::Register: OS << "register "; break;
Chris Lattnerb4522b42007-06-02 05:27:01 +0000109 }
110 }
111
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000112 std::string Name = VD->getName();
113 VD->getType().getAsStringInternal(Name);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000114 OS << Name;
115
Chris Lattner79c57592007-07-12 00:36:32 +0000116 // If this is a vardecl with an initializer, emit it.
117 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
118 if (V->getInit()) {
119 OS << " = ";
120 PrintExpr(V->getInit());
121 }
122 }
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000123 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000124 // FIXME: "struct x;"
125 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000126 }
127}
128
129
130void StmtPrinter::VisitNullStmt(NullStmt *Node) {
131 Indent() << ";\n";
132}
133
134void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Chris Lattner776fac82007-06-09 00:53:06 +0000135 for (Decl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
136 Indent();
137 PrintRawDecl(D);
138 OS << ";\n";
139 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000140}
141
Chris Lattner073926e2007-05-20 23:04:55 +0000142void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
143 Indent();
144 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000145 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000146}
147
Chris Lattner6c0ff132006-11-05 00:19:50 +0000148void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000149 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000150 PrintExpr(Node->getLHS());
151 if (Node->getRHS()) {
152 OS << " ... ";
153 PrintExpr(Node->getRHS());
154 }
155 OS << ":\n";
156
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000157 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000158}
159
160void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000161 Indent(-1) << "default:\n";
162 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000163}
164
165void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000166 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000167 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000168}
169
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000170void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
171 OS << "if ";
Chris Lattner882f7882006-11-04 18:52:07 +0000172 PrintExpr(If->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000173
174 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
175 OS << ' ';
176 PrintRawCompoundStmt(CS);
177 OS << (If->getElse() ? ' ' : '\n');
178 } else {
179 OS << '\n';
180 PrintStmt(If->getThen());
181 if (If->getElse()) Indent();
182 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000183
Chris Lattner073926e2007-05-20 23:04:55 +0000184 if (Stmt *Else = If->getElse()) {
185 OS << "else";
186
187 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
188 OS << ' ';
189 PrintRawCompoundStmt(CS);
190 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000191 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
192 OS << ' ';
193 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000194 } else {
195 OS << '\n';
196 PrintStmt(If->getElse());
197 }
Chris Lattner882f7882006-11-04 18:52:07 +0000198 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000199}
200
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000201void StmtPrinter::VisitIfStmt(IfStmt *If) {
202 Indent();
203 PrintRawIfStmt(If);
204}
205
Chris Lattnerf2174b62006-11-04 20:59:27 +0000206void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
207 Indent() << "switch (";
208 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000209 OS << ")";
210
211 // Pretty print compoundstmt bodies (very common).
212 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
213 OS << " ";
214 PrintRawCompoundStmt(CS);
215 OS << "\n";
216 } else {
217 OS << "\n";
218 PrintStmt(Node->getBody());
219 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000220}
221
Anders Carlsson51873c22007-07-22 07:07:56 +0000222void StmtPrinter::VisitSwitchCase(SwitchCase*) {
223 assert(0 && "SwitchCase is an abstract class");
224}
225
Chris Lattner85ed8732006-11-04 20:40:44 +0000226void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
227 Indent() << "while (";
228 PrintExpr(Node->getCond());
229 OS << ")\n";
230 PrintStmt(Node->getBody());
231}
232
233void StmtPrinter::VisitDoStmt(DoStmt *Node) {
234 Indent() << "do\n";
235 PrintStmt(Node->getBody());
Chris Lattnera076fde2007-05-31 18:21:33 +0000236 Indent() << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000237 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000238 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000239}
240
Chris Lattner71e23ce2006-11-04 20:18:38 +0000241void StmtPrinter::VisitForStmt(ForStmt *Node) {
242 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000243 if (Node->getInit()) {
244 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
245 PrintRawDecl(DS->getDecl());
246 else
247 PrintExpr(cast<Expr>(Node->getInit()));
248 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000249 OS << "; ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000250 if (Node->getCond())
251 PrintExpr(Node->getCond());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000252 OS << "; ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000253 if (Node->getInc())
254 PrintExpr(Node->getInc());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000255 OS << ")\n";
Chris Lattner85ed8732006-11-04 20:40:44 +0000256 PrintStmt(Node->getBody());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000257}
258
Chris Lattner16976d32006-11-05 01:46:01 +0000259void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000260 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000261}
262
263void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000264 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000265 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000266 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000267}
268
269void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000270 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000271}
272
273void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000274 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000275}
276
277
Chris Lattner882f7882006-11-04 18:52:07 +0000278void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
279 Indent() << "return";
280 if (Node->getRetValue()) {
281 OS << " ";
282 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000283 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000284 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000285}
286
Chris Lattner71e23ce2006-11-04 20:18:38 +0000287//===----------------------------------------------------------------------===//
288// Expr printing methods.
289//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000290
Chris Lattner882f7882006-11-04 18:52:07 +0000291void StmtPrinter::VisitExpr(Expr *Node) {
292 OS << "<<unknown expr type>>";
293}
294
295void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner5efbb332006-11-20 05:01:40 +0000296 OS << Node->getDecl()->getName();
Chris Lattner882f7882006-11-04 18:52:07 +0000297}
298
Anders Carlsson625bfc82007-07-21 05:21:51 +0000299void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
300 switch (Node->getIdentType()) {
301 default:
302 assert(0 && "unknown case");
303 case PreDefinedExpr::Func:
304 OS << "__func__";
305 break;
306 case PreDefinedExpr::Function:
307 OS << "__FUNCTION__";
308 break;
309 case PreDefinedExpr::PrettyFunction:
310 OS << "__PRETTY_FUNCTION__";
311 break;
312 }
313}
314
Steve Naroffae4143e2007-04-26 20:39:23 +0000315void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner666115c2007-07-13 23:58:20 +0000316 // FIXME should print an L for wchar_t constants
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000317 unsigned value = Node->getValue();
Chris Lattner666115c2007-07-13 23:58:20 +0000318 switch (value) {
319 case '\\':
320 OS << "'\\\\'";
321 break;
322 case '\'':
323 OS << "'\\''";
324 break;
325 case '\a':
326 // TODO: K&R: the meaning of '\\a' is different in traditional C
327 OS << "'\\a'";
328 break;
329 case '\b':
330 OS << "'\\b'";
331 break;
332 // Nonstandard escape sequence.
333 /*case '\e':
334 OS << "'\\e'";
335 break;*/
336 case '\f':
337 OS << "'\\f'";
338 break;
339 case '\n':
340 OS << "'\\n'";
341 break;
342 case '\r':
343 OS << "'\\r'";
344 break;
345 case '\t':
346 OS << "'\\t'";
347 break;
348 case '\v':
349 OS << "'\\v'";
350 break;
351 default:
352 if (isprint(value) && value < 256) {
353 OS << "'" << (char)value << "'";
354 } else if (value < 256) {
355 OS << "'\\x" << std::hex << value << std::dec << "'";
356 } else {
357 // FIXME what to really do here?
358 OS << value;
359 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000360 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000361}
362
Steve Naroffdf7855b2007-02-21 23:46:25 +0000363void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000364 bool isSigned = Node->getType()->isSignedIntegerType();
365 OS << Node->getValue().toString(10, isSigned);
366
367 // Emit suffixes. Integer literals are always a builtin integer type.
368 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
369 default: assert(0 && "Unexpected type for integer literal!");
370 case BuiltinType::Int: break; // no suffix.
371 case BuiltinType::UInt: OS << 'U'; break;
372 case BuiltinType::Long: OS << 'L'; break;
373 case BuiltinType::ULong: OS << "UL"; break;
374 case BuiltinType::LongLong: OS << "LL"; break;
375 case BuiltinType::ULongLong: OS << "ULL"; break;
376 }
Chris Lattner882f7882006-11-04 18:52:07 +0000377}
Steve Naroffab624882007-02-21 22:05:47 +0000378void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000379 // FIXME: print value more precisely.
380 OS << Node->getValue();
Chris Lattner882f7882006-11-04 18:52:07 +0000381}
Chris Lattner1c20a172007-08-26 03:42:43 +0000382
383void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
384 PrintExpr(Node->getSubExpr());
385 OS << "i";
386}
387
Steve Naroffdf7855b2007-02-21 23:46:25 +0000388void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000389 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000390 OS << '"';
391
392 // FIXME: this doesn't print wstrings right.
393 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
394 switch (Str->getStrData()[i]) {
395 default: OS << Str->getStrData()[i]; break;
396 // Handle some common ones to make dumps prettier.
397 case '\\': OS << "\\\\"; break;
398 case '"': OS << "\\\""; break;
399 case '\n': OS << "\\n"; break;
400 case '\t': OS << "\\t"; break;
401 case '\a': OS << "\\a"; break;
402 case '\b': OS << "\\b"; break;
403 }
404 }
405 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000406}
407void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
408 OS << "(";
409 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000410 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000411}
412void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000413 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000414 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000415
416 // Print a space if this is an "identifier operator" like sizeof or __real.
417 switch (Node->getOpcode()) {
418 default: break;
419 case UnaryOperator::SizeOf:
420 case UnaryOperator::AlignOf:
421 case UnaryOperator::Real:
422 case UnaryOperator::Imag:
423 case UnaryOperator::Extension:
424 OS << ' ';
425 break;
426 }
427 }
Chris Lattner882f7882006-11-04 18:52:07 +0000428 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000429
430 if (Node->isPostfix())
431 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
432
Chris Lattner882f7882006-11-04 18:52:07 +0000433}
434void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner33e8a552006-11-19 01:48:02 +0000435 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
Chris Lattner3dc3d772007-05-16 18:07:12 +0000436 OS << Node->getArgumentType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000437}
438void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000439 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000440 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000441 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000442 OS << "]";
443}
444
445void StmtPrinter::VisitCallExpr(CallExpr *Call) {
446 PrintExpr(Call->getCallee());
447 OS << "(";
448 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
449 if (i) OS << ", ";
450 PrintExpr(Call->getArg(i));
451 }
452 OS << ")";
453}
454void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
455 PrintExpr(Node->getBase());
456 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000457
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000458 FieldDecl *Field = Node->getMemberDecl();
459 assert(Field && "MemberExpr should alway reference a field!");
460 OS << Field->getName();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000461}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000462void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000463 PrintExpr(Node->getBase());
464 OS << ".";
465 OS << Node->getAccessor().getName();
466}
Chris Lattner882f7882006-11-04 18:52:07 +0000467void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000468 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000469 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000470}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000471void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
472 OS << "(" << Node->getType().getAsString() << ")";
473 PrintExpr(Node->getInitializer());
474}
Steve Naroff7a5af782007-07-13 16:58:59 +0000475void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000476 // No need to print anything, simply forward to the sub expression.
477 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000478}
Chris Lattner882f7882006-11-04 18:52:07 +0000479void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
480 PrintExpr(Node->getLHS());
481 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
482 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000483}
Chris Lattner86928112007-08-25 02:00:02 +0000484void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
485 PrintExpr(Node->getLHS());
486 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
487 PrintExpr(Node->getRHS());
488}
Chris Lattner882f7882006-11-04 18:52:07 +0000489void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
490 PrintExpr(Node->getCond());
491 OS << " ? ";
492 PrintExpr(Node->getLHS());
Bill Wendling48fbdd02007-05-23 08:04:21 +0000493 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000494 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000495}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000496
Chris Lattnereefa10e2007-05-28 06:56:27 +0000497// GNU extensions.
498
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000499void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000500 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000501}
502
Chris Lattner366727f2007-07-24 16:58:17 +0000503void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
504 OS << "(";
505 PrintRawCompoundStmt(E->getSubStmt());
506 OS << ")";
507}
508
Steve Naroff78864672007-08-01 22:05:33 +0000509void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
510 OS << "__builtin_types_compatible_p(";
511 OS << Node->getArgType1().getAsString() << ",";
512 OS << Node->getArgType2().getAsString() << ")";
513}
514
Steve Naroff9efdabc2007-08-03 21:21:27 +0000515void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
516 OS << "__builtin_choose_expr(";
517 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000518 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000519 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000520 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000521 PrintExpr(Node->getRHS());
522 OS << ")";
523}
Chris Lattner366727f2007-07-24 16:58:17 +0000524
Chris Lattnereefa10e2007-05-28 06:56:27 +0000525// C++
526
527void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner3f5e4682007-08-09 17:34:19 +0000528 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Chris Lattnereefa10e2007-05-28 06:56:27 +0000529 OS << Node->getDestType().getAsString() << ">(";
530 PrintExpr(Node->getSubExpr());
531 OS << ")";
532}
533
534void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
535 OS << (Node->getValue() ? "true" : "false");
536}
537
Anders Carlsson76f4a902007-08-21 17:43:55 +0000538// Obj-C
539
540void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
541 OS << "@";
542 VisitStringLiteral(Node->getString());
543}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000544
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000545void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
546 OS << "@encode(";
547 OS << Node->getEncodedType().getAsString() << ")";
548}
549
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000550//===----------------------------------------------------------------------===//
551// Stmt method implementations
552//===----------------------------------------------------------------------===//
553
Chris Lattnercbe4f772007-08-08 22:51:59 +0000554void Stmt::dumpPretty() const {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000555 // FIXME: eliminate use of <iostream>
Chris Lattnercbe4f772007-08-08 22:51:59 +0000556 printPretty(std::cerr);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000557}
558
Chris Lattnercbe4f772007-08-08 22:51:59 +0000559void Stmt::printPretty(std::ostream &OS) const {
Chris Lattner882f7882006-11-04 18:52:07 +0000560 if (this == 0) {
561 OS << "<NULL>";
562 return;
563 }
564
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000565 StmtPrinter P(OS);
Chris Lattner62249a62007-08-21 04:04:25 +0000566 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000567}