blob: aabbf2ab4a367ffd50096a401c27f97192e4e4bb [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 ";
Chris Lattner07fa7742009-02-20 18:10:37 +0000101 for (ObjCForwardProtocolDecl::iterator I = OFPD->begin(), E = OFPD->end();
102 I != E; ++I) {
103 if (I != OFPD->begin()) Out << ", ";
104 Out << (*I)->getNameAsString();
Chris Lattneref5a85d2008-01-02 21:04:16 +0000105 }
106 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000107 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000108 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000109 PrintObjCImplementationDecl(OID);
110 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000111 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000112 PrintObjCCategoryImplDecl(OID);
113 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000114 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000115 PrintObjCCategoryDecl(OID);
116 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000117 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000118 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +0000119 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
120 Out << "@class ";
Chris Lattner67956052009-02-20 18:04:31 +0000121 for (ObjCClassDecl::iterator I = OFCD->begin(), E = OFCD->end();
122 I != E; ++I) {
123 if (I != OFCD->begin()) Out << ", ";
124 Out << (*I)->getNameAsString();
Chris Lattner23a0e452008-06-21 21:40:20 +0000125 }
126 Out << ";\n";
Douglas Gregor45579f52008-12-17 02:04:30 +0000127 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
128 Out << "enum " << ED->getNameAsString() << " {\n";
Douglas Gregor6ab35242009-04-09 21:40:53 +0000129 // FIXME: Shouldn't pass a NULL context
130 ASTContext *Context = 0;
131 for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(*Context),
132 EEnd = ED->enumerator_end(*Context);
Douglas Gregor45579f52008-12-17 02:04:30 +0000133 E != EEnd; ++E)
134 Out << " " << (*E)->getNameAsString() << ",\n";
135 Out << "};\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000136 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000137 // print a free standing tag decl (e.g. "struct x;").
138 Out << TD->getKindName();
139 Out << " ";
140 if (const IdentifierInfo *II = TD->getIdentifier())
141 Out << II->getName();
142
143 Out << " {\n";
144 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000145 // FIXME: Shouldn't pass a NULL context
146 ASTContext *Context = 0;
147 for (DeclContext::decl_iterator i = TD->decls_begin(*Context);
148 i != TD->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000149 ++i)
150 PrintDecl(*i);
151 ChangeIndent(-1);
152 Indent();
153 Out << "}";
154
155 Out << "\n";
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000156 } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) {
157 PrintTemplateDecl(TempD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000158 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
159 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000160 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
161 Out << "asm(";
162 AD->getAsmString()->printPretty(Out);
163 Out << ")\n";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000164 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000165 Print(ND);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000166 } else {
167 assert(0 && "Unknown decl type!");
168 }
169}
170
Mike Stump071e4da2009-02-10 20:16:46 +0000171void DeclPrinter::Print(NamedDecl *ND) {
172 switch (ND->getKind()) {
173 default:
174 // FIXME: Handle the rest of the NamedDecls.
175 Out << "### NamedDecl " << ND->getNameAsString() << "\n";
176 break;
177 case Decl::Field:
178 case Decl::Var: {
179 // Emit storage class for vardecls.
180 if (VarDecl *V = dyn_cast<VarDecl>(ND)) {
181 switch (V->getStorageClass()) {
182 default: assert(0 && "Unknown storage class!");
Mike Stumpc5840c02009-02-10 23:49:50 +0000183 case VarDecl::None: break;
184 case VarDecl::Auto: Out << "auto "; break;
185 case VarDecl::Register: Out << "register "; break;
186 case VarDecl::Extern: Out << "extern "; break;
187 case VarDecl::Static: Out << "static "; break;
Daniel Dunbar7ab41f72009-02-13 22:49:34 +0000188 case VarDecl::PrivateExtern: Out << "__private_extern__ "; break;
Mike Stump071e4da2009-02-10 20:16:46 +0000189 }
190 }
191 std::string Name = ND->getNameAsString();
192 // This forms: "int a".
193 dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name);
Douglas Gregor087fd532009-04-14 23:32:43 +0000194 Out << Name;
195 if (VarDecl *Var = dyn_cast<VarDecl>(ND)) {
196 if (Var->getInit()) {
197 Out << " = ";
198 Var->getInit()->printPretty(Out);
199 }
200 }
201 Out << ";\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000202 break;
203 }
204 case Decl::Namespace:
205 Print(dyn_cast<NamespaceDecl>(ND));
206 break;
207 }
208}
209
210void DeclPrinter::Print(NamespaceDecl *NS) {
211 Out << "namespace " << NS->getNameAsString() << " {\n";
212 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000213 // FIXME: Shouldn't pass a NULL context
214 ASTContext *Context = 0;
215 for (DeclContext::decl_iterator i = NS->decls_begin(*Context);
216 i != NS->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000217 ++i)
218 PrintDecl(*i);
219 ChangeIndent(-1);
220 Indent();
221 Out << "}\n";
222}
223
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000224void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Douglas Gregor72971342009-04-18 00:02:19 +0000225 // FIXME: pass a context so that we can use getBody.
226 bool HasBody = FD->getBodyIfAvailable();
Reid Spencer5f016e22007-07-11 17:01:13 +0000227
Ted Kremenekea75c552007-11-28 21:32:21 +0000228 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000229
Mike Stump071e4da2009-02-10 20:16:46 +0000230 Indent();
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000231 switch (FD->getStorageClass()) {
232 default: assert(0 && "Unknown storage class");
233 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000234 case FunctionDecl::Extern: Out << "extern "; break;
235 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000236 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000237 }
238
239 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000240 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000241
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000242 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000243 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000244
Douglas Gregor72564e72009-02-26 23:50:07 +0000245 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 Proto += "(";
247 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
248 if (i) Proto += ", ";
249 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000250 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000251
252 FT->getArgType(i).getAsStringInternal(ParamStr);
253 Proto += ParamStr;
254 }
255
256 if (FT->isVariadic()) {
257 if (FD->getNumParams()) Proto += ", ";
258 Proto += "...";
259 }
260 Proto += ")";
261 } else {
Douglas Gregor72564e72009-02-26 23:50:07 +0000262 assert(isa<FunctionNoProtoType>(AFT));
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 Proto += "()";
264 }
265
266 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000267 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000268
Douglas Gregor72971342009-04-18 00:02:19 +0000269 if (!FD->getBodyIfAvailable())
Ted Kremenekea75c552007-11-28 21:32:21 +0000270 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000271 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000272}
273
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000274void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000275 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000277 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000278}
279
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000280void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
281 const char *l;
282 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
283 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000284 else {
285 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
286 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000287 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000288 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000289
290 Out << "extern \"" << l << "\" ";
Mike Stump071e4da2009-02-10 20:16:46 +0000291 if (LS->hasBraces()) {
Douglas Gregorf44515a2008-12-16 22:23:02 +0000292 Out << "{\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000293 ChangeIndent(1);
294 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000295
Douglas Gregor6ab35242009-04-09 21:40:53 +0000296 // FIXME: Should not use a NULL DeclContext!
297 ASTContext *Context = 0;
298 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context),
299 DEnd = LS->decls_end(*Context);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000300 D != DEnd; ++D)
301 PrintDecl(*D);
302
Mike Stump071e4da2009-02-10 20:16:46 +0000303 if (LS->hasBraces()) {
304 ChangeIndent(-1);
305 Indent() << "}";
306 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000307 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000308}
309
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000310void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000311 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000312 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000313 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000314 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000315 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000316 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000317
Chris Lattner077bf5e2008-11-24 03:33:13 +0000318 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000319 std::string::size_type pos, lastPos = 0;
Chris Lattner89951a82009-02-20 18:43:26 +0000320 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
321 E = OMD->param_end(); PI != E; ++PI) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000322 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000323 pos = name.find_first_of(":", lastPos);
324 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattner89951a82009-02-20 18:43:26 +0000325 Out << ":(" << (*PI)->getType().getAsString() << ")"
326 << (*PI)->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000327 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000328 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000329
Chris Lattner89951a82009-02-20 18:43:26 +0000330 if (OMD->param_begin() == OMD->param_end())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000331 Out << " " << name;
332
333 if (OMD->isVariadic())
334 Out << ", ...";
335
336 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000337}
338
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000339void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000340 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000341 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000342
343 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000344 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000345 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000346 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000347
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000348 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000349 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000350 ObjCMethodDecl *OMD = *I;
351 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000352 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000353 Out << ' ';
354 OMD->getBody()->printPretty(Out);
355 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000356 }
357 }
358
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000359 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000360 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000361 ObjCMethodDecl *OMD = *I;
362 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000363 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000364 Out << ' ';
365 OMD->getBody()->printPretty(Out);
366 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000367 }
368 }
369
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000370 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
371 E = OID->propimpl_end(); I != E; ++I)
372 PrintObjCPropertyImplDecl(*I);
373
Ted Kremenekea75c552007-11-28 21:32:21 +0000374 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000375}
376
377
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000378void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000379 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000380 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000381
382 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000383 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000384 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000385 Out << "@interface " << I;
386
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000387 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000388 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
389 if (!Protocols.empty()) {
390 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
391 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000392 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000393 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000394
Chris Lattner3db6cae2008-07-21 18:19:38 +0000395 if (!Protocols.empty())
396 Out << ">";
397 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000398
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000399 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000400 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000401 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000402 E = OID->ivar_end(); I != E; ++I) {
403 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000404 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000405 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000406 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000407 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000408
Douglas Gregor6ab35242009-04-09 21:40:53 +0000409 // FIXME: Should not use a NULL DeclContext!
410 ASTContext *Context = 0;
411 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context),
412 E = OID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000413 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000414 bool eol_needed = false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000415 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context),
416 E = OID->classmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000417 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000418
Douglas Gregor6ab35242009-04-09 21:40:53 +0000419 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context),
420 E = OID->instmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000421 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000422
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000423 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000424 // FIXME: implement the rest...
425}
426
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000427void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000428 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000429
Douglas Gregor6ab35242009-04-09 21:40:53 +0000430 // FIXME: Should not use a NULL DeclContext!
431 ASTContext *Context = 0;
432 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context),
433 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000434 PrintObjCPropertyDecl(*I);
435 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000436 // FIXME: implement the rest...
437}
438
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000439void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000440 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000441 << PID->getClassInterface()->getNameAsString()
442 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000443 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
444 E = PID->propimpl_end(); I != E; ++I)
445 PrintObjCPropertyImplDecl(*I);
446 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000447 // FIXME: implement the rest...
448}
449
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000450void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000451 // FIXME: Should not use a NULL DeclContext!
452 ASTContext *Context = 0;
Ted Kremenekea75c552007-11-28 21:32:21 +0000453 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000454 << PID->getClassInterface()->getNameAsString()
455 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000456 // Output property declarations.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000457 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context),
458 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000459 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000460 Out << "@end\n";
461
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000462 // FIXME: implement the rest...
463}
464
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000465void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000466 Out << "@compatibility_alias " << AID->getNameAsString()
467 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000468}
469
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000470/// PrintObjCPropertyDecl - print a property declaration.
471///
472void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000473 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
474 Out << "@required\n";
475 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
476 Out << "@optional\n";
477
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000478 Out << "@property";
479 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
480 bool first = true;
481 Out << " (";
482 if (PDecl->getPropertyAttributes() &
483 ObjCPropertyDecl::OBJC_PR_readonly) {
484 Out << (first ? ' ' : ',') << "readonly";
485 first = false;
486 }
487
488 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
489 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000490 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000491 first = false;
492 }
493 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
494 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000495 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000496 first = false;
497 }
498
499 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
500 Out << (first ? ' ' : ',') << "assign";
501 first = false;
502 }
503
504 if (PDecl->getPropertyAttributes() &
505 ObjCPropertyDecl::OBJC_PR_readwrite) {
506 Out << (first ? ' ' : ',') << "readwrite";
507 first = false;
508 }
509
510 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
511 Out << (first ? ' ' : ',') << "retain";
512 first = false;
513 }
514
515 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
516 Out << (first ? ' ' : ',') << "copy";
517 first = false;
518 }
519
520 if (PDecl->getPropertyAttributes() &
521 ObjCPropertyDecl::OBJC_PR_nonatomic) {
522 Out << (first ? ' ' : ',') << "nonatomic";
523 first = false;
524 }
525 Out << " )";
526 }
527 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000528 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000529
530 Out << ";\n";
531}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000532
533/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
534/// declaration syntax.
535///
536void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000537 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000538 Out << "\n@synthesize ";
539 else
540 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000541 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000542 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000543 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000544 Out << ";\n";
545}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000546
547/// PrintTemplateParams - Print a template parameter list and recursively print
548/// it's underlying top-level definition.
549void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
550 // TODO: Write template parameters.
551 Out << "template <...> ";
552 PrintDecl(TD->getTemplatedDecl());
553}
554
555
556
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000557//===----------------------------------------------------------------------===//
558/// ASTPrinter - Pretty-printer of ASTs
559
Chris Lattner3d4997d2007-09-15 23:02:28 +0000560namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000561 class ASTPrinter : public ASTConsumer, public DeclPrinter {
562 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000563 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000564
Chris Lattner682bf922009-03-29 16:50:03 +0000565 virtual void HandleTopLevelDecl(DeclGroupRef D) {
566 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
567 PrintDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000569 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000570} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +0000571
Ted Kremeneka95d3752008-09-13 05:16:45 +0000572ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000573 return new ASTPrinter(out);
574}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000575
576//===----------------------------------------------------------------------===//
577/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000578
579namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000580 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000581 SourceManager *SM;
582 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000583 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000584
Ted Kremenek95041a22007-12-19 22:51:13 +0000585 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000586 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000587 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000588
Chris Lattner682bf922009-03-29 16:50:03 +0000589 virtual void HandleTopLevelDecl(DeclGroupRef D) {
590 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
591 HandleTopLevelSingleDecl(*I);
592 }
593 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattner3d4997d2007-09-15 23:02:28 +0000594 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000595} // end anonymous namespace
596
Chris Lattner682bf922009-03-29 16:50:03 +0000597void ASTDumper::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000598 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
599 PrintFunctionDeclStart(FD);
600
Douglas Gregor72971342009-04-18 00:02:19 +0000601 if (FD->getBodyIfAvailable()) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000602 Out << '\n';
603 // FIXME: convert dumper to use std::ostream?
Douglas Gregor72971342009-04-18 00:02:19 +0000604 FD->getBodyIfAvailable()->dumpAll(*SM);
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000605 Out << '\n';
606 }
607 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
608 PrintTypeDefDecl(TD);
609 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
610 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
611 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
612 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
613 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
614 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
615 } else if (isa<ObjCForwardProtocolDecl>(D)) {
616 Out << "Read objc fwd protocol decl\n";
617 } else if (isa<ObjCClassDecl>(D)) {
618 Out << "Read objc fwd class decl\n";
619 } else if (isa<FileScopeAsmDecl>(D)) {
620 Out << "Read file scope asm decl\n";
621 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
622 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
623 << "'\n";
624 if (MD->getBody()) {
625 // FIXME: convert dumper to use std::ostream?
626 MD->getBody()->dumpAll(*SM);
627 Out << '\n';
628 }
629 } else if (isa<ObjCImplementationDecl>(D)) {
630 Out << "Read objc implementation decl\n";
631 } else if (isa<ObjCCategoryImplDecl>(D)) {
632 Out << "Read objc category implementation decl\n";
633 } else if (isa<LinkageSpecDecl>(D)) {
634 Out << "Read linkage spec decl\n";
635 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
636 Out << "Read top-level variable decl: '" << ND->getNameAsString()
637 << "'\n";
638 } else {
639 assert(0 && "Unknown decl type!");
640 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000641}
642
Chris Lattner3d4997d2007-09-15 23:02:28 +0000643ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
644
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000645//===----------------------------------------------------------------------===//
646/// ASTViewer - AST Visualization
647
Ted Kremenek80de08f2007-09-19 21:29:43 +0000648namespace {
649 class ASTViewer : public ASTConsumer {
650 SourceManager *SM;
651 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000652 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000653 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000654 }
Chris Lattner682bf922009-03-29 16:50:03 +0000655
656 virtual void HandleTopLevelDecl(DeclGroupRef D) {
657 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
658 HandleTopLevelSingleDecl(*I);
659 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000660
Chris Lattner682bf922009-03-29 16:50:03 +0000661 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000662 };
663}
664
Chris Lattner682bf922009-03-29 16:50:03 +0000665void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000666 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
667 DeclPrinter().PrintFunctionDeclStart(FD);
668
Douglas Gregor72971342009-04-18 00:02:19 +0000669 if (FD->getBodyIfAvailable()) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000670 llvm::cerr << '\n';
Douglas Gregor72971342009-04-18 00:02:19 +0000671 FD->getBodyIfAvailable()->viewAST();
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000672 llvm::cerr << '\n';
673 }
674 return;
675 }
676
677 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
678 DeclPrinter().PrintObjCMethodDecl(MD);
679
680 if (MD->getBody()) {
681 llvm::cerr << '\n';
682 MD->getBody()->viewAST();
683 llvm::cerr << '\n';
684 }
685 }
686}
687
688
Ted Kremenek80de08f2007-09-19 21:29:43 +0000689ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
690
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000691//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000692/// DeclContextPrinter - Decl and DeclContext Visualization
693
694namespace {
695
696class DeclContextPrinter : public ASTConsumer {
697 llvm::raw_ostream& Out;
698public:
699 DeclContextPrinter() : Out(llvm::errs()) {}
700
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000701 void HandleTranslationUnit(ASTContext &C) {
702 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000703 }
704
705 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
706};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000707} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000708
709void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
710 unsigned Indentation) {
711 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000712 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000713 case Decl::TranslationUnit:
714 Out << "[translation unit] " << DC;
715 break;
716 case Decl::Namespace: {
717 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000718 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000719 Out << ND->getNameAsString();
720 break;
721 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000722 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000723 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000724 if (ED->isDefinition())
725 Out << "[enum] ";
726 else
727 Out << "<enum> ";
728 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000729 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000730 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000731 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000732 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000733 if (RD->isDefinition())
734 Out << "[struct] ";
735 else
736 Out << "<struct> ";
737 Out << RD->getNameAsString();
738 break;
739 }
740 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000741 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000742 if (RD->isDefinition())
743 Out << "[class] ";
744 else
745 Out << "<class> ";
746 Out << RD->getNameAsString() << " " << DC;
747 break;
748 }
749 case Decl::ObjCMethod:
750 Out << "[objc method]";
751 break;
752 case Decl::ObjCInterface:
753 Out << "[objc interface]";
754 break;
755 case Decl::ObjCCategory:
756 Out << "[objc category]";
757 break;
758 case Decl::ObjCProtocol:
759 Out << "[objc protocol]";
760 break;
761 case Decl::ObjCImplementation:
762 Out << "[objc implementation]";
763 break;
764 case Decl::ObjCCategoryImpl:
765 Out << "[objc categoryimpl]";
766 break;
767 case Decl::LinkageSpec:
768 Out << "[linkage spec]";
769 break;
770 case Decl::Block:
771 Out << "[block]";
772 break;
773 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000774 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000775 if (FD->isThisDeclarationADefinition())
776 Out << "[function] ";
777 else
778 Out << "<function> ";
779 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000780 // Print the parameters.
781 Out << "(";
782 bool PrintComma = false;
783 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
784 E = FD->param_end(); I != E; ++I) {
785 if (PrintComma)
786 Out << ", ";
787 else
788 PrintComma = true;
789 Out << (*I)->getNameAsString();
790 }
791 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000792 break;
793 }
794 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000795 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000796 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000797 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000798 else if (D->isImplicit())
799 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000800 else
801 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000802 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000803 // Print the parameters.
804 Out << "(";
805 bool PrintComma = false;
806 for (FunctionDecl::param_const_iterator I = D->param_begin(),
807 E = D->param_end(); I != E; ++I) {
808 if (PrintComma)
809 Out << ", ";
810 else
811 PrintComma = true;
812 Out << (*I)->getNameAsString();
813 }
814 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000815
816 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000817 const DeclContext* SemaDC = D->getDeclContext();
818 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000819 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000820 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000821
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000822 break;
823 }
824 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000825 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000826 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000827 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000828 else if (D->isImplicit())
829 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000830 else
831 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000832 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000833 // Print the parameters.
834 Out << "(";
835 bool PrintComma = false;
836 for (FunctionDecl::param_const_iterator I = D->param_begin(),
837 E = D->param_end(); I != E; ++I) {
838 if (PrintComma)
839 Out << ", ";
840 else
841 PrintComma = true;
842 Out << (*I)->getNameAsString();
843 }
844 Out << ")";
845
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000846 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000847 const DeclContext* SemaDC = D->getDeclContext();
848 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000849 if (SemaDC != LexicalDC)
850 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000851 break;
852 }
853 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000854 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000855 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000856 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000857 else if (D->isImplicit())
858 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000859 else
860 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000861 Out << D->getNameAsString();
862 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000863 const DeclContext* SemaDC = D->getDeclContext();
864 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000865 if (SemaDC != LexicalDC)
866 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000867 break;
868 }
869 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000870 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000871 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000872 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000873 else if (D->isImplicit())
874 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000875 else
876 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000877 Out << D->getNameAsString();
878 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000879 const DeclContext* SemaDC = D->getDeclContext();
880 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000881 if (SemaDC != LexicalDC)
882 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000883 break;
884 }
885
886 default:
887 assert(0 && "a decl that inherits DeclContext isn't handled");
888 }
889
890 Out << "\n";
891
892 // Print decls in the DeclContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000893 // FIXME: Should not use a NULL DeclContext!
894 ASTContext *Context = 0;
895 for (DeclContext::decl_iterator I = DC->decls_begin(*Context),
896 E = DC->decls_end(*Context);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000897 I != E; ++I) {
898 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000899 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000900
901 Decl::Kind DK = I->getKind();
902 switch (DK) {
903 case Decl::Namespace:
904 case Decl::Enum:
905 case Decl::Record:
906 case Decl::CXXRecord:
907 case Decl::ObjCMethod:
908 case Decl::ObjCInterface:
909 case Decl::ObjCCategory:
910 case Decl::ObjCProtocol:
911 case Decl::ObjCImplementation:
912 case Decl::ObjCCategoryImpl:
913 case Decl::LinkageSpec:
914 case Decl::Block:
915 case Decl::Function:
916 case Decl::CXXMethod:
917 case Decl::CXXConstructor:
918 case Decl::CXXDestructor:
919 case Decl::CXXConversion:
920 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000921 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000922 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000923 break;
924 }
925 case Decl::Field: {
926 FieldDecl* FD = cast<FieldDecl>(*I);
927 Out << "<field> " << FD->getNameAsString() << "\n";
928 break;
929 }
930 case Decl::Typedef: {
931 TypedefDecl* TD = cast<TypedefDecl>(*I);
932 Out << "<typedef> " << TD->getNameAsString() << "\n";
933 break;
934 }
935 case Decl::EnumConstant: {
936 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
937 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
938 break;
939 }
940 case Decl::Var: {
941 VarDecl* VD = cast<VarDecl>(*I);
942 Out << "<var> " << VD->getNameAsString() << "\n";
943 break;
944 }
945 case Decl::ImplicitParam: {
946 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
947 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
948 break;
949 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000950 case Decl::ParmVar: {
951 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
952 Out << "<parameter> " << PVD->getNameAsString() << "\n";
953 break;
954 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000955 case Decl::OriginalParmVar: {
956 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
957 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
958 break;
959 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000960 case Decl::ObjCProperty: {
961 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
962 Out << "<objc property> " << OPD->getNameAsString() << "\n";
963 break;
964 }
965 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000966 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000967 assert(0 && "decl unhandled");
968 }
969 }
970}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000971ASTConsumer *clang::CreateDeclContextPrinter() {
972 return new DeclContextPrinter();
973}
974
975//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000976/// InheritanceViewer - C++ Inheritance Visualization
977
978namespace {
979class InheritanceViewer : public ASTConsumer {
980 const std::string clsname;
981public:
982 InheritanceViewer(const std::string& cname) : clsname(cname) {}
983
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000984 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000985 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000986 if (RecordType *T = dyn_cast<RecordType>(*I)) {
987 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
988 // FIXME: This lookup needs to be generalized to handle namespaces and
989 // (when we support them) templates.
990 if (D->getNameAsString() == clsname) {
991 D->viewInheritance(C);
992 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000993 }
994 }
995 }
996};
997}
998
999ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
1000 return new InheritanceViewer(clsname);
1001}
1002
1003//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001004// AST Serializer
1005
1006namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +00001007
1008class ASTSerializer : public ASTConsumer {
1009protected:
Nico Weberdae86962008-08-09 18:32:11 +00001010 Diagnostic& Diags;
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001011
Ted Kremenekf06c9282007-12-19 23:49:37 +00001012public:
Nico Weberbe1eb5c2008-08-10 19:20:05 +00001013 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +00001014};
Eli Friedmand8a65c12008-06-09 20:02:51 +00001015
Ted Kremenekf06c9282007-12-19 23:49:37 +00001016class SingleFileSerializer : public ASTSerializer {
1017 const llvm::sys::Path FName;
1018public:
Nico Weberdae86962008-08-09 18:32:11 +00001019 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
1020 : ASTSerializer(diags), FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +00001021
Chris Lattnerdacbc5d2009-03-28 04:11:33 +00001022 virtual void HandleTranslationUnit(ASTContext &Ctx) {
Nico Weberdae86962008-08-09 18:32:11 +00001023 if (Diags.hasErrorOccurred())
1024 return;
Chris Lattner80a03332009-03-28 03:53:02 +00001025
1026 // Reserve 256K for bitstream buffer.
1027 std::vector<unsigned char> Buffer;
1028 Buffer.reserve(256*1024);
1029
Chris Lattner557c5b12009-03-28 04:27:18 +00001030 Ctx.EmitASTBitcodeBuffer(Buffer);
Chris Lattner80a03332009-03-28 03:53:02 +00001031
1032 // Write the bits to disk.
1033 if (FILE* fp = fopen(FName.c_str(),"wb")) {
1034 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
1035 fclose(fp);
1036 }
Ted Kremenekf06c9282007-12-19 23:49:37 +00001037 }
1038};
1039
1040class BuildSerializer : public ASTSerializer {
1041 llvm::sys::Path EmitDir;
1042public:
Nico Weberdae86962008-08-09 18:32:11 +00001043 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
1044 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +00001045
Chris Lattnerb23ff6b2009-03-28 05:44:17 +00001046 virtual void HandleTranslationUnit(ASTContext &Ctx);
Ted Kremenekf06c9282007-12-19 23:49:37 +00001047};
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001048} // end anonymous namespace
1049
1050
Chris Lattnerb23ff6b2009-03-28 05:44:17 +00001051void BuildSerializer::HandleTranslationUnit(ASTContext &Ctx) {
1052 if (Diags.hasErrorOccurred())
1053 return;
1054
1055 SourceManager& SourceMgr = Ctx.getSourceManager();
1056 FileID ID = SourceMgr.getMainFileID();
1057 assert(!ID.isInvalid() && "MainFileID not set!");
1058 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
1059 assert(FE && "No FileEntry for main file.");
1060
1061 // FIXME: This is not portable to Windows.
1062 // FIXME: This logic should probably be moved elsewhere later.
1063
1064 llvm::sys::Path FName(EmitDir);
1065
1066 std::vector<char> buf;
1067 buf.reserve(strlen(FE->getName())+100);
1068
1069 sprintf(&buf[0], "dev_%llx", (unsigned long long) FE->getDevice());
1070 FName.appendComponent(&buf[0]);
1071 FName.createDirectoryOnDisk(true);
1072 if (!FName.canWrite() || !FName.isDirectory()) {
1073 assert (false && "Could not create 'device' serialization directory.");
1074 return;
1075 }
1076
1077 sprintf(&buf[0], "%s-%llX.ast", FE->getName(),
1078 (unsigned long long) FE->getInode());
1079 FName.appendComponent(&buf[0]);
1080
1081
1082 // Reserve 256K for bitstream buffer.
1083 std::vector<unsigned char> Buffer;
1084 Buffer.reserve(256*1024);
1085
1086 Ctx.EmitASTBitcodeBuffer(Buffer);
1087
1088 // Write the bits to disk.
1089 if (FILE* fp = fopen(FName.c_str(),"wb")) {
1090 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
1091 fclose(fp);
1092 }
1093
1094 // Now emit the sources.
1095
1096}
1097
1098
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001099ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +00001100 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001101 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +00001102
Ted Kremenekf06c9282007-12-19 23:49:37 +00001103 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +00001104 if (InFile == "-") {
1105 llvm::cerr <<
1106 "error: Cannot use --serialize with -o for source read from STDIN.\n";
1107 return NULL;
1108 }
1109
Ted Kremenekf06c9282007-12-19 23:49:37 +00001110 // The user specified an AST-emission directory. Determine if the path
1111 // is absolute.
1112 llvm::sys::Path EmitDir(OutputFile);
1113
1114 if (!EmitDir.isAbsolute()) {
1115 llvm::cerr <<
1116 "error: Output directory for --serialize must be an absolute path.\n";
1117
1118 return NULL;
1119 }
1120
1121 // Create the directory if it does not exist.
1122 EmitDir.createDirectoryOnDisk(true);
1123 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
1124 llvm::cerr <<
1125 "error: Could not create output directory for --serialize.\n";
1126
1127 return NULL;
1128 }
1129
Ted Kremenek54117722007-12-20 00:34:58 +00001130 // FIXME: We should probably only allow using BuildSerializer when
1131 // the ASTs come from parsed source files, and not from .ast files.
Nico Weberdae86962008-08-09 18:32:11 +00001132 return new BuildSerializer(EmitDir, Diags);
Ted Kremenekf06c9282007-12-19 23:49:37 +00001133 }
1134
1135 // The user did not specify an output directory for serialized ASTs.
1136 // Serialize the translation to a single file whose name is the same
1137 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +00001138
Ted Kremenekf06c9282007-12-19 23:49:37 +00001139 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +00001140 FName.appendSuffix("ast");
Nico Weberdae86962008-08-09 18:32:11 +00001141 return new SingleFileSerializer(FName, Diags);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001142}