blob: b0f06fad24455b077c017a63be14b2a4e0a1066c [file] [log] [blame]
Chris Lattner97e8b6f2007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
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.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner97e8b6f2007-10-07 06:04:32 +000010// AST Consumer Implementations.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner97e8b6f2007-10-07 06:04:32 +000014#include "ASTConsumers.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000015#include "clang/Frontend/PathDiagnosticClients.h"
Nico Weberdae86962008-08-09 18:32:11 +000016#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000020#include "clang/AST/ASTConsumer.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000021#include "clang/AST/ASTContext.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000022#include "clang/CodeGen/ModuleBuilder.h"
23#include "llvm/Module.h"
Daniel Dunbard46075f2008-10-21 23:54:00 +000024#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000025#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000027#include "llvm/System/Path.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000028using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000029
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000030//===----------------------------------------------------------------------===//
31/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000032
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000033namespace {
34 class DeclPrinter {
35 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +000036 llvm::raw_ostream& Out;
Mike Stump071e4da2009-02-10 20:16:46 +000037 unsigned Indentation;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000038
Mike Stump071e4da2009-02-10 20:16:46 +000039 DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()),
40 Indentation(0) {}
41 DeclPrinter() : Out(llvm::errs()), Indentation(0) {}
Ted Kremeneka95d3752008-09-13 05:16:45 +000042 virtual ~DeclPrinter();
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000043
Mike Stump071e4da2009-02-10 20:16:46 +000044 void ChangeIndent(int I) {
45 Indentation += I;
46 }
47
48 llvm::raw_ostream& Indent() {
49 for (unsigned i = 0; i < Indentation; ++i)
50 Out << " ";
51 return Out;
52 }
53
Chris Lattneref5a85d2008-01-02 21:04:16 +000054 void PrintDecl(Decl *D);
Mike Stump071e4da2009-02-10 20:16:46 +000055 void Print(NamedDecl *ND);
56 void Print(NamespaceDecl *NS);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000057 void PrintFunctionDeclStart(FunctionDecl *FD);
58 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000059 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000060 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
61 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
62 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
63 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
64 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
65 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
66 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +000067 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +000068 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Douglas Gregoraaba5e32009-02-04 19:02:06 +000069
70 void PrintTemplateDecl(TemplateDecl *TD);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000071 };
72} // end anonymous namespace
73
Ted Kremeneka95d3752008-09-13 05:16:45 +000074DeclPrinter::~DeclPrinter() {
75 Out.flush();
76}
77
Chris Lattneref5a85d2008-01-02 21:04:16 +000078void DeclPrinter:: PrintDecl(Decl *D) {
Mike Stump071e4da2009-02-10 20:16:46 +000079 Indent();
Chris Lattneref5a85d2008-01-02 21:04:16 +000080 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
81 PrintFunctionDeclStart(FD);
82
Douglas Gregor72971342009-04-18 00:02:19 +000083 // FIXME: Pass a context here so we can use getBody()
84 if (FD->getBodyIfAvailable()) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000085 Out << ' ';
Douglas Gregor72971342009-04-18 00:02:19 +000086 FD->getBodyIfAvailable()->printPretty(Out, 0, Indentation, true);
Chris Lattneref5a85d2008-01-02 21:04:16 +000087 Out << '\n';
88 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000089 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000090 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000091 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000092 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
93 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000094 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
95 PrintObjCInterfaceDecl(OID);
96 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
97 PrintObjCProtocolDecl(PID);
98 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000099 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000100 Out << "@protocol ";
Steve Naroff30833f82009-04-21 15:12:33 +0000101 for (ObjCForwardProtocolDecl::protocol_iterator I = OFPD->protocol_begin(),
102 E = OFPD->protocol_end();
Chris Lattner07fa7742009-02-20 18:10:37 +0000103 I != E; ++I) {
Steve Naroff30833f82009-04-21 15:12:33 +0000104 if (I != OFPD->protocol_begin()) Out << ", ";
Chris Lattner07fa7742009-02-20 18:10:37 +0000105 Out << (*I)->getNameAsString();
Chris Lattneref5a85d2008-01-02 21:04:16 +0000106 }
107 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000108 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000109 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 PrintObjCImplementationDecl(OID);
111 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000112 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000113 PrintObjCCategoryImplDecl(OID);
114 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000115 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000116 PrintObjCCategoryDecl(OID);
117 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000118 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000119 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +0000120 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
121 Out << "@class ";
Chris Lattner67956052009-02-20 18:04:31 +0000122 for (ObjCClassDecl::iterator I = OFCD->begin(), E = OFCD->end();
123 I != E; ++I) {
124 if (I != OFCD->begin()) Out << ", ";
125 Out << (*I)->getNameAsString();
Chris Lattner23a0e452008-06-21 21:40:20 +0000126 }
127 Out << ";\n";
Douglas Gregor45579f52008-12-17 02:04:30 +0000128 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
129 Out << "enum " << ED->getNameAsString() << " {\n";
Douglas Gregor6ab35242009-04-09 21:40:53 +0000130 // FIXME: Shouldn't pass a NULL context
131 ASTContext *Context = 0;
132 for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(*Context),
133 EEnd = ED->enumerator_end(*Context);
Douglas Gregor45579f52008-12-17 02:04:30 +0000134 E != EEnd; ++E)
135 Out << " " << (*E)->getNameAsString() << ",\n";
136 Out << "};\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000137 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000138 // print a free standing tag decl (e.g. "struct x;").
139 Out << TD->getKindName();
140 Out << " ";
141 if (const IdentifierInfo *II = TD->getIdentifier())
142 Out << II->getName();
143
144 Out << " {\n";
145 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000146 // FIXME: Shouldn't pass a NULL context
147 ASTContext *Context = 0;
148 for (DeclContext::decl_iterator i = TD->decls_begin(*Context);
149 i != TD->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000150 ++i)
151 PrintDecl(*i);
152 ChangeIndent(-1);
153 Indent();
154 Out << "}";
155
156 Out << "\n";
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000157 } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) {
158 PrintTemplateDecl(TempD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000159 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
160 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000161 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
162 Out << "asm(";
163 AD->getAsmString()->printPretty(Out);
164 Out << ")\n";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000165 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000166 Print(ND);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000167 } else {
168 assert(0 && "Unknown decl type!");
169 }
170}
171
Mike Stump071e4da2009-02-10 20:16:46 +0000172void DeclPrinter::Print(NamedDecl *ND) {
173 switch (ND->getKind()) {
174 default:
175 // FIXME: Handle the rest of the NamedDecls.
176 Out << "### NamedDecl " << ND->getNameAsString() << "\n";
177 break;
178 case Decl::Field:
179 case Decl::Var: {
180 // Emit storage class for vardecls.
181 if (VarDecl *V = dyn_cast<VarDecl>(ND)) {
182 switch (V->getStorageClass()) {
183 default: assert(0 && "Unknown storage class!");
Mike Stumpc5840c02009-02-10 23:49:50 +0000184 case VarDecl::None: break;
185 case VarDecl::Auto: Out << "auto "; break;
186 case VarDecl::Register: Out << "register "; break;
187 case VarDecl::Extern: Out << "extern "; break;
188 case VarDecl::Static: Out << "static "; break;
Daniel Dunbar7ab41f72009-02-13 22:49:34 +0000189 case VarDecl::PrivateExtern: Out << "__private_extern__ "; break;
Mike Stump071e4da2009-02-10 20:16:46 +0000190 }
191 }
192 std::string Name = ND->getNameAsString();
193 // This forms: "int a".
194 dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name);
Douglas Gregor087fd532009-04-14 23:32:43 +0000195 Out << Name;
196 if (VarDecl *Var = dyn_cast<VarDecl>(ND)) {
197 if (Var->getInit()) {
198 Out << " = ";
199 Var->getInit()->printPretty(Out);
200 }
201 }
202 Out << ";\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000203 break;
204 }
205 case Decl::Namespace:
206 Print(dyn_cast<NamespaceDecl>(ND));
207 break;
208 }
209}
210
211void DeclPrinter::Print(NamespaceDecl *NS) {
212 Out << "namespace " << NS->getNameAsString() << " {\n";
213 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000214 // FIXME: Shouldn't pass a NULL context
215 ASTContext *Context = 0;
216 for (DeclContext::decl_iterator i = NS->decls_begin(*Context);
217 i != NS->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000218 ++i)
219 PrintDecl(*i);
220 ChangeIndent(-1);
221 Indent();
222 Out << "}\n";
223}
224
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000225void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Douglas Gregor72971342009-04-18 00:02:19 +0000226 // FIXME: pass a context so that we can use getBody.
227 bool HasBody = FD->getBodyIfAvailable();
Reid Spencer5f016e22007-07-11 17:01:13 +0000228
Ted Kremenekea75c552007-11-28 21:32:21 +0000229 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000230
Mike Stump071e4da2009-02-10 20:16:46 +0000231 Indent();
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000232 switch (FD->getStorageClass()) {
233 default: assert(0 && "Unknown storage class");
234 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000235 case FunctionDecl::Extern: Out << "extern "; break;
236 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000237 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000238 }
239
240 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000241 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000242
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000243 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000244 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000245
Douglas Gregor72564e72009-02-26 23:50:07 +0000246 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 Proto += "(";
248 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
249 if (i) Proto += ", ";
250 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000251 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000252
253 FT->getArgType(i).getAsStringInternal(ParamStr);
254 Proto += ParamStr;
255 }
256
257 if (FT->isVariadic()) {
258 if (FD->getNumParams()) Proto += ", ";
259 Proto += "...";
260 }
261 Proto += ")";
262 } else {
Douglas Gregor72564e72009-02-26 23:50:07 +0000263 assert(isa<FunctionNoProtoType>(AFT));
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 Proto += "()";
265 }
266
267 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000268 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000269
Douglas Gregor72971342009-04-18 00:02:19 +0000270 if (!FD->getBodyIfAvailable())
Ted Kremenekea75c552007-11-28 21:32:21 +0000271 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000272 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000273}
274
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000275void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000276 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000278 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000279}
280
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000281void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
282 const char *l;
283 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
284 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000285 else {
286 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
287 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000288 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000289 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000290
291 Out << "extern \"" << l << "\" ";
Mike Stump071e4da2009-02-10 20:16:46 +0000292 if (LS->hasBraces()) {
Douglas Gregorf44515a2008-12-16 22:23:02 +0000293 Out << "{\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000294 ChangeIndent(1);
295 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000296
Douglas Gregor6ab35242009-04-09 21:40:53 +0000297 // FIXME: Should not use a NULL DeclContext!
298 ASTContext *Context = 0;
299 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context),
300 DEnd = LS->decls_end(*Context);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000301 D != DEnd; ++D)
302 PrintDecl(*D);
303
Mike Stump071e4da2009-02-10 20:16:46 +0000304 if (LS->hasBraces()) {
305 ChangeIndent(-1);
306 Indent() << "}";
307 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000308 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000309}
310
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000311void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000312 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000313 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000314 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000315 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000316 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000317 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000318
Chris Lattner077bf5e2008-11-24 03:33:13 +0000319 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000320 std::string::size_type pos, lastPos = 0;
Chris Lattner89951a82009-02-20 18:43:26 +0000321 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
322 E = OMD->param_end(); PI != E; ++PI) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000323 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000324 pos = name.find_first_of(":", lastPos);
325 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattner89951a82009-02-20 18:43:26 +0000326 Out << ":(" << (*PI)->getType().getAsString() << ")"
327 << (*PI)->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000328 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000329 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000330
Chris Lattner89951a82009-02-20 18:43:26 +0000331 if (OMD->param_begin() == OMD->param_end())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000332 Out << " " << name;
333
334 if (OMD->isVariadic())
335 Out << ", ...";
336
337 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000338}
339
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000340void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000341 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000342 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000343
344 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000345 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000346 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000347 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000348
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000349 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000350 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000351 ObjCMethodDecl *OMD = *I;
352 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000353 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000354 Out << ' ';
355 OMD->getBody()->printPretty(Out);
356 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000357 }
358 }
359
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000360 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000361 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000362 ObjCMethodDecl *OMD = *I;
363 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000364 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000365 Out << ' ';
366 OMD->getBody()->printPretty(Out);
367 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000368 }
369 }
370
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000371 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
372 E = OID->propimpl_end(); I != E; ++I)
373 PrintObjCPropertyImplDecl(*I);
374
Ted Kremenekea75c552007-11-28 21:32:21 +0000375 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000376}
377
378
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000379void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000380 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000381 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000382
383 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000384 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000385 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000386 Out << "@interface " << I;
387
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000388 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000389 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
390 if (!Protocols.empty()) {
391 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
392 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000393 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000394 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000395
Chris Lattner3db6cae2008-07-21 18:19:38 +0000396 if (!Protocols.empty())
397 Out << ">";
398 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000399
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000400 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000401 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000402 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000403 E = OID->ivar_end(); I != E; ++I) {
404 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000405 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000406 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000407 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000408 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000409
Douglas Gregor6ab35242009-04-09 21:40:53 +0000410 // FIXME: Should not use a NULL DeclContext!
411 ASTContext *Context = 0;
412 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context),
413 E = OID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000414 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000415 bool eol_needed = false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000416 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context),
417 E = OID->classmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000418 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000419
Douglas Gregor6ab35242009-04-09 21:40:53 +0000420 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context),
421 E = OID->instmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000422 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000423
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000424 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000425 // FIXME: implement the rest...
426}
427
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000428void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000429 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000430
Douglas Gregor6ab35242009-04-09 21:40:53 +0000431 // FIXME: Should not use a NULL DeclContext!
432 ASTContext *Context = 0;
433 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context),
434 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000435 PrintObjCPropertyDecl(*I);
436 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000437 // FIXME: implement the rest...
438}
439
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000440void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000441 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000442 << PID->getClassInterface()->getNameAsString()
443 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000444 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
445 E = PID->propimpl_end(); I != E; ++I)
446 PrintObjCPropertyImplDecl(*I);
447 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000448 // FIXME: implement the rest...
449}
450
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000451void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000452 // FIXME: Should not use a NULL DeclContext!
453 ASTContext *Context = 0;
Ted Kremenekea75c552007-11-28 21:32:21 +0000454 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000455 << PID->getClassInterface()->getNameAsString()
456 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000457 // Output property declarations.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000458 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context),
459 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000460 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000461 Out << "@end\n";
462
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000463 // FIXME: implement the rest...
464}
465
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000466void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000467 Out << "@compatibility_alias " << AID->getNameAsString()
468 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000469}
470
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000471/// PrintObjCPropertyDecl - print a property declaration.
472///
473void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000474 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
475 Out << "@required\n";
476 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
477 Out << "@optional\n";
478
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000479 Out << "@property";
480 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
481 bool first = true;
482 Out << " (";
483 if (PDecl->getPropertyAttributes() &
484 ObjCPropertyDecl::OBJC_PR_readonly) {
485 Out << (first ? ' ' : ',') << "readonly";
486 first = false;
487 }
488
489 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
490 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000491 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000492 first = false;
493 }
494 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
495 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000496 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000497 first = false;
498 }
499
500 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
501 Out << (first ? ' ' : ',') << "assign";
502 first = false;
503 }
504
505 if (PDecl->getPropertyAttributes() &
506 ObjCPropertyDecl::OBJC_PR_readwrite) {
507 Out << (first ? ' ' : ',') << "readwrite";
508 first = false;
509 }
510
511 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
512 Out << (first ? ' ' : ',') << "retain";
513 first = false;
514 }
515
516 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
517 Out << (first ? ' ' : ',') << "copy";
518 first = false;
519 }
520
521 if (PDecl->getPropertyAttributes() &
522 ObjCPropertyDecl::OBJC_PR_nonatomic) {
523 Out << (first ? ' ' : ',') << "nonatomic";
524 first = false;
525 }
526 Out << " )";
527 }
528 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000529 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000530
531 Out << ";\n";
532}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000533
534/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
535/// declaration syntax.
536///
537void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000538 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000539 Out << "\n@synthesize ";
540 else
541 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000542 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000543 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000544 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000545 Out << ";\n";
546}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000547
548/// PrintTemplateParams - Print a template parameter list and recursively print
549/// it's underlying top-level definition.
550void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
551 // TODO: Write template parameters.
552 Out << "template <...> ";
553 PrintDecl(TD->getTemplatedDecl());
554}
555
556
557
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000558//===----------------------------------------------------------------------===//
559/// ASTPrinter - Pretty-printer of ASTs
560
Chris Lattner3d4997d2007-09-15 23:02:28 +0000561namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000562 class ASTPrinter : public ASTConsumer, public DeclPrinter {
563 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000564 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000565
Chris Lattner682bf922009-03-29 16:50:03 +0000566 virtual void HandleTopLevelDecl(DeclGroupRef D) {
567 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
568 PrintDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000570 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000571} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +0000572
Ted Kremeneka95d3752008-09-13 05:16:45 +0000573ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000574 return new ASTPrinter(out);
575}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000576
577//===----------------------------------------------------------------------===//
578/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000579
580namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000581 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000582 SourceManager *SM;
583 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000584 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000585
Ted Kremenek95041a22007-12-19 22:51:13 +0000586 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000587 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000588 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000589
Chris Lattner682bf922009-03-29 16:50:03 +0000590 virtual void HandleTopLevelDecl(DeclGroupRef D) {
591 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
592 HandleTopLevelSingleDecl(*I);
593 }
594 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattner3d4997d2007-09-15 23:02:28 +0000595 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000596} // end anonymous namespace
597
Chris Lattner682bf922009-03-29 16:50:03 +0000598void ASTDumper::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000599 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
600 PrintFunctionDeclStart(FD);
601
Douglas Gregor72971342009-04-18 00:02:19 +0000602 if (FD->getBodyIfAvailable()) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000603 Out << '\n';
604 // FIXME: convert dumper to use std::ostream?
Douglas Gregor72971342009-04-18 00:02:19 +0000605 FD->getBodyIfAvailable()->dumpAll(*SM);
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000606 Out << '\n';
607 }
608 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
609 PrintTypeDefDecl(TD);
610 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
611 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
612 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
613 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
614 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
615 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
616 } else if (isa<ObjCForwardProtocolDecl>(D)) {
617 Out << "Read objc fwd protocol decl\n";
618 } else if (isa<ObjCClassDecl>(D)) {
619 Out << "Read objc fwd class decl\n";
620 } else if (isa<FileScopeAsmDecl>(D)) {
621 Out << "Read file scope asm decl\n";
622 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
623 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
624 << "'\n";
625 if (MD->getBody()) {
626 // FIXME: convert dumper to use std::ostream?
627 MD->getBody()->dumpAll(*SM);
628 Out << '\n';
629 }
630 } else if (isa<ObjCImplementationDecl>(D)) {
631 Out << "Read objc implementation decl\n";
632 } else if (isa<ObjCCategoryImplDecl>(D)) {
633 Out << "Read objc category implementation decl\n";
634 } else if (isa<LinkageSpecDecl>(D)) {
635 Out << "Read linkage spec decl\n";
636 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
637 Out << "Read top-level variable decl: '" << ND->getNameAsString()
638 << "'\n";
639 } else {
640 assert(0 && "Unknown decl type!");
641 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000642}
643
Chris Lattner3d4997d2007-09-15 23:02:28 +0000644ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
645
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000646//===----------------------------------------------------------------------===//
647/// ASTViewer - AST Visualization
648
Ted Kremenek80de08f2007-09-19 21:29:43 +0000649namespace {
650 class ASTViewer : public ASTConsumer {
651 SourceManager *SM;
652 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000653 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000654 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000655 }
Chris Lattner682bf922009-03-29 16:50:03 +0000656
657 virtual void HandleTopLevelDecl(DeclGroupRef D) {
658 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
659 HandleTopLevelSingleDecl(*I);
660 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000661
Chris Lattner682bf922009-03-29 16:50:03 +0000662 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000663 };
664}
665
Chris Lattner682bf922009-03-29 16:50:03 +0000666void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000667 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
668 DeclPrinter().PrintFunctionDeclStart(FD);
669
Douglas Gregor72971342009-04-18 00:02:19 +0000670 if (FD->getBodyIfAvailable()) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000671 llvm::cerr << '\n';
Douglas Gregor72971342009-04-18 00:02:19 +0000672 FD->getBodyIfAvailable()->viewAST();
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000673 llvm::cerr << '\n';
674 }
675 return;
676 }
677
678 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
679 DeclPrinter().PrintObjCMethodDecl(MD);
680
681 if (MD->getBody()) {
682 llvm::cerr << '\n';
683 MD->getBody()->viewAST();
684 llvm::cerr << '\n';
685 }
686 }
687}
688
689
Ted Kremenek80de08f2007-09-19 21:29:43 +0000690ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
691
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000692//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000693/// DeclContextPrinter - Decl and DeclContext Visualization
694
695namespace {
696
697class DeclContextPrinter : public ASTConsumer {
698 llvm::raw_ostream& Out;
699public:
700 DeclContextPrinter() : Out(llvm::errs()) {}
701
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000702 void HandleTranslationUnit(ASTContext &C) {
703 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000704 }
705
706 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
707};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000708} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000709
710void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
711 unsigned Indentation) {
712 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000713 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000714 case Decl::TranslationUnit:
715 Out << "[translation unit] " << DC;
716 break;
717 case Decl::Namespace: {
718 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000719 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000720 Out << ND->getNameAsString();
721 break;
722 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000723 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000724 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000725 if (ED->isDefinition())
726 Out << "[enum] ";
727 else
728 Out << "<enum> ";
729 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000730 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000731 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000732 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000733 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000734 if (RD->isDefinition())
735 Out << "[struct] ";
736 else
737 Out << "<struct> ";
738 Out << RD->getNameAsString();
739 break;
740 }
741 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000742 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000743 if (RD->isDefinition())
744 Out << "[class] ";
745 else
746 Out << "<class> ";
747 Out << RD->getNameAsString() << " " << DC;
748 break;
749 }
750 case Decl::ObjCMethod:
751 Out << "[objc method]";
752 break;
753 case Decl::ObjCInterface:
754 Out << "[objc interface]";
755 break;
756 case Decl::ObjCCategory:
757 Out << "[objc category]";
758 break;
759 case Decl::ObjCProtocol:
760 Out << "[objc protocol]";
761 break;
762 case Decl::ObjCImplementation:
763 Out << "[objc implementation]";
764 break;
765 case Decl::ObjCCategoryImpl:
766 Out << "[objc categoryimpl]";
767 break;
768 case Decl::LinkageSpec:
769 Out << "[linkage spec]";
770 break;
771 case Decl::Block:
772 Out << "[block]";
773 break;
774 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000775 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000776 if (FD->isThisDeclarationADefinition())
777 Out << "[function] ";
778 else
779 Out << "<function> ";
780 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000781 // Print the parameters.
782 Out << "(";
783 bool PrintComma = false;
784 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
785 E = FD->param_end(); I != E; ++I) {
786 if (PrintComma)
787 Out << ", ";
788 else
789 PrintComma = true;
790 Out << (*I)->getNameAsString();
791 }
792 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000793 break;
794 }
795 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000796 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000797 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000798 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000799 else if (D->isImplicit())
800 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000801 else
802 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000803 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000804 // Print the parameters.
805 Out << "(";
806 bool PrintComma = false;
807 for (FunctionDecl::param_const_iterator I = D->param_begin(),
808 E = D->param_end(); I != E; ++I) {
809 if (PrintComma)
810 Out << ", ";
811 else
812 PrintComma = true;
813 Out << (*I)->getNameAsString();
814 }
815 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000816
817 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000818 const DeclContext* SemaDC = D->getDeclContext();
819 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000820 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000821 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000822
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000823 break;
824 }
825 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000826 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000827 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000828 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000829 else if (D->isImplicit())
830 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000831 else
832 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000833 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000834 // Print the parameters.
835 Out << "(";
836 bool PrintComma = false;
837 for (FunctionDecl::param_const_iterator I = D->param_begin(),
838 E = D->param_end(); I != E; ++I) {
839 if (PrintComma)
840 Out << ", ";
841 else
842 PrintComma = true;
843 Out << (*I)->getNameAsString();
844 }
845 Out << ")";
846
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000847 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000848 const DeclContext* SemaDC = D->getDeclContext();
849 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000850 if (SemaDC != LexicalDC)
851 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000852 break;
853 }
854 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000855 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000856 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000857 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000858 else if (D->isImplicit())
859 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000860 else
861 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000862 Out << D->getNameAsString();
863 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000864 const DeclContext* SemaDC = D->getDeclContext();
865 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000866 if (SemaDC != LexicalDC)
867 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000868 break;
869 }
870 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000871 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000872 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000873 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000874 else if (D->isImplicit())
875 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000876 else
877 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000878 Out << D->getNameAsString();
879 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000880 const DeclContext* SemaDC = D->getDeclContext();
881 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000882 if (SemaDC != LexicalDC)
883 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000884 break;
885 }
886
887 default:
888 assert(0 && "a decl that inherits DeclContext isn't handled");
889 }
890
891 Out << "\n";
892
893 // Print decls in the DeclContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000894 // FIXME: Should not use a NULL DeclContext!
895 ASTContext *Context = 0;
896 for (DeclContext::decl_iterator I = DC->decls_begin(*Context),
897 E = DC->decls_end(*Context);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000898 I != E; ++I) {
899 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000900 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000901
902 Decl::Kind DK = I->getKind();
903 switch (DK) {
904 case Decl::Namespace:
905 case Decl::Enum:
906 case Decl::Record:
907 case Decl::CXXRecord:
908 case Decl::ObjCMethod:
909 case Decl::ObjCInterface:
910 case Decl::ObjCCategory:
911 case Decl::ObjCProtocol:
912 case Decl::ObjCImplementation:
913 case Decl::ObjCCategoryImpl:
914 case Decl::LinkageSpec:
915 case Decl::Block:
916 case Decl::Function:
917 case Decl::CXXMethod:
918 case Decl::CXXConstructor:
919 case Decl::CXXDestructor:
920 case Decl::CXXConversion:
921 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000922 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000923 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000924 break;
925 }
926 case Decl::Field: {
927 FieldDecl* FD = cast<FieldDecl>(*I);
928 Out << "<field> " << FD->getNameAsString() << "\n";
929 break;
930 }
931 case Decl::Typedef: {
932 TypedefDecl* TD = cast<TypedefDecl>(*I);
933 Out << "<typedef> " << TD->getNameAsString() << "\n";
934 break;
935 }
936 case Decl::EnumConstant: {
937 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
938 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
939 break;
940 }
941 case Decl::Var: {
942 VarDecl* VD = cast<VarDecl>(*I);
943 Out << "<var> " << VD->getNameAsString() << "\n";
944 break;
945 }
946 case Decl::ImplicitParam: {
947 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
948 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
949 break;
950 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000951 case Decl::ParmVar: {
952 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
953 Out << "<parameter> " << PVD->getNameAsString() << "\n";
954 break;
955 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000956 case Decl::OriginalParmVar: {
957 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
958 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
959 break;
960 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000961 case Decl::ObjCProperty: {
962 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
963 Out << "<objc property> " << OPD->getNameAsString() << "\n";
964 break;
965 }
966 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000967 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000968 assert(0 && "decl unhandled");
969 }
970 }
971}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000972ASTConsumer *clang::CreateDeclContextPrinter() {
973 return new DeclContextPrinter();
974}
975
976//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000977/// InheritanceViewer - C++ Inheritance Visualization
978
979namespace {
980class InheritanceViewer : public ASTConsumer {
981 const std::string clsname;
982public:
983 InheritanceViewer(const std::string& cname) : clsname(cname) {}
984
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000985 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000986 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000987 if (RecordType *T = dyn_cast<RecordType>(*I)) {
988 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
989 // FIXME: This lookup needs to be generalized to handle namespaces and
990 // (when we support them) templates.
991 if (D->getNameAsString() == clsname) {
992 D->viewInheritance(C);
993 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000994 }
995 }
996 }
997};
998}
999
1000ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
1001 return new InheritanceViewer(clsname);
1002}