blob: 62dcf60be05e5b7c944c85a6754352732fe25c42 [file] [log] [blame]
Chris Lattner6000dac2007-08-08 22:51:59 +00001//===--- StmtDumper.cpp - Dumping implementation for Stmt ASTs ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner6000dac2007-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 Kremenek91d1d7a2007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000018#include "clang/AST/PrettyPrinter.h"
Chris Lattnere300c872007-08-30 06:17:34 +000019#include "clang/Basic/SourceManager.h"
Daniel Dunbar806c12e2009-12-03 09:13:13 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtDumper Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000028 class StmtDumper : public StmtVisitor<StmtDumper> {
Chris Lattnere300c872007-08-30 06:17:34 +000029 SourceManager *SM;
Daniel Dunbar806c12e2009-12-03 09:13:13 +000030 llvm::raw_ostream &OS;
Chris Lattner6000dac2007-08-08 22:51:59 +000031 unsigned IndentLevel;
Mike Stump1eb44332009-09-09 15:08:12 +000032
Chris Lattner6000dac2007-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 Stump1eb44332009-09-09 15:08:12 +000037
Chris Lattnere300c872007-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 Gregord249e1d1f2009-05-29 20:38:28 +000042
Chris Lattner6000dac2007-08-08 22:51:59 +000043 public:
Daniel Dunbar806c12e2009-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 Lattnere300c872007-08-30 06:17:34 +000046 LastLocFilename = "";
47 LastLocLine = ~0U;
48 }
Mike Stump1eb44332009-09-09 15:08:12 +000049
Chris Lattnerf9e05812007-08-09 18:03:18 +000050 void DumpSubTree(Stmt *S) {
Chris Lattner6000dac2007-08-08 22:51:59 +000051 // Prune the recursion if not using dump all.
52 if (MaxDepth == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +000053
Chris Lattnerf9e05812007-08-09 18:03:18 +000054 ++IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000055 if (S) {
Ted Kremenek5399ce22007-12-12 06:59:42 +000056 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
57 VisitDeclStmt(DS);
Mike Stump1eb44332009-09-09 15:08:12 +000058 else {
Ted Kremenek5399ce22007-12-12 06:59:42 +000059 Visit(S);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Ted Kremenek5399ce22007-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 Dunbar806c12e2009-12-03 09:13:13 +000065 OS << '\n';
Ted Kremenek5399ce22007-12-12 06:59:42 +000066 DumpSubTree(*CI++);
67 }
Chris Lattnerb3938792007-08-30 00:53:54 +000068 }
69 }
Chris Lattnera46325e2010-05-25 17:56:43 +000070 OS << ')';
Chris Lattner6000dac2007-08-08 22:51:59 +000071 } else {
72 Indent();
Daniel Dunbar806c12e2009-12-03 09:13:13 +000073 OS << "<<<NULL>>>";
Chris Lattner6000dac2007-08-08 22:51:59 +000074 }
Chris Lattnerf9e05812007-08-09 18:03:18 +000075 --IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000076 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattnerf9e05812007-08-09 18:03:18 +000078 void DumpDeclarator(Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Chris Lattner6000dac2007-08-08 22:51:59 +000080 void Indent() const {
81 for (int i = 0, e = IndentLevel; i < e; ++i)
Daniel Dunbar806c12e2009-12-03 09:13:13 +000082 OS << " ";
Chris Lattner6000dac2007-08-08 22:51:59 +000083 }
Mike Stump1eb44332009-09-09 15:08:12 +000084
Steve Naroff9dcbfa42007-09-01 21:08:38 +000085 void DumpType(QualType T) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +000086 OS << "'" << T.getAsString() << "'";
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000087
Douglas Gregor61366e92008-12-24 00:01:03 +000088 if (!T.isNull()) {
John McCall0953e762009-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 Dunbar806c12e2009-12-03 09:13:13 +000092 OS << ":'" << Simplified.getAsString() << "'";
Chris Lattnerbad37852008-04-02 05:06:23 +000093 }
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000094 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +000095 void DumpStmt(const Stmt *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +000096 Indent();
Daniel Dunbar806c12e2009-12-03 09:13:13 +000097 OS << "(" << Node->getStmtClassName()
98 << " " << (void*)Node;
Steve Naroff9dcbfa42007-09-01 21:08:38 +000099 DumpSourceRange(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000100 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000101 void DumpExpr(const Expr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000102 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000103 OS << ' ';
Chris Lattnerfd8f7da2007-08-09 00:36:22 +0000104 DumpType(Node->getType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000105 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000106 void DumpSourceRange(const Stmt *Node);
Chris Lattnere300c872007-08-30 06:17:34 +0000107 void DumpLocation(SourceLocation Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner17a1a722007-08-30 01:00:35 +0000109 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000110 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000111 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000112 void VisitLabelStmt(LabelStmt *Node);
113 void VisitGotoStmt(GotoStmt *Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner17a1a722007-08-30 01:00:35 +0000115 // Exprs
116 void VisitExpr(Expr *Node);
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000117 void VisitCastExpr(CastExpr *Node);
Anders Carlsson0e489ea2009-11-14 22:35:18 +0000118 void VisitImplicitCastExpr(ImplicitCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000119 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000120 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-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 Redl05189992008-11-11 17:56:53 +0000126 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000127 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000128 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-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 Gregor49badde2008-10-27 19:41:14 +0000135 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000136 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000137 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000138 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000139 void VisitCXXConstructExpr(CXXConstructExpr *Node);
140 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
141 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node);
John McCall9d5f35e2009-12-11 21:50:11 +0000142 void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node);
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000143 void DumpCXXTemporary(CXXTemporary *Temporary);
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Chris Lattner17a1a722007-08-30 01:00:35 +0000145 // ObjC
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000146 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000147 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000148 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000149 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000150 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000151 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000152 void VisitObjCImplicitSetterGetterRefExpr(
153 ObjCImplicitSetterGetterRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000154 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000155 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000156 };
157}
158
159//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000160// Utilities
161//===----------------------------------------------------------------------===//
162
163void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000164 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000166 if (SpellingLoc.isInvalid()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000167 OS << "<invalid sloc>";
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000168 return;
169 }
Chris Lattnere300c872007-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 Lattnerb9c3f962009-01-27 07:57:44 +0000173 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
174
175 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000176 OS << PLoc.getFilename() << ':' << PLoc.getLine()
177 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000178 LastLocFilename = PLoc.getFilename();
179 LastLocLine = PLoc.getLine();
180 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000181 OS << "line" << ':' << PLoc.getLine()
182 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000183 LastLocLine = PLoc.getLine();
Chris Lattnere300c872007-08-30 06:17:34 +0000184 } else {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000185 OS << "col" << ':' << PLoc.getColumn();
Chris Lattnere300c872007-08-30 06:17:34 +0000186 }
187}
188
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000189void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000190 // Can't translate locations if a SourceManager isn't available.
191 if (SM == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattnere300c872007-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 Stump1eb44332009-09-09 15:08:12 +0000196
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000197 OS << " <";
Chris Lattner311ff022007-10-16 22:36:42 +0000198 DumpLocation(R.getBegin());
199 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000200 OS << ", ";
Chris Lattner311ff022007-10-16 22:36:42 +0000201 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000202 }
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000203 OS << ">";
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Chris Lattnere300c872007-08-30 06:17:34 +0000205 // <t2.c:123:421[blah], t2.c:412:321>
206
207}
208
209
210//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000211// Stmt printing methods.
212//===----------------------------------------------------------------------===//
213
214void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000215 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000216}
217
Chris Lattnerf9e05812007-08-09 18:03:18 +0000218void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-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 Dunbar806c12e2009-12-03 09:13:13 +0000222 OS << "\"typedef " << localType->getUnderlyingType().getAsString()
Benjamin Kramer900fc632010-04-17 09:33:03 +0000223 << ' ' << localType << '"';
Chris Lattner6000dac2007-08-08 22:51:59 +0000224 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000225 OS << "\"";
Chris Lattner6000dac2007-08-08 22:51:59 +0000226 // Emit storage class for vardecls.
227 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000228 if (V->getStorageClass() != VarDecl::None)
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000229 OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
230 << " ";
Chris Lattner6000dac2007-08-08 22:51:59 +0000231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattner39f34e92008-11-24 04:00:27 +0000233 std::string Name = VD->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000234 VD->getType().getAsStringInternal(Name,
Chris Lattnere4f21422009-06-30 01:26:17 +0000235 PrintingPolicy(VD->getASTContext().getLangOptions()));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000236 OS << Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattner6000dac2007-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 Dunbar806c12e2009-12-03 09:13:13 +0000241 OS << " =\n";
Chris Lattnerf9e05812007-08-09 18:03:18 +0000242 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000243 }
244 }
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000245 OS << '"';
Steve Naroff92199282007-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 Dunbare013d682009-10-18 20:26:12 +0000250 tagname = II->getNameStart();
Steve Naroff92199282007-11-17 21:37:36 +0000251 else
252 tagname = "<anonymous>";
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000253 OS << '"' << TD->getKindName() << ' ' << tagname << ";\"";
Steve Naroff92199282007-11-17 21:37:36 +0000254 // FIXME: print tag bodies.
Douglas Gregor2a3009a2009-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 Dunbare013d682009-10-18 20:26:12 +0000259 ns = II->getNameStart();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000260 else
261 ns = "<anonymous>";
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000262 OS << '"' << UD->getDeclKindName() << ns << ";\"";
Sebastian Redl0ca43592010-05-04 10:20:17 +0000263 } else if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
264 // print using decl (e.g. "using std::string;")
265 const char *tn = UD->isTypeName() ? "typename " : "";
266 OS << '"' << UD->getDeclKindName() << tn;
267 UD->getTargetNestedNameDecl()->print(OS,
268 PrintingPolicy(UD->getASTContext().getLangOptions()));
269 OS << ";\"";
Chris Lattner6000dac2007-08-08 22:51:59 +0000270 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000271 assert(0 && "Unexpected decl");
272 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000273}
274
Ted Kremenek5399ce22007-12-12 06:59:42 +0000275void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
276 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000277 OS << "\n";
Ted Kremenek04a72b72008-10-06 18:38:35 +0000278 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
279 DI != DE; ++DI) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000280 Decl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000281 ++IndentLevel;
282 Indent();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000283 OS << (void*) D << " ";
Ted Kremenek5399ce22007-12-12 06:59:42 +0000284 DumpDeclarator(D);
Chris Lattnerf2797252009-03-29 16:04:50 +0000285 if (DI+1 != DE)
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000286 OS << "\n";
Ted Kremenek5399ce22007-12-12 06:59:42 +0000287 --IndentLevel;
288 }
289}
290
Chris Lattner6000dac2007-08-08 22:51:59 +0000291void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
292 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000293 OS << " '" << Node->getName() << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +0000294}
295
Chris Lattner6000dac2007-08-08 22:51:59 +0000296void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
297 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000298 OS << " '" << Node->getLabel()->getName()
299 << "':" << (void*)Node->getLabel();
Chris Lattner6000dac2007-08-08 22:51:59 +0000300}
301
Chris Lattner6000dac2007-08-08 22:51:59 +0000302//===----------------------------------------------------------------------===//
303// Expr printing methods.
304//===----------------------------------------------------------------------===//
305
306void StmtDumper::VisitExpr(Expr *Node) {
307 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000308}
309
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000310static void DumpBasePath(llvm::raw_ostream &OS, CastExpr *Node) {
311 if (Node->getBasePath().empty())
312 return;
313
314 OS << " (";
315 bool First = true;
316 for (CXXBaseSpecifierArray::iterator I = Node->getBasePath().begin(),
317 E = Node->getBasePath().end(); I != E; ++I) {
318 const CXXBaseSpecifier *Base = *I;
319 if (!First)
320 OS << " -> ";
321
322 const CXXRecordDecl *RD =
323 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
324
325 if (Base->isVirtual())
326 OS << "virtual ";
327 OS << RD->getName();
328 First = false;
329 }
330
331 OS << ')';
332}
333
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000334void StmtDumper::VisitCastExpr(CastExpr *Node) {
335 DumpExpr(Node);
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000336 OS << " <" << Node->getCastKindName();
337 DumpBasePath(OS, Node);
338 OS << ">";
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000339}
340
Anders Carlsson0e489ea2009-11-14 22:35:18 +0000341void StmtDumper::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
342 VisitCastExpr(Node);
Sebastian Redl906082e2010-07-20 04:20:21 +0000343 switch (Node->getCategory()) {
344 case ImplicitCastExpr::LValue:
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000345 OS << " lvalue";
Sebastian Redl906082e2010-07-20 04:20:21 +0000346 break;
347 case ImplicitCastExpr::XValue:
348 OS << " xvalue";
349 break;
350 default:
351 break;
352 }
Anders Carlsson0e489ea2009-11-14 22:35:18 +0000353}
354
Chris Lattner6000dac2007-08-08 22:51:59 +0000355void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
356 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000357
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000358 OS << " ";
Ted Kremenekeb641f92007-09-10 17:32:55 +0000359 switch (Node->getDecl()->getKind()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000360 default: OS << "Decl"; break;
361 case Decl::Function: OS << "FunctionDecl"; break;
362 case Decl::Var: OS << "Var"; break;
363 case Decl::ParmVar: OS << "ParmVar"; break;
364 case Decl::EnumConstant: OS << "EnumConstant"; break;
365 case Decl::Typedef: OS << "Typedef"; break;
366 case Decl::Record: OS << "Record"; break;
367 case Decl::Enum: OS << "Enum"; break;
368 case Decl::CXXRecord: OS << "CXXRecord"; break;
369 case Decl::ObjCInterface: OS << "ObjCInterface"; break;
370 case Decl::ObjCClass: OS << "ObjCClass"; break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Benjamin Kramer900fc632010-04-17 09:33:03 +0000373 OS << "='" << Node->getDecl() << "' " << (void*)Node->getDecl();
Chris Lattner6000dac2007-08-08 22:51:59 +0000374}
375
John McCall9d5f35e2009-12-11 21:50:11 +0000376void StmtDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
377 DumpExpr(Node);
378 OS << " (";
379 if (!Node->requiresADL()) OS << "no ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000380 OS << "ADL) = '" << Node->getName() << '\'';
John McCall9d5f35e2009-12-11 21:50:11 +0000381
382 UnresolvedLookupExpr::decls_iterator
383 I = Node->decls_begin(), E = Node->decls_end();
384 if (I == E) OS << " empty";
385 for (; I != E; ++I)
386 OS << " " << (void*) *I;
387}
388
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000389void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000390 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000391
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000392 OS << " " << Node->getDecl()->getDeclKindName()
Benjamin Kramer900fc632010-04-17 09:33:03 +0000393 << "Decl='" << Node->getDecl()
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000394 << "' " << (void*)Node->getDecl();
Steve Naroff218543b2008-05-23 22:01:24 +0000395 if (Node->isFreeIvar())
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000396 OS << " isFreeIvar";
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000397}
398
Chris Lattnerd9f69102008-08-10 01:53:14 +0000399void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000400 DumpExpr(Node);
401 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000402 default: assert(0 && "unknown case");
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000403 case PredefinedExpr::Func: OS << " __func__"; break;
404 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
405 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000406 }
407}
408
409void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000410 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000411 OS << Node->getValue();
Chris Lattner6000dac2007-08-08 22:51:59 +0000412}
413
414void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
415 DumpExpr(Node);
416
417 bool isSigned = Node->getType()->isSignedIntegerType();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000418 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattner6000dac2007-08-08 22:51:59 +0000419}
420void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
421 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000422 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattner6000dac2007-08-08 22:51:59 +0000423}
Chris Lattner5d661452007-08-26 03:42:43 +0000424
Chris Lattner6000dac2007-08-08 22:51:59 +0000425void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000426 DumpExpr(Str);
427 // FIXME: this doesn't print wstrings right.
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000428 OS << " ";
429 if (Str->isWide())
430 OS << "L";
431 OS << '"';
432 OS.write_escaped(llvm::StringRef(Str->getStrData(),
433 Str->getByteLength()));
434 OS << '"';
Chris Lattner6000dac2007-08-08 22:51:59 +0000435}
Chris Lattner17a1a722007-08-30 01:00:35 +0000436
Chris Lattner6000dac2007-08-08 22:51:59 +0000437void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000438 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000439 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
440 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +0000441}
Sebastian Redl05189992008-11-11 17:56:53 +0000442void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000443 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000444 OS << " " << (Node->isSizeOf() ? "sizeof" : "alignof") << " ";
Sebastian Redl05189992008-11-11 17:56:53 +0000445 if (Node->isArgumentType())
446 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000447}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000448
Chris Lattner6000dac2007-08-08 22:51:59 +0000449void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000450 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000451 OS << " " << (Node->isArrow() ? "->" : ".")
Benjamin Kramer900fc632010-04-17 09:33:03 +0000452 << Node->getMemberDecl() << ' '
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000453 << (void*)Node->getMemberDecl();
Chris Lattner6000dac2007-08-08 22:51:59 +0000454}
Nate Begeman213541a2008-04-18 23:10:10 +0000455void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000456 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000457 OS << " " << Node->getAccessor().getNameStart();
Chris Lattner6000dac2007-08-08 22:51:59 +0000458}
Chris Lattner6000dac2007-08-08 22:51:59 +0000459void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
460 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000461 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnereb14fe82007-08-25 02:00:02 +0000462}
463void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
464 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000465 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
466 << "' ComputeLHSTy=";
Eli Friedmanab3a8522009-03-28 01:22:36 +0000467 DumpType(Node->getComputationLHSType());
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000468 OS << " ComputeResultTy=";
Eli Friedmanab3a8522009-03-28 01:22:36 +0000469 DumpType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000470}
Chris Lattner6000dac2007-08-08 22:51:59 +0000471
472// GNU extensions.
473
474void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000475 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000476 OS << " " << Node->getLabel()->getName()
477 << " " << (void*)Node->getLabel();
Chris Lattner6000dac2007-08-08 22:51:59 +0000478}
479
Chris Lattner6000dac2007-08-08 22:51:59 +0000480void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000481 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000482 OS << " ";
Chris Lattner13cb21f2007-08-09 17:35:30 +0000483 DumpType(Node->getArgType1());
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000484 OS << " ";
Chris Lattner13cb21f2007-08-09 17:35:30 +0000485 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000486}
487
Chris Lattnerf9e05812007-08-09 18:03:18 +0000488//===----------------------------------------------------------------------===//
489// C++ Expressions
490//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000491
Douglas Gregor49badde2008-10-27 19:41:14 +0000492void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000493 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000494 OS << " " << Node->getCastName()
495 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000496 << " <" << Node->getCastKindName();
497 DumpBasePath(OS, Node);
498 OS << ">";
Chris Lattner6000dac2007-08-08 22:51:59 +0000499}
500
501void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000502 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000503 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000504}
505
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000506void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
507 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000508 OS << " this";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000509}
510
Douglas Gregor49badde2008-10-27 19:41:14 +0000511void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
512 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000513 OS << " functional cast to " << Node->getTypeAsWritten().getAsString();
Douglas Gregor49badde2008-10-27 19:41:14 +0000514}
515
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000516void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
517 DumpExpr(Node);
John McCalld4bbdfe2010-02-02 19:03:45 +0000518 CXXConstructorDecl *Ctor = Node->getConstructor();
519 DumpType(Ctor->getType());
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000520 if (Node->isElidable())
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000521 OS << " elidable";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000522}
523
524void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
525 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000526 OS << " ";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000527 DumpCXXTemporary(Node->getTemporary());
528}
529
530void StmtDumper::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node) {
531 DumpExpr(Node);
532 ++IndentLevel;
533 for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000534 OS << "\n";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000535 Indent();
536 DumpCXXTemporary(Node->getTemporary(i));
537 }
538 --IndentLevel;
539}
540
541void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000542 OS << "(CXXTemporary " << (void *)Temporary << ")";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000543}
544
Anders Carlsson55085182007-08-21 17:43:55 +0000545//===----------------------------------------------------------------------===//
546// Obj-C Expressions
547//===----------------------------------------------------------------------===//
548
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000549void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
550 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000551 OS << " selector=" << Node->getSelector().getAsString();
Douglas Gregor04badcf2010-04-21 00:45:42 +0000552 switch (Node->getReceiverKind()) {
553 case ObjCMessageExpr::Instance:
554 break;
555
556 case ObjCMessageExpr::Class:
557 OS << " class=";
558 DumpType(Node->getClassReceiver());
559 break;
560
561 case ObjCMessageExpr::SuperInstance:
562 OS << " super (instance)";
563 break;
564
565 case ObjCMessageExpr::SuperClass:
566 OS << " super (class)";
567 break;
568 }
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000569}
570
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000571void StmtDumper::VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node) {
572 DumpStmt(Node);
Douglas Gregorc00d8e12010-04-26 16:46:50 +0000573 if (VarDecl *CatchParam = Node->getCatchParamDecl()) {
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000574 OS << " catch parm = ";
575 DumpDeclarator(CatchParam);
576 } else {
577 OS << " catch all";
578 }
579}
580
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000581void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
582 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000583 OS << " ";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000584 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000585}
586
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000587void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
588 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000590 OS << " " << Node->getSelector().getAsString();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000591}
592
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000593void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
594 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Benjamin Kramer900fc632010-04-17 09:33:03 +0000596 OS << ' ' << Node->getProtocol();
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000597}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000598
599void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
600 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000601
Benjamin Kramer900fc632010-04-17 09:33:03 +0000602 OS << " Kind=PropertyRef Property=\"" << Node->getProperty() << '"';
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000603}
604
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000605void StmtDumper::VisitObjCImplicitSetterGetterRefExpr(
606 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000607 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000609 ObjCMethodDecl *Getter = Node->getGetterMethod();
610 ObjCMethodDecl *Setter = Node->getSetterMethod();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000611 OS << " Kind=MethodRef Getter=\""
612 << Getter->getSelector().getAsString()
613 << "\" Setter=\"";
614 if (Setter)
615 OS << Setter->getSelector().getAsString();
616 else
617 OS << "(null)";
618 OS << "\"";
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000619}
620
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000621void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
622 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000623 OS << " super";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000624}
625
Chris Lattner6000dac2007-08-08 22:51:59 +0000626//===----------------------------------------------------------------------===//
627// Stmt method implementations
628//===----------------------------------------------------------------------===//
629
630/// dump - This does a local dump of the specified AST fragment. It dumps the
631/// specified node and a few nodes underneath it, but not the whole subtree.
632/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000633void Stmt::dump(SourceManager &SM) const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000634 StmtDumper P(&SM, llvm::errs(), 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000635 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000636 llvm::errs() << "\n";
Chris Lattner0c727a32007-08-30 00:40:08 +0000637}
638
639/// dump - This does a local dump of the specified AST fragment. It dumps the
640/// specified node and a few nodes underneath it, but not the whole subtree.
641/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000642void Stmt::dump() const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000643 StmtDumper P(0, llvm::errs(), 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000644 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000645 llvm::errs() << "\n";
Chris Lattner0c727a32007-08-30 00:40:08 +0000646}
647
648/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000649void Stmt::dumpAll(SourceManager &SM) const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000650 StmtDumper P(&SM, llvm::errs(), ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000651 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000652 llvm::errs() << "\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000653}
654
655/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
656void Stmt::dumpAll() const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000657 StmtDumper P(0, llvm::errs(), ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000658 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000659 llvm::errs() << "\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000660}