blob: cf6aecd97f0db6f45a9aa43daf075b4856dc6c5f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::dump/Stmt::print methods.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/StmtVisitor.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/ExprCXX.h"
17#include "clang/Lex/IdentifierTable.h"
18#include "llvm/Support/Compiler.h"
19#include <iostream>
Chris Lattnerb0a721a2007-07-13 05:18:11 +000020#include <iomanip>
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
28 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor {
29 std::ostream &OS;
30 unsigned IndentLevel;
31 public:
32 StmtPrinter(std::ostream &os) : OS(os), IndentLevel(0) {}
33
34 void PrintStmt(Stmt *S, int SubIndent = 1) {
35 IndentLevel += SubIndent;
36 if (S && isa<Expr>(S)) {
37 // If this is an expr used in a stmt context, indent and newline it.
38 Indent();
39 S->visit(*this);
40 OS << ";\n";
41 } else if (S) {
42 S->visit(*this);
43 } else {
44 Indent() << "<<<NULL STATEMENT>>>\n";
45 }
46 IndentLevel -= SubIndent;
47 }
48
49 void PrintRawCompoundStmt(CompoundStmt *S);
50 void PrintRawDecl(Decl *D);
51 void PrintRawIfStmt(IfStmt *If);
52
53 void PrintExpr(Expr *E) {
54 if (E)
55 E->visit(*this);
56 else
57 OS << "<null expr>";
58 }
59
60 std::ostream &Indent(int Delta = 0) const {
61 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
62 OS << " ";
63 return OS;
64 }
65
66 virtual void VisitStmt(Stmt *Node);
67#define STMT(N, CLASS, PARENT) \
68 virtual void Visit##CLASS(CLASS *Node);
69#include "clang/AST/StmtNodes.def"
70 };
71}
72
73//===----------------------------------------------------------------------===//
74// Stmt printing methods.
75//===----------------------------------------------------------------------===//
76
77void StmtPrinter::VisitStmt(Stmt *Node) {
78 Indent() << "<<unknown stmt type>>\n";
79}
80
81/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
82/// with no newline after the }.
83void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
84 OS << "{\n";
85 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
86 I != E; ++I)
87 PrintStmt(*I);
88
89 Indent() << "}";
90}
91
92void StmtPrinter::PrintRawDecl(Decl *D) {
93 // FIXME: Need to complete/beautify this... this code simply shows the
94 // nodes are where they need to be.
95 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
96 OS << "typedef " << localType->getUnderlyingType().getAsString();
97 OS << " " << localType->getName();
98 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
99 // Emit storage class for vardecls.
100 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
101 switch (V->getStorageClass()) {
102 default: assert(0 && "Unknown storage class!");
103 case VarDecl::None: break;
104 case VarDecl::Extern: OS << "extern "; break;
105 case VarDecl::Static: OS << "static "; break;
106 case VarDecl::Auto: OS << "auto "; break;
107 case VarDecl::Register: OS << "register "; break;
108 }
109 }
110
111 std::string Name = VD->getName();
112 VD->getType().getAsStringInternal(Name);
113 OS << Name;
114
Chris Lattner24c39902007-07-12 00:36:32 +0000115 // If this is a vardecl with an initializer, emit it.
116 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
117 if (V->getInit()) {
118 OS << " = ";
119 PrintExpr(V->getInit());
120 }
121 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 } else {
123 // FIXME: "struct x;"
124 assert(0 && "Unexpected decl");
125 }
126}
127
128
129void StmtPrinter::VisitNullStmt(NullStmt *Node) {
130 Indent() << ";\n";
131}
132
133void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
134 for (Decl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
135 Indent();
136 PrintRawDecl(D);
137 OS << ";\n";
138 }
139}
140
141void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
142 Indent();
143 PrintRawCompoundStmt(Node);
144 OS << "\n";
145}
146
147void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
148 Indent(-1) << "case ";
149 PrintExpr(Node->getLHS());
150 if (Node->getRHS()) {
151 OS << " ... ";
152 PrintExpr(Node->getRHS());
153 }
154 OS << ":\n";
155
156 PrintStmt(Node->getSubStmt(), 0);
157}
158
159void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
160 Indent(-1) << "default:\n";
161 PrintStmt(Node->getSubStmt(), 0);
162}
163
164void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
165 Indent(-1) << Node->getName() << ":\n";
166 PrintStmt(Node->getSubStmt(), 0);
167}
168
169void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
170 OS << "if ";
171 PrintExpr(If->getCond());
172
173 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
174 OS << ' ';
175 PrintRawCompoundStmt(CS);
176 OS << (If->getElse() ? ' ' : '\n');
177 } else {
178 OS << '\n';
179 PrintStmt(If->getThen());
180 if (If->getElse()) Indent();
181 }
182
183 if (Stmt *Else = If->getElse()) {
184 OS << "else";
185
186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << '\n';
190 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
191 OS << ' ';
192 PrintRawIfStmt(ElseIf);
193 } else {
194 OS << '\n';
195 PrintStmt(If->getElse());
196 }
197 }
198}
199
200void StmtPrinter::VisitIfStmt(IfStmt *If) {
201 Indent();
202 PrintRawIfStmt(If);
203}
204
205void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
206 Indent() << "switch (";
207 PrintExpr(Node->getCond());
208 OS << ")";
209
210 // Pretty print compoundstmt bodies (very common).
211 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
212 OS << " ";
213 PrintRawCompoundStmt(CS);
214 OS << "\n";
215 } else {
216 OS << "\n";
217 PrintStmt(Node->getBody());
218 }
219}
220
221void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
222 Indent() << "while (";
223 PrintExpr(Node->getCond());
224 OS << ")\n";
225 PrintStmt(Node->getBody());
226}
227
228void StmtPrinter::VisitDoStmt(DoStmt *Node) {
229 Indent() << "do\n";
230 PrintStmt(Node->getBody());
231 Indent() << "while ";
232 PrintExpr(Node->getCond());
233 OS << ";\n";
234}
235
236void StmtPrinter::VisitForStmt(ForStmt *Node) {
237 Indent() << "for (";
238 if (Node->getInit()) {
239 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
240 PrintRawDecl(DS->getDecl());
241 else
242 PrintExpr(cast<Expr>(Node->getInit()));
243 }
244 OS << "; ";
245 if (Node->getCond())
246 PrintExpr(Node->getCond());
247 OS << "; ";
248 if (Node->getInc())
249 PrintExpr(Node->getInc());
250 OS << ")\n";
251 PrintStmt(Node->getBody());
252}
253
254void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
255 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
256}
257
258void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
259 Indent() << "goto *";
260 PrintExpr(Node->getTarget());
261 OS << ";\n";
262}
263
264void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
265 Indent() << "continue;\n";
266}
267
268void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
269 Indent() << "break;\n";
270}
271
272
273void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
274 Indent() << "return";
275 if (Node->getRetValue()) {
276 OS << " ";
277 PrintExpr(Node->getRetValue());
278 }
279 OS << ";\n";
280}
281
282//===----------------------------------------------------------------------===//
283// Expr printing methods.
284//===----------------------------------------------------------------------===//
285
286void StmtPrinter::VisitExpr(Expr *Node) {
287 OS << "<<unknown expr type>>";
288}
289
290void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
291 OS << Node->getDecl()->getName();
292}
293
294void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000295 unsigned value = Node->getValue();
296 if (isprint(value)) {
297 OS << "'" << (char)value << "'";
298 } else {
299 // FIXME something to indicate this is a character literal?
300 OS << std::hex << std::setiosflags(std::ios_base::showbase) << value
301 << std::dec << std::resetiosflags(std::ios_base::showbase);
302 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000303}
304
305void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
306 bool isSigned = Node->getType()->isSignedIntegerType();
307 OS << Node->getValue().toString(10, isSigned);
308
309 // Emit suffixes. Integer literals are always a builtin integer type.
310 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
311 default: assert(0 && "Unexpected type for integer literal!");
312 case BuiltinType::Int: break; // no suffix.
313 case BuiltinType::UInt: OS << 'U'; break;
314 case BuiltinType::Long: OS << 'L'; break;
315 case BuiltinType::ULong: OS << "UL"; break;
316 case BuiltinType::LongLong: OS << "LL"; break;
317 case BuiltinType::ULongLong: OS << "ULL"; break;
318 }
319}
320void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
321 // FIXME: print value.
322 OS << "~1.0~";
323}
324void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
325 if (Str->isWide()) OS << 'L';
326 OS << '"';
327
328 // FIXME: this doesn't print wstrings right.
329 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
330 switch (Str->getStrData()[i]) {
331 default: OS << Str->getStrData()[i]; break;
332 // Handle some common ones to make dumps prettier.
333 case '\\': OS << "\\\\"; break;
334 case '"': OS << "\\\""; break;
335 case '\n': OS << "\\n"; break;
336 case '\t': OS << "\\t"; break;
337 case '\a': OS << "\\a"; break;
338 case '\b': OS << "\\b"; break;
339 }
340 }
341 OS << '"';
342}
343void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
344 OS << "(";
345 PrintExpr(Node->getSubExpr());
346 OS << ")";
347}
348void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
349 if (!Node->isPostfix())
350 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
351 PrintExpr(Node->getSubExpr());
352
353 if (Node->isPostfix())
354 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
355
356}
357void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
358 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
359 OS << Node->getArgumentType().getAsString() << ")";
360}
361void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
362 PrintExpr(Node->getBase());
363 OS << "[";
364 PrintExpr(Node->getIdx());
365 OS << "]";
366}
367
368void StmtPrinter::VisitCallExpr(CallExpr *Call) {
369 PrintExpr(Call->getCallee());
370 OS << "(";
371 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
372 if (i) OS << ", ";
373 PrintExpr(Call->getArg(i));
374 }
375 OS << ")";
376}
377void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
378 PrintExpr(Node->getBase());
379 OS << (Node->isArrow() ? "->" : ".");
380
381 FieldDecl *Field = Node->getMemberDecl();
382 assert(Field && "MemberExpr should alway reference a field!");
383 OS << Field->getName();
384}
385void StmtPrinter::VisitCastExpr(CastExpr *Node) {
386 OS << "(" << Node->getDestType().getAsString() << ")";
387 PrintExpr(Node->getSubExpr());
388}
Steve Naroff49b45262007-07-13 16:58:59 +0000389void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
390 // No need to print anything.
391}
Reid Spencer5f016e22007-07-11 17:01:13 +0000392void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
393 PrintExpr(Node->getLHS());
394 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
395 PrintExpr(Node->getRHS());
396}
397void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
398 PrintExpr(Node->getCond());
399 OS << " ? ";
400 PrintExpr(Node->getLHS());
401 OS << " : ";
402 PrintExpr(Node->getRHS());
403}
404
405// GNU extensions.
406
407void StmtPrinter::VisitAddrLabel(AddrLabel *Node) {
408 OS << "&&" << Node->getLabel()->getName();
409
410}
411
412// C++
413
414void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
415 switch (Node->getOpcode()) {
416 default:
417 assert(0 && "Not a C++ cast expression");
418 abort();
419 case CXXCastExpr::ConstCast: OS << "const_cast<"; break;
420 case CXXCastExpr::DynamicCast: OS << "dynamic_cast<"; break;
421 case CXXCastExpr::ReinterpretCast: OS << "reinterpret_cast<"; break;
422 case CXXCastExpr::StaticCast: OS << "static_cast<"; break;
423 }
424
425 OS << Node->getDestType().getAsString() << ">(";
426 PrintExpr(Node->getSubExpr());
427 OS << ")";
428}
429
430void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
431 OS << (Node->getValue() ? "true" : "false");
432}
433
434
435//===----------------------------------------------------------------------===//
436// Stmt method implementations
437//===----------------------------------------------------------------------===//
438
439void Stmt::dump() const {
440 // FIXME: eliminate use of <iostream>
441 print(std::cerr);
442}
443
444void Stmt::print(std::ostream &OS) const {
445 if (this == 0) {
446 OS << "<NULL>";
447 return;
448 }
449
450 StmtPrinter P(OS);
451 const_cast<Stmt*>(this)->visit(P);
452}