blob: 01014cb72a45f34ef42d9ced77dff185e0f899a8 [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"
Zhongxing Xu81488392008-08-24 02:33:36 +000015#include "clang/Driver/HTMLDiagnostics.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 << ", ";
86 Out << D->getName();
87 }
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 << ", ";
107 Out << D->getName();
108 }
109 Out << ";\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000110 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
111 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
112 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
113 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000114 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
115 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000116 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
117 Out << "asm(";
118 AD->getAsmString()->printPretty(Out);
119 Out << ")\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000120 } else {
121 assert(0 && "Unknown decl type!");
122 }
123}
124
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000125void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 bool HasBody = FD->getBody();
127
Ted Kremenekea75c552007-11-28 21:32:21 +0000128 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000129
130 switch (FD->getStorageClass()) {
131 default: assert(0 && "Unknown storage class");
132 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000133 case FunctionDecl::Extern: Out << "extern "; break;
134 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000135 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000136 }
137
138 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000139 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000142 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000143
Chris Lattner0d6ca112007-12-03 21:43:25 +0000144 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 Proto += "(";
146 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
147 if (i) Proto += ", ";
148 std::string ParamStr;
149 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
150
151 FT->getArgType(i).getAsStringInternal(ParamStr);
152 Proto += ParamStr;
153 }
154
155 if (FT->isVariadic()) {
156 if (FD->getNumParams()) Proto += ", ";
157 Proto += "...";
158 }
159 Proto += ")";
160 } else {
161 assert(isa<FunctionTypeNoProto>(AFT));
162 Proto += "()";
163 }
164
165 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000166 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000167
Chris Lattner6000dac2007-08-08 22:51:59 +0000168 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000169 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000170 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000171}
172
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000173void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 std::string S = TD->getName();
175 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000176 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000177}
178
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000179void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
180 const char *l;
181 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
182 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000183 else {
184 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
185 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000186 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000187 }
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000188 Out << "extern \"" << l << "\" { ";
189 PrintDecl(LS->getDecl());
190 Out << "}\n";
191}
192
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000193void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000194 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000195 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000196 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000197 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000198 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000199 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000200
Chris Lattnerefe8a962008-08-22 06:59:15 +0000201 std::string name = OMD->getSelector().getName();
202 std::string::size_type pos, lastPos = 0;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000203 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000204 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000205 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000206 pos = name.find_first_of(":", lastPos);
207 Out << " " << name.substr(lastPos, pos - lastPos);
208 Out << ":(" << PDecl->getType().getAsString() << ")" << PDecl->getName();
209 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000210 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000211
212 if (OMD->getNumParams() == 0)
213 Out << " " << name;
214
215 if (OMD->isVariadic())
216 Out << ", ...";
217
218 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000219}
220
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000221void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000222 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000223 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000224
225 if (SID)
226 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000227 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000228 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000229
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000230 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000231 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000232 ObjCMethodDecl *OMD = *I;
233 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000234 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000235 Out << ' ';
236 OMD->getBody()->printPretty(Out);
237 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000238 }
239 }
240
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000241 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000242 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000243 ObjCMethodDecl *OMD = *I;
244 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000245 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000246 Out << ' ';
247 OMD->getBody()->printPretty(Out);
248 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000249 }
250 }
251
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000252 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
253 E = OID->propimpl_end(); I != E; ++I)
254 PrintObjCPropertyImplDecl(*I);
255
Ted Kremenekea75c552007-11-28 21:32:21 +0000256 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000257}
258
259
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000260void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000261 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000262 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000263
264 if (SID)
265 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000266 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000267 Out << "@interface " << I;
268
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000269 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000270 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
271 if (!Protocols.empty()) {
272 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
273 E = Protocols.end(); I != E; ++I)
274 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000275 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000276
Chris Lattner3db6cae2008-07-21 18:19:38 +0000277 if (!Protocols.empty())
278 Out << ">";
279 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000280
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000281 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000282 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000283 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000284 E = OID->ivar_end(); I != E; ++I) {
285 Out << '\t' << (*I)->getType().getAsString()
286 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000287 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000288 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000289 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000290
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000291 for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(),
292 E = OID->classprop_end(); I != E; ++I)
293 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000294 bool eol_needed = false;
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000295 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
296 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000297 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000298
299 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
300 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000301 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000302
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000303 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000304 // FIXME: implement the rest...
305}
306
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000307void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000308 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000309
310 for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(),
311 E = PID->classprop_end(); I != E; ++I)
312 PrintObjCPropertyDecl(*I);
313 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000314 // FIXME: implement the rest...
315}
316
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000317void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000318 Out << "@implementation "
319 << PID->getClassInterface()->getName()
320 << '(' << PID->getName() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000321 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
322 E = PID->propimpl_end(); I != E; ++I)
323 PrintObjCPropertyImplDecl(*I);
324 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000325 // FIXME: implement the rest...
326}
327
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000328void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000329 Out << "@interface "
330 << PID->getClassInterface()->getName()
331 << '(' << PID->getName() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000332 // Output property declarations.
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000333 for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(),
334 E = PID->classprop_end(); I != E; ++I)
335 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000336 Out << "@end\n";
337
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000338 // FIXME: implement the rest...
339}
340
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000341void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000342 Out << "@compatibility_alias " << AID->getName()
343 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000344}
345
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000346/// PrintObjCPropertyDecl - print a property declaration.
347///
348void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000349 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
350 Out << "@required\n";
351 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
352 Out << "@optional\n";
353
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000354 Out << "@property";
355 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
356 bool first = true;
357 Out << " (";
358 if (PDecl->getPropertyAttributes() &
359 ObjCPropertyDecl::OBJC_PR_readonly) {
360 Out << (first ? ' ' : ',') << "readonly";
361 first = false;
362 }
363
364 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
365 Out << (first ? ' ' : ',') << "getter = "
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000366 << PDecl->getGetterName().getName();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000367 first = false;
368 }
369 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
370 Out << (first ? ' ' : ',') << "setter = "
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000371 << PDecl->getSetterName().getName();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000372 first = false;
373 }
374
375 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
376 Out << (first ? ' ' : ',') << "assign";
377 first = false;
378 }
379
380 if (PDecl->getPropertyAttributes() &
381 ObjCPropertyDecl::OBJC_PR_readwrite) {
382 Out << (first ? ' ' : ',') << "readwrite";
383 first = false;
384 }
385
386 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
387 Out << (first ? ' ' : ',') << "retain";
388 first = false;
389 }
390
391 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
392 Out << (first ? ' ' : ',') << "copy";
393 first = false;
394 }
395
396 if (PDecl->getPropertyAttributes() &
397 ObjCPropertyDecl::OBJC_PR_nonatomic) {
398 Out << (first ? ' ' : ',') << "nonatomic";
399 first = false;
400 }
401 Out << " )";
402 }
403 Out << ' ' << PDecl->getType().getAsString()
404 << ' ' << PDecl->getName();
405
406 Out << ";\n";
407}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000408
409/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
410/// declaration syntax.
411///
412void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000413 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000414 Out << "\n@synthesize ";
415 else
416 Out << "\n@dynamic ";
417 Out << PID->getPropertyDecl()->getName();
418 if (PID->getPropertyIvarDecl())
419 Out << "=" << PID->getPropertyIvarDecl()->getName();
420 Out << ";\n";
421}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000422//===----------------------------------------------------------------------===//
423/// ASTPrinter - Pretty-printer of ASTs
424
Chris Lattner3d4997d2007-09-15 23:02:28 +0000425namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000426 class ASTPrinter : public ASTConsumer, public DeclPrinter {
427 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000428 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000429
Chris Lattner3d4997d2007-09-15 23:02:28 +0000430 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000431 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000433 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000434}
Chris Lattner6000dac2007-08-08 22:51:59 +0000435
Ted Kremeneka95d3752008-09-13 05:16:45 +0000436ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000437 return new ASTPrinter(out);
438}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000439
440//===----------------------------------------------------------------------===//
441/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000442
443namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000444 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000445 SourceManager *SM;
446 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000447 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000448
Ted Kremenek95041a22007-12-19 22:51:13 +0000449 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000450 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000451 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000452
453 virtual void HandleTopLevelDecl(Decl *D) {
454 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
455 PrintFunctionDeclStart(FD);
456
457 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000458 Out << '\n';
459 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000460 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000461 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000462 }
463 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
464 PrintTypeDefDecl(TD);
465 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000466 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000467 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000468 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000469 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000470 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000471 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000472 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000473 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000474 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000475 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000476 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000477 } else if (isa<FileScopeAsmDecl>(D)) {
478 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000479 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
480 Out << "Read objc method decl: '" << MD->getSelector().getName()
481 << "'\n";
Steve Naroff1a2b90d2008-05-23 18:50:58 +0000482 if (MD->getBody()) {
483 // FIXME: convert dumper to use std::ostream?
484 MD->getBody()->dumpAll(*SM);
485 Out << '\n';
486 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000487 } else if (isa<ObjCImplementationDecl>(D)) {
488 Out << "Read objc implementation decl\n";
Daniel Dunbar539ced12008-10-05 00:31:15 +0000489 } else if (isa<ObjCCategoryImplDecl>(D)) {
490 Out << "Read objc category implementation decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000491 }
492 else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000493 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000494 }
495 }
496 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000497}
498
Chris Lattner3d4997d2007-09-15 23:02:28 +0000499ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
500
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000501//===----------------------------------------------------------------------===//
502/// ASTViewer - AST Visualization
503
Ted Kremenek80de08f2007-09-19 21:29:43 +0000504namespace {
505 class ASTViewer : public ASTConsumer {
506 SourceManager *SM;
507 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000508 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000509 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000510 }
511
512 virtual void HandleTopLevelDecl(Decl *D) {
513 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000514 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000515
516 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000517 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000518 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000519 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000520 }
521 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000522 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
523 DeclPrinter().PrintObjCMethodDecl(MD);
524
525 if (MD->getBody()) {
526 llvm::cerr << '\n';
527 MD->getBody()->viewAST();
528 llvm::cerr << '\n';
529 }
530 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000531 }
532 };
533}
534
535ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
536
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000537//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000538/// InheritanceViewer - C++ Inheritance Visualization
539
540namespace {
541class InheritanceViewer : public ASTConsumer {
542 const std::string clsname;
543public:
544 InheritanceViewer(const std::string& cname) : clsname(cname) {}
545
546 void HandleTranslationUnit(TranslationUnit& TU) {
547 ASTContext& C = TU.getContext();
548 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
549 if (CXXRecordType *T = dyn_cast<CXXRecordType>(*I)) {
550 CXXRecordDecl* D = T->getDecl();
551 // FIXME: This lookup needs to be generalized to handle namespaces and
552 // (when we support them) templates.
553 if (D->getName() == clsname) {
Douglas Gregor1f812302008-10-24 19:53:54 +0000554 D->viewInheritance(C);
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000555 }
556 }
557 }
558};
559}
560
561ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
562 return new InheritanceViewer(clsname);
563}
564
565//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000566// AST Serializer
567
568namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000569
570class ASTSerializer : public ASTConsumer {
571protected:
Nico Weberdae86962008-08-09 18:32:11 +0000572 Diagnostic& Diags;
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000573
Ted Kremenekf06c9282007-12-19 23:49:37 +0000574public:
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000575 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000576};
Eli Friedmand8a65c12008-06-09 20:02:51 +0000577
Ted Kremenekf06c9282007-12-19 23:49:37 +0000578class SingleFileSerializer : public ASTSerializer {
579 const llvm::sys::Path FName;
580public:
Nico Weberdae86962008-08-09 18:32:11 +0000581 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
582 : ASTSerializer(diags), FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000583
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000584 virtual void HandleTranslationUnit(TranslationUnit& TU) {
Nico Weberdae86962008-08-09 18:32:11 +0000585 if (Diags.hasErrorOccurred())
586 return;
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000587 EmitASTBitcodeFile(&TU, FName);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000588 }
589};
590
591class BuildSerializer : public ASTSerializer {
592 llvm::sys::Path EmitDir;
593public:
Nico Weberdae86962008-08-09 18:32:11 +0000594 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
595 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000596
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000597 virtual void HandleTranslationUnit(TranslationUnit& TU) {
598 if (Diags.hasErrorOccurred())
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000599 return;
600
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000601 SourceManager& SourceMgr = TU.getContext().getSourceManager();
Ted Kremenek54117722007-12-20 00:34:58 +0000602 unsigned ID = SourceMgr.getMainFileID();
603 assert (ID && "MainFileID not set!");
604 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
605 assert (FE && "No FileEntry for main file.");
606
607 // FIXME: This is not portable to Windows.
608 // FIXME: This logic should probably be moved elsewhere later.
609
Ted Kremenekee533642007-12-20 19:47:16 +0000610 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000611
612 std::vector<char> buf;
613 buf.reserve(strlen(FE->getName())+100);
614
615 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000616 FName.appendComponent(&buf[0]);
617 FName.createDirectoryOnDisk(true);
618 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000619 assert (false && "Could not create 'device' serialization directory.");
620 return;
621 }
Ted Kremenekee533642007-12-20 19:47:16 +0000622
Ted Kremenek54117722007-12-20 00:34:58 +0000623 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000624 FName.appendComponent(&buf[0]);
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000625 EmitASTBitcodeFile(&TU, FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000626
Ted Kremenekee533642007-12-20 19:47:16 +0000627 // Now emit the sources.
628
Ted Kremenek54117722007-12-20 00:34:58 +0000629 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000630};
631
632
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000633} // end anonymous namespace
634
635
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000636ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000637 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000638 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000639
Ted Kremenekf06c9282007-12-19 23:49:37 +0000640 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000641 if (InFile == "-") {
642 llvm::cerr <<
643 "error: Cannot use --serialize with -o for source read from STDIN.\n";
644 return NULL;
645 }
646
Ted Kremenekf06c9282007-12-19 23:49:37 +0000647 // The user specified an AST-emission directory. Determine if the path
648 // is absolute.
649 llvm::sys::Path EmitDir(OutputFile);
650
651 if (!EmitDir.isAbsolute()) {
652 llvm::cerr <<
653 "error: Output directory for --serialize must be an absolute path.\n";
654
655 return NULL;
656 }
657
658 // Create the directory if it does not exist.
659 EmitDir.createDirectoryOnDisk(true);
660 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
661 llvm::cerr <<
662 "error: Could not create output directory for --serialize.\n";
663
664 return NULL;
665 }
666
Ted Kremenek54117722007-12-20 00:34:58 +0000667 // FIXME: We should probably only allow using BuildSerializer when
668 // the ASTs come from parsed source files, and not from .ast files.
Nico Weberdae86962008-08-09 18:32:11 +0000669 return new BuildSerializer(EmitDir, Diags);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000670 }
671
672 // The user did not specify an output directory for serialized ASTs.
673 // Serialize the translation to a single file whose name is the same
674 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000675
Ted Kremenekf06c9282007-12-19 23:49:37 +0000676 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000677 FName.appendSuffix("ast");
Nico Weberdae86962008-08-09 18:32:11 +0000678 return new SingleFileSerializer(FName, Diags);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000679}