blob: 74683e3031739f5a60cc283cd4d08bd171c7985a [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
83 if (FD->getBody()) {
84 Out << ' ';
Mike Stump071e4da2009-02-10 20:16:46 +000085 FD->getBody()->printPretty(Out, 0, Indentation, true);
Chris Lattneref5a85d2008-01-02 21:04:16 +000086 Out << '\n';
87 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000088 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000089 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000090 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000091 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
92 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000093 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
94 PrintObjCInterfaceDecl(OID);
95 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
96 PrintObjCProtocolDecl(PID);
97 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000098 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000099 Out << "@protocol ";
Chris Lattner07fa7742009-02-20 18:10:37 +0000100 for (ObjCForwardProtocolDecl::iterator I = OFPD->begin(), E = OFPD->end();
101 I != E; ++I) {
102 if (I != OFPD->begin()) Out << ", ";
103 Out << (*I)->getNameAsString();
Chris Lattneref5a85d2008-01-02 21:04:16 +0000104 }
105 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000106 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000107 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000108 PrintObjCImplementationDecl(OID);
109 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000110 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000111 PrintObjCCategoryImplDecl(OID);
112 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000113 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000114 PrintObjCCategoryDecl(OID);
115 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000116 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000117 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +0000118 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
119 Out << "@class ";
Chris Lattner67956052009-02-20 18:04:31 +0000120 for (ObjCClassDecl::iterator I = OFCD->begin(), E = OFCD->end();
121 I != E; ++I) {
122 if (I != OFCD->begin()) Out << ", ";
123 Out << (*I)->getNameAsString();
Chris Lattner23a0e452008-06-21 21:40:20 +0000124 }
125 Out << ";\n";
Douglas Gregor45579f52008-12-17 02:04:30 +0000126 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
127 Out << "enum " << ED->getNameAsString() << " {\n";
Douglas Gregor6ab35242009-04-09 21:40:53 +0000128 // FIXME: Shouldn't pass a NULL context
129 ASTContext *Context = 0;
130 for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(*Context),
131 EEnd = ED->enumerator_end(*Context);
Douglas Gregor45579f52008-12-17 02:04:30 +0000132 E != EEnd; ++E)
133 Out << " " << (*E)->getNameAsString() << ",\n";
134 Out << "};\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000135 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000136 // print a free standing tag decl (e.g. "struct x;").
137 Out << TD->getKindName();
138 Out << " ";
139 if (const IdentifierInfo *II = TD->getIdentifier())
140 Out << II->getName();
141
142 Out << " {\n";
143 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000144 // FIXME: Shouldn't pass a NULL context
145 ASTContext *Context = 0;
146 for (DeclContext::decl_iterator i = TD->decls_begin(*Context);
147 i != TD->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000148 ++i)
149 PrintDecl(*i);
150 ChangeIndent(-1);
151 Indent();
152 Out << "}";
153
154 Out << "\n";
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000155 } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) {
156 PrintTemplateDecl(TempD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000157 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
158 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000159 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
160 Out << "asm(";
161 AD->getAsmString()->printPretty(Out);
162 Out << ")\n";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000163 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000164 Print(ND);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000165 } else {
166 assert(0 && "Unknown decl type!");
167 }
168}
169
Mike Stump071e4da2009-02-10 20:16:46 +0000170void DeclPrinter::Print(NamedDecl *ND) {
171 switch (ND->getKind()) {
172 default:
173 // FIXME: Handle the rest of the NamedDecls.
174 Out << "### NamedDecl " << ND->getNameAsString() << "\n";
175 break;
176 case Decl::Field:
177 case Decl::Var: {
178 // Emit storage class for vardecls.
179 if (VarDecl *V = dyn_cast<VarDecl>(ND)) {
180 switch (V->getStorageClass()) {
181 default: assert(0 && "Unknown storage class!");
Mike Stumpc5840c02009-02-10 23:49:50 +0000182 case VarDecl::None: break;
183 case VarDecl::Auto: Out << "auto "; break;
184 case VarDecl::Register: Out << "register "; break;
185 case VarDecl::Extern: Out << "extern "; break;
186 case VarDecl::Static: Out << "static "; break;
Daniel Dunbar7ab41f72009-02-13 22:49:34 +0000187 case VarDecl::PrivateExtern: Out << "__private_extern__ "; break;
Mike Stump071e4da2009-02-10 20:16:46 +0000188 }
189 }
190 std::string Name = ND->getNameAsString();
191 // This forms: "int a".
192 dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name);
193 Out << Name << ";\n";
194 break;
195 }
196 case Decl::Namespace:
197 Print(dyn_cast<NamespaceDecl>(ND));
198 break;
199 }
200}
201
202void DeclPrinter::Print(NamespaceDecl *NS) {
203 Out << "namespace " << NS->getNameAsString() << " {\n";
204 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000205 // FIXME: Shouldn't pass a NULL context
206 ASTContext *Context = 0;
207 for (DeclContext::decl_iterator i = NS->decls_begin(*Context);
208 i != NS->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000209 ++i)
210 PrintDecl(*i);
211 ChangeIndent(-1);
212 Indent();
213 Out << "}\n";
214}
215
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000216void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 bool HasBody = FD->getBody();
218
Ted Kremenekea75c552007-11-28 21:32:21 +0000219 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000220
Mike Stump071e4da2009-02-10 20:16:46 +0000221 Indent();
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000222 switch (FD->getStorageClass()) {
223 default: assert(0 && "Unknown storage class");
224 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000225 case FunctionDecl::Extern: Out << "extern "; break;
226 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000227 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000228 }
229
230 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000231 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000232
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000233 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000234 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000235
Douglas Gregor72564e72009-02-26 23:50:07 +0000236 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 Proto += "(";
238 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
239 if (i) Proto += ", ";
240 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000241 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000242
243 FT->getArgType(i).getAsStringInternal(ParamStr);
244 Proto += ParamStr;
245 }
246
247 if (FT->isVariadic()) {
248 if (FD->getNumParams()) Proto += ", ";
249 Proto += "...";
250 }
251 Proto += ")";
252 } else {
Douglas Gregor72564e72009-02-26 23:50:07 +0000253 assert(isa<FunctionNoProtoType>(AFT));
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 Proto += "()";
255 }
256
257 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000258 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000259
Chris Lattner6000dac2007-08-08 22:51:59 +0000260 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000261 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000262 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000263}
264
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000265void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000266 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000268 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000269}
270
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000271void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
272 const char *l;
273 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
274 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000275 else {
276 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
277 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000278 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000279 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000280
281 Out << "extern \"" << l << "\" ";
Mike Stump071e4da2009-02-10 20:16:46 +0000282 if (LS->hasBraces()) {
Douglas Gregorf44515a2008-12-16 22:23:02 +0000283 Out << "{\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000284 ChangeIndent(1);
285 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000286
Douglas Gregor6ab35242009-04-09 21:40:53 +0000287 // FIXME: Should not use a NULL DeclContext!
288 ASTContext *Context = 0;
289 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context),
290 DEnd = LS->decls_end(*Context);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000291 D != DEnd; ++D)
292 PrintDecl(*D);
293
Mike Stump071e4da2009-02-10 20:16:46 +0000294 if (LS->hasBraces()) {
295 ChangeIndent(-1);
296 Indent() << "}";
297 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000298 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000299}
300
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000301void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000302 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000303 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000304 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000305 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000306 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000307 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000308
Chris Lattner077bf5e2008-11-24 03:33:13 +0000309 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000310 std::string::size_type pos, lastPos = 0;
Chris Lattner89951a82009-02-20 18:43:26 +0000311 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
312 E = OMD->param_end(); PI != E; ++PI) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000313 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000314 pos = name.find_first_of(":", lastPos);
315 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattner89951a82009-02-20 18:43:26 +0000316 Out << ":(" << (*PI)->getType().getAsString() << ")"
317 << (*PI)->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000318 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000319 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000320
Chris Lattner89951a82009-02-20 18:43:26 +0000321 if (OMD->param_begin() == OMD->param_end())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000322 Out << " " << name;
323
324 if (OMD->isVariadic())
325 Out << ", ...";
326
327 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000328}
329
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000330void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000331 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000332 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000333
334 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000335 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000336 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000337 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000338
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000339 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000340 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000341 ObjCMethodDecl *OMD = *I;
342 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000343 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000344 Out << ' ';
345 OMD->getBody()->printPretty(Out);
346 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000347 }
348 }
349
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000350 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000351 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000352 ObjCMethodDecl *OMD = *I;
353 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000354 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000355 Out << ' ';
356 OMD->getBody()->printPretty(Out);
357 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000358 }
359 }
360
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000361 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
362 E = OID->propimpl_end(); I != E; ++I)
363 PrintObjCPropertyImplDecl(*I);
364
Ted Kremenekea75c552007-11-28 21:32:21 +0000365 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000366}
367
368
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000369void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000370 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000371 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000372
373 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000374 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000375 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000376 Out << "@interface " << I;
377
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000378 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000379 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
380 if (!Protocols.empty()) {
381 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
382 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000383 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000384 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000385
Chris Lattner3db6cae2008-07-21 18:19:38 +0000386 if (!Protocols.empty())
387 Out << ">";
388 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000389
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000390 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000391 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000392 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000393 E = OID->ivar_end(); I != E; ++I) {
394 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000395 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000396 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000397 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000398 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000399
Douglas Gregor6ab35242009-04-09 21:40:53 +0000400 // FIXME: Should not use a NULL DeclContext!
401 ASTContext *Context = 0;
402 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context),
403 E = OID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000404 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000405 bool eol_needed = false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000406 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context),
407 E = OID->classmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000408 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000409
Douglas Gregor6ab35242009-04-09 21:40:53 +0000410 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context),
411 E = OID->instmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000412 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000413
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000414 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000415 // FIXME: implement the rest...
416}
417
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000418void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000419 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000420
Douglas Gregor6ab35242009-04-09 21:40:53 +0000421 // FIXME: Should not use a NULL DeclContext!
422 ASTContext *Context = 0;
423 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context),
424 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000425 PrintObjCPropertyDecl(*I);
426 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000427 // FIXME: implement the rest...
428}
429
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000430void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000431 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000432 << PID->getClassInterface()->getNameAsString()
433 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000434 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
435 E = PID->propimpl_end(); I != E; ++I)
436 PrintObjCPropertyImplDecl(*I);
437 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000438 // FIXME: implement the rest...
439}
440
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000441void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000442 // FIXME: Should not use a NULL DeclContext!
443 ASTContext *Context = 0;
Ted Kremenekea75c552007-11-28 21:32:21 +0000444 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000445 << PID->getClassInterface()->getNameAsString()
446 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000447 // Output property declarations.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000448 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context),
449 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000450 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000451 Out << "@end\n";
452
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000453 // FIXME: implement the rest...
454}
455
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000456void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000457 Out << "@compatibility_alias " << AID->getNameAsString()
458 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000459}
460
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000461/// PrintObjCPropertyDecl - print a property declaration.
462///
463void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000464 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
465 Out << "@required\n";
466 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
467 Out << "@optional\n";
468
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000469 Out << "@property";
470 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
471 bool first = true;
472 Out << " (";
473 if (PDecl->getPropertyAttributes() &
474 ObjCPropertyDecl::OBJC_PR_readonly) {
475 Out << (first ? ' ' : ',') << "readonly";
476 first = false;
477 }
478
479 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
480 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000481 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000482 first = false;
483 }
484 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
485 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000486 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000487 first = false;
488 }
489
490 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
491 Out << (first ? ' ' : ',') << "assign";
492 first = false;
493 }
494
495 if (PDecl->getPropertyAttributes() &
496 ObjCPropertyDecl::OBJC_PR_readwrite) {
497 Out << (first ? ' ' : ',') << "readwrite";
498 first = false;
499 }
500
501 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
502 Out << (first ? ' ' : ',') << "retain";
503 first = false;
504 }
505
506 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
507 Out << (first ? ' ' : ',') << "copy";
508 first = false;
509 }
510
511 if (PDecl->getPropertyAttributes() &
512 ObjCPropertyDecl::OBJC_PR_nonatomic) {
513 Out << (first ? ' ' : ',') << "nonatomic";
514 first = false;
515 }
516 Out << " )";
517 }
518 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000519 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000520
521 Out << ";\n";
522}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000523
524/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
525/// declaration syntax.
526///
527void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000528 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000529 Out << "\n@synthesize ";
530 else
531 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000532 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000533 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000534 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000535 Out << ";\n";
536}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000537
538/// PrintTemplateParams - Print a template parameter list and recursively print
539/// it's underlying top-level definition.
540void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
541 // TODO: Write template parameters.
542 Out << "template <...> ";
543 PrintDecl(TD->getTemplatedDecl());
544}
545
546
547
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000548//===----------------------------------------------------------------------===//
549/// ASTPrinter - Pretty-printer of ASTs
550
Chris Lattner3d4997d2007-09-15 23:02:28 +0000551namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000552 class ASTPrinter : public ASTConsumer, public DeclPrinter {
553 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000554 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000555
Chris Lattner682bf922009-03-29 16:50:03 +0000556 virtual void HandleTopLevelDecl(DeclGroupRef D) {
557 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
558 PrintDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000560 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000561} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +0000562
Ted Kremeneka95d3752008-09-13 05:16:45 +0000563ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000564 return new ASTPrinter(out);
565}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000566
567//===----------------------------------------------------------------------===//
568/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000569
570namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000571 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000572 SourceManager *SM;
573 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000574 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000575
Ted Kremenek95041a22007-12-19 22:51:13 +0000576 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000577 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000578 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000579
Chris Lattner682bf922009-03-29 16:50:03 +0000580 virtual void HandleTopLevelDecl(DeclGroupRef D) {
581 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
582 HandleTopLevelSingleDecl(*I);
583 }
584 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattner3d4997d2007-09-15 23:02:28 +0000585 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000586} // end anonymous namespace
587
Chris Lattner682bf922009-03-29 16:50:03 +0000588void ASTDumper::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000589 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
590 PrintFunctionDeclStart(FD);
591
592 if (FD->getBody()) {
593 Out << '\n';
594 // FIXME: convert dumper to use std::ostream?
595 FD->getBody()->dumpAll(*SM);
596 Out << '\n';
597 }
598 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
599 PrintTypeDefDecl(TD);
600 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
601 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
602 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
603 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
604 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
605 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
606 } else if (isa<ObjCForwardProtocolDecl>(D)) {
607 Out << "Read objc fwd protocol decl\n";
608 } else if (isa<ObjCClassDecl>(D)) {
609 Out << "Read objc fwd class decl\n";
610 } else if (isa<FileScopeAsmDecl>(D)) {
611 Out << "Read file scope asm decl\n";
612 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
613 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
614 << "'\n";
615 if (MD->getBody()) {
616 // FIXME: convert dumper to use std::ostream?
617 MD->getBody()->dumpAll(*SM);
618 Out << '\n';
619 }
620 } else if (isa<ObjCImplementationDecl>(D)) {
621 Out << "Read objc implementation decl\n";
622 } else if (isa<ObjCCategoryImplDecl>(D)) {
623 Out << "Read objc category implementation decl\n";
624 } else if (isa<LinkageSpecDecl>(D)) {
625 Out << "Read linkage spec decl\n";
626 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
627 Out << "Read top-level variable decl: '" << ND->getNameAsString()
628 << "'\n";
629 } else {
630 assert(0 && "Unknown decl type!");
631 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000632}
633
Chris Lattner3d4997d2007-09-15 23:02:28 +0000634ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
635
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000636//===----------------------------------------------------------------------===//
637/// ASTViewer - AST Visualization
638
Ted Kremenek80de08f2007-09-19 21:29:43 +0000639namespace {
640 class ASTViewer : public ASTConsumer {
641 SourceManager *SM;
642 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000643 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000644 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000645 }
Chris Lattner682bf922009-03-29 16:50:03 +0000646
647 virtual void HandleTopLevelDecl(DeclGroupRef D) {
648 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
649 HandleTopLevelSingleDecl(*I);
650 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000651
Chris Lattner682bf922009-03-29 16:50:03 +0000652 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000653 };
654}
655
Chris Lattner682bf922009-03-29 16:50:03 +0000656void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000657 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
658 DeclPrinter().PrintFunctionDeclStart(FD);
659
660 if (FD->getBody()) {
661 llvm::cerr << '\n';
662 FD->getBody()->viewAST();
663 llvm::cerr << '\n';
664 }
665 return;
666 }
667
668 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
669 DeclPrinter().PrintObjCMethodDecl(MD);
670
671 if (MD->getBody()) {
672 llvm::cerr << '\n';
673 MD->getBody()->viewAST();
674 llvm::cerr << '\n';
675 }
676 }
677}
678
679
Ted Kremenek80de08f2007-09-19 21:29:43 +0000680ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
681
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000682//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000683/// DeclContextPrinter - Decl and DeclContext Visualization
684
685namespace {
686
687class DeclContextPrinter : public ASTConsumer {
688 llvm::raw_ostream& Out;
689public:
690 DeclContextPrinter() : Out(llvm::errs()) {}
691
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000692 void HandleTranslationUnit(ASTContext &C) {
693 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000694 }
695
696 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
697};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000698} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000699
700void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
701 unsigned Indentation) {
702 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000703 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000704 case Decl::TranslationUnit:
705 Out << "[translation unit] " << DC;
706 break;
707 case Decl::Namespace: {
708 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000709 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000710 Out << ND->getNameAsString();
711 break;
712 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000713 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000714 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000715 if (ED->isDefinition())
716 Out << "[enum] ";
717 else
718 Out << "<enum> ";
719 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000720 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000721 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000722 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000723 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000724 if (RD->isDefinition())
725 Out << "[struct] ";
726 else
727 Out << "<struct> ";
728 Out << RD->getNameAsString();
729 break;
730 }
731 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000732 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000733 if (RD->isDefinition())
734 Out << "[class] ";
735 else
736 Out << "<class> ";
737 Out << RD->getNameAsString() << " " << DC;
738 break;
739 }
740 case Decl::ObjCMethod:
741 Out << "[objc method]";
742 break;
743 case Decl::ObjCInterface:
744 Out << "[objc interface]";
745 break;
746 case Decl::ObjCCategory:
747 Out << "[objc category]";
748 break;
749 case Decl::ObjCProtocol:
750 Out << "[objc protocol]";
751 break;
752 case Decl::ObjCImplementation:
753 Out << "[objc implementation]";
754 break;
755 case Decl::ObjCCategoryImpl:
756 Out << "[objc categoryimpl]";
757 break;
758 case Decl::LinkageSpec:
759 Out << "[linkage spec]";
760 break;
761 case Decl::Block:
762 Out << "[block]";
763 break;
764 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000765 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000766 if (FD->isThisDeclarationADefinition())
767 Out << "[function] ";
768 else
769 Out << "<function> ";
770 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000771 // Print the parameters.
772 Out << "(";
773 bool PrintComma = false;
774 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
775 E = FD->param_end(); I != E; ++I) {
776 if (PrintComma)
777 Out << ", ";
778 else
779 PrintComma = true;
780 Out << (*I)->getNameAsString();
781 }
782 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000783 break;
784 }
785 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000786 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000787 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000788 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000789 else if (D->isImplicit())
790 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000791 else
792 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000793 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000794 // Print the parameters.
795 Out << "(";
796 bool PrintComma = false;
797 for (FunctionDecl::param_const_iterator I = D->param_begin(),
798 E = D->param_end(); I != E; ++I) {
799 if (PrintComma)
800 Out << ", ";
801 else
802 PrintComma = true;
803 Out << (*I)->getNameAsString();
804 }
805 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000806
807 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000808 const DeclContext* SemaDC = D->getDeclContext();
809 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000810 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000811 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000812
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000813 break;
814 }
815 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000816 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000817 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000818 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000819 else if (D->isImplicit())
820 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000821 else
822 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000823 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000824 // Print the parameters.
825 Out << "(";
826 bool PrintComma = false;
827 for (FunctionDecl::param_const_iterator I = D->param_begin(),
828 E = D->param_end(); I != E; ++I) {
829 if (PrintComma)
830 Out << ", ";
831 else
832 PrintComma = true;
833 Out << (*I)->getNameAsString();
834 }
835 Out << ")";
836
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000837 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000838 const DeclContext* SemaDC = D->getDeclContext();
839 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000840 if (SemaDC != LexicalDC)
841 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000842 break;
843 }
844 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000845 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000846 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000847 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000848 else if (D->isImplicit())
849 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000850 else
851 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000852 Out << D->getNameAsString();
853 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000854 const DeclContext* SemaDC = D->getDeclContext();
855 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000856 if (SemaDC != LexicalDC)
857 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000858 break;
859 }
860 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000861 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000862 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000863 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000864 else if (D->isImplicit())
865 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000866 else
867 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000868 Out << D->getNameAsString();
869 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000870 const DeclContext* SemaDC = D->getDeclContext();
871 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000872 if (SemaDC != LexicalDC)
873 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000874 break;
875 }
876
877 default:
878 assert(0 && "a decl that inherits DeclContext isn't handled");
879 }
880
881 Out << "\n";
882
883 // Print decls in the DeclContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000884 // FIXME: Should not use a NULL DeclContext!
885 ASTContext *Context = 0;
886 for (DeclContext::decl_iterator I = DC->decls_begin(*Context),
887 E = DC->decls_end(*Context);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000888 I != E; ++I) {
889 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000890 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000891
892 Decl::Kind DK = I->getKind();
893 switch (DK) {
894 case Decl::Namespace:
895 case Decl::Enum:
896 case Decl::Record:
897 case Decl::CXXRecord:
898 case Decl::ObjCMethod:
899 case Decl::ObjCInterface:
900 case Decl::ObjCCategory:
901 case Decl::ObjCProtocol:
902 case Decl::ObjCImplementation:
903 case Decl::ObjCCategoryImpl:
904 case Decl::LinkageSpec:
905 case Decl::Block:
906 case Decl::Function:
907 case Decl::CXXMethod:
908 case Decl::CXXConstructor:
909 case Decl::CXXDestructor:
910 case Decl::CXXConversion:
911 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000912 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000913 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000914 break;
915 }
916 case Decl::Field: {
917 FieldDecl* FD = cast<FieldDecl>(*I);
918 Out << "<field> " << FD->getNameAsString() << "\n";
919 break;
920 }
921 case Decl::Typedef: {
922 TypedefDecl* TD = cast<TypedefDecl>(*I);
923 Out << "<typedef> " << TD->getNameAsString() << "\n";
924 break;
925 }
926 case Decl::EnumConstant: {
927 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
928 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
929 break;
930 }
931 case Decl::Var: {
932 VarDecl* VD = cast<VarDecl>(*I);
933 Out << "<var> " << VD->getNameAsString() << "\n";
934 break;
935 }
936 case Decl::ImplicitParam: {
937 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
938 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
939 break;
940 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000941 case Decl::ParmVar: {
942 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
943 Out << "<parameter> " << PVD->getNameAsString() << "\n";
944 break;
945 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000946 case Decl::OriginalParmVar: {
947 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
948 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
949 break;
950 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000951 case Decl::ObjCProperty: {
952 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
953 Out << "<objc property> " << OPD->getNameAsString() << "\n";
954 break;
955 }
956 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000957 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000958 assert(0 && "decl unhandled");
959 }
960 }
961}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000962ASTConsumer *clang::CreateDeclContextPrinter() {
963 return new DeclContextPrinter();
964}
965
966//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000967/// InheritanceViewer - C++ Inheritance Visualization
968
969namespace {
970class InheritanceViewer : public ASTConsumer {
971 const std::string clsname;
972public:
973 InheritanceViewer(const std::string& cname) : clsname(cname) {}
974
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000975 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000976 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000977 if (RecordType *T = dyn_cast<RecordType>(*I)) {
978 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
979 // FIXME: This lookup needs to be generalized to handle namespaces and
980 // (when we support them) templates.
981 if (D->getNameAsString() == clsname) {
982 D->viewInheritance(C);
983 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000984 }
985 }
986 }
987};
988}
989
990ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
991 return new InheritanceViewer(clsname);
992}
993
994//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000995// AST Serializer
996
997namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000998
999class ASTSerializer : public ASTConsumer {
1000protected:
Nico Weberdae86962008-08-09 18:32:11 +00001001 Diagnostic& Diags;
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001002
Ted Kremenekf06c9282007-12-19 23:49:37 +00001003public:
Nico Weberbe1eb5c2008-08-10 19:20:05 +00001004 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +00001005};
Eli Friedmand8a65c12008-06-09 20:02:51 +00001006
Ted Kremenekf06c9282007-12-19 23:49:37 +00001007class SingleFileSerializer : public ASTSerializer {
1008 const llvm::sys::Path FName;
1009public:
Nico Weberdae86962008-08-09 18:32:11 +00001010 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
1011 : ASTSerializer(diags), FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +00001012
Chris Lattnerdacbc5d2009-03-28 04:11:33 +00001013 virtual void HandleTranslationUnit(ASTContext &Ctx) {
Nico Weberdae86962008-08-09 18:32:11 +00001014 if (Diags.hasErrorOccurred())
1015 return;
Chris Lattner80a03332009-03-28 03:53:02 +00001016
1017 // Reserve 256K for bitstream buffer.
1018 std::vector<unsigned char> Buffer;
1019 Buffer.reserve(256*1024);
1020
Chris Lattner557c5b12009-03-28 04:27:18 +00001021 Ctx.EmitASTBitcodeBuffer(Buffer);
Chris Lattner80a03332009-03-28 03:53:02 +00001022
1023 // Write the bits to disk.
1024 if (FILE* fp = fopen(FName.c_str(),"wb")) {
1025 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
1026 fclose(fp);
1027 }
Ted Kremenekf06c9282007-12-19 23:49:37 +00001028 }
1029};
1030
1031class BuildSerializer : public ASTSerializer {
1032 llvm::sys::Path EmitDir;
1033public:
Nico Weberdae86962008-08-09 18:32:11 +00001034 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
1035 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +00001036
Chris Lattnerb23ff6b2009-03-28 05:44:17 +00001037 virtual void HandleTranslationUnit(ASTContext &Ctx);
Ted Kremenekf06c9282007-12-19 23:49:37 +00001038};
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001039} // end anonymous namespace
1040
1041
Chris Lattnerb23ff6b2009-03-28 05:44:17 +00001042void BuildSerializer::HandleTranslationUnit(ASTContext &Ctx) {
1043 if (Diags.hasErrorOccurred())
1044 return;
1045
1046 SourceManager& SourceMgr = Ctx.getSourceManager();
1047 FileID ID = SourceMgr.getMainFileID();
1048 assert(!ID.isInvalid() && "MainFileID not set!");
1049 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
1050 assert(FE && "No FileEntry for main file.");
1051
1052 // FIXME: This is not portable to Windows.
1053 // FIXME: This logic should probably be moved elsewhere later.
1054
1055 llvm::sys::Path FName(EmitDir);
1056
1057 std::vector<char> buf;
1058 buf.reserve(strlen(FE->getName())+100);
1059
1060 sprintf(&buf[0], "dev_%llx", (unsigned long long) FE->getDevice());
1061 FName.appendComponent(&buf[0]);
1062 FName.createDirectoryOnDisk(true);
1063 if (!FName.canWrite() || !FName.isDirectory()) {
1064 assert (false && "Could not create 'device' serialization directory.");
1065 return;
1066 }
1067
1068 sprintf(&buf[0], "%s-%llX.ast", FE->getName(),
1069 (unsigned long long) FE->getInode());
1070 FName.appendComponent(&buf[0]);
1071
1072
1073 // Reserve 256K for bitstream buffer.
1074 std::vector<unsigned char> Buffer;
1075 Buffer.reserve(256*1024);
1076
1077 Ctx.EmitASTBitcodeBuffer(Buffer);
1078
1079 // Write the bits to disk.
1080 if (FILE* fp = fopen(FName.c_str(),"wb")) {
1081 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
1082 fclose(fp);
1083 }
1084
1085 // Now emit the sources.
1086
1087}
1088
1089
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001090ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +00001091 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001092 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +00001093
Ted Kremenekf06c9282007-12-19 23:49:37 +00001094 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +00001095 if (InFile == "-") {
1096 llvm::cerr <<
1097 "error: Cannot use --serialize with -o for source read from STDIN.\n";
1098 return NULL;
1099 }
1100
Ted Kremenekf06c9282007-12-19 23:49:37 +00001101 // The user specified an AST-emission directory. Determine if the path
1102 // is absolute.
1103 llvm::sys::Path EmitDir(OutputFile);
1104
1105 if (!EmitDir.isAbsolute()) {
1106 llvm::cerr <<
1107 "error: Output directory for --serialize must be an absolute path.\n";
1108
1109 return NULL;
1110 }
1111
1112 // Create the directory if it does not exist.
1113 EmitDir.createDirectoryOnDisk(true);
1114 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
1115 llvm::cerr <<
1116 "error: Could not create output directory for --serialize.\n";
1117
1118 return NULL;
1119 }
1120
Ted Kremenek54117722007-12-20 00:34:58 +00001121 // FIXME: We should probably only allow using BuildSerializer when
1122 // the ASTs come from parsed source files, and not from .ast files.
Nico Weberdae86962008-08-09 18:32:11 +00001123 return new BuildSerializer(EmitDir, Diags);
Ted Kremenekf06c9282007-12-19 23:49:37 +00001124 }
1125
1126 // The user did not specify an output directory for serialized ASTs.
1127 // Serialize the translation to a single file whose name is the same
1128 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +00001129
Ted Kremenekf06c9282007-12-19 23:49:37 +00001130 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +00001131 FName.appendSuffix("ast");
Nico Weberdae86962008-08-09 18:32:11 +00001132 return new SingleFileSerializer(FName, Diags);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001133}