blob: 397170e60d2c87e57013c768a4cc31987126d79e [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"
Ted Kremenekad99dbf2008-11-03 22:31:48 +000015#include "clang/Driver/PathDiagnosticClients.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000016#include "clang/AST/TranslationUnit.h"
Nico Weberdae86962008-08-09 18:32:11 +000017#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000021#include "clang/AST/ASTConsumer.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"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000027
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 ";
100 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000101 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000102 if (i) Out << ", ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000103 Out << D->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";
128 for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(),
129 EEnd = ED->enumerator_end();
130 E != EEnd; ++E)
131 Out << " " << (*E)->getNameAsString() << ",\n";
132 Out << "};\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000133 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000134 // print a free standing tag decl (e.g. "struct x;").
135 Out << TD->getKindName();
136 Out << " ";
137 if (const IdentifierInfo *II = TD->getIdentifier())
138 Out << II->getName();
139
140 Out << " {\n";
141 ChangeIndent(1);
142 for (DeclContext::decl_iterator i = TD->decls_begin();
143 i != TD->decls_end();
144 ++i)
145 PrintDecl(*i);
146 ChangeIndent(-1);
147 Indent();
148 Out << "}";
149
150 Out << "\n";
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000151 } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) {
152 PrintTemplateDecl(TempD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000153 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
154 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000155 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
156 Out << "asm(";
157 AD->getAsmString()->printPretty(Out);
158 Out << ")\n";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000159 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Mike Stump071e4da2009-02-10 20:16:46 +0000160 Print(ND);
Chris Lattneref5a85d2008-01-02 21:04:16 +0000161 } else {
162 assert(0 && "Unknown decl type!");
163 }
164}
165
Mike Stump071e4da2009-02-10 20:16:46 +0000166void DeclPrinter::Print(NamedDecl *ND) {
167 switch (ND->getKind()) {
168 default:
169 // FIXME: Handle the rest of the NamedDecls.
170 Out << "### NamedDecl " << ND->getNameAsString() << "\n";
171 break;
172 case Decl::Field:
173 case Decl::Var: {
174 // Emit storage class for vardecls.
175 if (VarDecl *V = dyn_cast<VarDecl>(ND)) {
176 switch (V->getStorageClass()) {
177 default: assert(0 && "Unknown storage class!");
Mike Stumpc5840c02009-02-10 23:49:50 +0000178 case VarDecl::None: break;
179 case VarDecl::Auto: Out << "auto "; break;
180 case VarDecl::Register: Out << "register "; break;
181 case VarDecl::Extern: Out << "extern "; break;
182 case VarDecl::Static: Out << "static "; break;
Daniel Dunbar7ab41f72009-02-13 22:49:34 +0000183 case VarDecl::PrivateExtern: Out << "__private_extern__ "; break;
Mike Stump071e4da2009-02-10 20:16:46 +0000184 }
185 }
186 std::string Name = ND->getNameAsString();
187 // This forms: "int a".
188 dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name);
189 Out << Name << ";\n";
190 break;
191 }
192 case Decl::Namespace:
193 Print(dyn_cast<NamespaceDecl>(ND));
194 break;
195 }
196}
197
198void DeclPrinter::Print(NamespaceDecl *NS) {
199 Out << "namespace " << NS->getNameAsString() << " {\n";
200 ChangeIndent(1);
201 for (DeclContext::decl_iterator i = NS->decls_begin();
202 i != NS->decls_end();
203 ++i)
204 PrintDecl(*i);
205 ChangeIndent(-1);
206 Indent();
207 Out << "}\n";
208}
209
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000210void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 bool HasBody = FD->getBody();
212
Ted Kremenekea75c552007-11-28 21:32:21 +0000213 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000214
Mike Stump071e4da2009-02-10 20:16:46 +0000215 Indent();
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000216 switch (FD->getStorageClass()) {
217 default: assert(0 && "Unknown storage class");
218 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000219 case FunctionDecl::Extern: Out << "extern "; break;
220 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000221 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000222 }
223
224 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000225 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000226
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000227 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000228 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000229
Chris Lattner0d6ca112007-12-03 21:43:25 +0000230 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 Proto += "(";
232 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
233 if (i) Proto += ", ";
234 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000235 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000236
237 FT->getArgType(i).getAsStringInternal(ParamStr);
238 Proto += ParamStr;
239 }
240
241 if (FT->isVariadic()) {
242 if (FD->getNumParams()) Proto += ", ";
243 Proto += "...";
244 }
245 Proto += ")";
246 } else {
247 assert(isa<FunctionTypeNoProto>(AFT));
248 Proto += "()";
249 }
250
251 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000252 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000253
Chris Lattner6000dac2007-08-08 22:51:59 +0000254 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000255 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000256 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000257}
258
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000259void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000260 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000262 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000263}
264
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000265void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
266 const char *l;
267 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
268 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000269 else {
270 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
271 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000272 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000273 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000274
275 Out << "extern \"" << l << "\" ";
Mike Stump071e4da2009-02-10 20:16:46 +0000276 if (LS->hasBraces()) {
Douglas Gregorf44515a2008-12-16 22:23:02 +0000277 Out << "{\n";
Mike Stump071e4da2009-02-10 20:16:46 +0000278 ChangeIndent(1);
279 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000280
Douglas Gregor074149e2009-01-05 19:45:36 +0000281 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(),
282 DEnd = LS->decls_end();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000283 D != DEnd; ++D)
284 PrintDecl(*D);
285
Mike Stump071e4da2009-02-10 20:16:46 +0000286 if (LS->hasBraces()) {
287 ChangeIndent(-1);
288 Indent() << "}";
289 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000290 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000291}
292
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000293void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000294 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000295 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000296 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000297 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000298 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000299 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000300
Chris Lattner077bf5e2008-11-24 03:33:13 +0000301 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000302 std::string::size_type pos, lastPos = 0;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000303 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000304 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000305 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000306 pos = name.find_first_of(":", lastPos);
307 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000308 Out << ":(" << PDecl->getType().getAsString() << ")"
309 << PDecl->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000310 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000311 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000312
313 if (OMD->getNumParams() == 0)
314 Out << " " << name;
315
316 if (OMD->isVariadic())
317 Out << ", ...";
318
319 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000320}
321
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000322void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000323 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000324 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000325
326 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000327 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000328 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000329 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000330
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000331 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000332 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000333 ObjCMethodDecl *OMD = *I;
334 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000335 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000336 Out << ' ';
337 OMD->getBody()->printPretty(Out);
338 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000339 }
340 }
341
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000342 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000343 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000344 ObjCMethodDecl *OMD = *I;
345 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000346 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000347 Out << ' ';
348 OMD->getBody()->printPretty(Out);
349 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000350 }
351 }
352
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000353 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
354 E = OID->propimpl_end(); I != E; ++I)
355 PrintObjCPropertyImplDecl(*I);
356
Ted Kremenekea75c552007-11-28 21:32:21 +0000357 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000358}
359
360
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000361void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000362 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000363 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000364
365 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000366 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000367 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000368 Out << "@interface " << I;
369
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000370 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000371 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
372 if (!Protocols.empty()) {
373 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
374 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000375 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000376 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000377
Chris Lattner3db6cae2008-07-21 18:19:38 +0000378 if (!Protocols.empty())
379 Out << ">";
380 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000381
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000382 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000383 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000384 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000385 E = OID->ivar_end(); I != E; ++I) {
386 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000387 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000388 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000389 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000390 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000391
Steve Naroff09c47192009-01-09 15:36:25 +0000392 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(),
393 E = OID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000394 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000395 bool eol_needed = false;
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000396 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
397 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000398 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000399
400 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
401 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000402 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000403
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000404 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000405 // FIXME: implement the rest...
406}
407
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000408void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000409 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000410
Steve Naroff09c47192009-01-09 15:36:25 +0000411 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(),
412 E = PID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000413 PrintObjCPropertyDecl(*I);
414 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000415 // FIXME: implement the rest...
416}
417
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000418void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000419 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000420 << PID->getClassInterface()->getNameAsString()
421 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000422 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
423 E = PID->propimpl_end(); I != E; ++I)
424 PrintObjCPropertyImplDecl(*I);
425 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000426 // FIXME: implement the rest...
427}
428
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000429void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000430 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000431 << PID->getClassInterface()->getNameAsString()
432 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000433 // Output property declarations.
Steve Naroff09c47192009-01-09 15:36:25 +0000434 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(),
435 E = PID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000436 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000437 Out << "@end\n";
438
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000439 // FIXME: implement the rest...
440}
441
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000442void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000443 Out << "@compatibility_alias " << AID->getNameAsString()
444 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000445}
446
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000447/// PrintObjCPropertyDecl - print a property declaration.
448///
449void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000450 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
451 Out << "@required\n";
452 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
453 Out << "@optional\n";
454
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000455 Out << "@property";
456 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
457 bool first = true;
458 Out << " (";
459 if (PDecl->getPropertyAttributes() &
460 ObjCPropertyDecl::OBJC_PR_readonly) {
461 Out << (first ? ' ' : ',') << "readonly";
462 first = false;
463 }
464
465 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
466 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000467 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000468 first = false;
469 }
470 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
471 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000472 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000473 first = false;
474 }
475
476 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
477 Out << (first ? ' ' : ',') << "assign";
478 first = false;
479 }
480
481 if (PDecl->getPropertyAttributes() &
482 ObjCPropertyDecl::OBJC_PR_readwrite) {
483 Out << (first ? ' ' : ',') << "readwrite";
484 first = false;
485 }
486
487 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
488 Out << (first ? ' ' : ',') << "retain";
489 first = false;
490 }
491
492 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
493 Out << (first ? ' ' : ',') << "copy";
494 first = false;
495 }
496
497 if (PDecl->getPropertyAttributes() &
498 ObjCPropertyDecl::OBJC_PR_nonatomic) {
499 Out << (first ? ' ' : ',') << "nonatomic";
500 first = false;
501 }
502 Out << " )";
503 }
504 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000505 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000506
507 Out << ";\n";
508}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000509
510/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
511/// declaration syntax.
512///
513void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000514 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000515 Out << "\n@synthesize ";
516 else
517 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000518 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000519 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000520 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000521 Out << ";\n";
522}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000523
524/// PrintTemplateParams - Print a template parameter list and recursively print
525/// it's underlying top-level definition.
526void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
527 // TODO: Write template parameters.
528 Out << "template <...> ";
529 PrintDecl(TD->getTemplatedDecl());
530}
531
532
533
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000534//===----------------------------------------------------------------------===//
535/// ASTPrinter - Pretty-printer of ASTs
536
Chris Lattner3d4997d2007-09-15 23:02:28 +0000537namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000538 class ASTPrinter : public ASTConsumer, public DeclPrinter {
539 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000540 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000541
Chris Lattner3d4997d2007-09-15 23:02:28 +0000542 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000543 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000545 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000546}
Chris Lattner6000dac2007-08-08 22:51:59 +0000547
Ted Kremeneka95d3752008-09-13 05:16:45 +0000548ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000549 return new ASTPrinter(out);
550}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000551
552//===----------------------------------------------------------------------===//
553/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000554
555namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000556 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000557 SourceManager *SM;
558 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000559 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000560
Ted Kremenek95041a22007-12-19 22:51:13 +0000561 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000562 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000563 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000564
565 virtual void HandleTopLevelDecl(Decl *D) {
566 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
567 PrintFunctionDeclStart(FD);
568
569 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000570 Out << '\n';
571 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000572 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000573 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000574 }
575 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
576 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000577 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000578 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000579 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000580 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000581 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000582 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000583 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000584 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000585 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000586 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000587 } else if (isa<FileScopeAsmDecl>(D)) {
588 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000589 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000590 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
Ted Kremenek63bbe532008-03-14 17:31:00 +0000591 << "'\n";
Steve Naroff1a2b90d2008-05-23 18:50:58 +0000592 if (MD->getBody()) {
593 // FIXME: convert dumper to use std::ostream?
594 MD->getBody()->dumpAll(*SM);
595 Out << '\n';
596 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000597 } else if (isa<ObjCImplementationDecl>(D)) {
598 Out << "Read objc implementation decl\n";
Daniel Dunbar539ced12008-10-05 00:31:15 +0000599 } else if (isa<ObjCCategoryImplDecl>(D)) {
600 Out << "Read objc category implementation decl\n";
Eli Friedmanf595eb02008-12-16 22:14:15 +0000601 } else if (isa<LinkageSpecDecl>(D)) {
602 Out << "Read linkage spec decl\n";
Douglas Gregor305c22e2009-01-23 01:10:18 +0000603 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
604 Out << "Read top-level variable decl: '" << ND->getNameAsString()
605 << "'\n";
Eli Friedmanf595eb02008-12-16 22:14:15 +0000606 } else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000607 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000608 }
609 }
610 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000611}
612
Chris Lattner3d4997d2007-09-15 23:02:28 +0000613ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
614
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000615//===----------------------------------------------------------------------===//
616/// ASTViewer - AST Visualization
617
Ted Kremenek80de08f2007-09-19 21:29:43 +0000618namespace {
619 class ASTViewer : public ASTConsumer {
620 SourceManager *SM;
621 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000622 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000623 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000624 }
625
626 virtual void HandleTopLevelDecl(Decl *D) {
627 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000628 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000629
630 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000631 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000632 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000633 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000634 }
635 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000636 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
637 DeclPrinter().PrintObjCMethodDecl(MD);
638
639 if (MD->getBody()) {
640 llvm::cerr << '\n';
641 MD->getBody()->viewAST();
642 llvm::cerr << '\n';
643 }
644 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000645 }
646 };
647}
648
649ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
650
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000651//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000652/// DeclContextPrinter - Decl and DeclContext Visualization
653
654namespace {
655
656class DeclContextPrinter : public ASTConsumer {
657 llvm::raw_ostream& Out;
658public:
659 DeclContextPrinter() : Out(llvm::errs()) {}
660
661 void HandleTranslationUnit(TranslationUnit& TU) {
662 TranslationUnitDecl* TUD = TU.getContext().getTranslationUnitDecl();
663 PrintDeclContext(TUD, 4);
664 }
665
666 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
667};
668
669void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
670 unsigned Indentation) {
671 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000672 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000673 case Decl::TranslationUnit:
674 Out << "[translation unit] " << DC;
675 break;
676 case Decl::Namespace: {
677 Out << "[namespace] ";
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000678 const NamespaceDecl* ND = cast<NamespaceDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000679 Out << ND->getNameAsString();
680 break;
681 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000682 case Decl::Enum: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000683 const EnumDecl* ED = cast<EnumDecl>(DC);
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000684 if (ED->isDefinition())
685 Out << "[enum] ";
686 else
687 Out << "<enum> ";
688 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000689 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000690 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000691 case Decl::Record: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000692 const RecordDecl* RD = cast<RecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000693 if (RD->isDefinition())
694 Out << "[struct] ";
695 else
696 Out << "<struct> ";
697 Out << RD->getNameAsString();
698 break;
699 }
700 case Decl::CXXRecord: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000701 const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000702 if (RD->isDefinition())
703 Out << "[class] ";
704 else
705 Out << "<class> ";
706 Out << RD->getNameAsString() << " " << DC;
707 break;
708 }
709 case Decl::ObjCMethod:
710 Out << "[objc method]";
711 break;
712 case Decl::ObjCInterface:
713 Out << "[objc interface]";
714 break;
715 case Decl::ObjCCategory:
716 Out << "[objc category]";
717 break;
718 case Decl::ObjCProtocol:
719 Out << "[objc protocol]";
720 break;
721 case Decl::ObjCImplementation:
722 Out << "[objc implementation]";
723 break;
724 case Decl::ObjCCategoryImpl:
725 Out << "[objc categoryimpl]";
726 break;
727 case Decl::LinkageSpec:
728 Out << "[linkage spec]";
729 break;
730 case Decl::Block:
731 Out << "[block]";
732 break;
733 case Decl::Function: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000734 const FunctionDecl* FD = cast<FunctionDecl>(DC);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000735 if (FD->isThisDeclarationADefinition())
736 Out << "[function] ";
737 else
738 Out << "<function> ";
739 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000740 // Print the parameters.
741 Out << "(";
742 bool PrintComma = false;
743 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
744 E = FD->param_end(); I != E; ++I) {
745 if (PrintComma)
746 Out << ", ";
747 else
748 PrintComma = true;
749 Out << (*I)->getNameAsString();
750 }
751 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000752 break;
753 }
754 case Decl::CXXMethod: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000755 const CXXMethodDecl* D = cast<CXXMethodDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000756 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000757 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000758 else if (D->isImplicit())
759 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000760 else
761 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000762 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000763 // Print the parameters.
764 Out << "(";
765 bool PrintComma = false;
766 for (FunctionDecl::param_const_iterator I = D->param_begin(),
767 E = D->param_end(); I != E; ++I) {
768 if (PrintComma)
769 Out << ", ";
770 else
771 PrintComma = true;
772 Out << (*I)->getNameAsString();
773 }
774 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000775
776 // Check the semantic DeclContext.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000777 const DeclContext* SemaDC = D->getDeclContext();
778 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000779 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000780 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000781
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000782 break;
783 }
784 case Decl::CXXConstructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000785 const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000786 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000787 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000788 else if (D->isImplicit())
789 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000790 else
791 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000792 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000793 // Print the parameters.
794 Out << "(";
795 bool PrintComma = false;
796 for (FunctionDecl::param_const_iterator I = D->param_begin(),
797 E = D->param_end(); I != E; ++I) {
798 if (PrintComma)
799 Out << ", ";
800 else
801 PrintComma = true;
802 Out << (*I)->getNameAsString();
803 }
804 Out << ")";
805
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000806 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000807 const DeclContext* SemaDC = D->getDeclContext();
808 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000809 if (SemaDC != LexicalDC)
810 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000811 break;
812 }
813 case Decl::CXXDestructor: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000814 const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000815 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000816 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000817 else if (D->isImplicit())
818 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000819 else
820 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000821 Out << D->getNameAsString();
822 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000823 const DeclContext* SemaDC = D->getDeclContext();
824 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000825 if (SemaDC != LexicalDC)
826 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000827 break;
828 }
829 case Decl::CXXConversion: {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000830 const CXXConversionDecl* D = cast<CXXConversionDecl>(DC);
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000831 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000832 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000833 else if (D->isImplicit())
834 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000835 else
836 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000837 Out << D->getNameAsString();
838 // Check the semantic DC.
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000839 const DeclContext* SemaDC = D->getDeclContext();
840 const DeclContext* LexicalDC = D->getLexicalDeclContext();
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000841 if (SemaDC != LexicalDC)
842 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000843 break;
844 }
845
846 default:
847 assert(0 && "a decl that inherits DeclContext isn't handled");
848 }
849
850 Out << "\n";
851
852 // Print decls in the DeclContext.
853 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
854 I != E; ++I) {
855 for (unsigned i = 0; i < Indentation; ++i)
Mike Stump071e4da2009-02-10 20:16:46 +0000856 Out << " ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000857
858 Decl::Kind DK = I->getKind();
859 switch (DK) {
860 case Decl::Namespace:
861 case Decl::Enum:
862 case Decl::Record:
863 case Decl::CXXRecord:
864 case Decl::ObjCMethod:
865 case Decl::ObjCInterface:
866 case Decl::ObjCCategory:
867 case Decl::ObjCProtocol:
868 case Decl::ObjCImplementation:
869 case Decl::ObjCCategoryImpl:
870 case Decl::LinkageSpec:
871 case Decl::Block:
872 case Decl::Function:
873 case Decl::CXXMethod:
874 case Decl::CXXConstructor:
875 case Decl::CXXDestructor:
876 case Decl::CXXConversion:
877 {
Argyrios Kyrtzidis7ad5bf32009-02-16 14:29:59 +0000878 DeclContext* DC = cast<DeclContext>(*I);
Mike Stump071e4da2009-02-10 20:16:46 +0000879 PrintDeclContext(DC, Indentation+2);
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000880 break;
881 }
882 case Decl::Field: {
883 FieldDecl* FD = cast<FieldDecl>(*I);
884 Out << "<field> " << FD->getNameAsString() << "\n";
885 break;
886 }
887 case Decl::Typedef: {
888 TypedefDecl* TD = cast<TypedefDecl>(*I);
889 Out << "<typedef> " << TD->getNameAsString() << "\n";
890 break;
891 }
892 case Decl::EnumConstant: {
893 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
894 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
895 break;
896 }
897 case Decl::Var: {
898 VarDecl* VD = cast<VarDecl>(*I);
899 Out << "<var> " << VD->getNameAsString() << "\n";
900 break;
901 }
902 case Decl::ImplicitParam: {
903 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
904 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
905 break;
906 }
907 case Decl::CXXClassVar: {
908 CXXClassVarDecl* CVD = cast<CXXClassVarDecl>(*I);
909 Out << "<static member var> " << CVD->getNameAsString() << "\n";
910 break;
911 }
912 case Decl::ParmVar: {
913 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
914 Out << "<parameter> " << PVD->getNameAsString() << "\n";
915 break;
916 }
917 case Decl::ObjCProperty: {
918 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
919 Out << "<objc property> " << OPD->getNameAsString() << "\n";
920 break;
921 }
922 default:
923 fprintf(stderr, "DeclKind: %d\n", DK);
924 assert(0 && "decl unhandled");
925 }
926 }
927}
928
929}
930
931ASTConsumer *clang::CreateDeclContextPrinter() {
932 return new DeclContextPrinter();
933}
934
935//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000936/// InheritanceViewer - C++ Inheritance Visualization
937
938namespace {
939class InheritanceViewer : public ASTConsumer {
940 const std::string clsname;
941public:
942 InheritanceViewer(const std::string& cname) : clsname(cname) {}
943
944 void HandleTranslationUnit(TranslationUnit& TU) {
945 ASTContext& C = TU.getContext();
946 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
947 if (CXXRecordType *T = dyn_cast<CXXRecordType>(*I)) {
948 CXXRecordDecl* D = T->getDecl();
949 // FIXME: This lookup needs to be generalized to handle namespaces and
950 // (when we support them) templates.
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000951 if (D->getNameAsString() == clsname) {
Douglas Gregor1f812302008-10-24 19:53:54 +0000952 D->viewInheritance(C);
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000953 }
954 }
955 }
956};
957}
958
959ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
960 return new InheritanceViewer(clsname);
961}
962
963//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000964// AST Serializer
965
966namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000967
968class ASTSerializer : public ASTConsumer {
969protected:
Nico Weberdae86962008-08-09 18:32:11 +0000970 Diagnostic& Diags;
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000971
Ted Kremenekf06c9282007-12-19 23:49:37 +0000972public:
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000973 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000974};
Eli Friedmand8a65c12008-06-09 20:02:51 +0000975
Ted Kremenekf06c9282007-12-19 23:49:37 +0000976class SingleFileSerializer : public ASTSerializer {
977 const llvm::sys::Path FName;
978public:
Nico Weberdae86962008-08-09 18:32:11 +0000979 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
980 : ASTSerializer(diags), FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000981
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000982 virtual void HandleTranslationUnit(TranslationUnit& TU) {
Nico Weberdae86962008-08-09 18:32:11 +0000983 if (Diags.hasErrorOccurred())
984 return;
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000985 EmitASTBitcodeFile(&TU, FName);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000986 }
987};
988
989class BuildSerializer : public ASTSerializer {
990 llvm::sys::Path EmitDir;
991public:
Nico Weberdae86962008-08-09 18:32:11 +0000992 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
993 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000994
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000995 virtual void HandleTranslationUnit(TranslationUnit& TU) {
996 if (Diags.hasErrorOccurred())
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000997 return;
998
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000999 SourceManager& SourceMgr = TU.getContext().getSourceManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +00001000 FileID ID = SourceMgr.getMainFileID();
1001 assert(!ID.isInvalid() && "MainFileID not set!");
Ted Kremenek54117722007-12-20 00:34:58 +00001002 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
Chris Lattner2b2453a2009-01-17 06:22:33 +00001003 assert(FE && "No FileEntry for main file.");
Ted Kremenek54117722007-12-20 00:34:58 +00001004
1005 // FIXME: This is not portable to Windows.
1006 // FIXME: This logic should probably be moved elsewhere later.
1007
Ted Kremenekee533642007-12-20 19:47:16 +00001008 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +00001009
1010 std::vector<char> buf;
1011 buf.reserve(strlen(FE->getName())+100);
1012
1013 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +00001014 FName.appendComponent(&buf[0]);
1015 FName.createDirectoryOnDisk(true);
1016 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +00001017 assert (false && "Could not create 'device' serialization directory.");
1018 return;
1019 }
Ted Kremenekee533642007-12-20 19:47:16 +00001020
Ted Kremenek54117722007-12-20 00:34:58 +00001021 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +00001022 FName.appendComponent(&buf[0]);
Nico Weberbe1eb5c2008-08-10 19:20:05 +00001023 EmitASTBitcodeFile(&TU, FName);
Ted Kremenek54117722007-12-20 00:34:58 +00001024
Ted Kremenekee533642007-12-20 19:47:16 +00001025 // Now emit the sources.
1026
Ted Kremenek54117722007-12-20 00:34:58 +00001027 }
Ted Kremenekf06c9282007-12-19 23:49:37 +00001028};
1029
1030
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001031} // end anonymous namespace
1032
1033
Ted Kremenekfdfc1982007-12-19 22:24:34 +00001034ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +00001035 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001036 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +00001037
Ted Kremenekf06c9282007-12-19 23:49:37 +00001038 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +00001039 if (InFile == "-") {
1040 llvm::cerr <<
1041 "error: Cannot use --serialize with -o for source read from STDIN.\n";
1042 return NULL;
1043 }
1044
Ted Kremenekf06c9282007-12-19 23:49:37 +00001045 // The user specified an AST-emission directory. Determine if the path
1046 // is absolute.
1047 llvm::sys::Path EmitDir(OutputFile);
1048
1049 if (!EmitDir.isAbsolute()) {
1050 llvm::cerr <<
1051 "error: Output directory for --serialize must be an absolute path.\n";
1052
1053 return NULL;
1054 }
1055
1056 // Create the directory if it does not exist.
1057 EmitDir.createDirectoryOnDisk(true);
1058 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
1059 llvm::cerr <<
1060 "error: Could not create output directory for --serialize.\n";
1061
1062 return NULL;
1063 }
1064
Ted Kremenek54117722007-12-20 00:34:58 +00001065 // FIXME: We should probably only allow using BuildSerializer when
1066 // the ASTs come from parsed source files, and not from .ast files.
Nico Weberdae86962008-08-09 18:32:11 +00001067 return new BuildSerializer(EmitDir, Diags);
Ted Kremenekf06c9282007-12-19 23:49:37 +00001068 }
1069
1070 // The user did not specify an output directory for serialized ASTs.
1071 // Serialize the translation to a single file whose name is the same
1072 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +00001073
Ted Kremenekf06c9282007-12-19 23:49:37 +00001074 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +00001075 FName.appendSuffix("ast");
Nico Weberdae86962008-08-09 18:32:11 +00001076 return new SingleFileSerializer(FName, Diags);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +00001077}