blob: 05adc0292e38b907fe83c56a821436b05132ead5 [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
146 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenek36748da2008-02-29 22:04:05 +0000147 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000148 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000149 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000150 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000151 void VisitObjCImplicitSetterGetterRefExpr(
152 ObjCImplicitSetterGetterRefExpr *Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +0000153 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000154 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000155 };
156}
157
158//===----------------------------------------------------------------------===//
Chris Lattner11e30d32007-08-30 06:17:34 +0000159// Utilities
160//===----------------------------------------------------------------------===//
161
162void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattner53e384f2009-01-16 07:00:02 +0000163 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000164
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000165 if (SpellingLoc.isInvalid()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000166 OS << "<invalid sloc>";
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000167 return;
168 }
Chris Lattner11e30d32007-08-30 06:17:34 +0000169
170 // The general format we print out is filename:line:col, but we drop pieces
171 // that haven't changed since the last loc printed.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000172 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
173
174 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000175 OS << PLoc.getFilename() << ':' << PLoc.getLine()
176 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000177 LastLocFilename = PLoc.getFilename();
178 LastLocLine = PLoc.getLine();
179 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000180 OS << "line" << ':' << PLoc.getLine()
181 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000182 LastLocLine = PLoc.getLine();
Chris Lattner11e30d32007-08-30 06:17:34 +0000183 } else {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000184 OS << "col" << ':' << PLoc.getColumn();
Chris Lattner11e30d32007-08-30 06:17:34 +0000185 }
186}
187
Steve Naroff42a350a2007-09-01 21:08:38 +0000188void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattner11e30d32007-08-30 06:17:34 +0000189 // Can't translate locations if a SourceManager isn't available.
190 if (SM == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +0000191
Chris Lattner11e30d32007-08-30 06:17:34 +0000192 // TODO: If the parent expression is available, we can print a delta vs its
193 // location.
194 SourceRange R = Node->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000195
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000196 OS << " <";
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000197 DumpLocation(R.getBegin());
198 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000199 OS << ", ";
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000200 DumpLocation(R.getEnd());
Chris Lattner11e30d32007-08-30 06:17:34 +0000201 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000202 OS << ">";
Mike Stump11289f42009-09-09 15:08:12 +0000203
Chris Lattner11e30d32007-08-30 06:17:34 +0000204 // <t2.c:123:421[blah], t2.c:412:321>
205
206}
207
208
209//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +0000210// Stmt printing methods.
211//===----------------------------------------------------------------------===//
212
213void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner84ca3762007-08-30 01:00:35 +0000214 DumpStmt(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000215}
216
Chris Lattner8f184b12007-08-09 18:03:18 +0000217void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000218 // FIXME: Need to complete/beautify this... this code simply shows the
219 // nodes are where they need to be.
220 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000221 OS << "\"typedef " << localType->getUnderlyingType().getAsString()
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000222 << ' ' << localType << '"';
Chris Lattnercbe4f772007-08-08 22:51:59 +0000223 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000224 OS << "\"";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000225 // Emit storage class for vardecls.
226 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +0000227 if (V->getStorageClass() != VarDecl::None)
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000228 OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
229 << " ";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000230 }
Mike Stump11289f42009-09-09 15:08:12 +0000231
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000232 std::string Name = VD->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +0000233 VD->getType().getAsStringInternal(Name,
Chris Lattnerc61089a2009-06-30 01:26:17 +0000234 PrintingPolicy(VD->getASTContext().getLangOptions()));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000235 OS << Name;
Mike Stump11289f42009-09-09 15:08:12 +0000236
Chris Lattnercbe4f772007-08-08 22:51:59 +0000237 // If this is a vardecl with an initializer, emit it.
238 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
239 if (V->getInit()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000240 OS << " =\n";
Chris Lattner8f184b12007-08-09 18:03:18 +0000241 DumpSubTree(V->getInit());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000242 }
243 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000244 OS << '"';
Steve Naroff14f5f792007-11-17 21:37:36 +0000245 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
246 // print a free standing tag decl (e.g. "struct x;").
247 const char *tagname;
248 if (const IdentifierInfo *II = TD->getIdentifier())
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000249 tagname = II->getNameStart();
Steve Naroff14f5f792007-11-17 21:37:36 +0000250 else
251 tagname = "<anonymous>";
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000252 OS << '"' << TD->getKindName() << ' ' << tagname << ";\"";
Steve Naroff14f5f792007-11-17 21:37:36 +0000253 // FIXME: print tag bodies.
Douglas Gregor889ceb72009-02-03 19:21:40 +0000254 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
255 // print using-directive decl (e.g. "using namespace x;")
256 const char *ns;
257 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000258 ns = II->getNameStart();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000259 else
260 ns = "<anonymous>";
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000261 OS << '"' << UD->getDeclKindName() << ns << ";\"";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000262 } else {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000263 assert(0 && "Unexpected decl");
264 }
Chris Lattnercbe4f772007-08-08 22:51:59 +0000265}
266
Ted Kremenek433a4922007-12-12 06:59:42 +0000267void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
268 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000269 OS << "\n";
Ted Kremenek62408482008-10-06 18:38:35 +0000270 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
271 DI != DE; ++DI) {
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000272 Decl* D = *DI;
Ted Kremenek433a4922007-12-12 06:59:42 +0000273 ++IndentLevel;
274 Indent();
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000275 OS << (void*) D << " ";
Ted Kremenek433a4922007-12-12 06:59:42 +0000276 DumpDeclarator(D);
Chris Lattner3d954d52009-03-29 16:04:50 +0000277 if (DI+1 != DE)
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000278 OS << "\n";
Ted Kremenek433a4922007-12-12 06:59:42 +0000279 --IndentLevel;
280 }
281}
282
Chris Lattnercbe4f772007-08-08 22:51:59 +0000283void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
284 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000285 OS << " '" << Node->getName() << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000286}
287
Chris Lattnercbe4f772007-08-08 22:51:59 +0000288void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
289 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000290 OS << " '" << Node->getLabel()->getName()
291 << "':" << (void*)Node->getLabel();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000292}
293
Chris Lattnercbe4f772007-08-08 22:51:59 +0000294//===----------------------------------------------------------------------===//
295// Expr printing methods.
296//===----------------------------------------------------------------------===//
297
298void StmtDumper::VisitExpr(Expr *Node) {
299 DumpExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000300}
301
Anders Carlssond7923c62009-08-22 23:33:40 +0000302void StmtDumper::VisitCastExpr(CastExpr *Node) {
303 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000304 OS << " <" << Node->getCastKindName() << ">";
Anders Carlssond7923c62009-08-22 23:33:40 +0000305}
306
Anders Carlsson66010572009-11-14 22:35:18 +0000307void StmtDumper::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
308 VisitCastExpr(Node);
309 if (Node->isLvalueCast())
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000310 OS << " lvalue";
Anders Carlsson66010572009-11-14 22:35:18 +0000311}
312
Chris Lattnercbe4f772007-08-08 22:51:59 +0000313void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
314 DumpExpr(Node);
Ted Kremenek5f64ca82007-09-10 17:32:55 +0000315
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000316 OS << " ";
Ted Kremenek5f64ca82007-09-10 17:32:55 +0000317 switch (Node->getDecl()->getKind()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000318 default: OS << "Decl"; break;
319 case Decl::Function: OS << "FunctionDecl"; break;
320 case Decl::Var: OS << "Var"; break;
321 case Decl::ParmVar: OS << "ParmVar"; break;
322 case Decl::EnumConstant: OS << "EnumConstant"; break;
323 case Decl::Typedef: OS << "Typedef"; break;
324 case Decl::Record: OS << "Record"; break;
325 case Decl::Enum: OS << "Enum"; break;
326 case Decl::CXXRecord: OS << "CXXRecord"; break;
327 case Decl::ObjCInterface: OS << "ObjCInterface"; break;
328 case Decl::ObjCClass: OS << "ObjCClass"; break;
Ted Kremenek5f64ca82007-09-10 17:32:55 +0000329 }
Mike Stump11289f42009-09-09 15:08:12 +0000330
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000331 OS << "='" << Node->getDecl() << "' " << (void*)Node->getDecl();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000332}
333
John McCall76d09942009-12-11 21:50:11 +0000334void StmtDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
335 DumpExpr(Node);
336 OS << " (";
337 if (!Node->requiresADL()) OS << "no ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000338 OS << "ADL) = '" << Node->getName() << '\'';
John McCall76d09942009-12-11 21:50:11 +0000339
340 UnresolvedLookupExpr::decls_iterator
341 I = Node->decls_begin(), E = Node->decls_end();
342 if (I == E) OS << " empty";
343 for (; I != E; ++I)
344 OS << " " << (void*) *I;
345}
346
Steve Naroff5d5efca2008-03-12 13:19:12 +0000347void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroffe3fa7132008-05-23 00:59:14 +0000348 DumpExpr(Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +0000349
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000350 OS << " " << Node->getDecl()->getDeclKindName()
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000351 << "Decl='" << Node->getDecl()
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000352 << "' " << (void*)Node->getDecl();
Steve Naroffb3424a92008-05-23 22:01:24 +0000353 if (Node->isFreeIvar())
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000354 OS << " isFreeIvar";
Steve Naroff5d5efca2008-03-12 13:19:12 +0000355}
356
Chris Lattner6307f192008-08-10 01:53:14 +0000357void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000358 DumpExpr(Node);
359 switch (Node->getIdentType()) {
Chris Lattnera9b3cae2008-06-21 18:04:54 +0000360 default: assert(0 && "unknown case");
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000361 case PredefinedExpr::Func: OS << " __func__"; break;
362 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
363 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Chris Lattnercbe4f772007-08-08 22:51:59 +0000364 }
365}
366
367void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner273a1ea2007-08-09 01:04:32 +0000368 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000369 OS << Node->getValue();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000370}
371
372void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
373 DumpExpr(Node);
374
375 bool isSigned = Node->getType()->isSignedIntegerType();
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000376 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000377}
378void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
379 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000380 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000381}
Chris Lattner1c20a172007-08-26 03:42:43 +0000382
Chris Lattnercbe4f772007-08-08 22:51:59 +0000383void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner273a1ea2007-08-09 01:04:32 +0000384 DumpExpr(Str);
385 // FIXME: this doesn't print wstrings right.
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000386 OS << " ";
387 if (Str->isWide())
388 OS << "L";
389 OS << '"';
390 OS.write_escaped(llvm::StringRef(Str->getStrData(),
391 Str->getByteLength()));
392 OS << '"';
Chris Lattnercbe4f772007-08-08 22:51:59 +0000393}
Chris Lattner84ca3762007-08-30 01:00:35 +0000394
Chris Lattnercbe4f772007-08-08 22:51:59 +0000395void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000396 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000397 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
398 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000399}
Sebastian Redl6f282892008-11-11 17:56:53 +0000400void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000401 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000402 OS << " " << (Node->isSizeOf() ? "sizeof" : "alignof") << " ";
Sebastian Redl6f282892008-11-11 17:56:53 +0000403 if (Node->isArgumentType())
404 DumpType(Node->getArgumentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000405}
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000406
Chris Lattnercbe4f772007-08-08 22:51:59 +0000407void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000408 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000409 OS << " " << (Node->isArrow() ? "->" : ".")
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000410 << Node->getMemberDecl() << ' '
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000411 << (void*)Node->getMemberDecl();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000412}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000413void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000414 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000415 OS << " " << Node->getAccessor().getNameStart();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000416}
Chris Lattnercbe4f772007-08-08 22:51:59 +0000417void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
418 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000419 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner86928112007-08-25 02:00:02 +0000420}
421void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
422 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000423 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
424 << "' ComputeLHSTy=";
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000425 DumpType(Node->getComputationLHSType());
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000426 OS << " ComputeResultTy=";
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000427 DumpType(Node->getComputationResultType());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000428}
Chris Lattnercbe4f772007-08-08 22:51:59 +0000429
430// GNU extensions.
431
432void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000433 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000434 OS << " " << Node->getLabel()->getName()
435 << " " << (void*)Node->getLabel();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000436}
437
Chris Lattnercbe4f772007-08-08 22:51:59 +0000438void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000439 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000440 OS << " ";
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000441 DumpType(Node->getArgType1());
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000442 OS << " ";
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000443 DumpType(Node->getArgType2());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000444}
445
Chris Lattner8f184b12007-08-09 18:03:18 +0000446//===----------------------------------------------------------------------===//
447// C++ Expressions
448//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +0000449
Douglas Gregore200adc2008-10-27 19:41:14 +0000450void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000451 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000452 OS << " " << Node->getCastName()
453 << "<" << Node->getTypeAsWritten().getAsString() << ">"
454 << " <" << Node->getCastKindName() << ">";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000455}
456
457void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000458 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000459 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattnercbe4f772007-08-08 22:51:59 +0000460}
461
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000462void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
463 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000464 OS << " this";
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000465}
466
Douglas Gregore200adc2008-10-27 19:41:14 +0000467void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
468 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000469 OS << " functional cast to " << Node->getTypeAsWritten().getAsString();
Douglas Gregore200adc2008-10-27 19:41:14 +0000470}
471
Anders Carlsson073846832009-08-12 00:21:52 +0000472void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
473 DumpExpr(Node);
John McCalleba90cd2010-02-02 19:03:45 +0000474 CXXConstructorDecl *Ctor = Node->getConstructor();
475 DumpType(Ctor->getType());
Anders Carlsson073846832009-08-12 00:21:52 +0000476 if (Node->isElidable())
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000477 OS << " elidable";
Anders Carlsson073846832009-08-12 00:21:52 +0000478}
479
480void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
481 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000482 OS << " ";
Anders Carlsson073846832009-08-12 00:21:52 +0000483 DumpCXXTemporary(Node->getTemporary());
484}
485
486void StmtDumper::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node) {
487 DumpExpr(Node);
488 ++IndentLevel;
489 for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000490 OS << "\n";
Anders Carlsson073846832009-08-12 00:21:52 +0000491 Indent();
492 DumpCXXTemporary(Node->getTemporary(i));
493 }
494 --IndentLevel;
495}
496
497void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000498 OS << "(CXXTemporary " << (void *)Temporary << ")";
Anders Carlsson073846832009-08-12 00:21:52 +0000499}
500
Anders Carlsson76f4a902007-08-21 17:43:55 +0000501//===----------------------------------------------------------------------===//
502// Obj-C Expressions
503//===----------------------------------------------------------------------===//
504
Ted Kremenek36748da2008-02-29 22:04:05 +0000505void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
506 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000507 OS << " selector=" << Node->getSelector().getAsString();
Douglas Gregor9a129192010-04-21 00:45:42 +0000508 switch (Node->getReceiverKind()) {
509 case ObjCMessageExpr::Instance:
510 break;
511
512 case ObjCMessageExpr::Class:
513 OS << " class=";
514 DumpType(Node->getClassReceiver());
515 break;
516
517 case ObjCMessageExpr::SuperInstance:
518 OS << " super (instance)";
519 break;
520
521 case ObjCMessageExpr::SuperClass:
522 OS << " super (class)";
523 break;
524 }
Ted Kremenek36748da2008-02-29 22:04:05 +0000525}
526
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000527void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
528 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000529 OS << " ";
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000530 DumpType(Node->getEncodedType());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000531}
532
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000533void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
534 DumpExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +0000535
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000536 OS << " " << Node->getSelector().getAsString();
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000537}
538
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000539void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
540 DumpExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +0000541
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000542 OS << ' ' << Node->getProtocol();
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000543}
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000544
545void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
546 DumpExpr(Node);
Daniel Dunbarc5d33042008-09-03 00:27:26 +0000547
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000548 OS << " Kind=PropertyRef Property=\"" << Node->getProperty() << '"';
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000549}
550
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000551void StmtDumper::VisitObjCImplicitSetterGetterRefExpr(
552 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000553 DumpExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +0000554
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000555 ObjCMethodDecl *Getter = Node->getGetterMethod();
556 ObjCMethodDecl *Setter = Node->getSetterMethod();
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000557 OS << " Kind=MethodRef Getter=\""
558 << Getter->getSelector().getAsString()
559 << "\" Setter=\"";
560 if (Setter)
561 OS << Setter->getSelector().getAsString();
562 else
563 OS << "(null)";
564 OS << "\"";
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000565}
566
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000567void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
568 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000569 OS << " super";
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000570}
571
Chris Lattnercbe4f772007-08-08 22:51:59 +0000572//===----------------------------------------------------------------------===//
573// Stmt method implementations
574//===----------------------------------------------------------------------===//
575
576/// dump - This does a local dump of the specified AST fragment. It dumps the
577/// specified node and a few nodes underneath it, but not the whole subtree.
578/// This is useful in a debugger.
Chris Lattner11e30d32007-08-30 06:17:34 +0000579void Stmt::dump(SourceManager &SM) const {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000580 StmtDumper P(&SM, llvm::errs(), 4);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000581 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000582 llvm::errs() << "\n";
Chris Lattner779d5d92007-08-30 00:40:08 +0000583}
584
585/// dump - This does a local dump of the specified AST fragment. It dumps the
586/// specified node and a few nodes underneath it, but not the whole subtree.
587/// This is useful in a debugger.
Chris Lattnercbe4f772007-08-08 22:51:59 +0000588void Stmt::dump() const {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000589 StmtDumper P(0, llvm::errs(), 4);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000590 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000591 llvm::errs() << "\n";
Chris Lattner779d5d92007-08-30 00:40:08 +0000592}
593
594/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattner11e30d32007-08-30 06:17:34 +0000595void Stmt::dumpAll(SourceManager &SM) const {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000596 StmtDumper P(&SM, llvm::errs(), ~0U);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000597 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000598 llvm::errs() << "\n";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000599}
600
601/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
602void Stmt::dumpAll() const {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000603 StmtDumper P(0, llvm::errs(), ~0U);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000604 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000605 llvm::errs() << "\n";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000606}