blob: ca62ed12d058cb649af2fe46d134ffeaf09ba75d [file] [log] [blame]
Chris Lattnercbe4f772007-08-08 22:51:59 +00001//===--- StmtDumper.cpp - Dumping implementation for Stmt ASTs ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnercbe4f772007-08-08 22:51:59 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::dump/Stmt::print methods, which dump out the
11// AST in a form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Ted Kremenek5c84c012007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000018#include "clang/AST/PrettyPrinter.h"
Chris Lattner11e30d32007-08-30 06:17:34 +000019#include "clang/Basic/SourceManager.h"
Daniel Dunbar34a96c82009-12-03 09:13:13 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattnercbe4f772007-08-08 22:51:59 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtDumper Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000028 class StmtDumper : public StmtVisitor<StmtDumper> {
Chris Lattner11e30d32007-08-30 06:17:34 +000029 SourceManager *SM;
Daniel Dunbar34a96c82009-12-03 09:13:13 +000030 llvm::raw_ostream &OS;
Chris Lattnercbe4f772007-08-08 22:51:59 +000031 unsigned IndentLevel;
Mike Stump11289f42009-09-09 15:08:12 +000032
Chris Lattnercbe4f772007-08-08 22:51:59 +000033 /// MaxDepth - When doing a normal dump (not dumpAll) we only want to dump
34 /// the first few levels of an AST. This keeps track of how many ast levels
35 /// are left.
36 unsigned MaxDepth;
Mike Stump11289f42009-09-09 15:08:12 +000037
Chris Lattner11e30d32007-08-30 06:17:34 +000038 /// LastLocFilename/LastLocLine - Keep track of the last location we print
39 /// out so that we can print out deltas from then on out.
40 const char *LastLocFilename;
41 unsigned LastLocLine;
Douglas Gregor7de59662009-05-29 20:38:28 +000042
Chris Lattnercbe4f772007-08-08 22:51:59 +000043 public:
Daniel Dunbar34a96c82009-12-03 09:13:13 +000044 StmtDumper(SourceManager *sm, llvm::raw_ostream &os, unsigned maxDepth)
45 : SM(sm), OS(os), IndentLevel(0-1), MaxDepth(maxDepth) {
Chris Lattner11e30d32007-08-30 06:17:34 +000046 LastLocFilename = "";
47 LastLocLine = ~0U;
48 }
Mike Stump11289f42009-09-09 15:08:12 +000049
Chris Lattner8f184b12007-08-09 18:03:18 +000050 void DumpSubTree(Stmt *S) {
Chris Lattnercbe4f772007-08-08 22:51:59 +000051 // Prune the recursion if not using dump all.
52 if (MaxDepth == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +000053
Chris Lattner8f184b12007-08-09 18:03:18 +000054 ++IndentLevel;
Chris Lattnercbe4f772007-08-08 22:51:59 +000055 if (S) {
Ted Kremenek433a4922007-12-12 06:59:42 +000056 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
57 VisitDeclStmt(DS);
Mike Stump11289f42009-09-09 15:08:12 +000058 else {
Ted Kremenek433a4922007-12-12 06:59:42 +000059 Visit(S);
Mike Stump11289f42009-09-09 15:08:12 +000060
Ted Kremenek433a4922007-12-12 06:59:42 +000061 // Print out children.
62 Stmt::child_iterator CI = S->child_begin(), CE = S->child_end();
63 if (CI != CE) {
64 while (CI != CE) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +000065 OS << '\n';
Ted Kremenek433a4922007-12-12 06:59:42 +000066 DumpSubTree(*CI++);
67 }
Chris Lattnercfb83dd2007-08-30 00:53:54 +000068 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +000069 OS << ')';
Chris Lattnercfb83dd2007-08-30 00:53:54 +000070 }
Chris Lattnercbe4f772007-08-08 22:51:59 +000071 } else {
72 Indent();
Daniel Dunbar34a96c82009-12-03 09:13:13 +000073 OS << "<<<NULL>>>";
Chris Lattnercbe4f772007-08-08 22:51:59 +000074 }
Chris Lattner8f184b12007-08-09 18:03:18 +000075 --IndentLevel;
Chris Lattnercbe4f772007-08-08 22:51:59 +000076 }
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattner8f184b12007-08-09 18:03:18 +000078 void DumpDeclarator(Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +000079
Chris Lattnercbe4f772007-08-08 22:51:59 +000080 void Indent() const {
81 for (int i = 0, e = IndentLevel; i < e; ++i)
Daniel Dunbar34a96c82009-12-03 09:13:13 +000082 OS << " ";
Chris Lattnercbe4f772007-08-08 22:51:59 +000083 }
Mike Stump11289f42009-09-09 15:08:12 +000084
Steve Naroff42a350a2007-09-01 21:08:38 +000085 void DumpType(QualType T) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +000086 OS << "'" << T.getAsString() << "'";
Chris Lattner9bcd9152007-08-09 00:36:22 +000087
Douglas Gregor58354032008-12-24 00:01:03 +000088 if (!T.isNull()) {
John McCall8ccfcb52009-09-24 19:53:00 +000089 // If the type is sugared, also dump a (shallow) desugared type.
90 QualType Simplified = T.getDesugaredType();
91 if (Simplified != T)
Daniel Dunbar34a96c82009-12-03 09:13:13 +000092 OS << ":'" << Simplified.getAsString() << "'";
Chris Lattner091718d2008-04-02 05:06:23 +000093 }
Chris Lattner9bcd9152007-08-09 00:36:22 +000094 }
Steve Naroff42a350a2007-09-01 21:08:38 +000095 void DumpStmt(const Stmt *Node) {
Chris Lattnercbe4f772007-08-08 22:51:59 +000096 Indent();
Daniel Dunbar34a96c82009-12-03 09:13:13 +000097 OS << "(" << Node->getStmtClassName()
98 << " " << (void*)Node;
Steve Naroff42a350a2007-09-01 21:08:38 +000099 DumpSourceRange(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000100 }
Steve Naroff42a350a2007-09-01 21:08:38 +0000101 void DumpExpr(const Expr *Node) {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000102 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000103 OS << ' ';
Chris Lattner9bcd9152007-08-09 00:36:22 +0000104 DumpType(Node->getType());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000105 }
Steve Naroff42a350a2007-09-01 21:08:38 +0000106 void DumpSourceRange(const Stmt *Node);
Chris Lattner11e30d32007-08-30 06:17:34 +0000107 void DumpLocation(SourceLocation Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000108
Chris Lattner84ca3762007-08-30 01:00:35 +0000109 // Stmts.
Chris Lattner62249a62007-08-21 04:04:25 +0000110 void VisitStmt(Stmt *Node);
Ted Kremenek433a4922007-12-12 06:59:42 +0000111 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000112 void VisitLabelStmt(LabelStmt *Node);
113 void VisitGotoStmt(GotoStmt *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattner84ca3762007-08-30 01:00:35 +0000115 // Exprs
116 void VisitExpr(Expr *Node);
Anders Carlssond7923c62009-08-22 23:33:40 +0000117 void VisitCastExpr(CastExpr *Node);
Anders Carlsson66010572009-11-14 22:35:18 +0000118 void VisitImplicitCastExpr(ImplicitCastExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000119 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattner6307f192008-08-10 01:53:14 +0000120 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000121 void VisitCharacterLiteral(CharacterLiteral *Node);
122 void VisitIntegerLiteral(IntegerLiteral *Node);
123 void VisitFloatingLiteral(FloatingLiteral *Node);
124 void VisitStringLiteral(StringLiteral *Str);
125 void VisitUnaryOperator(UnaryOperator *Node);
Sebastian Redl6f282892008-11-11 17:56:53 +0000126 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000127 void VisitMemberExpr(MemberExpr *Node);
Nate Begemance4d7fc2008-04-18 23:10:10 +0000128 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000129 void VisitBinaryOperator(BinaryOperator *Node);
130 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
131 void VisitAddrLabelExpr(AddrLabelExpr *Node);
132 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
133
134 // C++
Douglas Gregore200adc2008-10-27 19:41:14 +0000135 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000136 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000137 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregore200adc2008-10-27 19:41:14 +0000138 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Anders Carlsson073846832009-08-12 00:21:52 +0000139 void VisitCXXConstructExpr(CXXConstructExpr *Node);
140 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
141 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node);
John McCall76d09942009-12-11 21:50:11 +0000142 void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node);
Anders Carlsson073846832009-08-12 00:21:52 +0000143 void DumpCXXTemporary(CXXTemporary *Temporary);
Mike Stump11289f42009-09-09 15:08:12 +0000144
Chris Lattner84ca3762007-08-30 01:00:35 +0000145 // ObjC
Douglas Gregor96c79492010-04-23 22:50:49 +0000146 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000147 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenek36748da2008-02-29 22:04:05 +0000148 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000149 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000150 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000151 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000152 void VisitObjCImplicitSetterGetterRefExpr(
153 ObjCImplicitSetterGetterRefExpr *Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +0000154 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000155 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000156 };
157}
158
159//===----------------------------------------------------------------------===//
Chris Lattner11e30d32007-08-30 06:17:34 +0000160// Utilities
161//===----------------------------------------------------------------------===//
162
163void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattner53e384f2009-01-16 07:00:02 +0000164 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000165
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000166 if (SpellingLoc.isInvalid()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000167 OS << "<invalid sloc>";
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000168 return;
169 }
Chris Lattner11e30d32007-08-30 06:17:34 +0000170
171 // The general format we print out is filename:line:col, but we drop pieces
172 // that haven't changed since the last loc printed.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000173 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
174
175 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000176 OS << PLoc.getFilename() << ':' << PLoc.getLine()
177 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000178 LastLocFilename = PLoc.getFilename();
179 LastLocLine = PLoc.getLine();
180 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000181 OS << "line" << ':' << PLoc.getLine()
182 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000183 LastLocLine = PLoc.getLine();
Chris Lattner11e30d32007-08-30 06:17:34 +0000184 } else {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000185 OS << "col" << ':' << PLoc.getColumn();
Chris Lattner11e30d32007-08-30 06:17:34 +0000186 }
187}
188
Steve Naroff42a350a2007-09-01 21:08:38 +0000189void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattner11e30d32007-08-30 06:17:34 +0000190 // Can't translate locations if a SourceManager isn't available.
191 if (SM == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +0000192
Chris Lattner11e30d32007-08-30 06:17:34 +0000193 // TODO: If the parent expression is available, we can print a delta vs its
194 // location.
195 SourceRange R = Node->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000196
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000197 OS << " <";
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000198 DumpLocation(R.getBegin());
199 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000200 OS << ", ";
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000201 DumpLocation(R.getEnd());
Chris Lattner11e30d32007-08-30 06:17:34 +0000202 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000203 OS << ">";
Mike Stump11289f42009-09-09 15:08:12 +0000204
Chris Lattner11e30d32007-08-30 06:17:34 +0000205 // <t2.c:123:421[blah], t2.c:412:321>
206
207}
208
209
210//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +0000211// Stmt printing methods.
212//===----------------------------------------------------------------------===//
213
214void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner84ca3762007-08-30 01:00:35 +0000215 DumpStmt(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000216}
217
Chris Lattner8f184b12007-08-09 18:03:18 +0000218void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000219 // FIXME: Need to complete/beautify this... this code simply shows the
220 // nodes are where they need to be.
221 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000222 OS << "\"typedef " << localType->getUnderlyingType().getAsString()
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000223 << ' ' << localType << '"';
Chris Lattnercbe4f772007-08-08 22:51:59 +0000224 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000225 OS << "\"";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000226 // Emit storage class for vardecls.
227 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +0000228 if (V->getStorageClass() != VarDecl::None)
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000229 OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
230 << " ";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000231 }
Mike Stump11289f42009-09-09 15:08:12 +0000232
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000233 std::string Name = VD->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +0000234 VD->getType().getAsStringInternal(Name,
Chris Lattnerc61089a2009-06-30 01:26:17 +0000235 PrintingPolicy(VD->getASTContext().getLangOptions()));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000236 OS << Name;
Mike Stump11289f42009-09-09 15:08:12 +0000237
Chris Lattnercbe4f772007-08-08 22:51:59 +0000238 // If this is a vardecl with an initializer, emit it.
239 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
240 if (V->getInit()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000241 OS << " =\n";
Chris Lattner8f184b12007-08-09 18:03:18 +0000242 DumpSubTree(V->getInit());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000243 }
244 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000245 OS << '"';
Steve Naroff14f5f792007-11-17 21:37:36 +0000246 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
247 // print a free standing tag decl (e.g. "struct x;").
248 const char *tagname;
249 if (const IdentifierInfo *II = TD->getIdentifier())
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000250 tagname = II->getNameStart();
Steve Naroff14f5f792007-11-17 21:37:36 +0000251 else
252 tagname = "<anonymous>";
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000253 OS << '"' << TD->getKindName() << ' ' << tagname << ";\"";
Steve Naroff14f5f792007-11-17 21:37:36 +0000254 // FIXME: print tag bodies.
Douglas Gregor889ceb72009-02-03 19:21:40 +0000255 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
256 // print using-directive decl (e.g. "using namespace x;")
257 const char *ns;
258 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000259 ns = II->getNameStart();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000260 else
261 ns = "<anonymous>";
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000262 OS << '"' << UD->getDeclKindName() << ns << ";\"";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000263 } else {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000264 assert(0 && "Unexpected decl");
265 }
Chris Lattnercbe4f772007-08-08 22:51:59 +0000266}
267
Ted Kremenek433a4922007-12-12 06:59:42 +0000268void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
269 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000270 OS << "\n";
Ted Kremenek62408482008-10-06 18:38:35 +0000271 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
272 DI != DE; ++DI) {
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000273 Decl* D = *DI;
Ted Kremenek433a4922007-12-12 06:59:42 +0000274 ++IndentLevel;
275 Indent();
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000276 OS << (void*) D << " ";
Ted Kremenek433a4922007-12-12 06:59:42 +0000277 DumpDeclarator(D);
Chris Lattner3d954d52009-03-29 16:04:50 +0000278 if (DI+1 != DE)
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000279 OS << "\n";
Ted Kremenek433a4922007-12-12 06:59:42 +0000280 --IndentLevel;
281 }
282}
283
Chris Lattnercbe4f772007-08-08 22:51:59 +0000284void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
285 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000286 OS << " '" << Node->getName() << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000287}
288
Chris Lattnercbe4f772007-08-08 22:51:59 +0000289void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
290 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000291 OS << " '" << Node->getLabel()->getName()
292 << "':" << (void*)Node->getLabel();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000293}
294
Chris Lattnercbe4f772007-08-08 22:51:59 +0000295//===----------------------------------------------------------------------===//
296// Expr printing methods.
297//===----------------------------------------------------------------------===//
298
299void StmtDumper::VisitExpr(Expr *Node) {
300 DumpExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000301}
302
Anders Carlssona70cff62010-04-24 19:06:50 +0000303static void DumpBasePath(llvm::raw_ostream &OS, CastExpr *Node) {
304 if (Node->getBasePath().empty())
305 return;
306
307 OS << " (";
308 bool First = true;
309 for (CXXBaseSpecifierArray::iterator I = Node->getBasePath().begin(),
310 E = Node->getBasePath().end(); I != E; ++I) {
311 const CXXBaseSpecifier *Base = *I;
312 if (!First)
313 OS << " -> ";
314
315 const CXXRecordDecl *RD =
316 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
317
318 if (Base->isVirtual())
319 OS << "virtual ";
320 OS << RD->getName();
321 First = false;
322 }
323
324 OS << ')';
325}
326
Anders Carlssond7923c62009-08-22 23:33:40 +0000327void StmtDumper::VisitCastExpr(CastExpr *Node) {
328 DumpExpr(Node);
Anders Carlssona70cff62010-04-24 19:06:50 +0000329 OS << " <" << Node->getCastKindName();
330 DumpBasePath(OS, Node);
331 OS << ">";
Anders Carlssond7923c62009-08-22 23:33:40 +0000332}
333
Anders Carlsson66010572009-11-14 22:35:18 +0000334void StmtDumper::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
335 VisitCastExpr(Node);
336 if (Node->isLvalueCast())
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000337 OS << " lvalue";
Anders Carlsson66010572009-11-14 22:35:18 +0000338}
339
Chris Lattnercbe4f772007-08-08 22:51:59 +0000340void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
341 DumpExpr(Node);
Ted Kremenek5f64ca82007-09-10 17:32:55 +0000342
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000343 OS << " ";
Ted Kremenek5f64ca82007-09-10 17:32:55 +0000344 switch (Node->getDecl()->getKind()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000345 default: OS << "Decl"; break;
346 case Decl::Function: OS << "FunctionDecl"; break;
347 case Decl::Var: OS << "Var"; break;
348 case Decl::ParmVar: OS << "ParmVar"; break;
349 case Decl::EnumConstant: OS << "EnumConstant"; break;
350 case Decl::Typedef: OS << "Typedef"; break;
351 case Decl::Record: OS << "Record"; break;
352 case Decl::Enum: OS << "Enum"; break;
353 case Decl::CXXRecord: OS << "CXXRecord"; break;
354 case Decl::ObjCInterface: OS << "ObjCInterface"; break;
355 case Decl::ObjCClass: OS << "ObjCClass"; break;
Ted Kremenek5f64ca82007-09-10 17:32:55 +0000356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000358 OS << "='" << Node->getDecl() << "' " << (void*)Node->getDecl();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000359}
360
John McCall76d09942009-12-11 21:50:11 +0000361void StmtDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
362 DumpExpr(Node);
363 OS << " (";
364 if (!Node->requiresADL()) OS << "no ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000365 OS << "ADL) = '" << Node->getName() << '\'';
John McCall76d09942009-12-11 21:50:11 +0000366
367 UnresolvedLookupExpr::decls_iterator
368 I = Node->decls_begin(), E = Node->decls_end();
369 if (I == E) OS << " empty";
370 for (; I != E; ++I)
371 OS << " " << (void*) *I;
372}
373
Steve Naroff5d5efca2008-03-12 13:19:12 +0000374void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroffe3fa7132008-05-23 00:59:14 +0000375 DumpExpr(Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +0000376
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000377 OS << " " << Node->getDecl()->getDeclKindName()
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000378 << "Decl='" << Node->getDecl()
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000379 << "' " << (void*)Node->getDecl();
Steve Naroffb3424a92008-05-23 22:01:24 +0000380 if (Node->isFreeIvar())
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000381 OS << " isFreeIvar";
Steve Naroff5d5efca2008-03-12 13:19:12 +0000382}
383
Chris Lattner6307f192008-08-10 01:53:14 +0000384void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000385 DumpExpr(Node);
386 switch (Node->getIdentType()) {
Chris Lattnera9b3cae2008-06-21 18:04:54 +0000387 default: assert(0 && "unknown case");
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000388 case PredefinedExpr::Func: OS << " __func__"; break;
389 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
390 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Chris Lattnercbe4f772007-08-08 22:51:59 +0000391 }
392}
393
394void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner273a1ea2007-08-09 01:04:32 +0000395 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000396 OS << Node->getValue();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000397}
398
399void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
400 DumpExpr(Node);
401
402 bool isSigned = Node->getType()->isSignedIntegerType();
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000403 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000404}
405void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
406 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000407 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000408}
Chris Lattner1c20a172007-08-26 03:42:43 +0000409
Chris Lattnercbe4f772007-08-08 22:51:59 +0000410void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner273a1ea2007-08-09 01:04:32 +0000411 DumpExpr(Str);
412 // FIXME: this doesn't print wstrings right.
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000413 OS << " ";
414 if (Str->isWide())
415 OS << "L";
416 OS << '"';
417 OS.write_escaped(llvm::StringRef(Str->getStrData(),
418 Str->getByteLength()));
419 OS << '"';
Chris Lattnercbe4f772007-08-08 22:51:59 +0000420}
Chris Lattner84ca3762007-08-30 01:00:35 +0000421
Chris Lattnercbe4f772007-08-08 22:51:59 +0000422void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000423 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000424 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
425 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000426}
Sebastian Redl6f282892008-11-11 17:56:53 +0000427void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000428 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000429 OS << " " << (Node->isSizeOf() ? "sizeof" : "alignof") << " ";
Sebastian Redl6f282892008-11-11 17:56:53 +0000430 if (Node->isArgumentType())
431 DumpType(Node->getArgumentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000432}
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000433
Chris Lattnercbe4f772007-08-08 22:51:59 +0000434void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000435 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000436 OS << " " << (Node->isArrow() ? "->" : ".")
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000437 << Node->getMemberDecl() << ' '
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000438 << (void*)Node->getMemberDecl();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000439}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000440void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000441 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000442 OS << " " << Node->getAccessor().getNameStart();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000443}
Chris Lattnercbe4f772007-08-08 22:51:59 +0000444void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
445 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000446 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner86928112007-08-25 02:00:02 +0000447}
448void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
449 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000450 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
451 << "' ComputeLHSTy=";
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000452 DumpType(Node->getComputationLHSType());
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000453 OS << " ComputeResultTy=";
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000454 DumpType(Node->getComputationResultType());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000455}
Chris Lattnercbe4f772007-08-08 22:51:59 +0000456
457// GNU extensions.
458
459void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000460 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000461 OS << " " << Node->getLabel()->getName()
462 << " " << (void*)Node->getLabel();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000463}
464
Chris Lattnercbe4f772007-08-08 22:51:59 +0000465void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000466 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000467 OS << " ";
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000468 DumpType(Node->getArgType1());
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000469 OS << " ";
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000470 DumpType(Node->getArgType2());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000471}
472
Chris Lattner8f184b12007-08-09 18:03:18 +0000473//===----------------------------------------------------------------------===//
474// C++ Expressions
475//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +0000476
Douglas Gregore200adc2008-10-27 19:41:14 +0000477void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000478 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000479 OS << " " << Node->getCastName()
480 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlssona70cff62010-04-24 19:06:50 +0000481 << " <" << Node->getCastKindName();
482 DumpBasePath(OS, Node);
483 OS << ">";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000484}
485
486void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000487 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000488 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattnercbe4f772007-08-08 22:51:59 +0000489}
490
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000491void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
492 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000493 OS << " this";
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000494}
495
Douglas Gregore200adc2008-10-27 19:41:14 +0000496void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
497 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000498 OS << " functional cast to " << Node->getTypeAsWritten().getAsString();
Douglas Gregore200adc2008-10-27 19:41:14 +0000499}
500
Anders Carlsson073846832009-08-12 00:21:52 +0000501void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
502 DumpExpr(Node);
John McCalleba90cd2010-02-02 19:03:45 +0000503 CXXConstructorDecl *Ctor = Node->getConstructor();
504 DumpType(Ctor->getType());
Anders Carlsson073846832009-08-12 00:21:52 +0000505 if (Node->isElidable())
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000506 OS << " elidable";
Anders Carlsson073846832009-08-12 00:21:52 +0000507}
508
509void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
510 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000511 OS << " ";
Anders Carlsson073846832009-08-12 00:21:52 +0000512 DumpCXXTemporary(Node->getTemporary());
513}
514
515void StmtDumper::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node) {
516 DumpExpr(Node);
517 ++IndentLevel;
518 for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000519 OS << "\n";
Anders Carlsson073846832009-08-12 00:21:52 +0000520 Indent();
521 DumpCXXTemporary(Node->getTemporary(i));
522 }
523 --IndentLevel;
524}
525
526void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000527 OS << "(CXXTemporary " << (void *)Temporary << ")";
Anders Carlsson073846832009-08-12 00:21:52 +0000528}
529
Anders Carlsson76f4a902007-08-21 17:43:55 +0000530//===----------------------------------------------------------------------===//
531// Obj-C Expressions
532//===----------------------------------------------------------------------===//
533
Ted Kremenek36748da2008-02-29 22:04:05 +0000534void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
535 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000536 OS << " selector=" << Node->getSelector().getAsString();
Douglas Gregor9a129192010-04-21 00:45:42 +0000537 switch (Node->getReceiverKind()) {
538 case ObjCMessageExpr::Instance:
539 break;
540
541 case ObjCMessageExpr::Class:
542 OS << " class=";
543 DumpType(Node->getClassReceiver());
544 break;
545
546 case ObjCMessageExpr::SuperInstance:
547 OS << " super (instance)";
548 break;
549
550 case ObjCMessageExpr::SuperClass:
551 OS << " super (class)";
552 break;
553 }
Ted Kremenek36748da2008-02-29 22:04:05 +0000554}
555
Douglas Gregor96c79492010-04-23 22:50:49 +0000556void StmtDumper::VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node) {
557 DumpStmt(Node);
Douglas Gregor46a572b2010-04-26 16:46:50 +0000558 if (VarDecl *CatchParam = Node->getCatchParamDecl()) {
Douglas Gregor96c79492010-04-23 22:50:49 +0000559 OS << " catch parm = ";
560 DumpDeclarator(CatchParam);
561 } else {
562 OS << " catch all";
563 }
564}
565
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000566void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
567 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000568 OS << " ";
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000569 DumpType(Node->getEncodedType());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000570}
571
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000572void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
573 DumpExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +0000574
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000575 OS << " " << Node->getSelector().getAsString();
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000576}
577
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000578void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
579 DumpExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +0000580
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000581 OS << ' ' << Node->getProtocol();
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000582}
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000583
584void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
585 DumpExpr(Node);
Daniel Dunbarc5d33042008-09-03 00:27:26 +0000586
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000587 OS << " Kind=PropertyRef Property=\"" << Node->getProperty() << '"';
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000588}
589
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000590void StmtDumper::VisitObjCImplicitSetterGetterRefExpr(
591 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000592 DumpExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +0000593
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000594 ObjCMethodDecl *Getter = Node->getGetterMethod();
595 ObjCMethodDecl *Setter = Node->getSetterMethod();
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000596 OS << " Kind=MethodRef Getter=\""
597 << Getter->getSelector().getAsString()
598 << "\" Setter=\"";
599 if (Setter)
600 OS << Setter->getSelector().getAsString();
601 else
602 OS << "(null)";
603 OS << "\"";
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000604}
605
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000606void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
607 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000608 OS << " super";
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000609}
610
Chris Lattnercbe4f772007-08-08 22:51:59 +0000611//===----------------------------------------------------------------------===//
612// Stmt method implementations
613//===----------------------------------------------------------------------===//
614
615/// dump - This does a local dump of the specified AST fragment. It dumps the
616/// specified node and a few nodes underneath it, but not the whole subtree.
617/// This is useful in a debugger.
Chris Lattner11e30d32007-08-30 06:17:34 +0000618void Stmt::dump(SourceManager &SM) const {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000619 StmtDumper P(&SM, llvm::errs(), 4);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000620 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000621 llvm::errs() << "\n";
Chris Lattner779d5d92007-08-30 00:40:08 +0000622}
623
624/// dump - This does a local dump of the specified AST fragment. It dumps the
625/// specified node and a few nodes underneath it, but not the whole subtree.
626/// This is useful in a debugger.
Chris Lattnercbe4f772007-08-08 22:51:59 +0000627void Stmt::dump() const {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000628 StmtDumper P(0, llvm::errs(), 4);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000629 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000630 llvm::errs() << "\n";
Chris Lattner779d5d92007-08-30 00:40:08 +0000631}
632
633/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattner11e30d32007-08-30 06:17:34 +0000634void Stmt::dumpAll(SourceManager &SM) const {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000635 StmtDumper P(&SM, llvm::errs(), ~0U);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000636 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000637 llvm::errs() << "\n";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000638}
639
640/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
641void Stmt::dumpAll() const {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000642 StmtDumper P(0, llvm::errs(), ~0U);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000643 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000644 llvm::errs() << "\n";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000645}