blob: 4a2ef778a09db1aa171409e8b66d35fa3b096265 [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"
Ted Kremenekfc17b8a2007-12-20 00:34:58 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/FileManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/AST/AST.h"
Chris Lattnerb73abd52007-09-15 23:02:28 +000020#include "clang/AST/ASTConsumer.h"
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000021#include "clang/CodeGen/ModuleBuilder.h"
22#include "llvm/Module.h"
23#include "llvm/Bitcode/ReaderWriter.h"
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000024#include "llvm/Support/Streams.h"
Ted Kremenek0118bb52008-02-18 21:21:23 +000025#include "llvm/Support/Timer.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000026#include "llvm/ADT/OwningPtr.h"
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000027#include <fstream>
Ted Kremenekdd0126b2008-03-31 18:26:32 +000028
Chris Lattner95578782007-08-08 22:51:59 +000029using namespace clang;
Chris Lattner4b009652007-07-25 00:24:17 +000030
Ted Kremeneke09391a2007-11-27 21:46:50 +000031//===----------------------------------------------------------------------===//
32/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner95578782007-08-08 22:51:59 +000033
Ted Kremeneke09391a2007-11-27 21:46:50 +000034namespace {
35 class DeclPrinter {
36 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000037 std::ostream& Out;
Ted Kremeneke09391a2007-11-27 21:46:50 +000038
Chris Lattner216012f2008-01-10 01:43:14 +000039 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +000040 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +000041
Chris Lattner1c1aabb2008-01-02 21:04:16 +000042 void PrintDecl(Decl *D);
Ted Kremeneke09391a2007-11-27 21:46:50 +000043 void PrintFunctionDeclStart(FunctionDecl *FD);
44 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattner806a5f52008-01-12 07:05:38 +000045 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremenek42730c52008-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 Jahanian8516e9a2008-04-17 18:25:18 +000053 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +000054 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Ted Kremeneke09391a2007-11-27 21:46:50 +000055 };
56} // end anonymous namespace
57
Chris Lattner1c1aabb2008-01-02 21:04:16 +000058void DeclPrinter:: PrintDecl(Decl *D) {
59 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
60 PrintFunctionDeclStart(FD);
61
62 if (FD->getBody()) {
63 Out << ' ';
64 FD->getBody()->printPretty(Out);
65 Out << '\n';
66 }
Ted Kremenek42730c52008-01-07 19:49:32 +000067 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000068 // Do nothing, methods definitions are printed in
Ted Kremenek42730c52008-01-07 19:49:32 +000069 // PrintObjCImplementationDecl.
Chris Lattner1c1aabb2008-01-02 21:04:16 +000070 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
71 PrintTypeDefDecl(TD);
Ted Kremenek42730c52008-01-07 19:49:32 +000072 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
73 PrintObjCInterfaceDecl(OID);
74 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
75 PrintObjCProtocolDecl(PID);
76 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattner43b885f2008-02-25 21:04:36 +000077 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +000078 Out << "@protocol ";
79 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +000080 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattner1c1aabb2008-01-02 21:04:16 +000081 if (i) Out << ", ";
82 Out << D->getName();
83 }
84 Out << ";\n";
Ted Kremenek42730c52008-01-07 19:49:32 +000085 } else if (ObjCImplementationDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000086 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000087 PrintObjCImplementationDecl(OID);
88 } else if (ObjCCategoryImplDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000089 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000090 PrintObjCCategoryImplDecl(OID);
91 } else if (ObjCCategoryDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000092 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000093 PrintObjCCategoryDecl(OID);
94 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattner43b885f2008-02-25 21:04:36 +000095 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +000096 PrintObjCCompatibleAliasDecl(OID);
Chris Lattnere1349142008-06-21 21:40:20 +000097 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
98 Out << "@class ";
99 ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls();
100 for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) {
101 const ObjCInterfaceDecl *D = ForwardDecls[i];
102 if (i) Out << ", ";
103 Out << D->getName();
104 }
105 Out << ";\n";
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000106 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
107 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
108 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
109 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattner806a5f52008-01-12 07:05:38 +0000110 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
111 PrintLinkageSpec(LSD);
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000112 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
113 Out << "asm(";
114 AD->getAsmString()->printPretty(Out);
115 Out << ")\n";
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000116 } else {
117 assert(0 && "Unknown decl type!");
118 }
119}
120
Ted Kremeneke09391a2007-11-27 21:46:50 +0000121void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000122 bool HasBody = FD->getBody();
123
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000124 Out << '\n';
Chris Lattner987058a2007-08-26 04:02:13 +0000125
126 switch (FD->getStorageClass()) {
127 default: assert(0 && "Unknown storage class");
128 case FunctionDecl::None: break;
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000129 case FunctionDecl::Extern: Out << "extern "; break;
130 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenekc6d26e02008-04-15 03:57:09 +0000131 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner987058a2007-08-26 04:02:13 +0000132 }
133
134 if (FD->isInline())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000135 Out << "inline ";
Chris Lattner987058a2007-08-26 04:02:13 +0000136
Chris Lattner4b009652007-07-25 00:24:17 +0000137 std::string Proto = FD->getName();
Chris Lattner934fff62007-12-03 21:43:25 +0000138 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Chris Lattner4b009652007-07-25 00:24:17 +0000139
Chris Lattner934fff62007-12-03 21:43:25 +0000140 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000141 Proto += "(";
142 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
143 if (i) Proto += ", ";
144 std::string ParamStr;
145 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
146
147 FT->getArgType(i).getAsStringInternal(ParamStr);
148 Proto += ParamStr;
149 }
150
151 if (FT->isVariadic()) {
152 if (FD->getNumParams()) Proto += ", ";
153 Proto += "...";
154 }
155 Proto += ")";
156 } else {
157 assert(isa<FunctionTypeNoProto>(AFT));
158 Proto += "()";
159 }
160
161 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000162 Out << Proto;
Chris Lattner4b009652007-07-25 00:24:17 +0000163
Chris Lattner95578782007-08-08 22:51:59 +0000164 if (!FD->getBody())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000165 Out << ";\n";
Chris Lattner95578782007-08-08 22:51:59 +0000166 // Doesn't print the body.
Chris Lattner4b009652007-07-25 00:24:17 +0000167}
168
Ted Kremeneke09391a2007-11-27 21:46:50 +0000169void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattner4b009652007-07-25 00:24:17 +0000170 std::string S = TD->getName();
171 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000172 Out << "typedef " << S << ";\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000173}
174
Chris Lattner806a5f52008-01-12 07:05:38 +0000175void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
176 const char *l;
177 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
178 l = "C";
Chris Lattner690c2872008-04-08 05:52:18 +0000179 else {
180 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
181 "unknown language in linkage specification");
Chris Lattner806a5f52008-01-12 07:05:38 +0000182 l = "C++";
Chris Lattner690c2872008-04-08 05:52:18 +0000183 }
Chris Lattner806a5f52008-01-12 07:05:38 +0000184 Out << "extern \"" << l << "\" { ";
185 PrintDecl(LS->getDecl());
186 Out << "}\n";
187}
188
Ted Kremenek42730c52008-01-07 19:49:32 +0000189void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000190 if (OMD->isInstance())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000191 Out << "\n- ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000192 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000193 Out << "\n+ ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000194 if (!OMD->getResultType().isNull())
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000195 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000196 // FIXME: just print original selector name!
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000197 Out << OMD->getSelector().getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000198
Chris Lattner685d7922008-03-16 01:07:14 +0000199 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000200 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000201 // FIXME: selector is missing here!
202 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000203 }
204}
205
Ted Kremenek42730c52008-01-07 19:49:32 +0000206void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000207 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000208 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000209
210 if (SID)
211 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000212 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000213 Out << "@implementation " << I;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000214
Ted Kremenek42730c52008-01-07 19:49:32 +0000215 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000216 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000217 ObjCMethodDecl *OMD = *I;
218 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000219 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000220 Out << ' ';
221 OMD->getBody()->printPretty(Out);
222 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000223 }
224 }
225
Ted Kremenek42730c52008-01-07 19:49:32 +0000226 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000227 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000228 ObjCMethodDecl *OMD = *I;
229 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000230 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000231 Out << ' ';
232 OMD->getBody()->printPretty(Out);
233 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000234 }
235 }
236
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000237 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
238 E = OID->propimpl_end(); I != E; ++I)
239 PrintObjCPropertyImplDecl(*I);
240
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000241 Out << "@end\n";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000242}
243
244
Ted Kremenek42730c52008-01-07 19:49:32 +0000245void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000246 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000247 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000248
249 if (SID)
250 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000251 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000252 Out << "@interface " << I;
253
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000254 // Protocols?
Chris Lattner8bcb5252008-07-21 18:19:38 +0000255 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
256 if (!Protocols.empty()) {
257 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
258 E = Protocols.end(); I != E; ++I)
259 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000260 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000261
Chris Lattner8bcb5252008-07-21 18:19:38 +0000262 if (!Protocols.empty())
263 Out << ">";
264 Out << '\n';
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000265
Chris Lattnerec4979b2008-03-16 21:08:55 +0000266 if (OID->ivar_size() > 0) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000267 Out << '{';
Ted Kremenek42730c52008-01-07 19:49:32 +0000268 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerc7b06752007-12-12 07:56:42 +0000269 E = OID->ivar_end(); I != E; ++I) {
270 Out << '\t' << (*I)->getType().getAsString()
271 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000272 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000273 Out << "}\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000274 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000275
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000276 for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(),
277 E = OID->classprop_end(); I != E; ++I)
278 PrintObjCPropertyDecl(*I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000279 bool eol_needed = false;
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000280 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
281 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000282 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000283
284 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
285 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000286 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000287
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000288 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Narofffaed3bf2007-09-10 20:51:04 +0000289 // FIXME: implement the rest...
290}
291
Ted Kremenek42730c52008-01-07 19:49:32 +0000292void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000293 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000294
295 for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(),
296 E = PID->classprop_end(); I != E; ++I)
297 PrintObjCPropertyDecl(*I);
298 Out << "@end\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000299 // FIXME: implement the rest...
300}
301
Ted Kremenek42730c52008-01-07 19:49:32 +0000302void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000303 Out << "@implementation "
304 << PID->getClassInterface()->getName()
305 << '(' << PID->getName() << ");\n";
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000306 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
307 E = PID->propimpl_end(); I != E; ++I)
308 PrintObjCPropertyImplDecl(*I);
309 Out << "@end\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000310 // FIXME: implement the rest...
311}
312
Ted Kremenek42730c52008-01-07 19:49:32 +0000313void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000314 Out << "@interface "
315 << PID->getClassInterface()->getName()
316 << '(' << PID->getName() << ");\n";
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000317 // Output property declarations.
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000318 for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(),
319 E = PID->classprop_end(); I != E; ++I)
320 PrintObjCPropertyDecl(*I);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000321 Out << "@end\n";
322
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000323 // FIXME: implement the rest...
324}
325
Ted Kremenek42730c52008-01-07 19:49:32 +0000326void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000327 Out << "@compatibility_alias " << AID->getName()
328 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000329}
330
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000331/// PrintObjCPropertyDecl - print a property declaration.
332///
333void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000334 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
335 Out << "@required\n";
336 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
337 Out << "@optional\n";
338
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000339 Out << "@property";
340 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
341 bool first = true;
342 Out << " (";
343 if (PDecl->getPropertyAttributes() &
344 ObjCPropertyDecl::OBJC_PR_readonly) {
345 Out << (first ? ' ' : ',') << "readonly";
346 first = false;
347 }
348
349 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
350 Out << (first ? ' ' : ',') << "getter = "
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000351 << PDecl->getGetterName().getName();
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000352 first = false;
353 }
354 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
355 Out << (first ? ' ' : ',') << "setter = "
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000356 << PDecl->getSetterName().getName();
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000357 first = false;
358 }
359
360 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
361 Out << (first ? ' ' : ',') << "assign";
362 first = false;
363 }
364
365 if (PDecl->getPropertyAttributes() &
366 ObjCPropertyDecl::OBJC_PR_readwrite) {
367 Out << (first ? ' ' : ',') << "readwrite";
368 first = false;
369 }
370
371 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
372 Out << (first ? ' ' : ',') << "retain";
373 first = false;
374 }
375
376 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
377 Out << (first ? ' ' : ',') << "copy";
378 first = false;
379 }
380
381 if (PDecl->getPropertyAttributes() &
382 ObjCPropertyDecl::OBJC_PR_nonatomic) {
383 Out << (first ? ' ' : ',') << "nonatomic";
384 first = false;
385 }
386 Out << " )";
387 }
388 Out << ' ' << PDecl->getType().getAsString()
389 << ' ' << PDecl->getName();
390
391 Out << ";\n";
392}
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000393
394/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
395/// declaration syntax.
396///
397void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
398 if (PID->getPropertyImplementation() ==
399 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE)
400 Out << "\n@synthesize ";
401 else
402 Out << "\n@dynamic ";
403 Out << PID->getPropertyDecl()->getName();
404 if (PID->getPropertyIvarDecl())
405 Out << "=" << PID->getPropertyIvarDecl()->getName();
406 Out << ";\n";
407}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000408//===----------------------------------------------------------------------===//
409/// ASTPrinter - Pretty-printer of ASTs
410
Chris Lattnerb73abd52007-09-15 23:02:28 +0000411namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000412 class ASTPrinter : public ASTConsumer, public DeclPrinter {
413 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000414 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000415
Chris Lattnerb73abd52007-09-15 23:02:28 +0000416 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000417 PrintDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +0000418 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000419 };
Chris Lattner4b009652007-07-25 00:24:17 +0000420}
Chris Lattner95578782007-08-08 22:51:59 +0000421
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000422ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
423 return new ASTPrinter(out);
424}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000425
426//===----------------------------------------------------------------------===//
427/// ASTDumper - Low-level dumper of ASTs
Chris Lattnerb73abd52007-09-15 23:02:28 +0000428
429namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000430 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerb73abd52007-09-15 23:02:28 +0000431 SourceManager *SM;
432 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000433 ASTDumper() : DeclPrinter() {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000434
Ted Kremenek17861c52007-12-19 22:51:13 +0000435 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000436 SM = &Context.getSourceManager();
Chris Lattner95578782007-08-08 22:51:59 +0000437 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000438
439 virtual void HandleTopLevelDecl(Decl *D) {
440 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
441 PrintFunctionDeclStart(FD);
442
443 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000444 Out << '\n';
445 // FIXME: convert dumper to use std::ostream?
Chris Lattnerb73abd52007-09-15 23:02:28 +0000446 FD->getBody()->dumpAll(*SM);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000447 Out << '\n';
Chris Lattnerb73abd52007-09-15 23:02:28 +0000448 }
449 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
450 PrintTypeDefDecl(TD);
451 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000452 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000453 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000454 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000455 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000456 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000457 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000458 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000459 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000460 Out << "Read objc fwd protocol decl\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000461 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000462 Out << "Read objc fwd class decl\n";
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000463 } else if (isa<FileScopeAsmDecl>(D)) {
464 Out << "Read file scope asm decl\n";
Ted Kremenek5d257d42008-03-14 17:31:00 +0000465 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
466 Out << "Read objc method decl: '" << MD->getSelector().getName()
467 << "'\n";
Steve Naroff045e0e02008-05-23 18:50:58 +0000468 if (MD->getBody()) {
469 // FIXME: convert dumper to use std::ostream?
470 MD->getBody()->dumpAll(*SM);
471 Out << '\n';
472 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000473 } else if (isa<ObjCImplementationDecl>(D)) {
474 Out << "Read objc implementation decl\n";
475 }
476 else {
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000477 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000478 }
479 }
480 };
Chris Lattner95578782007-08-08 22:51:59 +0000481}
482
Chris Lattnerb73abd52007-09-15 23:02:28 +0000483ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
484
Ted Kremeneke09391a2007-11-27 21:46:50 +0000485//===----------------------------------------------------------------------===//
486/// ASTViewer - AST Visualization
487
Ted Kremenekb6976a22007-09-19 21:29:43 +0000488namespace {
489 class ASTViewer : public ASTConsumer {
490 SourceManager *SM;
491 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000492 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000493 SM = &Context.getSourceManager();
Ted Kremenekb6976a22007-09-19 21:29:43 +0000494 }
495
496 virtual void HandleTopLevelDecl(Decl *D) {
497 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000498 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenekb6976a22007-09-19 21:29:43 +0000499
500 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000501 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000502 FD->getBody()->viewAST();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000503 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000504 }
505 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000506 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
507 DeclPrinter().PrintObjCMethodDecl(MD);
508
509 if (MD->getBody()) {
510 llvm::cerr << '\n';
511 MD->getBody()->viewAST();
512 llvm::cerr << '\n';
513 }
514 }
Ted Kremenekb6976a22007-09-19 21:29:43 +0000515 }
516 };
517}
518
519ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
520
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000521//===----------------------------------------------------------------------===//
Ted Kremenek397de012007-12-13 00:37:31 +0000522// AST Serializer
523
524namespace {
Ted Kremenek21189012007-12-19 23:49:37 +0000525
526class ASTSerializer : public ASTConsumer {
527protected:
Ted Kremenek863b01f2008-04-23 16:25:39 +0000528 TranslationUnit* TU;
529
Ted Kremenek21189012007-12-19 23:49:37 +0000530public:
Eli Friedmanb4563692008-06-09 20:02:51 +0000531 ASTSerializer() : TU(0) {}
532
533 virtual void InitializeTU(TranslationUnit &tu) {
534 TU = &tu;
Ted Kremenek21189012007-12-19 23:49:37 +0000535 }
Eli Friedmanb4563692008-06-09 20:02:51 +0000536
Ted Kremenek21189012007-12-19 23:49:37 +0000537};
Eli Friedmanb4563692008-06-09 20:02:51 +0000538
Ted Kremenek21189012007-12-19 23:49:37 +0000539class SingleFileSerializer : public ASTSerializer {
540 const llvm::sys::Path FName;
541public:
Eli Friedmanb4563692008-06-09 20:02:51 +0000542 SingleFileSerializer(const llvm::sys::Path& F) : FName(F) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000543
544 ~SingleFileSerializer() {
Ted Kremenek863b01f2008-04-23 16:25:39 +0000545 EmitASTBitcodeFile(TU, FName);
Ted Kremenek21189012007-12-19 23:49:37 +0000546 }
547};
548
549class BuildSerializer : public ASTSerializer {
550 llvm::sys::Path EmitDir;
551public:
Eli Friedmanb4563692008-06-09 20:02:51 +0000552 BuildSerializer(const llvm::sys::Path& dir) : EmitDir(dir) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000553
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000554 ~BuildSerializer() {
Ted Kremenek863b01f2008-04-23 16:25:39 +0000555
556 if (!TU)
557 return;
558
559 SourceManager& SourceMgr = TU->getContext().getSourceManager();
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000560 unsigned ID = SourceMgr.getMainFileID();
561 assert (ID && "MainFileID not set!");
562 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
563 assert (FE && "No FileEntry for main file.");
564
565 // FIXME: This is not portable to Windows.
566 // FIXME: This logic should probably be moved elsewhere later.
567
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000568 llvm::sys::Path FName(EmitDir);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000569
570 std::vector<char> buf;
571 buf.reserve(strlen(FE->getName())+100);
572
573 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000574 FName.appendComponent(&buf[0]);
575 FName.createDirectoryOnDisk(true);
576 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000577 assert (false && "Could not create 'device' serialization directory.");
578 return;
579 }
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000580
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000581 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000582 FName.appendComponent(&buf[0]);
Ted Kremenek863b01f2008-04-23 16:25:39 +0000583 EmitASTBitcodeFile(TU, FName);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000584
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000585 // Now emit the sources.
586
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000587 }
Ted Kremenek21189012007-12-19 23:49:37 +0000588};
589
590
Ted Kremenek397de012007-12-13 00:37:31 +0000591} // end anonymous namespace
592
593
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000594ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenek21189012007-12-19 23:49:37 +0000595 const std::string& OutputFile,
Ted Kremenek842126e2008-06-04 15:55:15 +0000596 Diagnostic &Diags) {
Ted Kremenekbde30332007-12-19 17:25:59 +0000597
Ted Kremenek21189012007-12-19 23:49:37 +0000598 if (OutputFile.size()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000599 if (InFile == "-") {
600 llvm::cerr <<
601 "error: Cannot use --serialize with -o for source read from STDIN.\n";
602 return NULL;
603 }
604
Ted Kremenek21189012007-12-19 23:49:37 +0000605 // The user specified an AST-emission directory. Determine if the path
606 // is absolute.
607 llvm::sys::Path EmitDir(OutputFile);
608
609 if (!EmitDir.isAbsolute()) {
610 llvm::cerr <<
611 "error: Output directory for --serialize must be an absolute path.\n";
612
613 return NULL;
614 }
615
616 // Create the directory if it does not exist.
617 EmitDir.createDirectoryOnDisk(true);
618 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
619 llvm::cerr <<
620 "error: Could not create output directory for --serialize.\n";
621
622 return NULL;
623 }
624
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000625 // FIXME: We should probably only allow using BuildSerializer when
626 // the ASTs come from parsed source files, and not from .ast files.
Eli Friedmanb4563692008-06-09 20:02:51 +0000627 return new BuildSerializer(EmitDir);
Ted Kremenek21189012007-12-19 23:49:37 +0000628 }
629
630 // The user did not specify an output directory for serialized ASTs.
631 // Serialize the translation to a single file whose name is the same
632 // as the input file with the ".ast" extension appended.
Ted Kremenekab749372007-12-19 19:27:38 +0000633
Ted Kremenek21189012007-12-19 23:49:37 +0000634 llvm::sys::Path FName(InFile.c_str());
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000635 FName.appendSuffix("ast");
Eli Friedmanb4563692008-06-09 20:02:51 +0000636 return new SingleFileSerializer(FName);
Ted Kremenek397de012007-12-13 00:37:31 +0000637}
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000638
639class LLVMCodeGenWriter : public ASTConsumer {
640 llvm::OwningPtr<CodeGenerator> Gen;
641 const std::string &InFile;
642 const std::string &OutputFile;
643 bool EmitBitcode;
644public:
645
646 LLVMCodeGenWriter(bool EmitBC, Diagnostic &Diags, const LangOptions &Features,
647 const std::string& infile, const std::string& outfile,
648 bool GenerateDebugInfo)
649 : Gen(CreateLLVMCodeGen(Diags, Features, infile, GenerateDebugInfo)),
650 InFile(infile), OutputFile(outfile), EmitBitcode(EmitBC) {}
651
652 virtual void Initialize(ASTContext &Context) {
653 Gen->Initialize(Context);
654 }
655
656 virtual void HandleTopLevelDecl(Decl *D) {
657 Gen->HandleTopLevelDecl(D);
658 }
659
660 virtual void HandleTagDeclDefinition(TagDecl *D) {
661 Gen->HandleTagDeclDefinition(D);
662 }
663
664 virtual ~LLVMCodeGenWriter() {
665 llvm::OwningPtr<llvm::Module> CodeGenModule(Gen->ReleaseModule());
666
667 if (!CodeGenModule)
668 return;
669
670 std::ostream *Out;
671
672 if (OutputFile == "-") {
673 Out = llvm::cout.stream();
674 } else if (!OutputFile.empty()) {
675 Out = new std::ofstream(OutputFile.c_str(),
676 std::ios_base::binary|std::ios_base::out);
677 } else if (InFile == "-") {
678 Out = llvm::cout.stream();
679 } else {
680 llvm::sys::Path Path(InFile);
681 Path.eraseSuffix();
682 if (!EmitBitcode)
683 Path.appendSuffix("ll");
684 else
685 Path.appendSuffix("bc");
686
687 Out = new std::ofstream(Path.toString().c_str(),
688 std::ios_base::binary|std::ios_base::out);
689 }
690
691 if (!EmitBitcode)
692 CodeGenModule->print(*Out);
693 else
694 llvm::WriteBitcodeToFile(CodeGenModule.get(), *Out);
695
696 if (Out != llvm::cout.stream())
697 delete Out;
698 }
699};
700
701ASTConsumer *clang::CreateLLVMCodeGenWriter(bool EmitBC, Diagnostic &Diags,
702 const LangOptions &Features,
703 const std::string& InFile,
704 const std::string& OutFile,
705 bool GenerateDebugInfo) {
706
707 return new LLVMCodeGenWriter(EmitBC, Diags, Features, InFile, OutFile,
708 GenerateDebugInfo);
709}