blob: efeb9be8929462d55a2f682b8795c6d9a5546d8c [file] [log] [blame]
Chris Lattner97e8b6f2007-10-07 06:04:32 +00001//===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner97e8b6f2007-10-07 06:04:32 +000010// AST Consumer Implementations.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner97e8b6f2007-10-07 06:04:32 +000014#include "ASTConsumers.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000015#include "HTMLDiagnostics.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000016#include "clang/AST/TranslationUnit.h"
Ted Kremenek54117722007-12-20 00:34:58 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000020#include "clang/AST/ASTConsumer.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000021#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000022#include "llvm/Support/Timer.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000023#include "llvm/ADT/OwningPtr.h"
24
Chris Lattner6000dac2007-08-08 22:51:59 +000025using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000026
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000027//===----------------------------------------------------------------------===//
28/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000029
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000030namespace {
31 class DeclPrinter {
32 public:
Ted Kremenekea75c552007-11-28 21:32:21 +000033 std::ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000034
Chris Lattner4b1daf02008-01-10 01:43:14 +000035 DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
Ted Kremenekea75c552007-11-28 21:32:21 +000036 DeclPrinter() : Out(*llvm::cerr.stream()) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000037
Chris Lattneref5a85d2008-01-02 21:04:16 +000038 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000039 void PrintFunctionDeclStart(FunctionDecl *FD);
40 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000041 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000042 void PrintObjCMethodDecl(ObjCMethodDecl *OMD);
43 void PrintObjCImplementationDecl(ObjCImplementationDecl *OID);
44 void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID);
45 void PrintObjCProtocolDecl(ObjCProtocolDecl *PID);
46 void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID);
47 void PrintObjCCategoryDecl(ObjCCategoryDecl *PID);
48 void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +000049 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +000050 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000051 };
52} // end anonymous namespace
53
Chris Lattneref5a85d2008-01-02 21:04:16 +000054void DeclPrinter:: PrintDecl(Decl *D) {
55 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
56 PrintFunctionDeclStart(FD);
57
58 if (FD->getBody()) {
59 Out << ' ';
60 FD->getBody()->printPretty(Out);
61 Out << '\n';
62 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000063 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000064 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000065 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000066 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
67 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000068 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
69 PrintObjCInterfaceDecl(OID);
70 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
71 PrintObjCProtocolDecl(PID);
72 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000073 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000074 Out << "@protocol ";
75 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000077 if (i) Out << ", ";
78 Out << D->getName();
79 }
80 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000081 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000082 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000083 PrintObjCImplementationDecl(OID);
84 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000085 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000086 PrintObjCCategoryImplDecl(OID);
87 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000088 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000089 PrintObjCCategoryDecl(OID);
90 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000091 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000092 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +000093 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
94 Out << "@class ";
95 ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls();
96 for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) {
97 const ObjCInterfaceDecl *D = ForwardDecls[i];
98 if (i) Out << ", ";
99 Out << D->getName();
100 }
101 Out << ";\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000102 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
103 Out << "Read top-level tag decl: '" << TD->getName() << "'\n";
104 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
105 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000106 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
107 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000108 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
109 Out << "asm(";
110 AD->getAsmString()->printPretty(Out);
111 Out << ")\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000112 } else {
113 assert(0 && "Unknown decl type!");
114 }
115}
116
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000117void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 bool HasBody = FD->getBody();
119
Ted Kremenekea75c552007-11-28 21:32:21 +0000120 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000121
122 switch (FD->getStorageClass()) {
123 default: assert(0 && "Unknown storage class");
124 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000125 case FunctionDecl::Extern: Out << "extern "; break;
126 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000127 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000128 }
129
130 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000131 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 std::string Proto = FD->getName();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000134 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000135
Chris Lattner0d6ca112007-12-03 21:43:25 +0000136 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 Proto += "(";
138 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
139 if (i) Proto += ", ";
140 std::string ParamStr;
141 if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
142
143 FT->getArgType(i).getAsStringInternal(ParamStr);
144 Proto += ParamStr;
145 }
146
147 if (FT->isVariadic()) {
148 if (FD->getNumParams()) Proto += ", ";
149 Proto += "...";
150 }
151 Proto += ")";
152 } else {
153 assert(isa<FunctionTypeNoProto>(AFT));
154 Proto += "()";
155 }
156
157 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000158 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000159
Chris Lattner6000dac2007-08-08 22:51:59 +0000160 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000161 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000162 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000163}
164
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000165void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 std::string S = TD->getName();
167 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000168 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000169}
170
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000171void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
172 const char *l;
173 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
174 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000175 else {
176 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
177 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000178 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000179 }
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000180 Out << "extern \"" << l << "\" { ";
181 PrintDecl(LS->getDecl());
182 Out << "}\n";
183}
184
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000186 if (OMD->isInstance())
Ted Kremenekea75c552007-11-28 21:32:21 +0000187 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000188 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000189 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000190 if (!OMD->getResultType().isNull())
Ted Kremenekea75c552007-11-28 21:32:21 +0000191 Out << '(' << OMD->getResultType().getAsString() << ") ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000192 // FIXME: just print original selector name!
Ted Kremenekea75c552007-11-28 21:32:21 +0000193 Out << OMD->getSelector().getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000194
Chris Lattner58cce3b2008-03-16 01:07:14 +0000195 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000196 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000197 // FIXME: selector is missing here!
198 Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000199 }
200}
201
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000202void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000203 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000204 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000205
206 if (SID)
207 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000208 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000209 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000210
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000211 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000212 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000213 ObjCMethodDecl *OMD = *I;
214 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000215 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000216 Out << ' ';
217 OMD->getBody()->printPretty(Out);
218 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000219 }
220 }
221
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000222 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000223 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000224 ObjCMethodDecl *OMD = *I;
225 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000226 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000227 Out << ' ';
228 OMD->getBody()->printPretty(Out);
229 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000230 }
231 }
232
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000233 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
234 E = OID->propimpl_end(); I != E; ++I)
235 PrintObjCPropertyImplDecl(*I);
236
Ted Kremenekea75c552007-11-28 21:32:21 +0000237 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000238}
239
240
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000241void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000242 std::string I = OID->getName();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000243 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000244
245 if (SID)
246 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000247 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000248 Out << "@interface " << I;
249
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000250 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000251 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
252 if (!Protocols.empty()) {
253 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
254 E = Protocols.end(); I != E; ++I)
255 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getName();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000256 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000257
Chris Lattner3db6cae2008-07-21 18:19:38 +0000258 if (!Protocols.empty())
259 Out << ">";
260 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000261
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000262 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000263 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000264 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000265 E = OID->ivar_end(); I != E; ++I) {
266 Out << '\t' << (*I)->getType().getAsString()
267 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000268 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000269 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000270 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000271
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000272 for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(),
273 E = OID->classprop_end(); I != E; ++I)
274 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000275 bool eol_needed = false;
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000276 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
277 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000278 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000279
280 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
281 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000282 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000283
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000284 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000285 // FIXME: implement the rest...
286}
287
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000288void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000289 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000290
291 for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(),
292 E = PID->classprop_end(); I != E; ++I)
293 PrintObjCPropertyDecl(*I);
294 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000295 // FIXME: implement the rest...
296}
297
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000298void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000299 Out << "@implementation "
300 << PID->getClassInterface()->getName()
301 << '(' << PID->getName() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000302 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
303 E = PID->propimpl_end(); I != E; ++I)
304 PrintObjCPropertyImplDecl(*I);
305 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000306 // FIXME: implement the rest...
307}
308
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000309void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000310 Out << "@interface "
311 << PID->getClassInterface()->getName()
312 << '(' << PID->getName() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000313 // Output property declarations.
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000314 for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(),
315 E = PID->classprop_end(); I != E; ++I)
316 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000317 Out << "@end\n";
318
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000319 // FIXME: implement the rest...
320}
321
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000322void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000323 Out << "@compatibility_alias " << AID->getName()
324 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000325}
326
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000327/// PrintObjCPropertyDecl - print a property declaration.
328///
329void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000330 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
331 Out << "@required\n";
332 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
333 Out << "@optional\n";
334
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000335 Out << "@property";
336 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
337 bool first = true;
338 Out << " (";
339 if (PDecl->getPropertyAttributes() &
340 ObjCPropertyDecl::OBJC_PR_readonly) {
341 Out << (first ? ' ' : ',') << "readonly";
342 first = false;
343 }
344
345 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
346 Out << (first ? ' ' : ',') << "getter = "
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000347 << PDecl->getGetterName().getName();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000348 first = false;
349 }
350 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
351 Out << (first ? ' ' : ',') << "setter = "
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000352 << PDecl->getSetterName().getName();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000353 first = false;
354 }
355
356 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
357 Out << (first ? ' ' : ',') << "assign";
358 first = false;
359 }
360
361 if (PDecl->getPropertyAttributes() &
362 ObjCPropertyDecl::OBJC_PR_readwrite) {
363 Out << (first ? ' ' : ',') << "readwrite";
364 first = false;
365 }
366
367 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
368 Out << (first ? ' ' : ',') << "retain";
369 first = false;
370 }
371
372 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
373 Out << (first ? ' ' : ',') << "copy";
374 first = false;
375 }
376
377 if (PDecl->getPropertyAttributes() &
378 ObjCPropertyDecl::OBJC_PR_nonatomic) {
379 Out << (first ? ' ' : ',') << "nonatomic";
380 first = false;
381 }
382 Out << " )";
383 }
384 Out << ' ' << PDecl->getType().getAsString()
385 << ' ' << PDecl->getName();
386
387 Out << ";\n";
388}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000389
390/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
391/// declaration syntax.
392///
393void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
394 if (PID->getPropertyImplementation() ==
395 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE)
396 Out << "\n@synthesize ";
397 else
398 Out << "\n@dynamic ";
399 Out << PID->getPropertyDecl()->getName();
400 if (PID->getPropertyIvarDecl())
401 Out << "=" << PID->getPropertyIvarDecl()->getName();
402 Out << ";\n";
403}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000404//===----------------------------------------------------------------------===//
405/// ASTPrinter - Pretty-printer of ASTs
406
Chris Lattner3d4997d2007-09-15 23:02:28 +0000407namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000408 class ASTPrinter : public ASTConsumer, public DeclPrinter {
409 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000410 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000411
Chris Lattner3d4997d2007-09-15 23:02:28 +0000412 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000413 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000415 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000416}
Chris Lattner6000dac2007-08-08 22:51:59 +0000417
Ted Kremenekea75c552007-11-28 21:32:21 +0000418ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
419 return new ASTPrinter(out);
420}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000421
422//===----------------------------------------------------------------------===//
423/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000424
425namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000426 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000427 SourceManager *SM;
428 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000429 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000430
Ted Kremenek95041a22007-12-19 22:51:13 +0000431 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000432 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000433 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000434
435 virtual void HandleTopLevelDecl(Decl *D) {
436 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
437 PrintFunctionDeclStart(FD);
438
439 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000440 Out << '\n';
441 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000442 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000443 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000444 }
445 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
446 PrintTypeDefDecl(TD);
447 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000448 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000449 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000450 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000451 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000452 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000453 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000454 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000455 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000456 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000457 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000458 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000459 } else if (isa<FileScopeAsmDecl>(D)) {
460 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000461 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
462 Out << "Read objc method decl: '" << MD->getSelector().getName()
463 << "'\n";
Steve Naroff1a2b90d2008-05-23 18:50:58 +0000464 if (MD->getBody()) {
465 // FIXME: convert dumper to use std::ostream?
466 MD->getBody()->dumpAll(*SM);
467 Out << '\n';
468 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000469 } else if (isa<ObjCImplementationDecl>(D)) {
470 Out << "Read objc implementation decl\n";
471 }
472 else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000473 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000474 }
475 }
476 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000477}
478
Chris Lattner3d4997d2007-09-15 23:02:28 +0000479ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
480
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000481//===----------------------------------------------------------------------===//
482/// ASTViewer - AST Visualization
483
Ted Kremenek80de08f2007-09-19 21:29:43 +0000484namespace {
485 class ASTViewer : public ASTConsumer {
486 SourceManager *SM;
487 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000488 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000489 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000490 }
491
492 virtual void HandleTopLevelDecl(Decl *D) {
493 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000494 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000495
496 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000497 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000498 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000499 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000500 }
501 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000502 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
503 DeclPrinter().PrintObjCMethodDecl(MD);
504
505 if (MD->getBody()) {
506 llvm::cerr << '\n';
507 MD->getBody()->viewAST();
508 llvm::cerr << '\n';
509 }
510 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000511 }
512 };
513}
514
515ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
516
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000517//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000518// AST Serializer
519
520namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000521
522class ASTSerializer : public ASTConsumer {
523protected:
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000524 TranslationUnit* TU;
525
Ted Kremenekf06c9282007-12-19 23:49:37 +0000526public:
Eli Friedmand8a65c12008-06-09 20:02:51 +0000527 ASTSerializer() : TU(0) {}
528
529 virtual void InitializeTU(TranslationUnit &tu) {
530 TU = &tu;
Ted Kremenekf06c9282007-12-19 23:49:37 +0000531 }
Eli Friedmand8a65c12008-06-09 20:02:51 +0000532
Ted Kremenekf06c9282007-12-19 23:49:37 +0000533};
Eli Friedmand8a65c12008-06-09 20:02:51 +0000534
Ted Kremenekf06c9282007-12-19 23:49:37 +0000535class SingleFileSerializer : public ASTSerializer {
536 const llvm::sys::Path FName;
537public:
Eli Friedmand8a65c12008-06-09 20:02:51 +0000538 SingleFileSerializer(const llvm::sys::Path& F) : FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000539
540 ~SingleFileSerializer() {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000541 EmitASTBitcodeFile(TU, FName);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000542 }
543};
544
545class BuildSerializer : public ASTSerializer {
546 llvm::sys::Path EmitDir;
547public:
Eli Friedmand8a65c12008-06-09 20:02:51 +0000548 BuildSerializer(const llvm::sys::Path& dir) : EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000549
Ted Kremenek54117722007-12-20 00:34:58 +0000550 ~BuildSerializer() {
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000551
552 if (!TU)
553 return;
554
555 SourceManager& SourceMgr = TU->getContext().getSourceManager();
Ted Kremenek54117722007-12-20 00:34:58 +0000556 unsigned ID = SourceMgr.getMainFileID();
557 assert (ID && "MainFileID not set!");
558 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
559 assert (FE && "No FileEntry for main file.");
560
561 // FIXME: This is not portable to Windows.
562 // FIXME: This logic should probably be moved elsewhere later.
563
Ted Kremenekee533642007-12-20 19:47:16 +0000564 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000565
566 std::vector<char> buf;
567 buf.reserve(strlen(FE->getName())+100);
568
569 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000570 FName.appendComponent(&buf[0]);
571 FName.createDirectoryOnDisk(true);
572 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000573 assert (false && "Could not create 'device' serialization directory.");
574 return;
575 }
Ted Kremenekee533642007-12-20 19:47:16 +0000576
Ted Kremenek54117722007-12-20 00:34:58 +0000577 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000578 FName.appendComponent(&buf[0]);
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000579 EmitASTBitcodeFile(TU, FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000580
Ted Kremenekee533642007-12-20 19:47:16 +0000581 // Now emit the sources.
582
Ted Kremenek54117722007-12-20 00:34:58 +0000583 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000584};
585
586
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000587} // end anonymous namespace
588
589
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000590ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000591 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000592 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000593
Ted Kremenekf06c9282007-12-19 23:49:37 +0000594 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000595 if (InFile == "-") {
596 llvm::cerr <<
597 "error: Cannot use --serialize with -o for source read from STDIN.\n";
598 return NULL;
599 }
600
Ted Kremenekf06c9282007-12-19 23:49:37 +0000601 // The user specified an AST-emission directory. Determine if the path
602 // is absolute.
603 llvm::sys::Path EmitDir(OutputFile);
604
605 if (!EmitDir.isAbsolute()) {
606 llvm::cerr <<
607 "error: Output directory for --serialize must be an absolute path.\n";
608
609 return NULL;
610 }
611
612 // Create the directory if it does not exist.
613 EmitDir.createDirectoryOnDisk(true);
614 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
615 llvm::cerr <<
616 "error: Could not create output directory for --serialize.\n";
617
618 return NULL;
619 }
620
Ted Kremenek54117722007-12-20 00:34:58 +0000621 // FIXME: We should probably only allow using BuildSerializer when
622 // the ASTs come from parsed source files, and not from .ast files.
Eli Friedmand8a65c12008-06-09 20:02:51 +0000623 return new BuildSerializer(EmitDir);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000624 }
625
626 // The user did not specify an output directory for serialized ASTs.
627 // Serialize the translation to a single file whose name is the same
628 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000629
Ted Kremenekf06c9282007-12-19 23:49:37 +0000630 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000631 FName.appendSuffix("ast");
Eli Friedmand8a65c12008-06-09 20:02:51 +0000632 return new SingleFileSerializer(FName);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000633}