blob: 5def7d9a0b8c20c7c61992ca8228d35533aba957 [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 }
Fariborz Jahanianc6bf0bd2010-08-31 18:02:20 +000068 if (const ConditionalOperator *CO =
69 dyn_cast<ConditionalOperator>(S)) {
70 if (CO->getSAVE()) {
71 OS << '\n';
72 DumpSubTree(CO->getSAVE());
73 }
74 }
Chris Lattnercfb83dd2007-08-30 00:53:54 +000075 }
76 }
Chris Lattneredde8a92010-05-25 17:56:43 +000077 OS << ')';
Chris Lattnercbe4f772007-08-08 22:51:59 +000078 } else {
79 Indent();
Daniel Dunbar34a96c82009-12-03 09:13:13 +000080 OS << "<<<NULL>>>";
Chris Lattnercbe4f772007-08-08 22:51:59 +000081 }
Chris Lattner8f184b12007-08-09 18:03:18 +000082 --IndentLevel;
Chris Lattnercbe4f772007-08-08 22:51:59 +000083 }
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattner8f184b12007-08-09 18:03:18 +000085 void DumpDeclarator(Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +000086
Chris Lattnercbe4f772007-08-08 22:51:59 +000087 void Indent() const {
88 for (int i = 0, e = IndentLevel; i < e; ++i)
Daniel Dunbar34a96c82009-12-03 09:13:13 +000089 OS << " ";
Chris Lattnercbe4f772007-08-08 22:51:59 +000090 }
Mike Stump11289f42009-09-09 15:08:12 +000091
Steve Naroff42a350a2007-09-01 21:08:38 +000092 void DumpType(QualType T) {
John McCall717d9b02010-12-10 11:01:00 +000093 SplitQualType T_split = T.split();
94 OS << "'" << QualType::getAsString(T_split) << "'";
Chris Lattner9bcd9152007-08-09 00:36:22 +000095
Douglas Gregor58354032008-12-24 00:01:03 +000096 if (!T.isNull()) {
John McCall8ccfcb52009-09-24 19:53:00 +000097 // If the type is sugared, also dump a (shallow) desugared type.
John McCall717d9b02010-12-10 11:01:00 +000098 SplitQualType D_split = T.getSplitDesugaredType();
99 if (T_split != D_split)
100 OS << ":'" << QualType::getAsString(D_split) << "'";
Chris Lattner091718d2008-04-02 05:06:23 +0000101 }
Chris Lattner9bcd9152007-08-09 00:36:22 +0000102 }
John McCall351762c2011-02-07 10:33:21 +0000103 void DumpDeclRef(Decl *node);
Steve Naroff42a350a2007-09-01 21:08:38 +0000104 void DumpStmt(const Stmt *Node) {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000105 Indent();
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000106 OS << "(" << Node->getStmtClassName()
107 << " " << (void*)Node;
Steve Naroff42a350a2007-09-01 21:08:38 +0000108 DumpSourceRange(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000109 }
John McCall7decc9e2010-11-18 06:31:45 +0000110 void DumpValueKind(ExprValueKind K) {
111 switch (K) {
112 case VK_RValue: break;
113 case VK_LValue: OS << " lvalue"; break;
114 case VK_XValue: OS << " xvalue"; break;
115 }
116 }
117 void DumpObjectKind(ExprObjectKind K) {
118 switch (K) {
119 case OK_Ordinary: break;
120 case OK_BitField: OS << " bitfield"; break;
121 case OK_ObjCProperty: OS << " objcproperty"; break;
122 case OK_VectorComponent: OS << " vectorcomponent"; break;
123 }
124 }
Steve Naroff42a350a2007-09-01 21:08:38 +0000125 void DumpExpr(const Expr *Node) {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000126 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000127 OS << ' ';
Chris Lattner9bcd9152007-08-09 00:36:22 +0000128 DumpType(Node->getType());
John McCall7decc9e2010-11-18 06:31:45 +0000129 DumpValueKind(Node->getValueKind());
130 DumpObjectKind(Node->getObjectKind());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000131 }
Steve Naroff42a350a2007-09-01 21:08:38 +0000132 void DumpSourceRange(const Stmt *Node);
Chris Lattner11e30d32007-08-30 06:17:34 +0000133 void DumpLocation(SourceLocation Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000134
Chris Lattner84ca3762007-08-30 01:00:35 +0000135 // Stmts.
Chris Lattner62249a62007-08-21 04:04:25 +0000136 void VisitStmt(Stmt *Node);
Ted Kremenek433a4922007-12-12 06:59:42 +0000137 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000138 void VisitLabelStmt(LabelStmt *Node);
139 void VisitGotoStmt(GotoStmt *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000140
Chris Lattner84ca3762007-08-30 01:00:35 +0000141 // Exprs
142 void VisitExpr(Expr *Node);
Anders Carlssond7923c62009-08-22 23:33:40 +0000143 void VisitCastExpr(CastExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000144 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattner6307f192008-08-10 01:53:14 +0000145 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000146 void VisitCharacterLiteral(CharacterLiteral *Node);
147 void VisitIntegerLiteral(IntegerLiteral *Node);
148 void VisitFloatingLiteral(FloatingLiteral *Node);
149 void VisitStringLiteral(StringLiteral *Str);
150 void VisitUnaryOperator(UnaryOperator *Node);
Sebastian Redl6f282892008-11-11 17:56:53 +0000151 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000152 void VisitMemberExpr(MemberExpr *Node);
Nate Begemance4d7fc2008-04-18 23:10:10 +0000153 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000154 void VisitBinaryOperator(BinaryOperator *Node);
155 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
156 void VisitAddrLabelExpr(AddrLabelExpr *Node);
John McCall351762c2011-02-07 10:33:21 +0000157 void VisitBlockExpr(BlockExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000158
159 // C++
Douglas Gregore200adc2008-10-27 19:41:14 +0000160 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000161 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000162 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregore200adc2008-10-27 19:41:14 +0000163 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Anders Carlsson073846832009-08-12 00:21:52 +0000164 void VisitCXXConstructExpr(CXXConstructExpr *Node);
165 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
John McCall5d413782010-12-06 08:20:24 +0000166 void VisitExprWithCleanups(ExprWithCleanups *Node);
John McCall76d09942009-12-11 21:50:11 +0000167 void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node);
Anders Carlsson073846832009-08-12 00:21:52 +0000168 void DumpCXXTemporary(CXXTemporary *Temporary);
Mike Stump11289f42009-09-09 15:08:12 +0000169
Chris Lattner84ca3762007-08-30 01:00:35 +0000170 // ObjC
Douglas Gregor96c79492010-04-23 22:50:49 +0000171 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000172 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenek36748da2008-02-29 22:04:05 +0000173 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000174 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000175 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000176 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +0000177 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000178 };
179}
180
181//===----------------------------------------------------------------------===//
Chris Lattner11e30d32007-08-30 06:17:34 +0000182// Utilities
183//===----------------------------------------------------------------------===//
184
185void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattner53e384f2009-01-16 07:00:02 +0000186 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000187
Chris Lattner11e30d32007-08-30 06:17:34 +0000188 // The general format we print out is filename:line:col, but we drop pieces
189 // that haven't changed since the last loc printed.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000190 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
191
Douglas Gregor453b0122010-11-12 07:15:47 +0000192 if (PLoc.isInvalid()) {
193 OS << "<invalid sloc>";
194 return;
195 }
196
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000197 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000198 OS << PLoc.getFilename() << ':' << PLoc.getLine()
199 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000200 LastLocFilename = PLoc.getFilename();
201 LastLocLine = PLoc.getLine();
202 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000203 OS << "line" << ':' << PLoc.getLine()
204 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000205 LastLocLine = PLoc.getLine();
Chris Lattner11e30d32007-08-30 06:17:34 +0000206 } else {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000207 OS << "col" << ':' << PLoc.getColumn();
Chris Lattner11e30d32007-08-30 06:17:34 +0000208 }
209}
210
Steve Naroff42a350a2007-09-01 21:08:38 +0000211void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattner11e30d32007-08-30 06:17:34 +0000212 // Can't translate locations if a SourceManager isn't available.
213 if (SM == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +0000214
Chris Lattner11e30d32007-08-30 06:17:34 +0000215 // TODO: If the parent expression is available, we can print a delta vs its
216 // location.
217 SourceRange R = Node->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000218
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000219 OS << " <";
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000220 DumpLocation(R.getBegin());
221 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000222 OS << ", ";
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000223 DumpLocation(R.getEnd());
Chris Lattner11e30d32007-08-30 06:17:34 +0000224 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000225 OS << ">";
Mike Stump11289f42009-09-09 15:08:12 +0000226
Chris Lattner11e30d32007-08-30 06:17:34 +0000227 // <t2.c:123:421[blah], t2.c:412:321>
228
229}
230
231
232//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +0000233// Stmt printing methods.
234//===----------------------------------------------------------------------===//
235
236void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner84ca3762007-08-30 01:00:35 +0000237 DumpStmt(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000238}
239
Chris Lattner8f184b12007-08-09 18:03:18 +0000240void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000241 // FIXME: Need to complete/beautify this... this code simply shows the
242 // nodes are where they need to be.
243 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000244 OS << "\"typedef " << localType->getUnderlyingType().getAsString()
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000245 << ' ' << localType << '"';
Chris Lattnercbe4f772007-08-08 22:51:59 +0000246 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000247 OS << "\"";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000248 // Emit storage class for vardecls.
249 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
John McCall8e7d6562010-08-26 03:08:43 +0000250 if (V->getStorageClass() != SC_None)
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000251 OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
252 << " ";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000253 }
Mike Stump11289f42009-09-09 15:08:12 +0000254
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000255 std::string Name = VD->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +0000256 VD->getType().getAsStringInternal(Name,
Chris Lattnerc61089a2009-06-30 01:26:17 +0000257 PrintingPolicy(VD->getASTContext().getLangOptions()));
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000258 OS << Name;
Mike Stump11289f42009-09-09 15:08:12 +0000259
Chris Lattnercbe4f772007-08-08 22:51:59 +0000260 // If this is a vardecl with an initializer, emit it.
261 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
262 if (V->getInit()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000263 OS << " =\n";
Chris Lattner8f184b12007-08-09 18:03:18 +0000264 DumpSubTree(V->getInit());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000265 }
266 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000267 OS << '"';
Steve Naroff14f5f792007-11-17 21:37:36 +0000268 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
269 // print a free standing tag decl (e.g. "struct x;").
270 const char *tagname;
271 if (const IdentifierInfo *II = TD->getIdentifier())
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000272 tagname = II->getNameStart();
Steve Naroff14f5f792007-11-17 21:37:36 +0000273 else
274 tagname = "<anonymous>";
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000275 OS << '"' << TD->getKindName() << ' ' << tagname << ";\"";
Steve Naroff14f5f792007-11-17 21:37:36 +0000276 // FIXME: print tag bodies.
Douglas Gregor889ceb72009-02-03 19:21:40 +0000277 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
278 // print using-directive decl (e.g. "using namespace x;")
279 const char *ns;
280 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000281 ns = II->getNameStart();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000282 else
283 ns = "<anonymous>";
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000284 OS << '"' << UD->getDeclKindName() << ns << ";\"";
Sebastian Redl80fecff2010-05-04 10:20:17 +0000285 } else if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
286 // print using decl (e.g. "using std::string;")
287 const char *tn = UD->isTypeName() ? "typename " : "";
288 OS << '"' << UD->getDeclKindName() << tn;
289 UD->getTargetNestedNameDecl()->print(OS,
290 PrintingPolicy(UD->getASTContext().getLangOptions()));
291 OS << ";\"";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000292 } else {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000293 assert(0 && "Unexpected decl");
294 }
Chris Lattnercbe4f772007-08-08 22:51:59 +0000295}
296
Ted Kremenek433a4922007-12-12 06:59:42 +0000297void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
298 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000299 OS << "\n";
Ted Kremenek62408482008-10-06 18:38:35 +0000300 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
301 DI != DE; ++DI) {
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000302 Decl* D = *DI;
Ted Kremenek433a4922007-12-12 06:59:42 +0000303 ++IndentLevel;
304 Indent();
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000305 OS << (void*) D << " ";
Ted Kremenek433a4922007-12-12 06:59:42 +0000306 DumpDeclarator(D);
Chris Lattner3d954d52009-03-29 16:04:50 +0000307 if (DI+1 != DE)
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000308 OS << "\n";
Ted Kremenek433a4922007-12-12 06:59:42 +0000309 --IndentLevel;
310 }
311}
312
Chris Lattnercbe4f772007-08-08 22:51:59 +0000313void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
314 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000315 OS << " '" << Node->getName() << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000316}
317
Chris Lattnercbe4f772007-08-08 22:51:59 +0000318void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
319 DumpStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000320 OS << " '" << Node->getLabel()->getName()
321 << "':" << (void*)Node->getLabel();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000322}
323
Chris Lattnercbe4f772007-08-08 22:51:59 +0000324//===----------------------------------------------------------------------===//
325// Expr printing methods.
326//===----------------------------------------------------------------------===//
327
328void StmtDumper::VisitExpr(Expr *Node) {
329 DumpExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000330}
331
Anders Carlssona70cff62010-04-24 19:06:50 +0000332static void DumpBasePath(llvm::raw_ostream &OS, CastExpr *Node) {
John McCallcf142162010-08-07 06:22:56 +0000333 if (Node->path_empty())
Anders Carlssona70cff62010-04-24 19:06:50 +0000334 return;
335
336 OS << " (";
337 bool First = true;
John McCallcf142162010-08-07 06:22:56 +0000338 for (CastExpr::path_iterator
339 I = Node->path_begin(), E = Node->path_end(); I != E; ++I) {
Anders Carlssona70cff62010-04-24 19:06:50 +0000340 const CXXBaseSpecifier *Base = *I;
341 if (!First)
342 OS << " -> ";
343
344 const CXXRecordDecl *RD =
345 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
346
347 if (Base->isVirtual())
348 OS << "virtual ";
349 OS << RD->getName();
350 First = false;
351 }
352
353 OS << ')';
354}
355
Anders Carlssond7923c62009-08-22 23:33:40 +0000356void StmtDumper::VisitCastExpr(CastExpr *Node) {
357 DumpExpr(Node);
Anders Carlssona70cff62010-04-24 19:06:50 +0000358 OS << " <" << Node->getCastKindName();
359 DumpBasePath(OS, Node);
360 OS << ">";
Anders Carlssond7923c62009-08-22 23:33:40 +0000361}
362
Chris Lattnercbe4f772007-08-08 22:51:59 +0000363void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
364 DumpExpr(Node);
Ted Kremenek5f64ca82007-09-10 17:32:55 +0000365
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000366 OS << " ";
John McCall351762c2011-02-07 10:33:21 +0000367 DumpDeclRef(Node->getDecl());
368}
369
370void StmtDumper::DumpDeclRef(Decl *d) {
371 OS << d->getDeclKindName() << ' ' << (void*) d;
372
373 if (NamedDecl *nd = dyn_cast<NamedDecl>(d)) {
374 OS << " '";
375 nd->getDeclName().printName(OS);
376 OS << "'";
Ted Kremenek5f64ca82007-09-10 17:32:55 +0000377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
John McCall351762c2011-02-07 10:33:21 +0000379 if (ValueDecl *vd = dyn_cast<ValueDecl>(d)) {
380 OS << ' '; DumpType(vd->getType());
381 }
Chris Lattnercbe4f772007-08-08 22:51:59 +0000382}
383
John McCall76d09942009-12-11 21:50:11 +0000384void StmtDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
385 DumpExpr(Node);
386 OS << " (";
387 if (!Node->requiresADL()) OS << "no ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000388 OS << "ADL) = '" << Node->getName() << '\'';
John McCall76d09942009-12-11 21:50:11 +0000389
390 UnresolvedLookupExpr::decls_iterator
391 I = Node->decls_begin(), E = Node->decls_end();
392 if (I == E) OS << " empty";
393 for (; I != E; ++I)
394 OS << " " << (void*) *I;
395}
396
Steve Naroff5d5efca2008-03-12 13:19:12 +0000397void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroffe3fa7132008-05-23 00:59:14 +0000398 DumpExpr(Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +0000399
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000400 OS << " " << Node->getDecl()->getDeclKindName()
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000401 << "Decl='" << Node->getDecl()
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000402 << "' " << (void*)Node->getDecl();
Steve Naroffb3424a92008-05-23 22:01:24 +0000403 if (Node->isFreeIvar())
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000404 OS << " isFreeIvar";
Steve Naroff5d5efca2008-03-12 13:19:12 +0000405}
406
Chris Lattner6307f192008-08-10 01:53:14 +0000407void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattnercbe4f772007-08-08 22:51:59 +0000408 DumpExpr(Node);
409 switch (Node->getIdentType()) {
Chris Lattnera9b3cae2008-06-21 18:04:54 +0000410 default: assert(0 && "unknown case");
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000411 case PredefinedExpr::Func: OS << " __func__"; break;
412 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
413 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Chris Lattnercbe4f772007-08-08 22:51:59 +0000414 }
415}
416
417void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner273a1ea2007-08-09 01:04:32 +0000418 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000419 OS << Node->getValue();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000420}
421
422void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
423 DumpExpr(Node);
424
425 bool isSigned = Node->getType()->isSignedIntegerType();
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000426 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000427}
428void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
429 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000430 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000431}
Chris Lattner1c20a172007-08-26 03:42:43 +0000432
Chris Lattnercbe4f772007-08-08 22:51:59 +0000433void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner273a1ea2007-08-09 01:04:32 +0000434 DumpExpr(Str);
435 // FIXME: this doesn't print wstrings right.
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000436 OS << " ";
437 if (Str->isWide())
438 OS << "L";
439 OS << '"';
Benjamin Kramer35b077e2010-08-17 12:54:38 +0000440 OS.write_escaped(Str->getString());
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000441 OS << '"';
Chris Lattnercbe4f772007-08-08 22:51:59 +0000442}
Chris Lattner84ca3762007-08-30 01:00:35 +0000443
Chris Lattnercbe4f772007-08-08 22:51:59 +0000444void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000445 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000446 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
447 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000448}
Sebastian Redl6f282892008-11-11 17:56:53 +0000449void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000450 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000451 OS << " " << (Node->isSizeOf() ? "sizeof" : "alignof") << " ";
Sebastian Redl6f282892008-11-11 17:56:53 +0000452 if (Node->isArgumentType())
453 DumpType(Node->getArgumentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000454}
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000455
Chris Lattnercbe4f772007-08-08 22:51:59 +0000456void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000457 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000458 OS << " " << (Node->isArrow() ? "->" : ".")
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000459 << Node->getMemberDecl() << ' '
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000460 << (void*)Node->getMemberDecl();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000461}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000462void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000463 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000464 OS << " " << Node->getAccessor().getNameStart();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000465}
Chris Lattnercbe4f772007-08-08 22:51:59 +0000466void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
467 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000468 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner86928112007-08-25 02:00:02 +0000469}
470void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
471 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000472 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
473 << "' ComputeLHSTy=";
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000474 DumpType(Node->getComputationLHSType());
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000475 OS << " ComputeResultTy=";
Eli Friedman8b7b1b12009-03-28 01:22:36 +0000476 DumpType(Node->getComputationResultType());
Chris Lattnercbe4f772007-08-08 22:51:59 +0000477}
Chris Lattnercbe4f772007-08-08 22:51:59 +0000478
John McCall351762c2011-02-07 10:33:21 +0000479void StmtDumper::VisitBlockExpr(BlockExpr *Node) {
480 DumpExpr(Node);
481
482 IndentLevel++;
483 BlockDecl *block = Node->getBlockDecl();
484 if (block->capturesCXXThis()) {
485 OS << '\n'; Indent(); OS << "(capture this)";
486 }
487 for (BlockDecl::capture_iterator
488 i = block->capture_begin(), e = block->capture_end(); i != e; ++i) {
489 OS << '\n';
490 Indent();
491 OS << "(capture ";
492 if (i->isByRef()) OS << "byref ";
493 if (i->isNested()) OS << "nested ";
494 DumpDeclRef(i->getVariable());
495 if (i->hasCopyExpr()) DumpSubTree(i->getCopyExpr());
496 OS << ")";
497 }
498 IndentLevel--;
499
500 DumpSubTree(block->getBody());
501}
502
Chris Lattnercbe4f772007-08-08 22:51:59 +0000503// GNU extensions.
504
505void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000506 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000507 OS << " " << Node->getLabel()->getName()
508 << " " << (void*)Node->getLabel();
Chris Lattnercbe4f772007-08-08 22:51:59 +0000509}
510
Chris Lattner8f184b12007-08-09 18:03:18 +0000511//===----------------------------------------------------------------------===//
512// C++ Expressions
513//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +0000514
Douglas Gregore200adc2008-10-27 19:41:14 +0000515void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000516 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000517 OS << " " << Node->getCastName()
518 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlssona70cff62010-04-24 19:06:50 +0000519 << " <" << Node->getCastKindName();
520 DumpBasePath(OS, Node);
521 OS << ">";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000522}
523
524void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +0000525 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000526 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattnercbe4f772007-08-08 22:51:59 +0000527}
528
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000529void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
530 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000531 OS << " this";
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000532}
533
Douglas Gregore200adc2008-10-27 19:41:14 +0000534void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
535 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000536 OS << " functional cast to " << Node->getTypeAsWritten().getAsString();
Douglas Gregore200adc2008-10-27 19:41:14 +0000537}
538
Anders Carlsson073846832009-08-12 00:21:52 +0000539void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
540 DumpExpr(Node);
John McCalleba90cd2010-02-02 19:03:45 +0000541 CXXConstructorDecl *Ctor = Node->getConstructor();
542 DumpType(Ctor->getType());
Anders Carlsson073846832009-08-12 00:21:52 +0000543 if (Node->isElidable())
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000544 OS << " elidable";
John McCall85370042010-08-07 06:38:55 +0000545 if (Node->requiresZeroInitialization())
546 OS << " zeroing";
Anders Carlsson073846832009-08-12 00:21:52 +0000547}
548
549void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
550 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000551 OS << " ";
Anders Carlsson073846832009-08-12 00:21:52 +0000552 DumpCXXTemporary(Node->getTemporary());
553}
554
John McCall5d413782010-12-06 08:20:24 +0000555void StmtDumper::VisitExprWithCleanups(ExprWithCleanups *Node) {
Anders Carlsson073846832009-08-12 00:21:52 +0000556 DumpExpr(Node);
557 ++IndentLevel;
558 for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000559 OS << "\n";
Anders Carlsson073846832009-08-12 00:21:52 +0000560 Indent();
561 DumpCXXTemporary(Node->getTemporary(i));
562 }
563 --IndentLevel;
564}
565
566void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000567 OS << "(CXXTemporary " << (void *)Temporary << ")";
Anders Carlsson073846832009-08-12 00:21:52 +0000568}
569
Anders Carlsson76f4a902007-08-21 17:43:55 +0000570//===----------------------------------------------------------------------===//
571// Obj-C Expressions
572//===----------------------------------------------------------------------===//
573
Ted Kremenek36748da2008-02-29 22:04:05 +0000574void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
575 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000576 OS << " selector=" << Node->getSelector().getAsString();
Douglas Gregor9a129192010-04-21 00:45:42 +0000577 switch (Node->getReceiverKind()) {
578 case ObjCMessageExpr::Instance:
579 break;
580
581 case ObjCMessageExpr::Class:
582 OS << " class=";
583 DumpType(Node->getClassReceiver());
584 break;
585
586 case ObjCMessageExpr::SuperInstance:
587 OS << " super (instance)";
588 break;
589
590 case ObjCMessageExpr::SuperClass:
591 OS << " super (class)";
592 break;
593 }
Ted Kremenek36748da2008-02-29 22:04:05 +0000594}
595
Douglas Gregor96c79492010-04-23 22:50:49 +0000596void StmtDumper::VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node) {
597 DumpStmt(Node);
Douglas Gregor46a572b2010-04-26 16:46:50 +0000598 if (VarDecl *CatchParam = Node->getCatchParamDecl()) {
Douglas Gregor96c79492010-04-23 22:50:49 +0000599 OS << " catch parm = ";
600 DumpDeclarator(CatchParam);
601 } else {
602 OS << " catch all";
603 }
604}
605
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000606void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
607 DumpExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000608 OS << " ";
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000609 DumpType(Node->getEncodedType());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000610}
611
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000612void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
613 DumpExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +0000614
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000615 OS << " " << Node->getSelector().getAsString();
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000616}
617
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000618void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
619 DumpExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +0000620
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000621 OS << ' ' << Node->getProtocol();
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000622}
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000623
624void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
625 DumpExpr(Node);
John McCallb7bd14f2010-12-02 01:19:52 +0000626 if (Node->isImplicitProperty()) {
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +0000627 OS << " Kind=MethodRef Getter=\"";
628 if (Node->getImplicitPropertyGetter())
629 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
630 else
631 OS << "(null)";
632
633 OS << "\" Setter=\"";
John McCallb7bd14f2010-12-02 01:19:52 +0000634 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
635 OS << Setter->getSelector().getAsString();
636 else
637 OS << "(null)";
638 OS << "\"";
639 } else {
640 OS << " Kind=PropertyRef Property=\"" << Node->getExplicitProperty() << '"';
641 }
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000642
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000643 if (Node->isSuperReceiver())
644 OS << " super";
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000645}
646
Chris Lattnercbe4f772007-08-08 22:51:59 +0000647//===----------------------------------------------------------------------===//
648// Stmt method implementations
649//===----------------------------------------------------------------------===//
650
651/// dump - This does a local dump of the specified AST fragment. It dumps the
652/// specified node and a few nodes underneath it, but not the whole subtree.
653/// This is useful in a debugger.
Chris Lattner11e30d32007-08-30 06:17:34 +0000654void Stmt::dump(SourceManager &SM) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +0000655 dump(llvm::errs(), SM);
656}
657
658void Stmt::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
659 StmtDumper P(&SM, OS, 4);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000660 P.DumpSubTree(const_cast<Stmt*>(this));
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +0000661 OS << "\n";
Chris Lattner779d5d92007-08-30 00:40:08 +0000662}
663
664/// dump - This does a local dump of the specified AST fragment. It dumps the
665/// specified node and a few nodes underneath it, but not the whole subtree.
666/// This is useful in a debugger.
Chris Lattnercbe4f772007-08-08 22:51:59 +0000667void Stmt::dump() const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +0000668 StmtDumper P(0, llvm::errs(), 4);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000669 P.DumpSubTree(const_cast<Stmt*>(this));
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +0000670 llvm::errs() << "\n";
Chris Lattner779d5d92007-08-30 00:40:08 +0000671}
672
673/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattner11e30d32007-08-30 06:17:34 +0000674void Stmt::dumpAll(SourceManager &SM) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +0000675 StmtDumper P(&SM, llvm::errs(), ~0U);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000676 P.DumpSubTree(const_cast<Stmt*>(this));
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +0000677 llvm::errs() << "\n";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000678}
679
680/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
681void Stmt::dumpAll() const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +0000682 StmtDumper P(0, llvm::errs(), ~0U);
Chris Lattnercfb83dd2007-08-30 00:53:54 +0000683 P.DumpSubTree(const_cast<Stmt*>(this));
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +0000684 llvm::errs() << "\n";
Chris Lattnercbe4f772007-08-08 22:51:59 +0000685}