blob: 61a9e4460abd9e34051aed9b12fa13c40f5053ec [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);
Douglas Gregor087fd532009-04-14 23:32:43 +0000193 Out << Name;
194 if (VarDecl *Var = dyn_cast<VarDecl>(ND)) {
195 if (Var->getInit()) {
196 Out << " = ";
197 Var->getInit()->printPretty(Out);
198 }
199 }
200 Out << ";\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000201 break;
202 }
203 case Decl::Namespace:
204 Print(dyn_cast<NamespaceDecl>(ND));
205 break;
206 }
207}
208
209void DeclPrinter::Print(NamespaceDecl *NS) {
210 Out << "namespace " << NS->getNameAsString() << " {\n";
211 ChangeIndent(1);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000212 // FIXME: Shouldn't pass a NULL context
213 ASTContext *Context = 0;
214 for (DeclContext::decl_iterator i = NS->decls_begin(*Context);
215 i != NS->decls_end(*Context);
Mike Stump071e4da2009-02-10 20:16:46 +0000216 ++i)
217 PrintDecl(*i);
218 ChangeIndent(-1);
219 Indent();
220 Out << "}\n";
221}
222
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000223void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 bool HasBody = FD->getBody();
225
Ted Kremenekea75c552007-11-28 21:32:21 +0000226 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000227
Mike Stump071e4da2009-02-10 20:16:46 +0000228 Indent();
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000229 switch (FD->getStorageClass()) {
230 default: assert(0 && "Unknown storage class");
231 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000232 case FunctionDecl::Extern: Out << "extern "; break;
233 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000234 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000235 }
236
237 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000238 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000239
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000240 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000241 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000242
Douglas Gregor72564e72009-02-26 23:50:07 +0000243 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 Proto += "(";
245 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
246 if (i) Proto += ", ";
247 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000248 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000249
250 FT->getArgType(i).getAsStringInternal(ParamStr);
251 Proto += ParamStr;
252 }
253
254 if (FT->isVariadic()) {
255 if (FD->getNumParams()) Proto += ", ";
256 Proto += "...";
257 }
258 Proto += ")";
259 } else {
Douglas Gregor72564e72009-02-26 23:50:07 +0000260 assert(isa<FunctionNoProtoType>(AFT));
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 Proto += "()";
262 }
263
264 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000265 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000266
Chris Lattner6000dac2007-08-08 22:51:59 +0000267 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000268 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000269 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000270}
271
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000272void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000273 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000275 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000276}
277
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000278void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
279 const char *l;
280 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
281 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000282 else {
283 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
284 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000285 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000286 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000287
288 Out << "extern \"" << l << "\" ";
Mike Stump071e4da2009-02-10 20:16:46 +0000289 if (LS->hasBraces()) {
Douglas Gregorf44515a2008-12-16 22:23:02 +0000290 Out << "{\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000291 ChangeIndent(1);
292 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000293
Douglas Gregor6ab35242009-04-09 21:40:53 +0000294 // FIXME: Should not use a NULL DeclContext!
295 ASTContext *Context = 0;
296 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context),
297 DEnd = LS->decls_end(*Context);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000298 D != DEnd; ++D)
299 PrintDecl(*D);
300
Mike Stump071e4da2009-02-10 20:16:46 +0000301 if (LS->hasBraces()) {
302 ChangeIndent(-1);
303 Indent() << "}";
304 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000305 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000306}
307
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000308void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000309 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000310 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000311 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000312 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000313 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000314 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000315
Chris Lattner077bf5e2008-11-24 03:33:13 +0000316 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000317 std::string::size_type pos, lastPos = 0;
Chris Lattner89951a82009-02-20 18:43:26 +0000318 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
319 E = OMD->param_end(); PI != E; ++PI) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000320 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000321 pos = name.find_first_of(":", lastPos);
322 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattner89951a82009-02-20 18:43:26 +0000323 Out << ":(" << (*PI)->getType().getAsString() << ")"
324 << (*PI)->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000325 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000326 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000327
Chris Lattner89951a82009-02-20 18:43:26 +0000328 if (OMD->param_begin() == OMD->param_end())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000329 Out << " " << name;
330
331 if (OMD->isVariadic())
332 Out << ", ...";
333
334 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000335}
336
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000337void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000338 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000339 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000340
341 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000342 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000343 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000344 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000345
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000346 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000347 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000348 ObjCMethodDecl *OMD = *I;
349 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000350 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000351 Out << ' ';
352 OMD->getBody()->printPretty(Out);
353 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000354 }
355 }
356
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000357 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000358 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000359 ObjCMethodDecl *OMD = *I;
360 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000361 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000362 Out << ' ';
363 OMD->getBody()->printPretty(Out);
364 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000365 }
366 }
367
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000368 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
369 E = OID->propimpl_end(); I != E; ++I)
370 PrintObjCPropertyImplDecl(*I);
371
Ted Kremenekea75c552007-11-28 21:32:21 +0000372 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000373}
374
375
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000376void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000377 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000378 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000379
380 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000381 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000382 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000383 Out << "@interface " << I;
384
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000385 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000386 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
387 if (!Protocols.empty()) {
388 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
389 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000390 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000391 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000392
Chris Lattner3db6cae2008-07-21 18:19:38 +0000393 if (!Protocols.empty())
394 Out << ">";
395 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000396
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000397 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000398 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000399 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000400 E = OID->ivar_end(); I != E; ++I) {
401 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000402 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000403 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000404 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000405 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000406
Douglas Gregor6ab35242009-04-09 21:40:53 +0000407 // FIXME: Should not use a NULL DeclContext!
408 ASTContext *Context = 0;
409 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context),
410 E = OID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000411 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000412 bool eol_needed = false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000413 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context),
414 E = OID->classmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000415 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000416
Douglas Gregor6ab35242009-04-09 21:40:53 +0000417 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context),
418 E = OID->instmeth_end(*Context); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000419 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000420
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000421 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000422 // FIXME: implement the rest...
423}
424
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000425void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000426 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000427
Douglas Gregor6ab35242009-04-09 21:40:53 +0000428 // FIXME: Should not use a NULL DeclContext!
429 ASTContext *Context = 0;
430 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context),
431 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000432 PrintObjCPropertyDecl(*I);
433 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000434 // FIXME: implement the rest...
435}
436
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000437void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000438 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000439 << PID->getClassInterface()->getNameAsString()
440 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000441 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
442 E = PID->propimpl_end(); I != E; ++I)
443 PrintObjCPropertyImplDecl(*I);
444 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000445 // FIXME: implement the rest...
446}
447
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000448void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000449 // FIXME: Should not use a NULL DeclContext!
450 ASTContext *Context = 0;
Ted Kremenekea75c552007-11-28 21:32:21 +0000451 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000452 << PID->getClassInterface()->getNameAsString()
453 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000454 // Output property declarations.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000455 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context),
456 E = PID->prop_end(*Context); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000457 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000458 Out << "@end\n";
459
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000460 // FIXME: implement the rest...
461}
462
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000463void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000464 Out << "@compatibility_alias " << AID->getNameAsString()
465 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000466}
467
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000468/// PrintObjCPropertyDecl - print a property declaration.
469///
470void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000471 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
472 Out << "@required\n";
473 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
474 Out << "@optional\n";
475
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000476 Out << "@property";
477 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
478 bool first = true;
479 Out << " (";
480 if (PDecl->getPropertyAttributes() &
481 ObjCPropertyDecl::OBJC_PR_readonly) {
482 Out << (first ? ' ' : ',') << "readonly";
483 first = false;
484 }
485
486 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
487 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000488 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000489 first = false;
490 }
491 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
492 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000493 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000494 first = false;
495 }
496
497 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
498 Out << (first ? ' ' : ',') << "assign";
499 first = false;
500 }
501
502 if (PDecl->getPropertyAttributes() &
503 ObjCPropertyDecl::OBJC_PR_readwrite) {
504 Out << (first ? ' ' : ',') << "readwrite";
505 first = false;
506 }
507
508 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
509 Out << (first ? ' ' : ',') << "retain";
510 first = false;
511 }
512
513 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
514 Out << (first ? ' ' : ',') << "copy";
515 first = false;
516 }
517
518 if (PDecl->getPropertyAttributes() &
519 ObjCPropertyDecl::OBJC_PR_nonatomic) {
520 Out << (first ? ' ' : ',') << "nonatomic";
521 first = false;
522 }
523 Out << " )";
524 }
525 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000526 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000527
528 Out << ";\n";
529}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000530
531/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
532/// declaration syntax.
533///
534void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000535 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000536 Out << "\n@synthesize ";
537 else
538 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000539 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000540 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000541 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000542 Out << ";\n";
543}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000544
545/// PrintTemplateParams - Print a template parameter list and recursively print
546/// it's underlying top-level definition.
547void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
548 // TODO: Write template parameters.
549 Out << "template <...> ";
550 PrintDecl(TD->getTemplatedDecl());
551}
552
553
554
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000555//===----------------------------------------------------------------------===//
556/// ASTPrinter - Pretty-printer of ASTs
557
Chris Lattner3d4997d2007-09-15 23:02:28 +0000558namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000559 class ASTPrinter : public ASTConsumer, public DeclPrinter {
560 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000561 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000562
Chris Lattner682bf922009-03-29 16:50:03 +0000563 virtual void HandleTopLevelDecl(DeclGroupRef D) {
564 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
565 PrintDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000567 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000568} // end anonymous namespace
Chris Lattner6000dac2007-08-08 22:51:59 +0000569
Ted Kremeneka95d3752008-09-13 05:16:45 +0000570ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000571 return new ASTPrinter(out);
572}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000573
574//===----------------------------------------------------------------------===//
575/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000576
577namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000578 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000579 SourceManager *SM;
580 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000581 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000582
Ted Kremenek95041a22007-12-19 22:51:13 +0000583 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000584 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000585 }
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000586
Chris Lattner682bf922009-03-29 16:50:03 +0000587 virtual void HandleTopLevelDecl(DeclGroupRef D) {
588 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
589 HandleTopLevelSingleDecl(*I);
590 }
591 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattner3d4997d2007-09-15 23:02:28 +0000592 };
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000593} // end anonymous namespace
594
Chris Lattner682bf922009-03-29 16:50:03 +0000595void ASTDumper::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000596 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
597 PrintFunctionDeclStart(FD);
598
599 if (FD->getBody()) {
600 Out << '\n';
601 // FIXME: convert dumper to use std::ostream?
602 FD->getBody()->dumpAll(*SM);
603 Out << '\n';
604 }
605 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
606 PrintTypeDefDecl(TD);
607 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
608 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
609 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
610 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
611 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
612 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
613 } else if (isa<ObjCForwardProtocolDecl>(D)) {
614 Out << "Read objc fwd protocol decl\n";
615 } else if (isa<ObjCClassDecl>(D)) {
616 Out << "Read objc fwd class decl\n";
617 } else if (isa<FileScopeAsmDecl>(D)) {
618 Out << "Read file scope asm decl\n";
619 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
620 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
621 << "'\n";
622 if (MD->getBody()) {
623 // FIXME: convert dumper to use std::ostream?
624 MD->getBody()->dumpAll(*SM);
625 Out << '\n';
626 }
627 } else if (isa<ObjCImplementationDecl>(D)) {
628 Out << "Read objc implementation decl\n";
629 } else if (isa<ObjCCategoryImplDecl>(D)) {
630 Out << "Read objc category implementation decl\n";
631 } else if (isa<LinkageSpecDecl>(D)) {
632 Out << "Read linkage spec decl\n";
633 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
634 Out << "Read top-level variable decl: '" << ND->getNameAsString()
635 << "'\n";
636 } else {
637 assert(0 && "Unknown decl type!");
638 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000639}
640
Chris Lattner3d4997d2007-09-15 23:02:28 +0000641ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
642
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000643//===----------------------------------------------------------------------===//
644/// ASTViewer - AST Visualization
645
Ted Kremenek80de08f2007-09-19 21:29:43 +0000646namespace {
647 class ASTViewer : public ASTConsumer {
648 SourceManager *SM;
649 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000650 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000651 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000652 }
Chris Lattner682bf922009-03-29 16:50:03 +0000653
654 virtual void HandleTopLevelDecl(DeclGroupRef D) {
655 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
656 HandleTopLevelSingleDecl(*I);
657 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000658
Chris Lattner682bf922009-03-29 16:50:03 +0000659 void HandleTopLevelSingleDecl(Decl *D);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000660 };
661}
662
Chris Lattner682bf922009-03-29 16:50:03 +0000663void ASTViewer::HandleTopLevelSingleDecl(Decl *D) {
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000664 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
665 DeclPrinter().PrintFunctionDeclStart(FD);
666
667 if (FD->getBody()) {
668 llvm::cerr << '\n';
669 FD->getBody()->viewAST();
670 llvm::cerr << '\n';
671 }
672 return;
673 }
674
675 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
676 DeclPrinter().PrintObjCMethodDecl(MD);
677
678 if (MD->getBody()) {
679 llvm::cerr << '\n';
680 MD->getBody()->viewAST();
681 llvm::cerr << '\n';
682 }
683 }
684}
685
686
Ted Kremenek80de08f2007-09-19 21:29:43 +0000687ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
688
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000689//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000690/// DeclContextPrinter - Decl and DeclContext Visualization
691
692namespace {
693
694class DeclContextPrinter : public ASTConsumer {
695 llvm::raw_ostream& Out;
696public:
697 DeclContextPrinter() : Out(llvm::errs()) {}
698
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000699 void HandleTranslationUnit(ASTContext &C) {
700 PrintDeclContext(C.getTranslationUnitDecl(), 4);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000701 }
702
703 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
704};
Chris Lattnerb23ff6b2009-03-28 05:44:17 +0000705} // end anonymous namespace
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000706
707void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
708 unsigned Indentation) {
709 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000710 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000711 case Decl::TranslationUnit:
712 Out << "[translation unit] " << DC;
713 break;
714 case Decl::Namespace: {
715 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000716 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000717 Out << ND->getNameAsString();
718 break;
719 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000720 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000721 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000722 if (ED->isDefinition())
723 Out << "[enum] ";
724 else
725 Out << "<enum> ";
726 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000727 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000728 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000729 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000730 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000731 if (RD->isDefinition())
732 Out << "[struct] ";
733 else
734 Out << "<struct> ";
735 Out << RD->getNameAsString();
736 break;
737 }
738 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000739 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000740 if (RD->isDefinition())
741 Out << "[class] ";
742 else
743 Out << "<class> ";
744 Out << RD->getNameAsString() << " " << DC;
745 break;
746 }
747 case Decl::ObjCMethod:
748 Out << "[objc method]";
749 break;
750 case Decl::ObjCInterface:
751 Out << "[objc interface]";
752 break;
753 case Decl::ObjCCategory:
754 Out << "[objc category]";
755 break;
756 case Decl::ObjCProtocol:
757 Out << "[objc protocol]";
758 break;
759 case Decl::ObjCImplementation:
760 Out << "[objc implementation]";
761 break;
762 case Decl::ObjCCategoryImpl:
763 Out << "[objc categoryimpl]";
764 break;
765 case Decl::LinkageSpec:
766 Out << "[linkage spec]";
767 break;
768 case Decl::Block:
769 Out << "[block]";
770 break;
771 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000772 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000773 if (FD->isThisDeclarationADefinition())
774 Out << "[function] ";
775 else
776 Out << "<function> ";
777 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000778 // Print the parameters.
779 Out << "(";
780 bool PrintComma = false;
781 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
782 E = FD->param_end(); I != E; ++I) {
783 if (PrintComma)
784 Out << ", ";
785 else
786 PrintComma = true;
787 Out << (*I)->getNameAsString();
788 }
789 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000790 break;
791 }
792 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000793 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000794 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000795 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000796 else if (D->isImplicit())
797 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000798 else
799 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000800 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000801 // Print the parameters.
802 Out << "(";
803 bool PrintComma = false;
804 for (FunctionDecl::param_const_iterator I = D->param_begin(),
805 E = D->param_end(); I != E; ++I) {
806 if (PrintComma)
807 Out << ", ";
808 else
809 PrintComma = true;
810 Out << (*I)->getNameAsString();
811 }
812 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000813
814 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000815 const DeclContext* SemaDC = D->getDeclContext();
816 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000817 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000818 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000819
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000820 break;
821 }
822 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000823 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000824 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000825 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000826 else if (D->isImplicit())
827 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000828 else
829 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000830 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000831 // Print the parameters.
832 Out << "(";
833 bool PrintComma = false;
834 for (FunctionDecl::param_const_iterator I = D->param_begin(),
835 E = D->param_end(); I != E; ++I) {
836 if (PrintComma)
837 Out << ", ";
838 else
839 PrintComma = true;
840 Out << (*I)->getNameAsString();
841 }
842 Out << ")";
843
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000844 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000845 const DeclContext* SemaDC = D->getDeclContext();
846 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000847 if (SemaDC != LexicalDC)
848 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000849 break;
850 }
851 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000852 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000853 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000854 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000855 else if (D->isImplicit())
856 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000857 else
858 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000859 Out << D->getNameAsString();
860 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000861 const DeclContext* SemaDC = D->getDeclContext();
862 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000863 if (SemaDC != LexicalDC)
864 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000865 break;
866 }
867 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000868 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000869 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000870 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000871 else if (D->isImplicit())
872 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000873 else
874 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000875 Out << D->getNameAsString();
876 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000877 const DeclContext* SemaDC = D->getDeclContext();
878 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000879 if (SemaDC != LexicalDC)
880 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000881 break;
882 }
883
884 default:
885 assert(0 && "a decl that inherits DeclContext isn't handled");
886 }
887
888 Out << "\n";
889
890 // Print decls in the DeclContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000891 // FIXME: Should not use a NULL DeclContext!
892 ASTContext *Context = 0;
893 for (DeclContext::decl_iterator I = DC->decls_begin(*Context),
894 E = DC->decls_end(*Context);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000895 I != E; ++I) {
896 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000897 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000898
899 Decl::Kind DK = I->getKind();
900 switch (DK) {
901 case Decl::Namespace:
902 case Decl::Enum:
903 case Decl::Record:
904 case Decl::CXXRecord:
905 case Decl::ObjCMethod:
906 case Decl::ObjCInterface:
907 case Decl::ObjCCategory:
908 case Decl::ObjCProtocol:
909 case Decl::ObjCImplementation:
910 case Decl::ObjCCategoryImpl:
911 case Decl::LinkageSpec:
912 case Decl::Block:
913 case Decl::Function:
914 case Decl::CXXMethod:
915 case Decl::CXXConstructor:
916 case Decl::CXXDestructor:
917 case Decl::CXXConversion:
918 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000919 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000920 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000921 break;
922 }
923 case Decl::Field: {
924 FieldDecl* FD = cast<FieldDecl>(*I);
925 Out << "<field> " << FD->getNameAsString() << "\n";
926 break;
927 }
928 case Decl::Typedef: {
929 TypedefDecl* TD = cast<TypedefDecl>(*I);
930 Out << "<typedef> " << TD->getNameAsString() << "\n";
931 break;
932 }
933 case Decl::EnumConstant: {
934 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
935 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
936 break;
937 }
938 case Decl::Var: {
939 VarDecl* VD = cast<VarDecl>(*I);
940 Out << "<var> " << VD->getNameAsString() << "\n";
941 break;
942 }
943 case Decl::ImplicitParam: {
944 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
945 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
946 break;
947 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000948 case Decl::ParmVar: {
949 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
950 Out << "<parameter> " << PVD->getNameAsString() << "\n";
951 break;
952 }
Zhongxing Xuba16be92009-04-05 02:04:38 +0000953 case Decl::OriginalParmVar: {
954 OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I);
955 Out << "<original parameter> " << OPVD->getNameAsString() << "\n";
956 break;
957 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000958 case Decl::ObjCProperty: {
959 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
960 Out << "<objc property> " << OPD->getNameAsString() << "\n";
961 break;
962 }
963 default:
Zhongxing Xuba16be92009-04-05 02:04:38 +0000964 fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName());
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000965 assert(0 && "decl unhandled");
966 }
967 }
968}
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000969ASTConsumer *clang::CreateDeclContextPrinter() {
970 return new DeclContextPrinter();
971}
972
973//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000974/// InheritanceViewer - C++ Inheritance Visualization
975
976namespace {
977class InheritanceViewer : public ASTConsumer {
978 const std::string clsname;
979public:
980 InheritanceViewer(const std::string& cname) : clsname(cname) {}
981
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000982 void HandleTranslationUnit(ASTContext &C) {
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000983 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000984 if (RecordType *T = dyn_cast<RecordType>(*I)) {
985 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) {
986 // FIXME: This lookup needs to be generalized to handle namespaces and
987 // (when we support them) templates.
988 if (D->getNameAsString() == clsname) {
989 D->viewInheritance(C);
990 }
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000991 }
992 }
993 }
994};
995}
996
997ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
998 return new InheritanceViewer(clsname);
999}
1000
1001//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001002// AST Serializer
1003
1004namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +00001005
1006class ASTSerializer : public ASTConsumer {
1007protected:
Nico Weberdae86962008-08-09 18:32:11 +00001008 Diagnostic& Diags;
Ted Kremenekc1e9dea2008-04-23 16:25:39 +00001009
Ted Kremenekf06c9282007-12-19 23:49:37 +00001010public:
Nico Weberbe1eb5c2008-08-10 19:20:05 +00001011 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +00001012};
Eli Friedmand8a65c12008-06-09 20:02:51 +00001013
Ted Kremenekf06c9282007-12-19 23:49:37 +00001014class SingleFileSerializer : public ASTSerializer {
1015 const llvm::sys::Path FName;
1016public:
Nico Weberdae86962008-08-09 18:32:11 +00001017 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
1018 : ASTSerializer(diags), FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +00001019
Chris Lattnerdacbc5d2009-03-28 04:11:33 +00001020 virtual void HandleTranslationUnit(ASTContext &Ctx) {
Nico Weberdae86962008-08-09 18:32:11 +00001021 if (Diags.hasErrorOccurred())
1022 return;
Chris Lattner80a03332009-03-28 03:53:02 +00001023
1024 // Reserve 256K for bitstream buffer.
1025 std::vector<unsigned char> Buffer;
1026 Buffer.reserve(256*1024);
1027
Chris Lattner557c5b12009-03-28 04:27:18 +00001028 Ctx.EmitASTBitcodeBuffer(Buffer);
Chris Lattner80a03332009-03-28 03:53:02 +00001029
1030 // Write the bits to disk.
1031 if (FILE* fp = fopen(FName.c_str(),"wb")) {
1032 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
1033 fclose(fp);
1034 }
Ted Kremenekf06c9282007-12-19 23:49:37 +00001035 }
1036};
1037
1038class BuildSerializer : public ASTSerializer {
1039 llvm::sys::Path EmitDir;
1040public:
Nico Weberdae86962008-08-09 18:32:11 +00001041 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
1042 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +00001043
Chris Lattnerb23ff6b2009-03-28 05:44:17 +00001044 virtual void HandleTranslationUnit(ASTContext &Ctx);
Ted Kremenekf06c9282007-12-19 23:49:37 +00001045};
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001046} // end anonymous namespace
1047
1048
Chris Lattnerb23ff6b2009-03-28 05:44:17 +00001049void BuildSerializer::HandleTranslationUnit(ASTContext &Ctx) {
1050 if (Diags.hasErrorOccurred())
1051 return;
1052
1053 SourceManager& SourceMgr = Ctx.getSourceManager();
1054 FileID ID = SourceMgr.getMainFileID();
1055 assert(!ID.isInvalid() && "MainFileID not set!");
1056 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
1057 assert(FE && "No FileEntry for main file.");
1058
1059 // FIXME: This is not portable to Windows.
1060 // FIXME: This logic should probably be moved elsewhere later.
1061
1062 llvm::sys::Path FName(EmitDir);
1063
1064 std::vector<char> buf;
1065 buf.reserve(strlen(FE->getName())+100);
1066
1067 sprintf(&buf[0], "dev_%llx", (unsigned long long) FE->getDevice());
1068 FName.appendComponent(&buf[0]);
1069 FName.createDirectoryOnDisk(true);
1070 if (!FName.canWrite() || !FName.isDirectory()) {
1071 assert (false && "Could not create 'device' serialization directory.");
1072 return;
1073 }
1074
1075 sprintf(&buf[0], "%s-%llX.ast", FE->getName(),
1076 (unsigned long long) FE->getInode());
1077 FName.appendComponent(&buf[0]);
1078
1079
1080 // Reserve 256K for bitstream buffer.
1081 std::vector<unsigned char> Buffer;
1082 Buffer.reserve(256*1024);
1083
1084 Ctx.EmitASTBitcodeBuffer(Buffer);
1085
1086 // Write the bits to disk.
1087 if (FILE* fp = fopen(FName.c_str(),"wb")) {
1088 fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
1089 fclose(fp);
1090 }
1091
1092 // Now emit the sources.
1093
1094}
1095
1096
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001097ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +00001098 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001099 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +00001100
Ted Kremenekf06c9282007-12-19 23:49:37 +00001101 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +00001102 if (InFile == "-") {
1103 llvm::cerr <<
1104 "error: Cannot use --serialize with -o for source read from STDIN.\n";
1105 return NULL;
1106 }
1107
Ted Kremenekf06c9282007-12-19 23:49:37 +00001108 // The user specified an AST-emission directory. Determine if the path
1109 // is absolute.
1110 llvm::sys::Path EmitDir(OutputFile);
1111
1112 if (!EmitDir.isAbsolute()) {
1113 llvm::cerr <<
1114 "error: Output directory for --serialize must be an absolute path.\n";
1115
1116 return NULL;
1117 }
1118
1119 // Create the directory if it does not exist.
1120 EmitDir.createDirectoryOnDisk(true);
1121 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
1122 llvm::cerr <<
1123 "error: Could not create output directory for --serialize.\n";
1124
1125 return NULL;
1126 }
1127
Ted Kremenek54117722007-12-20 00:34:58 +00001128 // FIXME: We should probably only allow using BuildSerializer when
1129 // the ASTs come from parsed source files, and not from .ast files.
Nico Weberdae86962008-08-09 18:32:11 +00001130 return new BuildSerializer(EmitDir, Diags);
Ted Kremenekf06c9282007-12-19 23:49:37 +00001131 }
1132
1133 // The user did not specify an output directory for serialized ASTs.
1134 // Serialize the translation to a single file whose name is the same
1135 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +00001136
Ted Kremenekf06c9282007-12-19 23:49:37 +00001137 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +00001138 FName.appendSuffix("ast");
Nico Weberdae86962008-08-09 18:32:11 +00001139 return new SingleFileSerializer(FName, Diags);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001140}