blob: b388a3b18ffa4fb0c9f05503345af8a37900e04c [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);
343 if (Node->isLvalueCast())
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000344 OS << " lvalue";
Anders Carlsson0e489ea2009-11-14 22:35:18 +0000345}
346
Chris Lattner6000dac2007-08-08 22:51:59 +0000347void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
348 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000349
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000350 OS << " ";
Ted Kremenekeb641f92007-09-10 17:32:55 +0000351 switch (Node->getDecl()->getKind()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000352 default: OS << "Decl"; break;
353 case Decl::Function: OS << "FunctionDecl"; break;
354 case Decl::Var: OS << "Var"; break;
355 case Decl::ParmVar: OS << "ParmVar"; break;
356 case Decl::EnumConstant: OS << "EnumConstant"; break;
357 case Decl::Typedef: OS << "Typedef"; break;
358 case Decl::Record: OS << "Record"; break;
359 case Decl::Enum: OS << "Enum"; break;
360 case Decl::CXXRecord: OS << "CXXRecord"; break;
361 case Decl::ObjCInterface: OS << "ObjCInterface"; break;
362 case Decl::ObjCClass: OS << "ObjCClass"; break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Benjamin Kramer900fc632010-04-17 09:33:03 +0000365 OS << "='" << Node->getDecl() << "' " << (void*)Node->getDecl();
Chris Lattner6000dac2007-08-08 22:51:59 +0000366}
367
John McCall9d5f35e2009-12-11 21:50:11 +0000368void StmtDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
369 DumpExpr(Node);
370 OS << " (";
371 if (!Node->requiresADL()) OS << "no ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000372 OS << "ADL) = '" << Node->getName() << '\'';
John McCall9d5f35e2009-12-11 21:50:11 +0000373
374 UnresolvedLookupExpr::decls_iterator
375 I = Node->decls_begin(), E = Node->decls_end();
376 if (I == E) OS << " empty";
377 for (; I != E; ++I)
378 OS << " " << (void*) *I;
379}
380
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000381void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000382 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000383
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000384 OS << " " << Node->getDecl()->getDeclKindName()
Benjamin Kramer900fc632010-04-17 09:33:03 +0000385 << "Decl='" << Node->getDecl()
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000386 << "' " << (void*)Node->getDecl();
Steve Naroff218543b2008-05-23 22:01:24 +0000387 if (Node->isFreeIvar())
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000388 OS << " isFreeIvar";
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000389}
390
Chris Lattnerd9f69102008-08-10 01:53:14 +0000391void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000392 DumpExpr(Node);
393 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000394 default: assert(0 && "unknown case");
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000395 case PredefinedExpr::Func: OS << " __func__"; break;
396 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
397 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000398 }
399}
400
401void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000402 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000403 OS << Node->getValue();
Chris Lattner6000dac2007-08-08 22:51:59 +0000404}
405
406void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
407 DumpExpr(Node);
408
409 bool isSigned = Node->getType()->isSignedIntegerType();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000410 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattner6000dac2007-08-08 22:51:59 +0000411}
412void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
413 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000414 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattner6000dac2007-08-08 22:51:59 +0000415}
Chris Lattner5d661452007-08-26 03:42:43 +0000416
Chris Lattner6000dac2007-08-08 22:51:59 +0000417void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000418 DumpExpr(Str);
419 // FIXME: this doesn't print wstrings right.
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000420 OS << " ";
421 if (Str->isWide())
422 OS << "L";
423 OS << '"';
424 OS.write_escaped(llvm::StringRef(Str->getStrData(),
425 Str->getByteLength()));
426 OS << '"';
Chris Lattner6000dac2007-08-08 22:51:59 +0000427}
Chris Lattner17a1a722007-08-30 01:00:35 +0000428
Chris Lattner6000dac2007-08-08 22:51:59 +0000429void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000430 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000431 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
432 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +0000433}
Sebastian Redl05189992008-11-11 17:56:53 +0000434void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000435 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000436 OS << " " << (Node->isSizeOf() ? "sizeof" : "alignof") << " ";
Sebastian Redl05189992008-11-11 17:56:53 +0000437 if (Node->isArgumentType())
438 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000439}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000440
Chris Lattner6000dac2007-08-08 22:51:59 +0000441void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000442 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000443 OS << " " << (Node->isArrow() ? "->" : ".")
Benjamin Kramer900fc632010-04-17 09:33:03 +0000444 << Node->getMemberDecl() << ' '
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000445 << (void*)Node->getMemberDecl();
Chris Lattner6000dac2007-08-08 22:51:59 +0000446}
Nate Begeman213541a2008-04-18 23:10:10 +0000447void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000448 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000449 OS << " " << Node->getAccessor().getNameStart();
Chris Lattner6000dac2007-08-08 22:51:59 +0000450}
Chris Lattner6000dac2007-08-08 22:51:59 +0000451void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
452 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000453 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnereb14fe82007-08-25 02:00:02 +0000454}
455void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
456 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000457 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
458 << "' ComputeLHSTy=";
Eli Friedmanab3a8522009-03-28 01:22:36 +0000459 DumpType(Node->getComputationLHSType());
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000460 OS << " ComputeResultTy=";
Eli Friedmanab3a8522009-03-28 01:22:36 +0000461 DumpType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000462}
Chris Lattner6000dac2007-08-08 22:51:59 +0000463
464// GNU extensions.
465
466void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000467 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000468 OS << " " << Node->getLabel()->getName()
469 << " " << (void*)Node->getLabel();
Chris Lattner6000dac2007-08-08 22:51:59 +0000470}
471
Chris Lattner6000dac2007-08-08 22:51:59 +0000472void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000473 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000474 OS << " ";
Chris Lattner13cb21f2007-08-09 17:35:30 +0000475 DumpType(Node->getArgType1());
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000476 OS << " ";
Chris Lattner13cb21f2007-08-09 17:35:30 +0000477 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000478}
479
Chris Lattnerf9e05812007-08-09 18:03:18 +0000480//===----------------------------------------------------------------------===//
481// C++ Expressions
482//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000483
Douglas Gregor49badde2008-10-27 19:41:14 +0000484void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000485 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000486 OS << " " << Node->getCastName()
487 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000488 << " <" << Node->getCastKindName();
489 DumpBasePath(OS, Node);
490 OS << ">";
Chris Lattner6000dac2007-08-08 22:51:59 +0000491}
492
493void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000494 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000495 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000496}
497
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000498void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
499 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000500 OS << " this";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000501}
502
Douglas Gregor49badde2008-10-27 19:41:14 +0000503void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
504 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000505 OS << " functional cast to " << Node->getTypeAsWritten().getAsString();
Douglas Gregor49badde2008-10-27 19:41:14 +0000506}
507
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000508void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
509 DumpExpr(Node);
John McCalld4bbdfe2010-02-02 19:03:45 +0000510 CXXConstructorDecl *Ctor = Node->getConstructor();
511 DumpType(Ctor->getType());
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000512 if (Node->isElidable())
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000513 OS << " elidable";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000514}
515
516void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
517 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000518 OS << " ";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000519 DumpCXXTemporary(Node->getTemporary());
520}
521
522void StmtDumper::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node) {
523 DumpExpr(Node);
524 ++IndentLevel;
525 for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000526 OS << "\n";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000527 Indent();
528 DumpCXXTemporary(Node->getTemporary(i));
529 }
530 --IndentLevel;
531}
532
533void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000534 OS << "(CXXTemporary " << (void *)Temporary << ")";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000535}
536
Anders Carlsson55085182007-08-21 17:43:55 +0000537//===----------------------------------------------------------------------===//
538// Obj-C Expressions
539//===----------------------------------------------------------------------===//
540
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000541void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
542 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000543 OS << " selector=" << Node->getSelector().getAsString();
Douglas Gregor04badcf2010-04-21 00:45:42 +0000544 switch (Node->getReceiverKind()) {
545 case ObjCMessageExpr::Instance:
546 break;
547
548 case ObjCMessageExpr::Class:
549 OS << " class=";
550 DumpType(Node->getClassReceiver());
551 break;
552
553 case ObjCMessageExpr::SuperInstance:
554 OS << " super (instance)";
555 break;
556
557 case ObjCMessageExpr::SuperClass:
558 OS << " super (class)";
559 break;
560 }
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000561}
562
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000563void StmtDumper::VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node) {
564 DumpStmt(Node);
Douglas Gregorc00d8e12010-04-26 16:46:50 +0000565 if (VarDecl *CatchParam = Node->getCatchParamDecl()) {
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000566 OS << " catch parm = ";
567 DumpDeclarator(CatchParam);
568 } else {
569 OS << " catch all";
570 }
571}
572
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000573void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
574 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000575 OS << " ";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000576 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000577}
578
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000579void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
580 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000582 OS << " " << Node->getSelector().getAsString();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000583}
584
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000585void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
586 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Benjamin Kramer900fc632010-04-17 09:33:03 +0000588 OS << ' ' << Node->getProtocol();
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000589}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000590
591void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
592 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000593
Benjamin Kramer900fc632010-04-17 09:33:03 +0000594 OS << " Kind=PropertyRef Property=\"" << Node->getProperty() << '"';
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000595}
596
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000597void StmtDumper::VisitObjCImplicitSetterGetterRefExpr(
598 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000599 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000601 ObjCMethodDecl *Getter = Node->getGetterMethod();
602 ObjCMethodDecl *Setter = Node->getSetterMethod();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000603 OS << " Kind=MethodRef Getter=\""
604 << Getter->getSelector().getAsString()
605 << "\" Setter=\"";
606 if (Setter)
607 OS << Setter->getSelector().getAsString();
608 else
609 OS << "(null)";
610 OS << "\"";
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000611}
612
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000613void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
614 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000615 OS << " super";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000616}
617
Chris Lattner6000dac2007-08-08 22:51:59 +0000618//===----------------------------------------------------------------------===//
619// Stmt method implementations
620//===----------------------------------------------------------------------===//
621
622/// dump - This does a local dump of the specified AST fragment. It dumps the
623/// specified node and a few nodes underneath it, but not the whole subtree.
624/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000625void Stmt::dump(SourceManager &SM) const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000626 StmtDumper P(&SM, llvm::errs(), 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000627 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000628 llvm::errs() << "\n";
Chris Lattner0c727a32007-08-30 00:40:08 +0000629}
630
631/// dump - This does a local dump of the specified AST fragment. It dumps the
632/// specified node and a few nodes underneath it, but not the whole subtree.
633/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000634void Stmt::dump() const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000635 StmtDumper P(0, llvm::errs(), 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000636 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000637 llvm::errs() << "\n";
Chris Lattner0c727a32007-08-30 00:40:08 +0000638}
639
640/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000641void Stmt::dumpAll(SourceManager &SM) const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000642 StmtDumper P(&SM, llvm::errs(), ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000643 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000644 llvm::errs() << "\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000645}
646
647/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
648void Stmt::dumpAll() const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000649 StmtDumper P(0, llvm::errs(), ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000650 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000651 llvm::errs() << "\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000652}