blob: 9487a05072937890a68996a103ec0cbc378d96b5 [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;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000037
Ted Kremeneka95d3752008-09-13 05:16:45 +000038 DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()) {}
39 DeclPrinter() : Out(llvm::errs()) {}
40 virtual ~DeclPrinter();
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000041
Chris Lattneref5a85d2008-01-02 21:04:16 +000042 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000043 void PrintFunctionDeclStart(FunctionDecl *FD);
44 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000045 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000046 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
47 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
48 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
49 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
50 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
51 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
52 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +000053 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +000054 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000055 };
56} // end anonymous namespace
57
Ted Kremeneka95d3752008-09-13 05:16:45 +000058DeclPrinter::~DeclPrinter() {
59 Out.flush();
60}
61
Chris Lattneref5a85d2008-01-02 21:04:16 +000062void DeclPrinter:: PrintDecl(Decl *D) {
63 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
64 PrintFunctionDeclStart(FD);
65
66 if (FD->getBody()) {
67 Out << ' ';
68 FD->getBody()->printPretty(Out);
69 Out << '\n';
70 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000071 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000072 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000073 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000074 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
75 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
77 PrintObjCInterfaceDecl(OID);
78 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
79 PrintObjCProtocolDecl(PID);
80 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000081 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000082 Out << "@protocol ";
83 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000084 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000085 if (i) Out << ", ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000086 Out << D->getNameAsString();
Chris Lattneref5a85d2008-01-02 21:04:16 +000087 }
88 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000089 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000090 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000091 PrintObjCImplementationDecl(OID);
92 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000093 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000094 PrintObjCCategoryImplDecl(OID);
95 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000096 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 PrintObjCCategoryDecl(OID);
98 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000099 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000100 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +0000101 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
102 Out << "@class ";
103 ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls();
104 for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) {
105 const ObjCInterfaceDecl *D = ForwardDecls[i];
106 if (i) Out << ", ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000107 Out << D->getNameAsString();
Chris Lattner23a0e452008-06-21 21:40:20 +0000108 }
109 Out << ";\n";
Douglas Gregor45579f52008-12-17 02:04:30 +0000110 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
111 Out << "enum " << ED->getNameAsString() << " {\n";
112 for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(),
113 EEnd = ED->enumerator_end();
114 E != EEnd; ++E)
115 Out << " " << (*E)->getNameAsString() << ",\n";
116 Out << "};\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000117 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000118 Out << "Read top-level tag decl: '" << TD->getNameAsString() << "'\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000119 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
120 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000121 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
122 Out << "asm(";
123 AD->getAsmString()->printPretty(Out);
124 Out << ")\n";
Douglas Gregor074149e2009-01-05 19:45:36 +0000125 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
126 Out << "Read top-level variable decl: '" << SD->getNameAsString() << "'\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000127 } else {
128 assert(0 && "Unknown decl type!");
129 }
130}
131
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000132void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 bool HasBody = FD->getBody();
134
Ted Kremenekea75c552007-11-28 21:32:21 +0000135 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000136
137 switch (FD->getStorageClass()) {
138 default: assert(0 && "Unknown storage class");
139 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000140 case FunctionDecl::Extern: Out << "extern "; break;
141 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000142 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000143 }
144
145 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000146 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000147
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000148 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000149 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000150
Chris Lattner0d6ca112007-12-03 21:43:25 +0000151 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 Proto += "(";
153 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
154 if (i) Proto += ", ";
155 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000156 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000157
158 FT->getArgType(i).getAsStringInternal(ParamStr);
159 Proto += ParamStr;
160 }
161
162 if (FT->isVariadic()) {
163 if (FD->getNumParams()) Proto += ", ";
164 Proto += "...";
165 }
166 Proto += ")";
167 } else {
168 assert(isa<FunctionTypeNoProto>(AFT));
169 Proto += "()";
170 }
171
172 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000173 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000174
Chris Lattner6000dac2007-08-08 22:51:59 +0000175 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000176 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000177 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000178}
179
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000180void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000181 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000183 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000184}
185
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000186void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
187 const char *l;
188 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
189 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000190 else {
191 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
192 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000193 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000194 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000195
196 Out << "extern \"" << l << "\" ";
197 if (LS->hasBraces())
198 Out << "{\n";
199
Douglas Gregor074149e2009-01-05 19:45:36 +0000200 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(),
201 DEnd = LS->decls_end();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000202 D != DEnd; ++D)
203 PrintDecl(*D);
204
205 if (LS->hasBraces())
206 Out << "}";
207 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000208}
209
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000210void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000211 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000212 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000213 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000214 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000215 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000216 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000217
Chris Lattner077bf5e2008-11-24 03:33:13 +0000218 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000219 std::string::size_type pos, lastPos = 0;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000220 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000221 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000222 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000223 pos = name.find_first_of(":", lastPos);
224 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000225 Out << ":(" << PDecl->getType().getAsString() << ")"
226 << PDecl->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000227 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000228 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000229
230 if (OMD->getNumParams() == 0)
231 Out << " " << name;
232
233 if (OMD->isVariadic())
234 Out << ", ...";
235
236 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000237}
238
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000239void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000240 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000241 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000242
243 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000244 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000245 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000246 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000247
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000248 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000249 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000250 ObjCMethodDecl *OMD = *I;
251 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000252 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000253 Out << ' ';
254 OMD->getBody()->printPretty(Out);
255 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000256 }
257 }
258
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000259 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000260 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000261 ObjCMethodDecl *OMD = *I;
262 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000263 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000264 Out << ' ';
265 OMD->getBody()->printPretty(Out);
266 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000267 }
268 }
269
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000270 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
271 E = OID->propimpl_end(); I != E; ++I)
272 PrintObjCPropertyImplDecl(*I);
273
Ted Kremenekea75c552007-11-28 21:32:21 +0000274 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000275}
276
277
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000278void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000279 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000280 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000281
282 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000283 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000284 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000285 Out << "@interface " << I;
286
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000287 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000288 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
289 if (!Protocols.empty()) {
290 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
291 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000292 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000293 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000294
Chris Lattner3db6cae2008-07-21 18:19:38 +0000295 if (!Protocols.empty())
296 Out << ">";
297 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000298
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000299 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000300 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000301 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000302 E = OID->ivar_end(); I != E; ++I) {
303 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000304 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000305 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000306 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000307 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000308
Steve Naroff09c47192009-01-09 15:36:25 +0000309 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(),
310 E = OID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000311 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000312 bool eol_needed = false;
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000313 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
314 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000315 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000316
317 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
318 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000319 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000320
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000321 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000322 // FIXME: implement the rest...
323}
324
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000325void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000326 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000327
Steve Naroff09c47192009-01-09 15:36:25 +0000328 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(),
329 E = PID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000330 PrintObjCPropertyDecl(*I);
331 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000332 // FIXME: implement the rest...
333}
334
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000335void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000336 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000337 << PID->getClassInterface()->getNameAsString()
338 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000339 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
340 E = PID->propimpl_end(); I != E; ++I)
341 PrintObjCPropertyImplDecl(*I);
342 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000343 // FIXME: implement the rest...
344}
345
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000346void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000347 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000348 << PID->getClassInterface()->getNameAsString()
349 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000350 // Output property declarations.
Steve Naroff09c47192009-01-09 15:36:25 +0000351 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(),
352 E = PID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000353 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000354 Out << "@end\n";
355
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000356 // FIXME: implement the rest...
357}
358
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000359void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000360 Out << "@compatibility_alias " << AID->getNameAsString()
361 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000362}
363
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000364/// PrintObjCPropertyDecl - print a property declaration.
365///
366void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000367 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
368 Out << "@required\n";
369 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
370 Out << "@optional\n";
371
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000372 Out << "@property";
373 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
374 bool first = true;
375 Out << " (";
376 if (PDecl->getPropertyAttributes() &
377 ObjCPropertyDecl::OBJC_PR_readonly) {
378 Out << (first ? ' ' : ',') << "readonly";
379 first = false;
380 }
381
382 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
383 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000384 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000385 first = false;
386 }
387 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
388 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000389 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000390 first = false;
391 }
392
393 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
394 Out << (first ? ' ' : ',') << "assign";
395 first = false;
396 }
397
398 if (PDecl->getPropertyAttributes() &
399 ObjCPropertyDecl::OBJC_PR_readwrite) {
400 Out << (first ? ' ' : ',') << "readwrite";
401 first = false;
402 }
403
404 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
405 Out << (first ? ' ' : ',') << "retain";
406 first = false;
407 }
408
409 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
410 Out << (first ? ' ' : ',') << "copy";
411 first = false;
412 }
413
414 if (PDecl->getPropertyAttributes() &
415 ObjCPropertyDecl::OBJC_PR_nonatomic) {
416 Out << (first ? ' ' : ',') << "nonatomic";
417 first = false;
418 }
419 Out << " )";
420 }
421 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000422 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000423
424 Out << ";\n";
425}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000426
427/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
428/// declaration syntax.
429///
430void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000431 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000432 Out << "\n@synthesize ";
433 else
434 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000435 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000436 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000437 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000438 Out << ";\n";
439}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000440//===----------------------------------------------------------------------===//
441/// ASTPrinter - Pretty-printer of ASTs
442
Chris Lattner3d4997d2007-09-15 23:02:28 +0000443namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000444 class ASTPrinter : public ASTConsumer, public DeclPrinter {
445 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000446 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000447
Chris Lattner3d4997d2007-09-15 23:02:28 +0000448 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000449 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000451 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000452}
Chris Lattner6000dac2007-08-08 22:51:59 +0000453
Ted Kremeneka95d3752008-09-13 05:16:45 +0000454ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000455 return new ASTPrinter(out);
456}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000457
458//===----------------------------------------------------------------------===//
459/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000460
461namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000462 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000463 SourceManager *SM;
464 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000465 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000466
Ted Kremenek95041a22007-12-19 22:51:13 +0000467 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000468 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000469 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000470
471 virtual void HandleTopLevelDecl(Decl *D) {
472 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
473 PrintFunctionDeclStart(FD);
474
475 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000476 Out << '\n';
477 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000478 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000479 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000480 }
481 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
482 PrintTypeDefDecl(TD);
483 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000484 Out << "Read top-level variable decl: '" << SD->getNameAsString()
485 << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000486 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000487 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000488 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000489 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000490 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000491 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000492 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000493 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000494 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000495 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000496 } else if (isa<FileScopeAsmDecl>(D)) {
497 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000498 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000499 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
Ted Kremenek63bbe532008-03-14 17:31:00 +0000500 << "'\n";
Steve Naroff1a2b90d2008-05-23 18:50:58 +0000501 if (MD->getBody()) {
502 // FIXME: convert dumper to use std::ostream?
503 MD->getBody()->dumpAll(*SM);
504 Out << '\n';
505 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000506 } else if (isa<ObjCImplementationDecl>(D)) {
507 Out << "Read objc implementation decl\n";
Daniel Dunbar539ced12008-10-05 00:31:15 +0000508 } else if (isa<ObjCCategoryImplDecl>(D)) {
509 Out << "Read objc category implementation decl\n";
Eli Friedmanf595eb02008-12-16 22:14:15 +0000510 } else if (isa<LinkageSpecDecl>(D)) {
511 Out << "Read linkage spec decl\n";
512 } else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000513 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000514 }
515 }
516 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000517}
518
Chris Lattner3d4997d2007-09-15 23:02:28 +0000519ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
520
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000521//===----------------------------------------------------------------------===//
522/// ASTViewer - AST Visualization
523
Ted Kremenek80de08f2007-09-19 21:29:43 +0000524namespace {
525 class ASTViewer : public ASTConsumer {
526 SourceManager *SM;
527 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000528 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000529 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000530 }
531
532 virtual void HandleTopLevelDecl(Decl *D) {
533 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000534 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000535
536 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000537 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000538 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000539 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000540 }
541 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000542 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
543 DeclPrinter().PrintObjCMethodDecl(MD);
544
545 if (MD->getBody()) {
546 llvm::cerr << '\n';
547 MD->getBody()->viewAST();
548 llvm::cerr << '\n';
549 }
550 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000551 }
552 };
553}
554
555ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
556
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000557//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000558/// DeclContextPrinter - Decl and DeclContext Visualization
559
560namespace {
561
562class DeclContextPrinter : public ASTConsumer {
563 llvm::raw_ostream& Out;
564public:
565 DeclContextPrinter() : Out(llvm::errs()) {}
566
567 void HandleTranslationUnit(TranslationUnit& TU) {
568 TranslationUnitDecl* TUD = TU.getContext().getTranslationUnitDecl();
569 PrintDeclContext(TUD, 4);
570 }
571
572 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
573};
574
575void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
576 unsigned Indentation) {
577 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000578 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000579 case Decl::TranslationUnit:
580 Out << "[translation unit] " << DC;
581 break;
582 case Decl::Namespace: {
583 Out << "[namespace] ";
584 NamespaceDecl* ND = NamespaceDecl::castFromDeclContext(DC);
585 Out << ND->getNameAsString();
586 break;
587 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000588 case Decl::Enum: {
589 EnumDecl* ED = EnumDecl::castFromDeclContext(DC);
590 if (ED->isDefinition())
591 Out << "[enum] ";
592 else
593 Out << "<enum> ";
594 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000595 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000596 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000597 case Decl::Record: {
598 RecordDecl* RD = RecordDecl::castFromDeclContext(DC);
599 if (RD->isDefinition())
600 Out << "[struct] ";
601 else
602 Out << "<struct> ";
603 Out << RD->getNameAsString();
604 break;
605 }
606 case Decl::CXXRecord: {
607 CXXRecordDecl* RD = CXXRecordDecl::castFromDeclContext(DC);
608 if (RD->isDefinition())
609 Out << "[class] ";
610 else
611 Out << "<class> ";
612 Out << RD->getNameAsString() << " " << DC;
613 break;
614 }
615 case Decl::ObjCMethod:
616 Out << "[objc method]";
617 break;
618 case Decl::ObjCInterface:
619 Out << "[objc interface]";
620 break;
621 case Decl::ObjCCategory:
622 Out << "[objc category]";
623 break;
624 case Decl::ObjCProtocol:
625 Out << "[objc protocol]";
626 break;
627 case Decl::ObjCImplementation:
628 Out << "[objc implementation]";
629 break;
630 case Decl::ObjCCategoryImpl:
631 Out << "[objc categoryimpl]";
632 break;
633 case Decl::LinkageSpec:
634 Out << "[linkage spec]";
635 break;
636 case Decl::Block:
637 Out << "[block]";
638 break;
639 case Decl::Function: {
640 FunctionDecl* FD = FunctionDecl::castFromDeclContext(DC);
641 if (FD->isThisDeclarationADefinition())
642 Out << "[function] ";
643 else
644 Out << "<function> ";
645 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000646 // Print the parameters.
647 Out << "(";
648 bool PrintComma = false;
649 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
650 E = FD->param_end(); I != E; ++I) {
651 if (PrintComma)
652 Out << ", ";
653 else
654 PrintComma = true;
655 Out << (*I)->getNameAsString();
656 }
657 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000658 break;
659 }
660 case Decl::CXXMethod: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000661 CXXMethodDecl* D = CXXMethodDecl::castFromDeclContext(DC);
662 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000663 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000664 else if (D->isImplicit())
665 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000666 else
667 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000668 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000669 // Print the parameters.
670 Out << "(";
671 bool PrintComma = false;
672 for (FunctionDecl::param_const_iterator I = D->param_begin(),
673 E = D->param_end(); I != E; ++I) {
674 if (PrintComma)
675 Out << ", ";
676 else
677 PrintComma = true;
678 Out << (*I)->getNameAsString();
679 }
680 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000681
682 // Check the semantic DeclContext.
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000683 DeclContext* SemaDC = D->getDeclContext();
684 DeclContext* LexicalDC = D->getLexicalDeclContext();
685 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000686 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000687
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000688 break;
689 }
690 case Decl::CXXConstructor: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000691 CXXConstructorDecl* D = CXXConstructorDecl::castFromDeclContext(DC);
692 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000693 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000694 else if (D->isImplicit())
695 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000696 else
697 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000698 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000699 // Print the parameters.
700 Out << "(";
701 bool PrintComma = false;
702 for (FunctionDecl::param_const_iterator I = D->param_begin(),
703 E = D->param_end(); I != E; ++I) {
704 if (PrintComma)
705 Out << ", ";
706 else
707 PrintComma = true;
708 Out << (*I)->getNameAsString();
709 }
710 Out << ")";
711
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000712 // Check the semantic DC.
713 DeclContext* SemaDC = D->getDeclContext();
714 DeclContext* LexicalDC = D->getLexicalDeclContext();
715 if (SemaDC != LexicalDC)
716 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000717 break;
718 }
719 case Decl::CXXDestructor: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000720 CXXDestructorDecl* D = CXXDestructorDecl::castFromDeclContext(DC);
721 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000722 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000723 else if (D->isImplicit())
724 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000725 else
726 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000727 Out << D->getNameAsString();
728 // Check the semantic DC.
729 DeclContext* SemaDC = D->getDeclContext();
730 DeclContext* LexicalDC = D->getLexicalDeclContext();
731 if (SemaDC != LexicalDC)
732 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000733 break;
734 }
735 case Decl::CXXConversion: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000736 CXXConversionDecl* D = CXXConversionDecl::castFromDeclContext(DC);
737 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000738 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000739 else if (D->isImplicit())
740 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000741 else
742 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000743 Out << D->getNameAsString();
744 // Check the semantic DC.
745 DeclContext* SemaDC = D->getDeclContext();
746 DeclContext* LexicalDC = D->getLexicalDeclContext();
747 if (SemaDC != LexicalDC)
748 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000749 break;
750 }
751
752 default:
753 assert(0 && "a decl that inherits DeclContext isn't handled");
754 }
755
756 Out << "\n";
757
758 // Print decls in the DeclContext.
759 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
760 I != E; ++I) {
761 for (unsigned i = 0; i < Indentation; ++i)
762 Out << " ";
763
764 Decl::Kind DK = I->getKind();
765 switch (DK) {
766 case Decl::Namespace:
767 case Decl::Enum:
768 case Decl::Record:
769 case Decl::CXXRecord:
770 case Decl::ObjCMethod:
771 case Decl::ObjCInterface:
772 case Decl::ObjCCategory:
773 case Decl::ObjCProtocol:
774 case Decl::ObjCImplementation:
775 case Decl::ObjCCategoryImpl:
776 case Decl::LinkageSpec:
777 case Decl::Block:
778 case Decl::Function:
779 case Decl::CXXMethod:
780 case Decl::CXXConstructor:
781 case Decl::CXXDestructor:
782 case Decl::CXXConversion:
783 {
784 DeclContext* DC = Decl::castToDeclContext(*I);
785 PrintDeclContext(DC, Indentation+4);
786 break;
787 }
788 case Decl::Field: {
789 FieldDecl* FD = cast<FieldDecl>(*I);
790 Out << "<field> " << FD->getNameAsString() << "\n";
791 break;
792 }
793 case Decl::Typedef: {
794 TypedefDecl* TD = cast<TypedefDecl>(*I);
795 Out << "<typedef> " << TD->getNameAsString() << "\n";
796 break;
797 }
798 case Decl::EnumConstant: {
799 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
800 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
801 break;
802 }
803 case Decl::Var: {
804 VarDecl* VD = cast<VarDecl>(*I);
805 Out << "<var> " << VD->getNameAsString() << "\n";
806 break;
807 }
808 case Decl::ImplicitParam: {
809 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
810 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
811 break;
812 }
813 case Decl::CXXClassVar: {
814 CXXClassVarDecl* CVD = cast<CXXClassVarDecl>(*I);
815 Out << "<static member var> " << CVD->getNameAsString() << "\n";
816 break;
817 }
818 case Decl::ParmVar: {
819 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
820 Out << "<parameter> " << PVD->getNameAsString() << "\n";
821 break;
822 }
823 case Decl::ObjCProperty: {
824 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
825 Out << "<objc property> " << OPD->getNameAsString() << "\n";
826 break;
827 }
828 default:
829 fprintf(stderr, "DeclKind: %d\n", DK);
830 assert(0 && "decl unhandled");
831 }
832 }
833}
834
835}
836
837ASTConsumer *clang::CreateDeclContextPrinter() {
838 return new DeclContextPrinter();
839}
840
841//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000842/// InheritanceViewer - C++ Inheritance Visualization
843
844namespace {
845class InheritanceViewer : public ASTConsumer {
846 const std::string clsname;
847public:
848 InheritanceViewer(const std::string& cname) : clsname(cname) {}
849
850 void HandleTranslationUnit(TranslationUnit& TU) {
851 ASTContext& C = TU.getContext();
852 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
853 if (CXXRecordType *T = dyn_cast<CXXRecordType>(*I)) {
854 CXXRecordDecl* D = T->getDecl();
855 // FIXME: This lookup needs to be generalized to handle namespaces and
856 // (when we support them) templates.
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000857 if (D->getNameAsString() == clsname) {
Douglas Gregor1f812302008-10-24 19:53:54 +0000858 D->viewInheritance(C);
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000859 }
860 }
861 }
862};
863}
864
865ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
866 return new InheritanceViewer(clsname);
867}
868
869//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000870// AST Serializer
871
872namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000873
874class ASTSerializer : public ASTConsumer {
875protected:
Nico Weberdae86962008-08-09 18:32:11 +0000876 Diagnostic& Diags;
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000877
Ted Kremenekf06c9282007-12-19 23:49:37 +0000878public:
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000879 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000880};
Eli Friedmand8a65c12008-06-09 20:02:51 +0000881
Ted Kremenekf06c9282007-12-19 23:49:37 +0000882class SingleFileSerializer : public ASTSerializer {
883 const llvm::sys::Path FName;
884public:
Nico Weberdae86962008-08-09 18:32:11 +0000885 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
886 : ASTSerializer(diags), FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000887
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000888 virtual void HandleTranslationUnit(TranslationUnit& TU) {
Nico Weberdae86962008-08-09 18:32:11 +0000889 if (Diags.hasErrorOccurred())
890 return;
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000891 EmitASTBitcodeFile(&TU, FName);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000892 }
893};
894
895class BuildSerializer : public ASTSerializer {
896 llvm::sys::Path EmitDir;
897public:
Nico Weberdae86962008-08-09 18:32:11 +0000898 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
899 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000900
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000901 virtual void HandleTranslationUnit(TranslationUnit& TU) {
902 if (Diags.hasErrorOccurred())
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000903 return;
904
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000905 SourceManager& SourceMgr = TU.getContext().getSourceManager();
Ted Kremenek54117722007-12-20 00:34:58 +0000906 unsigned ID = SourceMgr.getMainFileID();
907 assert (ID && "MainFileID not set!");
908 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
909 assert (FE && "No FileEntry for main file.");
910
911 // FIXME: This is not portable to Windows.
912 // FIXME: This logic should probably be moved elsewhere later.
913
Ted Kremenekee533642007-12-20 19:47:16 +0000914 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000915
916 std::vector<char> buf;
917 buf.reserve(strlen(FE->getName())+100);
918
919 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000920 FName.appendComponent(&buf[0]);
921 FName.createDirectoryOnDisk(true);
922 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000923 assert (false && "Could not create 'device' serialization directory.");
924 return;
925 }
Ted Kremenekee533642007-12-20 19:47:16 +0000926
Ted Kremenek54117722007-12-20 00:34:58 +0000927 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000928 FName.appendComponent(&buf[0]);
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000929 EmitASTBitcodeFile(&TU, FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000930
Ted Kremenekee533642007-12-20 19:47:16 +0000931 // Now emit the sources.
932
Ted Kremenek54117722007-12-20 00:34:58 +0000933 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000934};
935
936
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000937} // end anonymous namespace
938
939
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000940ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000941 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000942 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000943
Ted Kremenekf06c9282007-12-19 23:49:37 +0000944 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000945 if (InFile == "-") {
946 llvm::cerr <<
947 "error: Cannot use --serialize with -o for source read from STDIN.\n";
948 return NULL;
949 }
950
Ted Kremenekf06c9282007-12-19 23:49:37 +0000951 // The user specified an AST-emission directory. Determine if the path
952 // is absolute.
953 llvm::sys::Path EmitDir(OutputFile);
954
955 if (!EmitDir.isAbsolute()) {
956 llvm::cerr <<
957 "error: Output directory for --serialize must be an absolute path.\n";
958
959 return NULL;
960 }
961
962 // Create the directory if it does not exist.
963 EmitDir.createDirectoryOnDisk(true);
964 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
965 llvm::cerr <<
966 "error: Could not create output directory for --serialize.\n";
967
968 return NULL;
969 }
970
Ted Kremenek54117722007-12-20 00:34:58 +0000971 // FIXME: We should probably only allow using BuildSerializer when
972 // the ASTs come from parsed source files, and not from .ast files.
Nico Weberdae86962008-08-09 18:32:11 +0000973 return new BuildSerializer(EmitDir, Diags);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000974 }
975
976 // The user did not specify an output directory for serialized ASTs.
977 // Serialize the translation to a single file whose name is the same
978 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000979
Ted Kremenekf06c9282007-12-19 23:49:37 +0000980 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000981 FName.appendSuffix("ast");
Nico Weberdae86962008-08-09 18:32:11 +0000982 return new SingleFileSerializer(FName, Diags);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000983}