blob: 7595c2a0fad5d2a6ff468fde9bb7fbd5f733aec1 [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
Douglas Gregor653f1b12009-04-23 01:02:12 +0000349 // FIXME: Don't use a NULL context
350 ASTContext *Context = 0;
351 for (ObjCImplementationDecl::instmeth_iterator
352 I = OID->instmeth_begin(*Context),
353 E = OID->instmeth_end(*Context);
354 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000355 ObjCMethodDecl *OMD = *I;
356 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000357 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000358 Out << ' ';
359 OMD->getBody()->printPretty(Out);
360 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000361 }
362 }
363
Douglas Gregor653f1b12009-04-23 01:02:12 +0000364 for (ObjCImplementationDecl::classmeth_iterator
365 I = OID->classmeth_begin(*Context),
366 E = OID->classmeth_end(*Context);
367 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000368 ObjCMethodDecl *OMD = *I;
369 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000370 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000371 Out << ' ';
372 OMD->getBody()->printPretty(Out);
373 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000374 }
375 }
376
Douglas Gregor653f1b12009-04-23 01:02:12 +0000377 for (ObjCImplementationDecl::propimpl_iterator
378 I = OID->propimpl_begin(*Context),
379 E = OID->propimpl_end(*Context); I != E; ++I)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000380 PrintObjCPropertyImplDecl(*I);
381
Ted Kremenekea75c552007-11-28 21:32:21 +0000382 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000383}
384
385
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000386void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000387 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000388 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000389
390 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000391 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000392 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000393 Out << "@interface " << I;
394
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000395 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000396 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
397 if (!Protocols.empty()) {
398 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
399 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000400 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000401 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000402
Chris Lattner3db6cae2008-07-21 18:19:38 +0000403 if (!Protocols.empty())
404 Out << ">";
405 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000406
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000407 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000408 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000409 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000410 E = OID->ivar_end(); I != E; ++I) {
411 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000412 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000413 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000414 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000415 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000416
Douglas Gregor6ab35242009-04-09 21:40:53 +0000417 // FIXME: Should not use a NULL DeclContext!
418 ASTContext *Context = 0;
419 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context),
420 E = OID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000421 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000422 bool eol_needed = false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000423 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context),
424 E = OID->classmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000425 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000426
Douglas Gregor6ab35242009-04-09 21:40:53 +0000427 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context),
428 E = OID->instmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000429 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000430
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000431 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000432 // FIXME: implement the rest...
433}
434
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000435void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000436 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000437
Douglas Gregor6ab35242009-04-09 21:40:53 +0000438 // FIXME: Should not use a NULL DeclContext!
439 ASTContext *Context = 0;
440 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context),
441 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000442 PrintObjCPropertyDecl(*I);
443 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000444 // FIXME: implement the rest...
445}
446
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000447void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000448 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000449 << PID->getClassInterface()->getNameAsString()
450 << '(' << PID->getNameAsString() << ");\n";
Douglas Gregor653f1b12009-04-23 01:02:12 +0000451
452 // FIXME: Don't use a NULL context here
453 ASTContext *Context = 0;
454 for (ObjCCategoryImplDecl::propimpl_iterator
455 I = PID->propimpl_begin(*Context),
456 E = PID->propimpl_end(*Context); I != E; ++I)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000457 PrintObjCPropertyImplDecl(*I);
458 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000459 // FIXME: implement the rest...
460}
461
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000462void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000463 // FIXME: Should not use a NULL DeclContext!
464 ASTContext *Context = 0;
Ted Kremenekea75c552007-11-28 21:32:21 +0000465 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000466 << PID->getClassInterface()->getNameAsString()
467 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000468 // Output property declarations.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000469 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context),
470 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000471 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000472 Out << "@end\n";
473
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000474 // FIXME: implement the rest...
475}
476
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000477void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000478 Out << "@compatibility_alias " << AID->getNameAsString()
479 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000480}
481
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000482/// PrintObjCPropertyDecl - print a property declaration.
483///
484void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000485 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
486 Out << "@required\n";
487 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
488 Out << "@optional\n";
489
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000490 Out << "@property";
491 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
492 bool first = true;
493 Out << " (";
494 if (PDecl->getPropertyAttributes() &
495 ObjCPropertyDecl::OBJC_PR_readonly) {
496 Out << (first ? ' ' : ',') << "readonly";
497 first = false;
498 }
499
500 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
501 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000502 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000503 first = false;
504 }
505 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
506 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000507 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000508 first = false;
509 }
510
511 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
512 Out << (first ? ' ' : ',') << "assign";
513 first = false;
514 }
515
516 if (PDecl->getPropertyAttributes() &
517 ObjCPropertyDecl::OBJC_PR_readwrite) {
518 Out << (first ? ' ' : ',') << "readwrite";
519 first = false;
520 }
521
522 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
523 Out << (first ? ' ' : ',') << "retain";
524 first = false;
525 }
526
527 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
528 Out << (first ? ' ' : ',') << "copy";
529 first = false;
530 }
531
532 if (PDecl->getPropertyAttributes() &
533 ObjCPropertyDecl::OBJC_PR_nonatomic) {
534 Out << (first ? ' ' : ',') << "nonatomic";
535 first = false;
536 }
537 Out << " )";
538 }
539 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000540 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000541
542 Out << ";\n";
543}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000544
545/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
546/// declaration syntax.
547///
548void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000549 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000550 Out << "\n@synthesize ";
551 else
552 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000553 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000554 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000555 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000556 Out << ";\n";
557}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000558
559/// PrintTemplateParams - Print a template parameter list and recursively print
560/// it's underlying top-level definition.
561void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
562 // TODO: Write template parameters.
563 Out << "template <...> ";
564 PrintDecl(TD->getTemplatedDecl());
565}
566
567
568
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000569//===----------------------------------------------------------------------===//
570/// ASTPrinter - Pretty-printer of ASTs
571
Chris Lattner3d4997d2007-09-15 23:02:28 +0000572namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000573 class ASTPrinter : public ASTConsumer, public DeclPrinter {
574 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000575 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000576
Chris Lattner682bf922009-03-29 16:50:03 +0000577 virtual void HandleTopLevelDecl(DeclGroupRef D) {
578 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
579 PrintDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000581 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000582} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +0000583
Ted Kremeneka95d3752008-09-13 05:16:45 +0000584ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000585 return new ASTPrinter(out);
586}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000587
588//===----------------------------------------------------------------------===//
589/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000590
591namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000592 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000593 SourceManager *SM;
594 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000595 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000596
Ted Kremenek95041a22007-12-19 22:51:13 +0000597 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000598 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000599 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000600
Chris Lattner682bf922009-03-29 16:50:03 +0000601 virtual void HandleTopLevelDecl(DeclGroupRef D) {
602 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
603 HandleTopLevelSingleDecl(*I);
604 }
605 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattner3d4997d2007-09-15 23:02:28 +0000606 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000607} // end anonymous namespace
608
Chris Lattner682bf922009-03-29 16:50:03 +0000609void ASTDumper::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000610 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
611 PrintFunctionDeclStart(FD);
612
Douglas Gregor72971342009-04-18 00:02:19 +0000613 if (FD->getBodyIfAvailable()) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000614 Out << '\n';
615 // FIXME: convert dumper to use std::ostream?
Douglas Gregor72971342009-04-18 00:02:19 +0000616 FD->getBodyIfAvailable()->dumpAll(*SM);
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000617 Out << '\n';
618 }
619 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
620 PrintTypeDefDecl(TD);
621 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
622 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
623 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
624 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
625 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
626 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
627 } else if (isa<ObjCForwardProtocolDecl>(D)) {
628 Out << "Read objc fwd protocol decl\n";
629 } else if (isa<ObjCClassDecl>(D)) {
630 Out << "Read objc fwd class decl\n";
631 } else if (isa<FileScopeAsmDecl>(D)) {
632 Out << "Read file scope asm decl\n";
633 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
634 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
635 << "'\n";
636 if (MD->getBody()) {
637 // FIXME: convert dumper to use std::ostream?
638 MD->getBody()->dumpAll(*SM);
639 Out << '\n';
640 }
641 } else if (isa<ObjCImplementationDecl>(D)) {
642 Out << "Read objc implementation decl\n";
643 } else if (isa<ObjCCategoryImplDecl>(D)) {
644 Out << "Read objc category implementation decl\n";
645 } else if (isa<LinkageSpecDecl>(D)) {
646 Out << "Read linkage spec decl\n";
647 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
648 Out << "Read top-level variable decl: '" << ND->getNameAsString()
649 << "'\n";
650 } else {
651 assert(0 && "Unknown decl type!");
652 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000653}
654
Chris Lattner3d4997d2007-09-15 23:02:28 +0000655ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
656
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000657//===----------------------------------------------------------------------===//
658/// ASTViewer - AST Visualization
659
Ted Kremenek80de08f2007-09-19 21:29:43 +0000660namespace {
661 class ASTViewer : public ASTConsumer {
662 SourceManager *SM;
663 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000664 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000665 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000666 }
Chris Lattner682bf922009-03-29 16:50:03 +0000667
668 virtual void HandleTopLevelDecl(DeclGroupRef D) {
669 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
670 HandleTopLevelSingleDecl(*I);
671 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000672
Chris Lattner682bf922009-03-29 16:50:03 +0000673 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000674 };
675}
676
Chris Lattner682bf922009-03-29 16:50:03 +0000677void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000678 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
679 DeclPrinter().PrintFunctionDeclStart(FD);
680
Douglas Gregor72971342009-04-18 00:02:19 +0000681 if (FD->getBodyIfAvailable()) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000682 llvm::cerr << '\n';
Douglas Gregor72971342009-04-18 00:02:19 +0000683 FD->getBodyIfAvailable()->viewAST();
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000684 llvm::cerr << '\n';
685 }
686 return;
687 }
688
689 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
690 DeclPrinter().PrintObjCMethodDecl(MD);
691
692 if (MD->getBody()) {
693 llvm::cerr << '\n';
694 MD->getBody()->viewAST();
695 llvm::cerr << '\n';
696 }
697 }
698}
699
700
Ted Kremenek80de08f2007-09-19 21:29:43 +0000701ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
702
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000703//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000704/// DeclContextPrinter - Decl and DeclContext Visualization
705
706namespace {
707
708class DeclContextPrinter : public ASTConsumer {
709 llvm::raw_ostream& Out;
710public:
711 DeclContextPrinter() : Out(llvm::errs()) {}
712
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000713 void HandleTranslationUnit(ASTContext &C) {
714 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000715 }
716
717 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
718};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000719} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000720
721void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
722 unsigned Indentation) {
723 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000724 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000725 case Decl::TranslationUnit:
726 Out << "[translation unit] " << DC;
727 break;
728 case Decl::Namespace: {
729 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000730 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000731 Out << ND->getNameAsString();
732 break;
733 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000734 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000735 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000736 if (ED->isDefinition())
737 Out << "[enum] ";
738 else
739 Out << "<enum> ";
740 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000741 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000742 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000743 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000744 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000745 if (RD->isDefinition())
746 Out << "[struct] ";
747 else
748 Out << "<struct> ";
749 Out << RD->getNameAsString();
750 break;
751 }
752 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000753 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000754 if (RD->isDefinition())
755 Out << "[class] ";
756 else
757 Out << "<class> ";
758 Out << RD->getNameAsString() << " " << DC;
759 break;
760 }
761 case Decl::ObjCMethod:
762 Out << "[objc method]";
763 break;
764 case Decl::ObjCInterface:
765 Out << "[objc interface]";
766 break;
767 case Decl::ObjCCategory:
768 Out << "[objc category]";
769 break;
770 case Decl::ObjCProtocol:
771 Out << "[objc protocol]";
772 break;
773 case Decl::ObjCImplementation:
774 Out << "[objc implementation]";
775 break;
776 case Decl::ObjCCategoryImpl:
777 Out << "[objc categoryimpl]";
778 break;
779 case Decl::LinkageSpec:
780 Out << "[linkage spec]";
781 break;
782 case Decl::Block:
783 Out << "[block]";
784 break;
785 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000786 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000787 if (FD->isThisDeclarationADefinition())
788 Out << "[function] ";
789 else
790 Out << "<function> ";
791 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000792 // Print the parameters.
793 Out << "(";
794 bool PrintComma = false;
795 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
796 E = FD->param_end(); I != E; ++I) {
797 if (PrintComma)
798 Out << ", ";
799 else
800 PrintComma = true;
801 Out << (*I)->getNameAsString();
802 }
803 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000804 break;
805 }
806 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000807 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000808 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000809 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000810 else if (D->isImplicit())
811 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000812 else
813 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000814 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000815 // Print the parameters.
816 Out << "(";
817 bool PrintComma = false;
818 for (FunctionDecl::param_const_iterator I = D->param_begin(),
819 E = D->param_end(); I != E; ++I) {
820 if (PrintComma)
821 Out << ", ";
822 else
823 PrintComma = true;
824 Out << (*I)->getNameAsString();
825 }
826 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000827
828 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000829 const DeclContext* SemaDC = D->getDeclContext();
830 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000831 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000832 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000833
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000834 break;
835 }
836 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000837 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000838 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000839 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000840 else if (D->isImplicit())
841 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000842 else
843 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000844 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000845 // Print the parameters.
846 Out << "(";
847 bool PrintComma = false;
848 for (FunctionDecl::param_const_iterator I = D->param_begin(),
849 E = D->param_end(); I != E; ++I) {
850 if (PrintComma)
851 Out << ", ";
852 else
853 PrintComma = true;
854 Out << (*I)->getNameAsString();
855 }
856 Out << ")";
857
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000858 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000859 const DeclContext* SemaDC = D->getDeclContext();
860 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000861 if (SemaDC != LexicalDC)
862 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000863 break;
864 }
865 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000866 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000867 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000868 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000869 else if (D->isImplicit())
870 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000871 else
872 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000873 Out << D->getNameAsString();
874 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000875 const DeclContext* SemaDC = D->getDeclContext();
876 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000877 if (SemaDC != LexicalDC)
878 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000879 break;
880 }
881 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000882 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000883 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000884 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000885 else if (D->isImplicit())
886 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000887 else
888 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000889 Out << D->getNameAsString();
890 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000891 const DeclContext* SemaDC = D->getDeclContext();
892 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000893 if (SemaDC != LexicalDC)
894 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000895 break;
896 }
897
898 default:
899 assert(0 && "a decl that inherits DeclContext isn't handled");
900 }
901
902 Out << "\n";
903
904 // Print decls in the DeclContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000905 // FIXME: Should not use a NULL DeclContext!
906 ASTContext *Context = 0;
907 for (DeclContext::decl_iterator I = DC->decls_begin(*Context),
908 E = DC->decls_end(*Context);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000909 I != E; ++I) {
910 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000911 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000912
913 Decl::Kind DK = I->getKind();
914 switch (DK) {
915 case Decl::Namespace:
916 case Decl::Enum:
917 case Decl::Record:
918 case Decl::CXXRecord:
919 case Decl::ObjCMethod:
920 case Decl::ObjCInterface:
921 case Decl::ObjCCategory:
922 case Decl::ObjCProtocol:
923 case Decl::ObjCImplementation:
924 case Decl::ObjCCategoryImpl:
925 case Decl::LinkageSpec:
926 case Decl::Block:
927 case Decl::Function:
928 case Decl::CXXMethod:
929 case Decl::CXXConstructor:
930 case Decl::CXXDestructor:
931 case Decl::CXXConversion:
932 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000933 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000934 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000935 break;
936 }
937 case Decl::Field: {
938 FieldDecl* FD = cast<FieldDecl>(*I);
939 Out << "<field> " << FD->getNameAsString() << "\n";
940 break;
941 }
942 case Decl::Typedef: {
943 TypedefDecl* TD = cast<TypedefDecl>(*I);
944 Out << "<typedef> " << TD->getNameAsString() << "\n";
945 break;
946 }
947 case Decl::EnumConstant: {
948 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
949 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
950 break;
951 }
952 case Decl::Var: {
953 VarDecl* VD = cast<VarDecl>(*I);
954 Out << "<var> " << VD->getNameAsString() << "\n";
955 break;
956 }
957 case Decl::ImplicitParam: {
958 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
959 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
960 break;
961 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000962 case Decl::ParmVar: {
963 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
964 Out << "<parameter> " << PVD->getNameAsString() << "\n";
965 break;
966 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000967 case Decl::OriginalParmVar: {
968 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
969 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
970 break;
971 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000972 case Decl::ObjCProperty: {
973 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
974 Out << "<objc property> " << OPD->getNameAsString() << "\n";
975 break;
976 }
977 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000978 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000979 assert(0 && "decl unhandled");
980 }
981 }
982}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000983ASTConsumer *clang::CreateDeclContextPrinter() {
984 return new DeclContextPrinter();
985}
986
987//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000988/// InheritanceViewer - C++ Inheritance Visualization
989
990namespace {
991class InheritanceViewer : public ASTConsumer {
992 const std::string clsname;
993public:
994 InheritanceViewer(const std::string& cname) : clsname(cname) {}
995
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000996 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000997 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000998 if (RecordType *T = dyn_cast<RecordType>(*I)) {
999 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
1000 // FIXME: This lookup needs to be generalized to handle namespaces and
1001 // (when we support them) templates.
1002 if (D->getNameAsString() == clsname) {
1003 D->viewInheritance(C);
1004 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +00001005 }
1006 }
1007 }
1008};
1009}
1010
1011ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
1012 return new InheritanceViewer(clsname);
1013}