blob: 38251775d2e7cc289c77a99cdae7e837620ee872 [file] [log] [blame]
Chris Lattnereb8c9632007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnereb8c9632007-10-07 06:04:32 +000010// AST Consumer Implementations.
Chris Lattner4b009652007-07-25 00:24:17 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnereb8c9632007-10-07 06:04:32 +000014#include "ASTConsumers.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000015#include "HTMLDiagnostics.h"
Ted Kremenekac881932007-12-18 21:34:28 +000016#include "clang/AST/TranslationUnit.h"
Nico Weber034bd872008-08-09 18:32:11 +000017#include "clang/Basic/Diagnostic.h"
Ted Kremenekfc17b8a2007-12-20 00:34:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/AST/AST.h"
Chris Lattnerb73abd52007-09-15 23:02:28 +000021#include "clang/AST/ASTConsumer.h"
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000022#include "clang/CodeGen/ModuleBuilder.h"
23#include "llvm/Module.h"
24#include "llvm/Bitcode/ReaderWriter.h"
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000025#include "llvm/Support/Streams.h"
Ted Kremenek0118bb52008-02-18 21:21:23 +000026#include "llvm/Support/Timer.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000027#include "llvm/ADT/OwningPtr.h"
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000028#include <fstream>
Ted Kremenekdd0126b2008-03-31 18:26:32 +000029
Chris Lattner95578782007-08-08 22:51:59 +000030using namespace clang;
Chris Lattner4b009652007-07-25 00:24:17 +000031
Ted Kremeneke09391a2007-11-27 21:46:50 +000032//===----------------------------------------------------------------------===//
33/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner95578782007-08-08 22:51:59 +000034
Ted Kremeneke09391a2007-11-27 21:46:50 +000035namespace {
36 class DeclPrinter {
37 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000038 std::ostream& Out;
Ted Kremeneke09391a2007-11-27 21:46:50 +000039
Chris Lattner216012f2008-01-10 01:43:14 +000040 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000041 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +000042
Chris Lattner1c1aabb2008-01-02 21:04:16 +000043 void PrintDecl(Decl *D);
Ted Kremeneke09391a2007-11-27 21:46:50 +000044 void PrintFunctionDeclStart(FunctionDecl *FD);
45 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattner806a5f52008-01-12 07:05:38 +000046 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremenek42730c52008-01-07 19:49:32 +000047 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
48 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
49 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
50 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
51 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
52 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
53 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +000054 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +000055 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Ted Kremeneke09391a2007-11-27 21:46:50 +000056 };
57} // end anonymous namespace
58
Chris Lattner1c1aabb2008-01-02 21:04:16 +000059void DeclPrinter:: PrintDecl(Decl *D) {
60 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
61 PrintFunctionDeclStart(FD);
62
63 if (FD->getBody()) {
64 Out << ' ';
65 FD->getBody()->printPretty(Out);
66 Out << '\n';
67 }
Ted Kremenek42730c52008-01-07 19:49:32 +000068 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000069 // Do nothing, methods definitions are printed in
Ted Kremenek42730c52008-01-07 19:49:32 +000070 // PrintObjCImplementationDecl.
Chris Lattner1c1aabb2008-01-02 21:04:16 +000071 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
72 PrintTypeDefDecl(TD);
Ted Kremenek42730c52008-01-07 19:49:32 +000073 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
74 PrintObjCInterfaceDecl(OID);
75 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
76 PrintObjCProtocolDecl(PID);
77 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattner43b885f2008-02-25 21:04:36 +000078 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000079 Out << "@protocol ";
80 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +000081 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattner1c1aabb2008-01-02 21:04:16 +000082 if (i) Out << ", ";
83 Out << D->getName();
84 }
85 Out << ";\n";
Ted Kremenek42730c52008-01-07 19:49:32 +000086 } else if (ObjCImplementationDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000087 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000088 PrintObjCImplementationDecl(OID);
89 } else if (ObjCCategoryImplDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000090 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000091 PrintObjCCategoryImplDecl(OID);
92 } else if (ObjCCategoryDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000093 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000094 PrintObjCCategoryDecl(OID);
95 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000096 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000097 PrintObjCCompatibleAliasDecl(OID);
Chris Lattnere1349142008-06-21 21:40:20 +000098 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
99 Out << "@class ";
100 ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls();
101 for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) {
102 const ObjCInterfaceDecl *D = ForwardDecls[i];
103 if (i) Out << ", ";
104 Out << D->getName();
105 }
106 Out << ";\n";
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000107 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
108 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
109 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
110 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner806a5f52008-01-12 07:05:38 +0000111 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
112 PrintLinkageSpec(LSD);
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000113 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
114 Out << "asm(";
115 AD->getAsmString()->printPretty(Out);
116 Out << ")\n";
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000117 } else {
118 assert(0 && "Unknown decl type!");
119 }
120}
121
Ted Kremeneke09391a2007-11-27 21:46:50 +0000122void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000123 bool HasBody = FD->getBody();
124
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000125 Out << '\n';
Chris Lattner987058a2007-08-26 04:02:13 +0000126
127 switch (FD->getStorageClass()) {
128 default: assert(0 && "Unknown storage class");
129 case FunctionDecl::None: break;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000130 case FunctionDecl::Extern: Out << "extern "; break;
131 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenekc6d26e02008-04-15 03:57:09 +0000132 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner987058a2007-08-26 04:02:13 +0000133 }
134
135 if (FD->isInline())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000136 Out << "inline ";
Chris Lattner987058a2007-08-26 04:02:13 +0000137
Chris Lattner4b009652007-07-25 00:24:17 +0000138 std::string Proto = FD->getName();
Chris Lattner934fff62007-12-03 21:43:25 +0000139 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Chris Lattner4b009652007-07-25 00:24:17 +0000140
Chris Lattner934fff62007-12-03 21:43:25 +0000141 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000142 Proto += "(";
143 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
144 if (i) Proto += ", ";
145 std::string ParamStr;
146 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
147
148 FT->getArgType(i).getAsStringInternal(ParamStr);
149 Proto += ParamStr;
150 }
151
152 if (FT->isVariadic()) {
153 if (FD->getNumParams()) Proto += ", ";
154 Proto += "...";
155 }
156 Proto += ")";
157 } else {
158 assert(isa<FunctionTypeNoProto>(AFT));
159 Proto += "()";
160 }
161
162 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000163 Out << Proto;
Chris Lattner4b009652007-07-25 00:24:17 +0000164
Chris Lattner95578782007-08-08 22:51:59 +0000165 if (!FD->getBody())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000166 Out << ";\n";
Chris Lattner95578782007-08-08 22:51:59 +0000167 // Doesn't print the body.
Chris Lattner4b009652007-07-25 00:24:17 +0000168}
169
Ted Kremeneke09391a2007-11-27 21:46:50 +0000170void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000171 std::string S = TD->getName();
172 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000173 Out << "typedef " << S << ";\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000174}
175
Chris Lattner806a5f52008-01-12 07:05:38 +0000176void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
177 const char *l;
178 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
179 l = "C";
Chris Lattner690c2872008-04-08 05:52:18 +0000180 else {
181 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
182 "unknown language in linkage specification");
Chris Lattner806a5f52008-01-12 07:05:38 +0000183 l = "C++";
Chris Lattner690c2872008-04-08 05:52:18 +0000184 }
Chris Lattner806a5f52008-01-12 07:05:38 +0000185 Out << "extern \"" << l << "\" { ";
186 PrintDecl(LS->getDecl());
187 Out << "}\n";
188}
189
Ted Kremenek42730c52008-01-07 19:49:32 +0000190void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000191 if (OMD->isInstance())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000192 Out << "\n- ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000193 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000194 Out << "\n+ ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000195 if (!OMD->getResultType().isNull())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000196 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000197 // FIXME: just print original selector name!
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000198 Out << OMD->getSelector().getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000199
Chris Lattner685d7922008-03-16 01:07:14 +0000200 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000201 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000202 // FIXME: selector is missing here!
203 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000204 }
205}
206
Ted Kremenek42730c52008-01-07 19:49:32 +0000207void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000208 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000209 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000210
211 if (SID)
212 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000213 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000214 Out << "@implementation " << I;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000215
Ted Kremenek42730c52008-01-07 19:49:32 +0000216 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000217 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000218 ObjCMethodDecl *OMD = *I;
219 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000220 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000221 Out << ' ';
222 OMD->getBody()->printPretty(Out);
223 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000224 }
225 }
226
Ted Kremenek42730c52008-01-07 19:49:32 +0000227 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000228 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000229 ObjCMethodDecl *OMD = *I;
230 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000231 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000232 Out << ' ';
233 OMD->getBody()->printPretty(Out);
234 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000235 }
236 }
237
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000238 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
239 E = OID->propimpl_end(); I != E; ++I)
240 PrintObjCPropertyImplDecl(*I);
241
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000242 Out << "@end\n";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000243}
244
245
Ted Kremenek42730c52008-01-07 19:49:32 +0000246void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000247 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000248 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000249
250 if (SID)
251 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000252 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000253 Out << "@interface " << I;
254
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000255 // Protocols?
Chris Lattner8bcb5252008-07-21 18:19:38 +0000256 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
257 if (!Protocols.empty()) {
258 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
259 E = Protocols.end(); I != E; ++I)
260 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000261 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000262
Chris Lattner8bcb5252008-07-21 18:19:38 +0000263 if (!Protocols.empty())
264 Out << ">";
265 Out << '\n';
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000266
Chris Lattnerec4979b2008-03-16 21:08:55 +0000267 if (OID->ivar_size() > 0) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000268 Out << '{';
Ted Kremenek42730c52008-01-07 19:49:32 +0000269 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerc7b06752007-12-12 07:56:42 +0000270 E = OID->ivar_end(); I != E; ++I) {
271 Out << '\t' << (*I)->getType().getAsString()
272 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000273 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000274 Out << "}\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000275 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000276
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000277 for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(),
278 E = OID->classprop_end(); I != E; ++I)
279 PrintObjCPropertyDecl(*I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000280 bool eol_needed = false;
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000281 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
282 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000283 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000284
285 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
286 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000287 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000288
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000289 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Narofffaed3bf2007-09-10 20:51:04 +0000290 // FIXME: implement the rest...
291}
292
Ted Kremenek42730c52008-01-07 19:49:32 +0000293void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000294 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000295
296 for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(),
297 E = PID->classprop_end(); I != E; ++I)
298 PrintObjCPropertyDecl(*I);
299 Out << "@end\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000300 // FIXME: implement the rest...
301}
302
Ted Kremenek42730c52008-01-07 19:49:32 +0000303void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000304 Out << "@implementation "
305 << PID->getClassInterface()->getName()
306 << '(' << PID->getName() << ");\n";
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000307 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
308 E = PID->propimpl_end(); I != E; ++I)
309 PrintObjCPropertyImplDecl(*I);
310 Out << "@end\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000311 // FIXME: implement the rest...
312}
313
Ted Kremenek42730c52008-01-07 19:49:32 +0000314void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000315 Out << "@interface "
316 << PID->getClassInterface()->getName()
317 << '(' << PID->getName() << ");\n";
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000318 // Output property declarations.
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000319 for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(),
320 E = PID->classprop_end(); I != E; ++I)
321 PrintObjCPropertyDecl(*I);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000322 Out << "@end\n";
323
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000324 // FIXME: implement the rest...
325}
326
Ted Kremenek42730c52008-01-07 19:49:32 +0000327void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000328 Out << "@compatibility_alias " << AID->getName()
329 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000330}
331
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000332/// PrintObjCPropertyDecl - print a property declaration.
333///
334void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000335 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
336 Out << "@required\n";
337 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
338 Out << "@optional\n";
339
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000340 Out << "@property";
341 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
342 bool first = true;
343 Out << " (";
344 if (PDecl->getPropertyAttributes() &
345 ObjCPropertyDecl::OBJC_PR_readonly) {
346 Out << (first ? ' ' : ',') << "readonly";
347 first = false;
348 }
349
350 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
351 Out << (first ? ' ' : ',') << "getter = "
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000352 << PDecl->getGetterName().getName();
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000353 first = false;
354 }
355 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
356 Out << (first ? ' ' : ',') << "setter = "
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000357 << PDecl->getSetterName().getName();
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000358 first = false;
359 }
360
361 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
362 Out << (first ? ' ' : ',') << "assign";
363 first = false;
364 }
365
366 if (PDecl->getPropertyAttributes() &
367 ObjCPropertyDecl::OBJC_PR_readwrite) {
368 Out << (first ? ' ' : ',') << "readwrite";
369 first = false;
370 }
371
372 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
373 Out << (first ? ' ' : ',') << "retain";
374 first = false;
375 }
376
377 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
378 Out << (first ? ' ' : ',') << "copy";
379 first = false;
380 }
381
382 if (PDecl->getPropertyAttributes() &
383 ObjCPropertyDecl::OBJC_PR_nonatomic) {
384 Out << (first ? ' ' : ',') << "nonatomic";
385 first = false;
386 }
387 Out << " )";
388 }
389 Out << ' ' << PDecl->getType().getAsString()
390 << ' ' << PDecl->getName();
391
392 Out << ";\n";
393}
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000394
395/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
396/// declaration syntax.
397///
398void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
399 if (PID->getPropertyImplementation() ==
400 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE)
401 Out << "\n@synthesize ";
402 else
403 Out << "\n@dynamic ";
404 Out << PID->getPropertyDecl()->getName();
405 if (PID->getPropertyIvarDecl())
406 Out << "=" << PID->getPropertyIvarDecl()->getName();
407 Out << ";\n";
408}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000409//===----------------------------------------------------------------------===//
410/// ASTPrinter - Pretty-printer of ASTs
411
Chris Lattnerb73abd52007-09-15 23:02:28 +0000412namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000413 class ASTPrinter : public ASTConsumer, public DeclPrinter {
414 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000415 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000416
Chris Lattnerb73abd52007-09-15 23:02:28 +0000417 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000418 PrintDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +0000419 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000420 };
Chris Lattner4b009652007-07-25 00:24:17 +0000421}
Chris Lattner95578782007-08-08 22:51:59 +0000422
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000423ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
424 return new ASTPrinter(out);
425}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000426
427//===----------------------------------------------------------------------===//
428/// ASTDumper - Low-level dumper of ASTs
Chris Lattnerb73abd52007-09-15 23:02:28 +0000429
430namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000431 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerb73abd52007-09-15 23:02:28 +0000432 SourceManager *SM;
433 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000434 ASTDumper() : DeclPrinter() {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000435
Ted Kremenek17861c52007-12-19 22:51:13 +0000436 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000437 SM = &Context.getSourceManager();
Chris Lattner95578782007-08-08 22:51:59 +0000438 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000439
440 virtual void HandleTopLevelDecl(Decl *D) {
441 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
442 PrintFunctionDeclStart(FD);
443
444 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000445 Out << '\n';
446 // FIXME: convert dumper to use std::ostream?
Chris Lattnerb73abd52007-09-15 23:02:28 +0000447 FD->getBody()->dumpAll(*SM);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000448 Out << '\n';
Chris Lattnerb73abd52007-09-15 23:02:28 +0000449 }
450 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
451 PrintTypeDefDecl(TD);
452 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000453 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000454 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000455 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000456 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000457 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000458 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000459 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000460 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000461 Out << "Read objc fwd protocol decl\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000462 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000463 Out << "Read objc fwd class decl\n";
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000464 } else if (isa<FileScopeAsmDecl>(D)) {
465 Out << "Read file scope asm decl\n";
Ted Kremenek5d257d42008-03-14 17:31:00 +0000466 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
467 Out << "Read objc method decl: '" << MD->getSelector().getName()
468 << "'\n";
Steve Naroff045e0e02008-05-23 18:50:58 +0000469 if (MD->getBody()) {
470 // FIXME: convert dumper to use std::ostream?
471 MD->getBody()->dumpAll(*SM);
472 Out << '\n';
473 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000474 } else if (isa<ObjCImplementationDecl>(D)) {
475 Out << "Read objc implementation decl\n";
476 }
477 else {
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000478 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000479 }
480 }
481 };
Chris Lattner95578782007-08-08 22:51:59 +0000482}
483
Chris Lattnerb73abd52007-09-15 23:02:28 +0000484ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
485
Ted Kremeneke09391a2007-11-27 21:46:50 +0000486//===----------------------------------------------------------------------===//
487/// ASTViewer - AST Visualization
488
Ted Kremenekb6976a22007-09-19 21:29:43 +0000489namespace {
490 class ASTViewer : public ASTConsumer {
491 SourceManager *SM;
492 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000493 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000494 SM = &Context.getSourceManager();
Ted Kremenekb6976a22007-09-19 21:29:43 +0000495 }
496
497 virtual void HandleTopLevelDecl(Decl *D) {
498 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000499 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenekb6976a22007-09-19 21:29:43 +0000500
501 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000502 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000503 FD->getBody()->viewAST();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000504 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000505 }
506 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000507 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
508 DeclPrinter().PrintObjCMethodDecl(MD);
509
510 if (MD->getBody()) {
511 llvm::cerr << '\n';
512 MD->getBody()->viewAST();
513 llvm::cerr << '\n';
514 }
515 }
Ted Kremenekb6976a22007-09-19 21:29:43 +0000516 }
517 };
518}
519
520ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
521
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000522//===----------------------------------------------------------------------===//
Ted Kremenek397de012007-12-13 00:37:31 +0000523// AST Serializer
524
525namespace {
Ted Kremenek21189012007-12-19 23:49:37 +0000526
527class ASTSerializer : public ASTConsumer {
528protected:
Ted Kremenek863b01f2008-04-23 16:25:39 +0000529 TranslationUnit* TU;
Nico Weber034bd872008-08-09 18:32:11 +0000530 Diagnostic& Diags;
Ted Kremenek863b01f2008-04-23 16:25:39 +0000531
Ted Kremenek21189012007-12-19 23:49:37 +0000532public:
Nico Weber034bd872008-08-09 18:32:11 +0000533 ASTSerializer(Diagnostic& diags) : TU(0), Diags(diags) {}
Eli Friedmanb4563692008-06-09 20:02:51 +0000534
535 virtual void InitializeTU(TranslationUnit &tu) {
536 TU = &tu;
Ted Kremenek21189012007-12-19 23:49:37 +0000537 }
Eli Friedmanb4563692008-06-09 20:02:51 +0000538
Ted Kremenek21189012007-12-19 23:49:37 +0000539};
Eli Friedmanb4563692008-06-09 20:02:51 +0000540
Ted Kremenek21189012007-12-19 23:49:37 +0000541class SingleFileSerializer : public ASTSerializer {
542 const llvm::sys::Path FName;
543public:
Nico Weber034bd872008-08-09 18:32:11 +0000544 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
545 : ASTSerializer(diags), FName(F) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000546
547 ~SingleFileSerializer() {
Nico Weber034bd872008-08-09 18:32:11 +0000548 if (Diags.hasErrorOccurred())
549 return;
Ted Kremenek863b01f2008-04-23 16:25:39 +0000550 EmitASTBitcodeFile(TU, FName);
Ted Kremenek21189012007-12-19 23:49:37 +0000551 }
552};
553
554class BuildSerializer : public ASTSerializer {
555 llvm::sys::Path EmitDir;
556public:
Nico Weber034bd872008-08-09 18:32:11 +0000557 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
558 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000559
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000560 ~BuildSerializer() {
Ted Kremenek863b01f2008-04-23 16:25:39 +0000561
Nico Weber034bd872008-08-09 18:32:11 +0000562 if (!TU || Diags.hasErrorOccurred())
Ted Kremenek863b01f2008-04-23 16:25:39 +0000563 return;
564
565 SourceManager& SourceMgr = TU->getContext().getSourceManager();
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000566 unsigned ID = SourceMgr.getMainFileID();
567 assert (ID && "MainFileID not set!");
568 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
569 assert (FE && "No FileEntry for main file.");
570
571 // FIXME: This is not portable to Windows.
572 // FIXME: This logic should probably be moved elsewhere later.
573
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000574 llvm::sys::Path FName(EmitDir);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000575
576 std::vector<char> buf;
577 buf.reserve(strlen(FE->getName())+100);
578
579 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000580 FName.appendComponent(&buf[0]);
581 FName.createDirectoryOnDisk(true);
582 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000583 assert (false && "Could not create 'device' serialization directory.");
584 return;
585 }
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000586
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000587 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000588 FName.appendComponent(&buf[0]);
Ted Kremenek863b01f2008-04-23 16:25:39 +0000589 EmitASTBitcodeFile(TU, FName);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000590
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000591 // Now emit the sources.
592
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000593 }
Ted Kremenek21189012007-12-19 23:49:37 +0000594};
595
596
Ted Kremenek397de012007-12-13 00:37:31 +0000597} // end anonymous namespace
598
599
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000600ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenek21189012007-12-19 23:49:37 +0000601 const std::string& OutputFile,
Ted Kremenek842126e2008-06-04 15:55:15 +0000602 Diagnostic &Diags) {
Ted Kremenekbde30332007-12-19 17:25:59 +0000603
Ted Kremenek21189012007-12-19 23:49:37 +0000604 if (OutputFile.size()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000605 if (InFile == "-") {
606 llvm::cerr <<
607 "error: Cannot use --serialize with -o for source read from STDIN.\n";
608 return NULL;
609 }
610
Ted Kremenek21189012007-12-19 23:49:37 +0000611 // The user specified an AST-emission directory. Determine if the path
612 // is absolute.
613 llvm::sys::Path EmitDir(OutputFile);
614
615 if (!EmitDir.isAbsolute()) {
616 llvm::cerr <<
617 "error: Output directory for --serialize must be an absolute path.\n";
618
619 return NULL;
620 }
621
622 // Create the directory if it does not exist.
623 EmitDir.createDirectoryOnDisk(true);
624 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
625 llvm::cerr <<
626 "error: Could not create output directory for --serialize.\n";
627
628 return NULL;
629 }
630
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000631 // FIXME: We should probably only allow using BuildSerializer when
632 // the ASTs come from parsed source files, and not from .ast files.
Nico Weber034bd872008-08-09 18:32:11 +0000633 return new BuildSerializer(EmitDir, Diags);
Ted Kremenek21189012007-12-19 23:49:37 +0000634 }
635
636 // The user did not specify an output directory for serialized ASTs.
637 // Serialize the translation to a single file whose name is the same
638 // as the input file with the ".ast" extension appended.
Ted Kremenekab749372007-12-19 19:27:38 +0000639
Ted Kremenek21189012007-12-19 23:49:37 +0000640 llvm::sys::Path FName(InFile.c_str());
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000641 FName.appendSuffix("ast");
Nico Weber034bd872008-08-09 18:32:11 +0000642 return new SingleFileSerializer(FName, Diags);
Ted Kremenek397de012007-12-13 00:37:31 +0000643}
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000644
645class LLVMCodeGenWriter : public ASTConsumer {
646 llvm::OwningPtr<CodeGenerator> Gen;
647 const std::string &InFile;
648 const std::string &OutputFile;
649 bool EmitBitcode;
650public:
651
652 LLVMCodeGenWriter(bool EmitBC, Diagnostic &Diags, const LangOptions &Features,
653 const std::string& infile, const std::string& outfile,
654 bool GenerateDebugInfo)
655 : Gen(CreateLLVMCodeGen(Diags, Features, infile, GenerateDebugInfo)),
656 InFile(infile), OutputFile(outfile), EmitBitcode(EmitBC) {}
657
658 virtual void Initialize(ASTContext &Context) {
659 Gen->Initialize(Context);
660 }
Matthijs Kooijman211dbe22008-08-07 16:04:15 +0000661
662 virtual void InitializeTU(TranslationUnit& TU) {
663 Gen->InitializeTU(TU);
664 }
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000665
666 virtual void HandleTopLevelDecl(Decl *D) {
667 Gen->HandleTopLevelDecl(D);
668 }
669
Matthijs Kooijman211dbe22008-08-07 16:04:15 +0000670 virtual void HandleTranslationUnit(TranslationUnit& TU) {
671 Gen->HandleTranslationUnit(TU);
672 }
673
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000674 virtual void HandleTagDeclDefinition(TagDecl *D) {
675 Gen->HandleTagDeclDefinition(D);
676 }
Matthijs Kooijman211dbe22008-08-07 16:04:15 +0000677
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000678 virtual ~LLVMCodeGenWriter() {
679 llvm::OwningPtr<llvm::Module> CodeGenModule(Gen->ReleaseModule());
680
681 if (!CodeGenModule)
682 return;
683
684 std::ostream *Out;
685
686 if (OutputFile == "-") {
687 Out = llvm::cout.stream();
688 } else if (!OutputFile.empty()) {
689 Out = new std::ofstream(OutputFile.c_str(),
690 std::ios_base::binary|std::ios_base::out);
691 } else if (InFile == "-") {
692 Out = llvm::cout.stream();
693 } else {
694 llvm::sys::Path Path(InFile);
695 Path.eraseSuffix();
696 if (!EmitBitcode)
697 Path.appendSuffix("ll");
698 else
699 Path.appendSuffix("bc");
700
701 Out = new std::ofstream(Path.toString().c_str(),
702 std::ios_base::binary|std::ios_base::out);
703 }
704
705 if (!EmitBitcode)
706 CodeGenModule->print(*Out);
707 else
708 llvm::WriteBitcodeToFile(CodeGenModule.get(), *Out);
709
710 if (Out != llvm::cout.stream())
711 delete Out;
712 }
713};
714
715ASTConsumer *clang::CreateLLVMCodeGenWriter(bool EmitBC, Diagnostic &Diags,
716 const LangOptions &Features,
717 const std::string& InFile,
718 const std::string& OutFile,
719 bool GenerateDebugInfo) {
720
721 return new LLVMCodeGenWriter(EmitBC, Diags, Features, InFile, OutFile,
722 GenerateDebugInfo);
723}