blob: 9c74301a9b7d86aad14cab57af2640778adff9ee [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"
24#include "llvm/Bitcode/ReaderWriter.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000025#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000026#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000028#include "llvm/ADT/OwningPtr.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000029#include <fstream>
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000030
Chris Lattner6000dac2007-08-08 22:51:59 +000031using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000032
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000033//===----------------------------------------------------------------------===//
34/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000035
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000036namespace {
37 class DeclPrinter {
38 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +000039 llvm::raw_ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000040
Ted Kremeneka95d3752008-09-13 05:16:45 +000041 DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()) {}
42 DeclPrinter() : Out(llvm::errs()) {}
43 virtual ~DeclPrinter();
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000044
Chris Lattneref5a85d2008-01-02 21:04:16 +000045 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000046 void PrintFunctionDeclStart(FunctionDecl *FD);
47 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000048 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000049 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
50 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
51 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
52 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
53 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
54 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
55 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +000056 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +000057 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000058 };
59} // end anonymous namespace
60
Ted Kremeneka95d3752008-09-13 05:16:45 +000061DeclPrinter::~DeclPrinter() {
62 Out.flush();
63}
64
Chris Lattneref5a85d2008-01-02 21:04:16 +000065void DeclPrinter:: PrintDecl(Decl *D) {
66 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
67 PrintFunctionDeclStart(FD);
68
69 if (FD->getBody()) {
70 Out << ' ';
71 FD->getBody()->printPretty(Out);
72 Out << '\n';
73 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000074 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000075 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000077 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
78 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000079 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
80 PrintObjCInterfaceDecl(OID);
81 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
82 PrintObjCProtocolDecl(PID);
83 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000084 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000085 Out << "@protocol ";
86 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000087 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000088 if (i) Out << ", ";
89 Out << D->getName();
90 }
91 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000092 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000093 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000094 PrintObjCImplementationDecl(OID);
95 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000096 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 PrintObjCCategoryImplDecl(OID);
98 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000099 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000100 PrintObjCCategoryDecl(OID);
101 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000102 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000103 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +0000104 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
105 Out << "@class ";
106 ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls();
107 for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) {
108 const ObjCInterfaceDecl *D = ForwardDecls[i];
109 if (i) Out << ", ";
110 Out << D->getName();
111 }
112 Out << ";\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000113 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
114 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
115 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
116 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000117 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
118 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000119 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
120 Out << "asm(";
121 AD->getAsmString()->printPretty(Out);
122 Out << ")\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000123 } else {
124 assert(0 && "Unknown decl type!");
125 }
126}
127
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000128void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 bool HasBody = FD->getBody();
130
Ted Kremenekea75c552007-11-28 21:32:21 +0000131 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000132
133 switch (FD->getStorageClass()) {
134 default: assert(0 && "Unknown storage class");
135 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000136 case FunctionDecl::Extern: Out << "extern "; break;
137 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000138 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000139 }
140
141 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000142 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000143
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000145 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000146
Chris Lattner0d6ca112007-12-03 21:43:25 +0000147 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 Proto += "(";
149 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
150 if (i) Proto += ", ";
151 std::string ParamStr;
152 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
153
154 FT->getArgType(i).getAsStringInternal(ParamStr);
155 Proto += ParamStr;
156 }
157
158 if (FT->isVariadic()) {
159 if (FD->getNumParams()) Proto += ", ";
160 Proto += "...";
161 }
162 Proto += ")";
163 } else {
164 assert(isa<FunctionTypeNoProto>(AFT));
165 Proto += "()";
166 }
167
168 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000169 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000170
Chris Lattner6000dac2007-08-08 22:51:59 +0000171 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000172 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000173 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000176void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 std::string S = TD->getName();
178 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000179 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
181
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000182void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
183 const char *l;
184 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
185 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000186 else {
187 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
188 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000189 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000190 }
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000191 Out << "extern \"" << l << "\" { ";
192 PrintDecl(LS->getDecl());
193 Out << "}\n";
194}
195
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000196void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000197 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000198 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000199 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000200 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000201 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000202 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000203
Chris Lattnerefe8a962008-08-22 06:59:15 +0000204 std::string name = OMD->getSelector().getName();
205 std::string::size_type pos, lastPos = 0;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000206 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000207 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000208 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000209 pos = name.find_first_of(":", lastPos);
210 Out << " " << name.substr(lastPos, pos - lastPos);
211 Out << ":(" << PDecl->getType().getAsString() << ")" << PDecl->getName();
212 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000213 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000214
215 if (OMD->getNumParams() == 0)
216 Out << " " << name;
217
218 if (OMD->isVariadic())
219 Out << ", ...";
220
221 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000222}
223
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000224void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000225 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000226 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000227
228 if (SID)
229 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000230 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000231 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000232
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000233 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000234 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000235 ObjCMethodDecl *OMD = *I;
236 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000237 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000238 Out << ' ';
239 OMD->getBody()->printPretty(Out);
240 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000241 }
242 }
243
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000244 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000245 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000246 ObjCMethodDecl *OMD = *I;
247 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000248 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000249 Out << ' ';
250 OMD->getBody()->printPretty(Out);
251 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000252 }
253 }
254
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000255 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
256 E = OID->propimpl_end(); I != E; ++I)
257 PrintObjCPropertyImplDecl(*I);
258
Ted Kremenekea75c552007-11-28 21:32:21 +0000259 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000260}
261
262
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000263void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000264 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000265 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000266
267 if (SID)
268 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000269 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000270 Out << "@interface " << I;
271
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000272 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000273 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
274 if (!Protocols.empty()) {
275 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
276 E = Protocols.end(); I != E; ++I)
277 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000278 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000279
Chris Lattner3db6cae2008-07-21 18:19:38 +0000280 if (!Protocols.empty())
281 Out << ">";
282 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000283
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000284 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000285 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000286 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000287 E = OID->ivar_end(); I != E; ++I) {
288 Out << '\t' << (*I)->getType().getAsString()
289 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000290 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000291 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000292 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000293
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000294 for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(),
295 E = OID->classprop_end(); I != E; ++I)
296 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000297 bool eol_needed = false;
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000298 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
299 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000300 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000301
302 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
303 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000304 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000305
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000306 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000307 // FIXME: implement the rest...
308}
309
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000310void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000311 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000312
313 for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(),
314 E = PID->classprop_end(); I != E; ++I)
315 PrintObjCPropertyDecl(*I);
316 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000317 // FIXME: implement the rest...
318}
319
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000320void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000321 Out << "@implementation "
322 << PID->getClassInterface()->getName()
323 << '(' << PID->getName() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000324 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
325 E = PID->propimpl_end(); I != E; ++I)
326 PrintObjCPropertyImplDecl(*I);
327 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000328 // FIXME: implement the rest...
329}
330
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000331void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000332 Out << "@interface "
333 << PID->getClassInterface()->getName()
334 << '(' << PID->getName() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000335 // Output property declarations.
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000336 for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(),
337 E = PID->classprop_end(); I != E; ++I)
338 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000339 Out << "@end\n";
340
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000341 // FIXME: implement the rest...
342}
343
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000344void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000345 Out << "@compatibility_alias " << AID->getName()
346 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000347}
348
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000349/// PrintObjCPropertyDecl - print a property declaration.
350///
351void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000352 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
353 Out << "@required\n";
354 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
355 Out << "@optional\n";
356
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000357 Out << "@property";
358 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
359 bool first = true;
360 Out << " (";
361 if (PDecl->getPropertyAttributes() &
362 ObjCPropertyDecl::OBJC_PR_readonly) {
363 Out << (first ? ' ' : ',') << "readonly";
364 first = false;
365 }
366
367 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
368 Out << (first ? ' ' : ',') << "getter = "
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000369 << PDecl->getGetterName().getName();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000370 first = false;
371 }
372 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
373 Out << (first ? ' ' : ',') << "setter = "
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000374 << PDecl->getSetterName().getName();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000375 first = false;
376 }
377
378 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
379 Out << (first ? ' ' : ',') << "assign";
380 first = false;
381 }
382
383 if (PDecl->getPropertyAttributes() &
384 ObjCPropertyDecl::OBJC_PR_readwrite) {
385 Out << (first ? ' ' : ',') << "readwrite";
386 first = false;
387 }
388
389 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
390 Out << (first ? ' ' : ',') << "retain";
391 first = false;
392 }
393
394 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
395 Out << (first ? ' ' : ',') << "copy";
396 first = false;
397 }
398
399 if (PDecl->getPropertyAttributes() &
400 ObjCPropertyDecl::OBJC_PR_nonatomic) {
401 Out << (first ? ' ' : ',') << "nonatomic";
402 first = false;
403 }
404 Out << " )";
405 }
406 Out << ' ' << PDecl->getType().getAsString()
407 << ' ' << PDecl->getName();
408
409 Out << ";\n";
410}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000411
412/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
413/// declaration syntax.
414///
415void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000416 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000417 Out << "\n@synthesize ";
418 else
419 Out << "\n@dynamic ";
420 Out << PID->getPropertyDecl()->getName();
421 if (PID->getPropertyIvarDecl())
422 Out << "=" << PID->getPropertyIvarDecl()->getName();
423 Out << ";\n";
424}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000425//===----------------------------------------------------------------------===//
426/// ASTPrinter - Pretty-printer of ASTs
427
Chris Lattner3d4997d2007-09-15 23:02:28 +0000428namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000429 class ASTPrinter : public ASTConsumer, public DeclPrinter {
430 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000431 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000432
Chris Lattner3d4997d2007-09-15 23:02:28 +0000433 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000434 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000436 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000437}
Chris Lattner6000dac2007-08-08 22:51:59 +0000438
Ted Kremeneka95d3752008-09-13 05:16:45 +0000439ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000440 return new ASTPrinter(out);
441}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000442
443//===----------------------------------------------------------------------===//
444/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000445
446namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000447 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000448 SourceManager *SM;
449 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000450 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000451
Ted Kremenek95041a22007-12-19 22:51:13 +0000452 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000453 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000454 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000455
456 virtual void HandleTopLevelDecl(Decl *D) {
457 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
458 PrintFunctionDeclStart(FD);
459
460 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000461 Out << '\n';
462 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000463 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000464 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000465 }
466 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
467 PrintTypeDefDecl(TD);
468 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000469 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000470 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000471 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000472 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000473 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000474 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000475 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000476 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000477 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000478 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000479 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000480 } else if (isa<FileScopeAsmDecl>(D)) {
481 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000482 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
483 Out << "Read objc method decl: '" << MD->getSelector().getName()
484 << "'\n";
Steve Naroff1a2b90d2008-05-23 18:50:58 +0000485 if (MD->getBody()) {
486 // FIXME: convert dumper to use std::ostream?
487 MD->getBody()->dumpAll(*SM);
488 Out << '\n';
489 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000490 } else if (isa<ObjCImplementationDecl>(D)) {
491 Out << "Read objc implementation decl\n";
492 }
493 else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000494 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000495 }
496 }
497 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000498}
499
Chris Lattner3d4997d2007-09-15 23:02:28 +0000500ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
501
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000502//===----------------------------------------------------------------------===//
503/// ASTViewer - AST Visualization
504
Ted Kremenek80de08f2007-09-19 21:29:43 +0000505namespace {
506 class ASTViewer : public ASTConsumer {
507 SourceManager *SM;
508 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000509 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000510 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000511 }
512
513 virtual void HandleTopLevelDecl(Decl *D) {
514 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000515 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000516
517 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000518 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000519 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000520 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000521 }
522 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000523 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
524 DeclPrinter().PrintObjCMethodDecl(MD);
525
526 if (MD->getBody()) {
527 llvm::cerr << '\n';
528 MD->getBody()->viewAST();
529 llvm::cerr << '\n';
530 }
531 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000532 }
533 };
534}
535
536ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
537
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000538//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000539// AST Serializer
540
541namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000542
543class ASTSerializer : public ASTConsumer {
544protected:
Nico Weberdae86962008-08-09 18:32:11 +0000545 Diagnostic& Diags;
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000546
Ted Kremenekf06c9282007-12-19 23:49:37 +0000547public:
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000548 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000549};
Eli Friedmand8a65c12008-06-09 20:02:51 +0000550
Ted Kremenekf06c9282007-12-19 23:49:37 +0000551class SingleFileSerializer : public ASTSerializer {
552 const llvm::sys::Path FName;
553public:
Nico Weberdae86962008-08-09 18:32:11 +0000554 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
555 : ASTSerializer(diags), FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000556
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000557 virtual void HandleTranslationUnit(TranslationUnit& TU) {
Nico Weberdae86962008-08-09 18:32:11 +0000558 if (Diags.hasErrorOccurred())
559 return;
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000560 EmitASTBitcodeFile(&TU, FName);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000561 }
562};
563
564class BuildSerializer : public ASTSerializer {
565 llvm::sys::Path EmitDir;
566public:
Nico Weberdae86962008-08-09 18:32:11 +0000567 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
568 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000569
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000570 virtual void HandleTranslationUnit(TranslationUnit& TU) {
571 if (Diags.hasErrorOccurred())
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000572 return;
573
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000574 SourceManager& SourceMgr = TU.getContext().getSourceManager();
Ted Kremenek54117722007-12-20 00:34:58 +0000575 unsigned ID = SourceMgr.getMainFileID();
576 assert (ID && "MainFileID not set!");
577 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
578 assert (FE && "No FileEntry for main file.");
579
580 // FIXME: This is not portable to Windows.
581 // FIXME: This logic should probably be moved elsewhere later.
582
Ted Kremenekee533642007-12-20 19:47:16 +0000583 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000584
585 std::vector<char> buf;
586 buf.reserve(strlen(FE->getName())+100);
587
588 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000589 FName.appendComponent(&buf[0]);
590 FName.createDirectoryOnDisk(true);
591 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000592 assert (false && "Could not create 'device' serialization directory.");
593 return;
594 }
Ted Kremenekee533642007-12-20 19:47:16 +0000595
Ted Kremenek54117722007-12-20 00:34:58 +0000596 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000597 FName.appendComponent(&buf[0]);
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000598 EmitASTBitcodeFile(&TU, FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000599
Ted Kremenekee533642007-12-20 19:47:16 +0000600 // Now emit the sources.
601
Ted Kremenek54117722007-12-20 00:34:58 +0000602 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000603};
604
605
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000606} // end anonymous namespace
607
608
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000609ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000610 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000611 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000612
Ted Kremenekf06c9282007-12-19 23:49:37 +0000613 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000614 if (InFile == "-") {
615 llvm::cerr <<
616 "error: Cannot use --serialize with -o for source read from STDIN.\n";
617 return NULL;
618 }
619
Ted Kremenekf06c9282007-12-19 23:49:37 +0000620 // The user specified an AST-emission directory. Determine if the path
621 // is absolute.
622 llvm::sys::Path EmitDir(OutputFile);
623
624 if (!EmitDir.isAbsolute()) {
625 llvm::cerr <<
626 "error: Output directory for --serialize must be an absolute path.\n";
627
628 return NULL;
629 }
630
631 // Create the directory if it does not exist.
632 EmitDir.createDirectoryOnDisk(true);
633 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
634 llvm::cerr <<
635 "error: Could not create output directory for --serialize.\n";
636
637 return NULL;
638 }
639
Ted Kremenek54117722007-12-20 00:34:58 +0000640 // FIXME: We should probably only allow using BuildSerializer when
641 // the ASTs come from parsed source files, and not from .ast files.
Nico Weberdae86962008-08-09 18:32:11 +0000642 return new BuildSerializer(EmitDir, Diags);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000643 }
644
645 // The user did not specify an output directory for serialized ASTs.
646 // Serialize the translation to a single file whose name is the same
647 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000648
Ted Kremenekf06c9282007-12-19 23:49:37 +0000649 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000650 FName.appendSuffix("ast");
Nico Weberdae86962008-08-09 18:32:11 +0000651 return new SingleFileSerializer(FName, Diags);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000652}
Ted Kremenek815c78f2008-08-05 18:50:11 +0000653
654class LLVMCodeGenWriter : public ASTConsumer {
655 llvm::OwningPtr<CodeGenerator> Gen;
656 const std::string &InFile;
657 const std::string &OutputFile;
658 bool EmitBitcode;
659public:
660
661 LLVMCodeGenWriter(bool EmitBC, Diagnostic &Diags, const LangOptions &Features,
662 const std::string& infile, const std::string& outfile,
663 bool GenerateDebugInfo)
664 : Gen(CreateLLVMCodeGen(Diags, Features, infile, GenerateDebugInfo)),
665 InFile(infile), OutputFile(outfile), EmitBitcode(EmitBC) {}
666
667 virtual void Initialize(ASTContext &Context) {
668 Gen->Initialize(Context);
669 }
Matthijs Kooijman992a8782008-08-07 16:04:15 +0000670
671 virtual void InitializeTU(TranslationUnit& TU) {
672 Gen->InitializeTU(TU);
673 }
Ted Kremenek815c78f2008-08-05 18:50:11 +0000674
675 virtual void HandleTopLevelDecl(Decl *D) {
676 Gen->HandleTopLevelDecl(D);
677 }
678
Matthijs Kooijman992a8782008-08-07 16:04:15 +0000679 virtual void HandleTranslationUnit(TranslationUnit& TU) {
680 Gen->HandleTranslationUnit(TU);
681 }
682
Ted Kremenek815c78f2008-08-05 18:50:11 +0000683 virtual void HandleTagDeclDefinition(TagDecl *D) {
684 Gen->HandleTagDeclDefinition(D);
685 }
Matthijs Kooijman992a8782008-08-07 16:04:15 +0000686
Ted Kremenek815c78f2008-08-05 18:50:11 +0000687 virtual ~LLVMCodeGenWriter() {
688 llvm::OwningPtr<llvm::Module> CodeGenModule(Gen->ReleaseModule());
689
690 if (!CodeGenModule)
691 return;
692
693 std::ostream *Out;
694
695 if (OutputFile == "-") {
696 Out = llvm::cout.stream();
697 } else if (!OutputFile.empty()) {
698 Out = new std::ofstream(OutputFile.c_str(),
699 std::ios_base::binary|std::ios_base::out);
700 } else if (InFile == "-") {
701 Out = llvm::cout.stream();
702 } else {
703 llvm::sys::Path Path(InFile);
704 Path.eraseSuffix();
705 if (!EmitBitcode)
706 Path.appendSuffix("ll");
707 else
708 Path.appendSuffix("bc");
709
710 Out = new std::ofstream(Path.toString().c_str(),
711 std::ios_base::binary|std::ios_base::out);
712 }
713
714 if (!EmitBitcode)
Chris Lattner405674c2008-08-23 22:23:37 +0000715 *Out << *CodeGenModule.get();
Ted Kremenek815c78f2008-08-05 18:50:11 +0000716 else
717 llvm::WriteBitcodeToFile(CodeGenModule.get(), *Out);
718
719 if (Out != llvm::cout.stream())
720 delete Out;
721 }
722};
723
724ASTConsumer *clang::CreateLLVMCodeGenWriter(bool EmitBC, Diagnostic &Diags,
725 const LangOptions &Features,
726 const std::string& InFile,
727 const std::string& OutFile,
728 bool GenerateDebugInfo) {
729
730 return new LLVMCodeGenWriter(EmitBC, Diags, Features, InFile, OutFile,
731 GenerateDebugInfo);
732}