blob: 8b62dcc940b19d6217b6dafc7d174b0cddd55966 [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 }
Fariborz Jahanianf9b949f2010-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 Lattnerb3938792007-08-30 00:53:54 +000075 }
76 }
Chris Lattnera46325e2010-05-25 17:56:43 +000077 OS << ')';
Chris Lattner6000dac2007-08-08 22:51:59 +000078 } else {
79 Indent();
Daniel Dunbar806c12e2009-12-03 09:13:13 +000080 OS << "<<<NULL>>>";
Chris Lattner6000dac2007-08-08 22:51:59 +000081 }
Chris Lattnerf9e05812007-08-09 18:03:18 +000082 --IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000083 }
Mike Stump1eb44332009-09-09 15:08:12 +000084
Chris Lattnerf9e05812007-08-09 18:03:18 +000085 void DumpDeclarator(Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattner6000dac2007-08-08 22:51:59 +000087 void Indent() const {
88 for (int i = 0, e = IndentLevel; i < e; ++i)
Daniel Dunbar806c12e2009-12-03 09:13:13 +000089 OS << " ";
Chris Lattner6000dac2007-08-08 22:51:59 +000090 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Steve Naroff9dcbfa42007-09-01 21:08:38 +000092 void DumpType(QualType T) {
John McCall49f4e1c2010-12-10 11:01:00 +000093 SplitQualType T_split = T.split();
94 OS << "'" << QualType::getAsString(T_split) << "'";
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000095
Douglas Gregor61366e92008-12-24 00:01:03 +000096 if (!T.isNull()) {
John McCall0953e762009-09-24 19:53:00 +000097 // If the type is sugared, also dump a (shallow) desugared type.
John McCall49f4e1c2010-12-10 11:01:00 +000098 SplitQualType D_split = T.getSplitDesugaredType();
99 if (T_split != D_split)
100 OS << ":'" << QualType::getAsString(D_split) << "'";
Chris Lattnerbad37852008-04-02 05:06:23 +0000101 }
Chris Lattnerfd8f7da2007-08-09 00:36:22 +0000102 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000103 void DumpStmt(const Stmt *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000104 Indent();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000105 OS << "(" << Node->getStmtClassName()
106 << " " << (void*)Node;
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000107 DumpSourceRange(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000108 }
John McCallf89e55a2010-11-18 06:31:45 +0000109 void DumpValueKind(ExprValueKind K) {
110 switch (K) {
111 case VK_RValue: break;
112 case VK_LValue: OS << " lvalue"; break;
113 case VK_XValue: OS << " xvalue"; break;
114 }
115 }
116 void DumpObjectKind(ExprObjectKind K) {
117 switch (K) {
118 case OK_Ordinary: break;
119 case OK_BitField: OS << " bitfield"; break;
120 case OK_ObjCProperty: OS << " objcproperty"; break;
121 case OK_VectorComponent: OS << " vectorcomponent"; break;
122 }
123 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000124 void DumpExpr(const Expr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000125 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000126 OS << ' ';
Chris Lattnerfd8f7da2007-08-09 00:36:22 +0000127 DumpType(Node->getType());
John McCallf89e55a2010-11-18 06:31:45 +0000128 DumpValueKind(Node->getValueKind());
129 DumpObjectKind(Node->getObjectKind());
Chris Lattner6000dac2007-08-08 22:51:59 +0000130 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000131 void DumpSourceRange(const Stmt *Node);
Chris Lattnere300c872007-08-30 06:17:34 +0000132 void DumpLocation(SourceLocation Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattner17a1a722007-08-30 01:00:35 +0000134 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000135 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000136 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000137 void VisitLabelStmt(LabelStmt *Node);
138 void VisitGotoStmt(GotoStmt *Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Chris Lattner17a1a722007-08-30 01:00:35 +0000140 // Exprs
141 void VisitExpr(Expr *Node);
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000142 void VisitCastExpr(CastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000143 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000144 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000145 void VisitCharacterLiteral(CharacterLiteral *Node);
146 void VisitIntegerLiteral(IntegerLiteral *Node);
147 void VisitFloatingLiteral(FloatingLiteral *Node);
148 void VisitStringLiteral(StringLiteral *Str);
149 void VisitUnaryOperator(UnaryOperator *Node);
Sebastian Redl05189992008-11-11 17:56:53 +0000150 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000151 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000152 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000153 void VisitBinaryOperator(BinaryOperator *Node);
154 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
155 void VisitAddrLabelExpr(AddrLabelExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000156
157 // C++
Douglas Gregor49badde2008-10-27 19:41:14 +0000158 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000159 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000160 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000161 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000162 void VisitCXXConstructExpr(CXXConstructExpr *Node);
163 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
John McCall4765fa02010-12-06 08:20:24 +0000164 void VisitExprWithCleanups(ExprWithCleanups *Node);
John McCall9d5f35e2009-12-11 21:50:11 +0000165 void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node);
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000166 void DumpCXXTemporary(CXXTemporary *Temporary);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattner17a1a722007-08-30 01:00:35 +0000168 // ObjC
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000169 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000170 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000171 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000172 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000173 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000174 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000175 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000176 };
177}
178
179//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000180// Utilities
181//===----------------------------------------------------------------------===//
182
183void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000184 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Chris Lattnere300c872007-08-30 06:17:34 +0000186 // The general format we print out is filename:line:col, but we drop pieces
187 // that haven't changed since the last loc printed.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000188 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
189
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000190 if (PLoc.isInvalid()) {
191 OS << "<invalid sloc>";
192 return;
193 }
194
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000195 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000196 OS << PLoc.getFilename() << ':' << PLoc.getLine()
197 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000198 LastLocFilename = PLoc.getFilename();
199 LastLocLine = PLoc.getLine();
200 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000201 OS << "line" << ':' << PLoc.getLine()
202 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000203 LastLocLine = PLoc.getLine();
Chris Lattnere300c872007-08-30 06:17:34 +0000204 } else {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000205 OS << "col" << ':' << PLoc.getColumn();
Chris Lattnere300c872007-08-30 06:17:34 +0000206 }
207}
208
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000209void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000210 // Can't translate locations if a SourceManager isn't available.
211 if (SM == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Chris Lattnere300c872007-08-30 06:17:34 +0000213 // TODO: If the parent expression is available, we can print a delta vs its
214 // location.
215 SourceRange R = Node->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000217 OS << " <";
Chris Lattner311ff022007-10-16 22:36:42 +0000218 DumpLocation(R.getBegin());
219 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000220 OS << ", ";
Chris Lattner311ff022007-10-16 22:36:42 +0000221 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000222 }
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000223 OS << ">";
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Chris Lattnere300c872007-08-30 06:17:34 +0000225 // <t2.c:123:421[blah], t2.c:412:321>
226
227}
228
229
230//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000231// Stmt printing methods.
232//===----------------------------------------------------------------------===//
233
234void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000235 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000236}
237
Chris Lattnerf9e05812007-08-09 18:03:18 +0000238void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000239 // FIXME: Need to complete/beautify this... this code simply shows the
240 // nodes are where they need to be.
241 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000242 OS << "\"typedef " << localType->getUnderlyingType().getAsString()
Benjamin Kramer900fc632010-04-17 09:33:03 +0000243 << ' ' << localType << '"';
Chris Lattner6000dac2007-08-08 22:51:59 +0000244 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000245 OS << "\"";
Chris Lattner6000dac2007-08-08 22:51:59 +0000246 // Emit storage class for vardecls.
247 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
John McCalld931b082010-08-26 03:08:43 +0000248 if (V->getStorageClass() != SC_None)
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000249 OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
250 << " ";
Chris Lattner6000dac2007-08-08 22:51:59 +0000251 }
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Chris Lattner39f34e92008-11-24 04:00:27 +0000253 std::string Name = VD->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000254 VD->getType().getAsStringInternal(Name,
Chris Lattnere4f21422009-06-30 01:26:17 +0000255 PrintingPolicy(VD->getASTContext().getLangOptions()));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000256 OS << Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner6000dac2007-08-08 22:51:59 +0000258 // If this is a vardecl with an initializer, emit it.
259 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
260 if (V->getInit()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000261 OS << " =\n";
Chris Lattnerf9e05812007-08-09 18:03:18 +0000262 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000263 }
264 }
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000265 OS << '"';
Steve Naroff92199282007-11-17 21:37:36 +0000266 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
267 // print a free standing tag decl (e.g. "struct x;").
268 const char *tagname;
269 if (const IdentifierInfo *II = TD->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +0000270 tagname = II->getNameStart();
Steve Naroff92199282007-11-17 21:37:36 +0000271 else
272 tagname = "<anonymous>";
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000273 OS << '"' << TD->getKindName() << ' ' << tagname << ";\"";
Steve Naroff92199282007-11-17 21:37:36 +0000274 // FIXME: print tag bodies.
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000275 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
276 // print using-directive decl (e.g. "using namespace x;")
277 const char *ns;
278 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +0000279 ns = II->getNameStart();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000280 else
281 ns = "<anonymous>";
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000282 OS << '"' << UD->getDeclKindName() << ns << ";\"";
Sebastian Redl0ca43592010-05-04 10:20:17 +0000283 } else if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
284 // print using decl (e.g. "using std::string;")
285 const char *tn = UD->isTypeName() ? "typename " : "";
286 OS << '"' << UD->getDeclKindName() << tn;
287 UD->getTargetNestedNameDecl()->print(OS,
288 PrintingPolicy(UD->getASTContext().getLangOptions()));
289 OS << ";\"";
Chris Lattner6000dac2007-08-08 22:51:59 +0000290 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000291 assert(0 && "Unexpected decl");
292 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000293}
294
Ted Kremenek5399ce22007-12-12 06:59:42 +0000295void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
296 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000297 OS << "\n";
Ted Kremenek04a72b72008-10-06 18:38:35 +0000298 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
299 DI != DE; ++DI) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000300 Decl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000301 ++IndentLevel;
302 Indent();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000303 OS << (void*) D << " ";
Ted Kremenek5399ce22007-12-12 06:59:42 +0000304 DumpDeclarator(D);
Chris Lattnerf2797252009-03-29 16:04:50 +0000305 if (DI+1 != DE)
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000306 OS << "\n";
Ted Kremenek5399ce22007-12-12 06:59:42 +0000307 --IndentLevel;
308 }
309}
310
Chris Lattner6000dac2007-08-08 22:51:59 +0000311void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
312 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000313 OS << " '" << Node->getName() << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +0000314}
315
Chris Lattner6000dac2007-08-08 22:51:59 +0000316void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
317 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000318 OS << " '" << Node->getLabel()->getName()
319 << "':" << (void*)Node->getLabel();
Chris Lattner6000dac2007-08-08 22:51:59 +0000320}
321
Chris Lattner6000dac2007-08-08 22:51:59 +0000322//===----------------------------------------------------------------------===//
323// Expr printing methods.
324//===----------------------------------------------------------------------===//
325
326void StmtDumper::VisitExpr(Expr *Node) {
327 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000328}
329
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000330static void DumpBasePath(llvm::raw_ostream &OS, CastExpr *Node) {
John McCallf871d0c2010-08-07 06:22:56 +0000331 if (Node->path_empty())
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000332 return;
333
334 OS << " (";
335 bool First = true;
John McCallf871d0c2010-08-07 06:22:56 +0000336 for (CastExpr::path_iterator
337 I = Node->path_begin(), E = Node->path_end(); I != E; ++I) {
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000338 const CXXBaseSpecifier *Base = *I;
339 if (!First)
340 OS << " -> ";
341
342 const CXXRecordDecl *RD =
343 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
344
345 if (Base->isVirtual())
346 OS << "virtual ";
347 OS << RD->getName();
348 First = false;
349 }
350
351 OS << ')';
352}
353
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000354void StmtDumper::VisitCastExpr(CastExpr *Node) {
355 DumpExpr(Node);
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000356 OS << " <" << Node->getCastKindName();
357 DumpBasePath(OS, Node);
358 OS << ">";
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000359}
360
Chris Lattner6000dac2007-08-08 22:51:59 +0000361void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
362 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000363
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000364 OS << " ";
Ted Kremenekeb641f92007-09-10 17:32:55 +0000365 switch (Node->getDecl()->getKind()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000366 default: OS << "Decl"; break;
367 case Decl::Function: OS << "FunctionDecl"; break;
368 case Decl::Var: OS << "Var"; break;
369 case Decl::ParmVar: OS << "ParmVar"; break;
370 case Decl::EnumConstant: OS << "EnumConstant"; break;
371 case Decl::Typedef: OS << "Typedef"; break;
372 case Decl::Record: OS << "Record"; break;
373 case Decl::Enum: OS << "Enum"; break;
374 case Decl::CXXRecord: OS << "CXXRecord"; break;
375 case Decl::ObjCInterface: OS << "ObjCInterface"; break;
376 case Decl::ObjCClass: OS << "ObjCClass"; break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Benjamin Kramer900fc632010-04-17 09:33:03 +0000379 OS << "='" << Node->getDecl() << "' " << (void*)Node->getDecl();
Chris Lattner6000dac2007-08-08 22:51:59 +0000380}
381
John McCall9d5f35e2009-12-11 21:50:11 +0000382void StmtDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
383 DumpExpr(Node);
384 OS << " (";
385 if (!Node->requiresADL()) OS << "no ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000386 OS << "ADL) = '" << Node->getName() << '\'';
John McCall9d5f35e2009-12-11 21:50:11 +0000387
388 UnresolvedLookupExpr::decls_iterator
389 I = Node->decls_begin(), E = Node->decls_end();
390 if (I == E) OS << " empty";
391 for (; I != E; ++I)
392 OS << " " << (void*) *I;
393}
394
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000395void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000396 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000397
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000398 OS << " " << Node->getDecl()->getDeclKindName()
Benjamin Kramer900fc632010-04-17 09:33:03 +0000399 << "Decl='" << Node->getDecl()
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000400 << "' " << (void*)Node->getDecl();
Steve Naroff218543b2008-05-23 22:01:24 +0000401 if (Node->isFreeIvar())
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000402 OS << " isFreeIvar";
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000403}
404
Chris Lattnerd9f69102008-08-10 01:53:14 +0000405void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000406 DumpExpr(Node);
407 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000408 default: assert(0 && "unknown case");
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000409 case PredefinedExpr::Func: OS << " __func__"; break;
410 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
411 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000412 }
413}
414
415void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000416 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000417 OS << Node->getValue();
Chris Lattner6000dac2007-08-08 22:51:59 +0000418}
419
420void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
421 DumpExpr(Node);
422
423 bool isSigned = Node->getType()->isSignedIntegerType();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000424 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattner6000dac2007-08-08 22:51:59 +0000425}
426void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
427 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000428 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattner6000dac2007-08-08 22:51:59 +0000429}
Chris Lattner5d661452007-08-26 03:42:43 +0000430
Chris Lattner6000dac2007-08-08 22:51:59 +0000431void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000432 DumpExpr(Str);
433 // FIXME: this doesn't print wstrings right.
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000434 OS << " ";
435 if (Str->isWide())
436 OS << "L";
437 OS << '"';
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000438 OS.write_escaped(Str->getString());
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000439 OS << '"';
Chris Lattner6000dac2007-08-08 22:51:59 +0000440}
Chris Lattner17a1a722007-08-30 01:00:35 +0000441
Chris Lattner6000dac2007-08-08 22:51:59 +0000442void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000443 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000444 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
445 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +0000446}
Sebastian Redl05189992008-11-11 17:56:53 +0000447void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000448 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000449 OS << " " << (Node->isSizeOf() ? "sizeof" : "alignof") << " ";
Sebastian Redl05189992008-11-11 17:56:53 +0000450 if (Node->isArgumentType())
451 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000452}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000453
Chris Lattner6000dac2007-08-08 22:51:59 +0000454void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000455 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000456 OS << " " << (Node->isArrow() ? "->" : ".")
Benjamin Kramer900fc632010-04-17 09:33:03 +0000457 << Node->getMemberDecl() << ' '
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000458 << (void*)Node->getMemberDecl();
Chris Lattner6000dac2007-08-08 22:51:59 +0000459}
Nate Begeman213541a2008-04-18 23:10:10 +0000460void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000461 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000462 OS << " " << Node->getAccessor().getNameStart();
Chris Lattner6000dac2007-08-08 22:51:59 +0000463}
Chris Lattner6000dac2007-08-08 22:51:59 +0000464void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
465 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000466 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnereb14fe82007-08-25 02:00:02 +0000467}
468void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
469 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000470 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
471 << "' ComputeLHSTy=";
Eli Friedmanab3a8522009-03-28 01:22:36 +0000472 DumpType(Node->getComputationLHSType());
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000473 OS << " ComputeResultTy=";
Eli Friedmanab3a8522009-03-28 01:22:36 +0000474 DumpType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000475}
Chris Lattner6000dac2007-08-08 22:51:59 +0000476
477// GNU extensions.
478
479void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000480 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000481 OS << " " << Node->getLabel()->getName()
482 << " " << (void*)Node->getLabel();
Chris Lattner6000dac2007-08-08 22:51:59 +0000483}
484
Chris Lattnerf9e05812007-08-09 18:03:18 +0000485//===----------------------------------------------------------------------===//
486// C++ Expressions
487//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000488
Douglas Gregor49badde2008-10-27 19:41:14 +0000489void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000490 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000491 OS << " " << Node->getCastName()
492 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlsson5cf86ba2010-04-24 19:06:50 +0000493 << " <" << Node->getCastKindName();
494 DumpBasePath(OS, Node);
495 OS << ">";
Chris Lattner6000dac2007-08-08 22:51:59 +0000496}
497
498void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000499 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000500 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000501}
502
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000503void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
504 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000505 OS << " this";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000506}
507
Douglas Gregor49badde2008-10-27 19:41:14 +0000508void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
509 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000510 OS << " functional cast to " << Node->getTypeAsWritten().getAsString();
Douglas Gregor49badde2008-10-27 19:41:14 +0000511}
512
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000513void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
514 DumpExpr(Node);
John McCalld4bbdfe2010-02-02 19:03:45 +0000515 CXXConstructorDecl *Ctor = Node->getConstructor();
516 DumpType(Ctor->getType());
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000517 if (Node->isElidable())
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000518 OS << " elidable";
John McCallf8cf0b02010-08-07 06:38:55 +0000519 if (Node->requiresZeroInitialization())
520 OS << " zeroing";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000521}
522
523void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
524 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000525 OS << " ";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000526 DumpCXXTemporary(Node->getTemporary());
527}
528
John McCall4765fa02010-12-06 08:20:24 +0000529void StmtDumper::VisitExprWithCleanups(ExprWithCleanups *Node) {
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000530 DumpExpr(Node);
531 ++IndentLevel;
532 for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000533 OS << "\n";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000534 Indent();
535 DumpCXXTemporary(Node->getTemporary(i));
536 }
537 --IndentLevel;
538}
539
540void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000541 OS << "(CXXTemporary " << (void *)Temporary << ")";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000542}
543
Anders Carlsson55085182007-08-21 17:43:55 +0000544//===----------------------------------------------------------------------===//
545// Obj-C Expressions
546//===----------------------------------------------------------------------===//
547
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000548void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
549 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000550 OS << " selector=" << Node->getSelector().getAsString();
Douglas Gregor04badcf2010-04-21 00:45:42 +0000551 switch (Node->getReceiverKind()) {
552 case ObjCMessageExpr::Instance:
553 break;
554
555 case ObjCMessageExpr::Class:
556 OS << " class=";
557 DumpType(Node->getClassReceiver());
558 break;
559
560 case ObjCMessageExpr::SuperInstance:
561 OS << " super (instance)";
562 break;
563
564 case ObjCMessageExpr::SuperClass:
565 OS << " super (class)";
566 break;
567 }
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000568}
569
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000570void StmtDumper::VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node) {
571 DumpStmt(Node);
Douglas Gregorc00d8e12010-04-26 16:46:50 +0000572 if (VarDecl *CatchParam = Node->getCatchParamDecl()) {
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000573 OS << " catch parm = ";
574 DumpDeclarator(CatchParam);
575 } else {
576 OS << " catch all";
577 }
578}
579
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000580void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
581 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000582 OS << " ";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000583 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000584}
585
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000586void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
587 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000589 OS << " " << Node->getSelector().getAsString();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000590}
591
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000592void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
593 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Benjamin Kramer900fc632010-04-17 09:33:03 +0000595 OS << ' ' << Node->getProtocol();
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000596}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000597
598void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
599 DumpExpr(Node);
John McCall12f78a62010-12-02 01:19:52 +0000600 if (Node->isImplicitProperty()) {
Fariborz Jahanian99130e52010-12-22 19:46:35 +0000601 OS << " Kind=MethodRef Getter=\"";
602 if (Node->getImplicitPropertyGetter())
603 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
604 else
605 OS << "(null)";
606
607 OS << "\" Setter=\"";
John McCall12f78a62010-12-02 01:19:52 +0000608 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
609 OS << Setter->getSelector().getAsString();
610 else
611 OS << "(null)";
612 OS << "\"";
613 } else {
614 OS << " Kind=PropertyRef Property=\"" << Node->getExplicitProperty() << '"';
615 }
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000616
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000617 if (Node->isSuperReceiver())
618 OS << " super";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000619}
620
Chris Lattner6000dac2007-08-08 22:51:59 +0000621//===----------------------------------------------------------------------===//
622// Stmt method implementations
623//===----------------------------------------------------------------------===//
624
625/// dump - This does a local dump of the specified AST fragment. It dumps the
626/// specified node and a few nodes underneath it, but not the whole subtree.
627/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000628void Stmt::dump(SourceManager &SM) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +0000629 dump(llvm::errs(), SM);
630}
631
632void Stmt::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
633 StmtDumper P(&SM, OS, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000634 P.DumpSubTree(const_cast<Stmt*>(this));
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +0000635 OS << "\n";
Chris Lattner0c727a32007-08-30 00:40:08 +0000636}
637
638/// dump - This does a local dump of the specified AST fragment. It dumps the
639/// specified node and a few nodes underneath it, but not the whole subtree.
640/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000641void Stmt::dump() const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +0000642 StmtDumper P(0, llvm::errs(), 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000643 P.DumpSubTree(const_cast<Stmt*>(this));
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +0000644 llvm::errs() << "\n";
Chris Lattner0c727a32007-08-30 00:40:08 +0000645}
646
647/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000648void Stmt::dumpAll(SourceManager &SM) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +0000649 StmtDumper P(&SM, llvm::errs(), ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000650 P.DumpSubTree(const_cast<Stmt*>(this));
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +0000651 llvm::errs() << "\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000652}
653
654/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
655void Stmt::dumpAll() const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +0000656 StmtDumper P(0, llvm::errs(), ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000657 P.DumpSubTree(const_cast<Stmt*>(this));
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +0000658 llvm::errs() << "\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000659}