blob: ba6218be1426acb794198f1d4995f919594358e6 [file] [log] [blame]
Chris Lattner6000dac2007-08-08 22:51:59 +00001//===--- StmtDumper.cpp - Dumping implementation for Stmt ASTs ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner6000dac2007-08-08 22:51:59 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::dump/Stmt::print methods, which dump out the
11// AST in a form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000018#include "clang/AST/PrettyPrinter.h"
Chris Lattnere300c872007-08-30 06:17:34 +000019#include "clang/Basic/SourceManager.h"
Daniel Dunbar806c12e2009-12-03 09:13:13 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtDumper Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000028 class StmtDumper : public StmtVisitor<StmtDumper> {
Chris Lattnere300c872007-08-30 06:17:34 +000029 SourceManager *SM;
Daniel Dunbar806c12e2009-12-03 09:13:13 +000030 llvm::raw_ostream &OS;
Chris Lattner6000dac2007-08-08 22:51:59 +000031 unsigned IndentLevel;
Mike Stump1eb44332009-09-09 15:08:12 +000032
Chris Lattner6000dac2007-08-08 22:51:59 +000033 /// MaxDepth - When doing a normal dump (not dumpAll) we only want to dump
34 /// the first few levels of an AST. This keeps track of how many ast levels
35 /// are left.
36 unsigned MaxDepth;
Mike Stump1eb44332009-09-09 15:08:12 +000037
Chris Lattnere300c872007-08-30 06:17:34 +000038 /// LastLocFilename/LastLocLine - Keep track of the last location we print
39 /// out so that we can print out deltas from then on out.
40 const char *LastLocFilename;
41 unsigned LastLocLine;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000042
Chris Lattner6000dac2007-08-08 22:51:59 +000043 public:
Daniel Dunbar806c12e2009-12-03 09:13:13 +000044 StmtDumper(SourceManager *sm, llvm::raw_ostream &os, unsigned maxDepth)
45 : SM(sm), OS(os), IndentLevel(0-1), MaxDepth(maxDepth) {
Chris Lattnere300c872007-08-30 06:17:34 +000046 LastLocFilename = "";
47 LastLocLine = ~0U;
48 }
Mike Stump1eb44332009-09-09 15:08:12 +000049
Chris Lattnerf9e05812007-08-09 18:03:18 +000050 void DumpSubTree(Stmt *S) {
Chris Lattner6000dac2007-08-08 22:51:59 +000051 // Prune the recursion if not using dump all.
52 if (MaxDepth == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +000053
Chris Lattnerf9e05812007-08-09 18:03:18 +000054 ++IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000055 if (S) {
Ted Kremenek5399ce22007-12-12 06:59:42 +000056 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
57 VisitDeclStmt(DS);
Mike Stump1eb44332009-09-09 15:08:12 +000058 else {
Ted Kremenek5399ce22007-12-12 06:59:42 +000059 Visit(S);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Ted Kremenek5399ce22007-12-12 06:59:42 +000061 // Print out children.
62 Stmt::child_iterator CI = S->child_begin(), CE = S->child_end();
63 if (CI != CE) {
64 while (CI != CE) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +000065 OS << '\n';
Ted Kremenek5399ce22007-12-12 06:59:42 +000066 DumpSubTree(*CI++);
67 }
Chris Lattnerb3938792007-08-30 00:53:54 +000068 }
Daniel Dunbar806c12e2009-12-03 09:13:13 +000069 OS << ')';
Chris Lattnerb3938792007-08-30 00:53:54 +000070 }
Chris Lattner6000dac2007-08-08 22:51:59 +000071 } else {
72 Indent();
Daniel Dunbar806c12e2009-12-03 09:13:13 +000073 OS << "<<<NULL>>>";
Chris Lattner6000dac2007-08-08 22:51:59 +000074 }
Chris Lattnerf9e05812007-08-09 18:03:18 +000075 --IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000076 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattnerf9e05812007-08-09 18:03:18 +000078 void DumpDeclarator(Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Chris Lattner6000dac2007-08-08 22:51:59 +000080 void Indent() const {
81 for (int i = 0, e = IndentLevel; i < e; ++i)
Daniel Dunbar806c12e2009-12-03 09:13:13 +000082 OS << " ";
Chris Lattner6000dac2007-08-08 22:51:59 +000083 }
Mike Stump1eb44332009-09-09 15:08:12 +000084
Steve Naroff9dcbfa42007-09-01 21:08:38 +000085 void DumpType(QualType T) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +000086 OS << "'" << T.getAsString() << "'";
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000087
Douglas Gregor61366e92008-12-24 00:01:03 +000088 if (!T.isNull()) {
John McCall0953e762009-09-24 19:53:00 +000089 // If the type is sugared, also dump a (shallow) desugared type.
90 QualType Simplified = T.getDesugaredType();
91 if (Simplified != T)
Daniel Dunbar806c12e2009-12-03 09:13:13 +000092 OS << ":'" << Simplified.getAsString() << "'";
Chris Lattnerbad37852008-04-02 05:06:23 +000093 }
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000094 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +000095 void DumpStmt(const Stmt *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +000096 Indent();
Daniel Dunbar806c12e2009-12-03 09:13:13 +000097 OS << "(" << Node->getStmtClassName()
98 << " " << (void*)Node;
Steve Naroff9dcbfa42007-09-01 21:08:38 +000099 DumpSourceRange(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000100 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000101 void DumpExpr(const Expr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000102 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000103 OS << ' ';
Chris Lattnerfd8f7da2007-08-09 00:36:22 +0000104 DumpType(Node->getType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000105 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000106 void DumpSourceRange(const Stmt *Node);
Chris Lattnere300c872007-08-30 06:17:34 +0000107 void DumpLocation(SourceLocation Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner17a1a722007-08-30 01:00:35 +0000109 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000110 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000111 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000112 void VisitLabelStmt(LabelStmt *Node);
113 void VisitGotoStmt(GotoStmt *Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner17a1a722007-08-30 01:00:35 +0000115 // Exprs
116 void VisitExpr(Expr *Node);
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000117 void VisitCastExpr(CastExpr *Node);
Anders Carlsson0e489ea2009-11-14 22:35:18 +0000118 void VisitImplicitCastExpr(ImplicitCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000119 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000120 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000121 void VisitCharacterLiteral(CharacterLiteral *Node);
122 void VisitIntegerLiteral(IntegerLiteral *Node);
123 void VisitFloatingLiteral(FloatingLiteral *Node);
124 void VisitStringLiteral(StringLiteral *Str);
125 void VisitUnaryOperator(UnaryOperator *Node);
Sebastian Redl05189992008-11-11 17:56:53 +0000126 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000127 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000128 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000129 void VisitBinaryOperator(BinaryOperator *Node);
130 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
131 void VisitAddrLabelExpr(AddrLabelExpr *Node);
132 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
133
134 // C++
Douglas Gregor49badde2008-10-27 19:41:14 +0000135 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000136 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000137 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000138 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000139 void VisitCXXConstructExpr(CXXConstructExpr *Node);
140 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
141 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node);
John McCall9d5f35e2009-12-11 21:50:11 +0000142 void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node);
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000143 void DumpCXXTemporary(CXXTemporary *Temporary);
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Chris Lattner17a1a722007-08-30 01:00:35 +0000145 // ObjC
146 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000147 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000148 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000149 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000150 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000151 void VisitObjCImplicitSetterGetterRefExpr(
152 ObjCImplicitSetterGetterRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000153 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000154 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000155 };
156}
157
158//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000159// Utilities
160//===----------------------------------------------------------------------===//
161
162void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000163 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000165 if (SpellingLoc.isInvalid()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000166 OS << "<invalid sloc>";
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000167 return;
168 }
Chris Lattnere300c872007-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 Lattnerb9c3f962009-01-27 07:57:44 +0000172 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
173
174 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000175 OS << PLoc.getFilename() << ':' << PLoc.getLine()
176 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000177 LastLocFilename = PLoc.getFilename();
178 LastLocLine = PLoc.getLine();
179 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000180 OS << "line" << ':' << PLoc.getLine()
181 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000182 LastLocLine = PLoc.getLine();
Chris Lattnere300c872007-08-30 06:17:34 +0000183 } else {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000184 OS << "col" << ':' << PLoc.getColumn();
Chris Lattnere300c872007-08-30 06:17:34 +0000185 }
186}
187
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000188void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000189 // Can't translate locations if a SourceManager isn't available.
190 if (SM == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Chris Lattnere300c872007-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 Stump1eb44332009-09-09 15:08:12 +0000195
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000196 OS << " <";
Chris Lattner311ff022007-10-16 22:36:42 +0000197 DumpLocation(R.getBegin());
198 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000199 OS << ", ";
Chris Lattner311ff022007-10-16 22:36:42 +0000200 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000201 }
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000202 OS << ">";
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattnere300c872007-08-30 06:17:34 +0000204 // <t2.c:123:421[blah], t2.c:412:321>
205
206}
207
208
209//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000210// Stmt printing methods.
211//===----------------------------------------------------------------------===//
212
213void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000214 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000215}
216
Chris Lattnerf9e05812007-08-09 18:03:18 +0000217void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-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 Dunbar806c12e2009-12-03 09:13:13 +0000221 OS << "\"typedef " << localType->getUnderlyingType().getAsString()
222 << " " << localType->getNameAsString() << "\"";
Chris Lattner6000dac2007-08-08 22:51:59 +0000223 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000224 OS << "\"";
Chris Lattner6000dac2007-08-08 22:51:59 +0000225 // Emit storage class for vardecls.
226 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000227 if (V->getStorageClass() != VarDecl::None)
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000228 OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
229 << " ";
Chris Lattner6000dac2007-08-08 22:51:59 +0000230 }
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Chris Lattner39f34e92008-11-24 04:00:27 +0000232 std::string Name = VD->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000233 VD->getType().getAsStringInternal(Name,
Chris Lattnere4f21422009-06-30 01:26:17 +0000234 PrintingPolicy(VD->getASTContext().getLangOptions()));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000235 OS << Name;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattner6000dac2007-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 Dunbar806c12e2009-12-03 09:13:13 +0000240 OS << " =\n";
Chris Lattnerf9e05812007-08-09 18:03:18 +0000241 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000242 }
243 }
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000244 OS << '"';
Steve Naroff92199282007-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 Dunbare013d682009-10-18 20:26:12 +0000249 tagname = II->getNameStart();
Steve Naroff92199282007-11-17 21:37:36 +0000250 else
251 tagname = "<anonymous>";
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000252 OS << '"' << TD->getKindName() << ' ' << tagname << ";\"";
Steve Naroff92199282007-11-17 21:37:36 +0000253 // FIXME: print tag bodies.
Douglas Gregor2a3009a2009-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 Dunbare013d682009-10-18 20:26:12 +0000258 ns = II->getNameStart();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000259 else
260 ns = "<anonymous>";
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000261 OS << '"' << UD->getDeclKindName() << ns << ";\"";
Chris Lattner6000dac2007-08-08 22:51:59 +0000262 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000263 assert(0 && "Unexpected decl");
264 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000265}
266
Ted Kremenek5399ce22007-12-12 06:59:42 +0000267void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
268 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000269 OS << "\n";
Ted Kremenek04a72b72008-10-06 18:38:35 +0000270 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
271 DI != DE; ++DI) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000272 Decl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000273 ++IndentLevel;
274 Indent();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000275 OS << (void*) D << " ";
Ted Kremenek5399ce22007-12-12 06:59:42 +0000276 DumpDeclarator(D);
Chris Lattnerf2797252009-03-29 16:04:50 +0000277 if (DI+1 != DE)
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000278 OS << "\n";
Ted Kremenek5399ce22007-12-12 06:59:42 +0000279 --IndentLevel;
280 }
281}
282
Chris Lattner6000dac2007-08-08 22:51:59 +0000283void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
284 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000285 OS << " '" << Node->getName() << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +0000286}
287
Chris Lattner6000dac2007-08-08 22:51:59 +0000288void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
289 DumpStmt(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000290 OS << " '" << Node->getLabel()->getName()
291 << "':" << (void*)Node->getLabel();
Chris Lattner6000dac2007-08-08 22:51:59 +0000292}
293
Chris Lattner6000dac2007-08-08 22:51:59 +0000294//===----------------------------------------------------------------------===//
295// Expr printing methods.
296//===----------------------------------------------------------------------===//
297
298void StmtDumper::VisitExpr(Expr *Node) {
299 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000300}
301
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000302void StmtDumper::VisitCastExpr(CastExpr *Node) {
303 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000304 OS << " <" << Node->getCastKindName() << ">";
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000305}
306
Anders Carlsson0e489ea2009-11-14 22:35:18 +0000307void StmtDumper::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
308 VisitCastExpr(Node);
309 if (Node->isLvalueCast())
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000310 OS << " lvalue";
Anders Carlsson0e489ea2009-11-14 22:35:18 +0000311}
312
Chris Lattner6000dac2007-08-08 22:51:59 +0000313void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
314 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000315
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000316 OS << " ";
Ted Kremenekeb641f92007-09-10 17:32:55 +0000317 switch (Node->getDecl()->getKind()) {
Daniel Dunbar806c12e2009-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 Kremenekeb641f92007-09-10 17:32:55 +0000329 }
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000331 OS << "='" << Node->getDecl()->getNameAsString()
332 << "' " << (void*)Node->getDecl();
Chris Lattner6000dac2007-08-08 22:51:59 +0000333}
334
John McCall9d5f35e2009-12-11 21:50:11 +0000335void StmtDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
336 DumpExpr(Node);
337 OS << " (";
338 if (!Node->requiresADL()) OS << "no ";
339 OS << "ADL) = '" << Node->getName().getAsString() << "'";
340
341 UnresolvedLookupExpr::decls_iterator
342 I = Node->decls_begin(), E = Node->decls_end();
343 if (I == E) OS << " empty";
344 for (; I != E; ++I)
345 OS << " " << (void*) *I;
346}
347
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000348void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000349 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000350
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000351 OS << " " << Node->getDecl()->getDeclKindName()
352 << "Decl='" << Node->getDecl()->getNameAsString()
353 << "' " << (void*)Node->getDecl();
Steve Naroff218543b2008-05-23 22:01:24 +0000354 if (Node->isFreeIvar())
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000355 OS << " isFreeIvar";
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000356}
357
Chris Lattnerd9f69102008-08-10 01:53:14 +0000358void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000359 DumpExpr(Node);
360 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000361 default: assert(0 && "unknown case");
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000362 case PredefinedExpr::Func: OS << " __func__"; break;
363 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
364 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000365 }
366}
367
368void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000369 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000370 OS << Node->getValue();
Chris Lattner6000dac2007-08-08 22:51:59 +0000371}
372
373void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
374 DumpExpr(Node);
375
376 bool isSigned = Node->getType()->isSignedIntegerType();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000377 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattner6000dac2007-08-08 22:51:59 +0000378}
379void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
380 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000381 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattner6000dac2007-08-08 22:51:59 +0000382}
Chris Lattner5d661452007-08-26 03:42:43 +0000383
Chris Lattner6000dac2007-08-08 22:51:59 +0000384void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000385 DumpExpr(Str);
386 // FIXME: this doesn't print wstrings right.
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000387 OS << " ";
388 if (Str->isWide())
389 OS << "L";
390 OS << '"';
391 OS.write_escaped(llvm::StringRef(Str->getStrData(),
392 Str->getByteLength()));
393 OS << '"';
Chris Lattner6000dac2007-08-08 22:51:59 +0000394}
Chris Lattner17a1a722007-08-30 01:00:35 +0000395
Chris Lattner6000dac2007-08-08 22:51:59 +0000396void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000397 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000398 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
399 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner6000dac2007-08-08 22:51:59 +0000400}
Sebastian Redl05189992008-11-11 17:56:53 +0000401void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000402 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000403 OS << " " << (Node->isSizeOf() ? "sizeof" : "alignof") << " ";
Sebastian Redl05189992008-11-11 17:56:53 +0000404 if (Node->isArgumentType())
405 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000406}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000407
Chris Lattner6000dac2007-08-08 22:51:59 +0000408void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000409 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000410 OS << " " << (Node->isArrow() ? "->" : ".")
411 << Node->getMemberDecl()->getNameAsString() << " "
412 << (void*)Node->getMemberDecl();
Chris Lattner6000dac2007-08-08 22:51:59 +0000413}
Nate Begeman213541a2008-04-18 23:10:10 +0000414void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000415 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000416 OS << " " << Node->getAccessor().getNameStart();
Chris Lattner6000dac2007-08-08 22:51:59 +0000417}
Chris Lattner6000dac2007-08-08 22:51:59 +0000418void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
419 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000420 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnereb14fe82007-08-25 02:00:02 +0000421}
422void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
423 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000424 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
425 << "' ComputeLHSTy=";
Eli Friedmanab3a8522009-03-28 01:22:36 +0000426 DumpType(Node->getComputationLHSType());
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000427 OS << " ComputeResultTy=";
Eli Friedmanab3a8522009-03-28 01:22:36 +0000428 DumpType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000429}
Chris Lattner6000dac2007-08-08 22:51:59 +0000430
431// GNU extensions.
432
433void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000434 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000435 OS << " " << Node->getLabel()->getName()
436 << " " << (void*)Node->getLabel();
Chris Lattner6000dac2007-08-08 22:51:59 +0000437}
438
Chris Lattner6000dac2007-08-08 22:51:59 +0000439void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000440 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000441 OS << " ";
Chris Lattner13cb21f2007-08-09 17:35:30 +0000442 DumpType(Node->getArgType1());
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000443 OS << " ";
Chris Lattner13cb21f2007-08-09 17:35:30 +0000444 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000445}
446
Chris Lattnerf9e05812007-08-09 18:03:18 +0000447//===----------------------------------------------------------------------===//
448// C++ Expressions
449//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000450
Douglas Gregor49badde2008-10-27 19:41:14 +0000451void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000452 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000453 OS << " " << Node->getCastName()
454 << "<" << Node->getTypeAsWritten().getAsString() << ">"
455 << " <" << Node->getCastKindName() << ">";
Chris Lattner6000dac2007-08-08 22:51:59 +0000456}
457
458void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000459 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000460 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000461}
462
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000463void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
464 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000465 OS << " this";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000466}
467
Douglas Gregor49badde2008-10-27 19:41:14 +0000468void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
469 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000470 OS << " functional cast to " << Node->getTypeAsWritten().getAsString();
Douglas Gregor49badde2008-10-27 19:41:14 +0000471}
472
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000473void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
474 DumpExpr(Node);
John McCalld4bbdfe2010-02-02 19:03:45 +0000475 CXXConstructorDecl *Ctor = Node->getConstructor();
476 DumpType(Ctor->getType());
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000477 if (Node->isElidable())
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000478 OS << " elidable";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000479}
480
481void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
482 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000483 OS << " ";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000484 DumpCXXTemporary(Node->getTemporary());
485}
486
487void StmtDumper::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node) {
488 DumpExpr(Node);
489 ++IndentLevel;
490 for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000491 OS << "\n";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000492 Indent();
493 DumpCXXTemporary(Node->getTemporary(i));
494 }
495 --IndentLevel;
496}
497
498void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000499 OS << "(CXXTemporary " << (void *)Temporary << ")";
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000500}
501
Anders Carlsson55085182007-08-21 17:43:55 +0000502//===----------------------------------------------------------------------===//
503// Obj-C Expressions
504//===----------------------------------------------------------------------===//
505
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000506void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
507 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000508 OS << " selector=" << Node->getSelector().getAsString();
509 if (IdentifierInfo *clsName = Node->getClassName())
510 OS << " class=" << clsName->getNameStart();
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000511}
512
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000513void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
514 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000515 OS << " ";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000516 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000517}
518
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000519void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
520 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000522 OS << " " << Node->getSelector().getAsString();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000523}
524
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000525void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
526 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000528 OS << " " << Node->getProtocol()->getNameAsString();
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000529}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000530
531void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
532 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000533
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000534 OS << " Kind=PropertyRef Property=\""
535 << Node->getProperty()->getNameAsString() << "\"";
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000536}
537
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000538void StmtDumper::VisitObjCImplicitSetterGetterRefExpr(
539 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000540 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000542 ObjCMethodDecl *Getter = Node->getGetterMethod();
543 ObjCMethodDecl *Setter = Node->getSetterMethod();
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000544 OS << " Kind=MethodRef Getter=\""
545 << Getter->getSelector().getAsString()
546 << "\" Setter=\"";
547 if (Setter)
548 OS << Setter->getSelector().getAsString();
549 else
550 OS << "(null)";
551 OS << "\"";
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000552}
553
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000554void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
555 DumpExpr(Node);
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000556 OS << " super";
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000557}
558
Chris Lattner6000dac2007-08-08 22:51:59 +0000559//===----------------------------------------------------------------------===//
560// Stmt method implementations
561//===----------------------------------------------------------------------===//
562
563/// dump - This does a local dump of the specified AST fragment. It dumps the
564/// specified node and a few nodes underneath it, but not the whole subtree.
565/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000566void Stmt::dump(SourceManager &SM) const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000567 StmtDumper P(&SM, llvm::errs(), 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000568 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000569 llvm::errs() << "\n";
Chris Lattner0c727a32007-08-30 00:40:08 +0000570}
571
572/// dump - This does a local dump of the specified AST fragment. It dumps the
573/// specified node and a few nodes underneath it, but not the whole subtree.
574/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000575void Stmt::dump() const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000576 StmtDumper P(0, llvm::errs(), 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000577 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000578 llvm::errs() << "\n";
Chris Lattner0c727a32007-08-30 00:40:08 +0000579}
580
581/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000582void Stmt::dumpAll(SourceManager &SM) const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000583 StmtDumper P(&SM, llvm::errs(), ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000584 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000585 llvm::errs() << "\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000586}
587
588/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
589void Stmt::dumpAll() const {
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000590 StmtDumper P(0, llvm::errs(), ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000591 P.DumpSubTree(const_cast<Stmt*>(this));
Daniel Dunbar806c12e2009-12-03 09:13:13 +0000592 llvm::errs() << "\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000593}