blob: 1e964f076e101f97bbeb558f3c374597e51cf91e [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())
Chris Lattner9dd07652008-08-22 06:59:15 +0000196 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000197
Chris Lattner9dd07652008-08-22 06:59:15 +0000198 std::string name = OMD->getSelector().getName();
199 std::string::size_type pos, lastPos = 0;
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!
Chris Lattner9dd07652008-08-22 06:59:15 +0000203 pos = name.find_first_of(":", lastPos);
204 Out << " " << name.substr(lastPos, pos - lastPos);
205 Out << ":(" << PDecl->getType().getAsString() << ")" << PDecl->getName();
206 lastPos = pos + 1;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000207 }
Chris Lattner9dd07652008-08-22 06:59:15 +0000208
209 if (OMD->getNumParams() == 0)
210 Out << " " << name;
211
212 if (OMD->isVariadic())
213 Out << ", ...";
214
215 Out << ";";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000216}
217
Ted Kremenek42730c52008-01-07 19:49:32 +0000218void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000219 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000220 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000221
222 if (SID)
223 Out << "@implementation " << I << " : " << SID->getName();
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000224 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000225 Out << "@implementation " << I;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000226
Ted Kremenek42730c52008-01-07 19:49:32 +0000227 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000228 E = OID->instmeth_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
Ted Kremenek42730c52008-01-07 19:49:32 +0000238 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000239 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000240 ObjCMethodDecl *OMD = *I;
241 PrintObjCMethodDecl(OMD);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000242 if (OMD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000243 Out << ' ';
244 OMD->getBody()->printPretty(Out);
245 Out << '\n';
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000246 }
247 }
248
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000249 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
250 E = OID->propimpl_end(); I != E; ++I)
251 PrintObjCPropertyImplDecl(*I);
252
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000253 Out << "@end\n";
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000254}
255
256
Ted Kremenek42730c52008-01-07 19:49:32 +0000257void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000258 std::string I = OID->getName();
Ted Kremenek42730c52008-01-07 19:49:32 +0000259 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000260
261 if (SID)
262 Out << "@interface " << I << " : " << SID->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000263 else
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000264 Out << "@interface " << I;
265
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000266 // Protocols?
Chris Lattner8bcb5252008-07-21 18:19:38 +0000267 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
268 if (!Protocols.empty()) {
269 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
270 E = Protocols.end(); I != E; ++I)
271 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getName();
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000272 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000273
Chris Lattner8bcb5252008-07-21 18:19:38 +0000274 if (!Protocols.empty())
275 Out << ">";
276 Out << '\n';
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000277
Chris Lattnerec4979b2008-03-16 21:08:55 +0000278 if (OID->ivar_size() > 0) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000279 Out << '{';
Ted Kremenek42730c52008-01-07 19:49:32 +0000280 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerc7b06752007-12-12 07:56:42 +0000281 E = OID->ivar_end(); I != E; ++I) {
282 Out << '\t' << (*I)->getType().getAsString()
283 << ' ' << (*I)->getName() << ";\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000284 }
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000285 Out << "}\n";
Fariborz Jahanianf468b312007-10-26 16:29:12 +0000286 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000287
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000288 for (ObjCInterfaceDecl::classprop_iterator I = OID->classprop_begin(),
289 E = OID->classprop_end(); I != E; ++I)
290 PrintObjCPropertyDecl(*I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000291 bool eol_needed = false;
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000292 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
293 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000294 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000295
296 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
297 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000298 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahaniandeeae052008-05-06 23:14:25 +0000299
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000300 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Narofffaed3bf2007-09-10 20:51:04 +0000301 // FIXME: implement the rest...
302}
303
Ted Kremenek42730c52008-01-07 19:49:32 +0000304void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000305 Out << "@protocol " << PID->getName() << '\n';
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000306
307 for (ObjCProtocolDecl::classprop_iterator I = PID->classprop_begin(),
308 E = PID->classprop_end(); I != E; ++I)
309 PrintObjCPropertyDecl(*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::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000315 Out << "@implementation "
316 << PID->getClassInterface()->getName()
317 << '(' << PID->getName() << ");\n";
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000318 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
319 E = PID->propimpl_end(); I != E; ++I)
320 PrintObjCPropertyImplDecl(*I);
321 Out << "@end\n";
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000322 // FIXME: implement the rest...
323}
324
Ted Kremenek42730c52008-01-07 19:49:32 +0000325void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000326 Out << "@interface "
327 << PID->getClassInterface()->getName()
328 << '(' << PID->getName() << ");\n";
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000329 // Output property declarations.
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000330 for (ObjCCategoryDecl::classprop_iterator I = PID->classprop_begin(),
331 E = PID->classprop_end(); I != E; ++I)
332 PrintObjCPropertyDecl(*I);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000333 Out << "@end\n";
334
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000335 // FIXME: implement the rest...
336}
337
Ted Kremenek42730c52008-01-07 19:49:32 +0000338void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000339 Out << "@compatibility_alias " << AID->getName()
340 << ' ' << AID->getClassInterface()->getName() << ";\n";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000341}
342
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000343/// PrintObjCPropertyDecl - print a property declaration.
344///
345void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +0000346 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
347 Out << "@required\n";
348 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
349 Out << "@optional\n";
350
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000351 Out << "@property";
352 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
353 bool first = true;
354 Out << " (";
355 if (PDecl->getPropertyAttributes() &
356 ObjCPropertyDecl::OBJC_PR_readonly) {
357 Out << (first ? ' ' : ',') << "readonly";
358 first = false;
359 }
360
361 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
362 Out << (first ? ' ' : ',') << "getter = "
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000363 << PDecl->getGetterName().getName();
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000364 first = false;
365 }
366 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
367 Out << (first ? ' ' : ',') << "setter = "
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +0000368 << PDecl->getSetterName().getName();
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000369 first = false;
370 }
371
372 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
373 Out << (first ? ' ' : ',') << "assign";
374 first = false;
375 }
376
377 if (PDecl->getPropertyAttributes() &
378 ObjCPropertyDecl::OBJC_PR_readwrite) {
379 Out << (first ? ' ' : ',') << "readwrite";
380 first = false;
381 }
382
383 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
384 Out << (first ? ' ' : ',') << "retain";
385 first = false;
386 }
387
388 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
389 Out << (first ? ' ' : ',') << "copy";
390 first = false;
391 }
392
393 if (PDecl->getPropertyAttributes() &
394 ObjCPropertyDecl::OBJC_PR_nonatomic) {
395 Out << (first ? ' ' : ',') << "nonatomic";
396 first = false;
397 }
398 Out << " )";
399 }
400 Out << ' ' << PDecl->getType().getAsString()
401 << ' ' << PDecl->getName();
402
403 Out << ";\n";
404}
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +0000405
406/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
407/// declaration syntax.
408///
409void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
410 if (PID->getPropertyImplementation() ==
411 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE)
412 Out << "\n@synthesize ";
413 else
414 Out << "\n@dynamic ";
415 Out << PID->getPropertyDecl()->getName();
416 if (PID->getPropertyIvarDecl())
417 Out << "=" << PID->getPropertyIvarDecl()->getName();
418 Out << ";\n";
419}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000420//===----------------------------------------------------------------------===//
421/// ASTPrinter - Pretty-printer of ASTs
422
Chris Lattnerb73abd52007-09-15 23:02:28 +0000423namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000424 class ASTPrinter : public ASTConsumer, public DeclPrinter {
425 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000426 ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000427
Chris Lattnerb73abd52007-09-15 23:02:28 +0000428 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner1c1aabb2008-01-02 21:04:16 +0000429 PrintDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +0000430 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000431 };
Chris Lattner4b009652007-07-25 00:24:17 +0000432}
Chris Lattner95578782007-08-08 22:51:59 +0000433
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000434ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
435 return new ASTPrinter(out);
436}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000437
438//===----------------------------------------------------------------------===//
439/// ASTDumper - Low-level dumper of ASTs
Chris Lattnerb73abd52007-09-15 23:02:28 +0000440
441namespace {
Ted Kremeneke09391a2007-11-27 21:46:50 +0000442 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattnerb73abd52007-09-15 23:02:28 +0000443 SourceManager *SM;
444 public:
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000445 ASTDumper() : DeclPrinter() {}
Ted Kremeneke09391a2007-11-27 21:46:50 +0000446
Ted Kremenek17861c52007-12-19 22:51:13 +0000447 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000448 SM = &Context.getSourceManager();
Chris Lattner95578782007-08-08 22:51:59 +0000449 }
Chris Lattnerb73abd52007-09-15 23:02:28 +0000450
451 virtual void HandleTopLevelDecl(Decl *D) {
452 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
453 PrintFunctionDeclStart(FD);
454
455 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000456 Out << '\n';
457 // FIXME: convert dumper to use std::ostream?
Chris Lattnerb73abd52007-09-15 23:02:28 +0000458 FD->getBody()->dumpAll(*SM);
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000459 Out << '\n';
Chris Lattnerb73abd52007-09-15 23:02:28 +0000460 }
461 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
462 PrintTypeDefDecl(TD);
463 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000464 Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000465 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000466 Out << "Read objc interface '" << OID->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000467 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000468 Out << "Read objc protocol '" << OPD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000469 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000470 Out << "Read objc category '" << OCD->getName() << "'\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000471 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000472 Out << "Read objc fwd protocol decl\n";
Ted Kremenek42730c52008-01-07 19:49:32 +0000473 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000474 Out << "Read objc fwd class decl\n";
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000475 } else if (isa<FileScopeAsmDecl>(D)) {
476 Out << "Read file scope asm decl\n";
Ted Kremenek5d257d42008-03-14 17:31:00 +0000477 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
478 Out << "Read objc method decl: '" << MD->getSelector().getName()
479 << "'\n";
Steve Naroff045e0e02008-05-23 18:50:58 +0000480 if (MD->getBody()) {
481 // FIXME: convert dumper to use std::ostream?
482 MD->getBody()->dumpAll(*SM);
483 Out << '\n';
484 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000485 } else if (isa<ObjCImplementationDecl>(D)) {
486 Out << "Read objc implementation decl\n";
487 }
488 else {
Chris Lattnerd5c9d3d2007-10-06 18:52:10 +0000489 assert(0 && "Unknown decl type!");
Chris Lattnerb73abd52007-09-15 23:02:28 +0000490 }
491 }
492 };
Chris Lattner95578782007-08-08 22:51:59 +0000493}
494
Chris Lattnerb73abd52007-09-15 23:02:28 +0000495ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
496
Ted Kremeneke09391a2007-11-27 21:46:50 +0000497//===----------------------------------------------------------------------===//
498/// ASTViewer - AST Visualization
499
Ted Kremenekb6976a22007-09-19 21:29:43 +0000500namespace {
501 class ASTViewer : public ASTConsumer {
502 SourceManager *SM;
503 public:
Ted Kremenek17861c52007-12-19 22:51:13 +0000504 void Initialize(ASTContext &Context) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000505 SM = &Context.getSourceManager();
Ted Kremenekb6976a22007-09-19 21:29:43 +0000506 }
507
508 virtual void HandleTopLevelDecl(Decl *D) {
509 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000510 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenekb6976a22007-09-19 21:29:43 +0000511
512 if (FD->getBody()) {
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000513 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000514 FD->getBody()->viewAST();
Ted Kremenekbe2ea3b2007-11-28 21:32:21 +0000515 llvm::cerr << '\n';
Ted Kremenekb6976a22007-09-19 21:29:43 +0000516 }
517 }
Ted Kremenek5d257d42008-03-14 17:31:00 +0000518 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
519 DeclPrinter().PrintObjCMethodDecl(MD);
520
521 if (MD->getBody()) {
522 llvm::cerr << '\n';
523 MD->getBody()->viewAST();
524 llvm::cerr << '\n';
525 }
526 }
Ted Kremenekb6976a22007-09-19 21:29:43 +0000527 }
528 };
529}
530
531ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
532
Ted Kremenek1e3c2022007-09-07 23:47:56 +0000533//===----------------------------------------------------------------------===//
Ted Kremenek397de012007-12-13 00:37:31 +0000534// AST Serializer
535
536namespace {
Ted Kremenek21189012007-12-19 23:49:37 +0000537
538class ASTSerializer : public ASTConsumer {
539protected:
Nico Weber034bd872008-08-09 18:32:11 +0000540 Diagnostic& Diags;
Ted Kremenek863b01f2008-04-23 16:25:39 +0000541
Ted Kremenek21189012007-12-19 23:49:37 +0000542public:
Nico Weber93eea802008-08-10 19:20:05 +0000543 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000544};
Eli Friedmanb4563692008-06-09 20:02:51 +0000545
Ted Kremenek21189012007-12-19 23:49:37 +0000546class SingleFileSerializer : public ASTSerializer {
547 const llvm::sys::Path FName;
548public:
Nico Weber034bd872008-08-09 18:32:11 +0000549 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
550 : ASTSerializer(diags), FName(F) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000551
Nico Weber93eea802008-08-10 19:20:05 +0000552 virtual void HandleTranslationUnit(TranslationUnit& TU) {
Nico Weber034bd872008-08-09 18:32:11 +0000553 if (Diags.hasErrorOccurred())
554 return;
Nico Weber93eea802008-08-10 19:20:05 +0000555 EmitASTBitcodeFile(&TU, FName);
Ted Kremenek21189012007-12-19 23:49:37 +0000556 }
557};
558
559class BuildSerializer : public ASTSerializer {
560 llvm::sys::Path EmitDir;
561public:
Nico Weber034bd872008-08-09 18:32:11 +0000562 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
563 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenek21189012007-12-19 23:49:37 +0000564
Nico Weber93eea802008-08-10 19:20:05 +0000565 virtual void HandleTranslationUnit(TranslationUnit& TU) {
566 if (Diags.hasErrorOccurred())
Ted Kremenek863b01f2008-04-23 16:25:39 +0000567 return;
568
Nico Weber93eea802008-08-10 19:20:05 +0000569 SourceManager& SourceMgr = TU.getContext().getSourceManager();
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000570 unsigned ID = SourceMgr.getMainFileID();
571 assert (ID && "MainFileID not set!");
572 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
573 assert (FE && "No FileEntry for main file.");
574
575 // FIXME: This is not portable to Windows.
576 // FIXME: This logic should probably be moved elsewhere later.
577
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000578 llvm::sys::Path FName(EmitDir);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000579
580 std::vector<char> buf;
581 buf.reserve(strlen(FE->getName())+100);
582
583 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000584 FName.appendComponent(&buf[0]);
585 FName.createDirectoryOnDisk(true);
586 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000587 assert (false && "Could not create 'device' serialization directory.");
588 return;
589 }
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000590
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000591 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000592 FName.appendComponent(&buf[0]);
Nico Weber93eea802008-08-10 19:20:05 +0000593 EmitASTBitcodeFile(&TU, FName);
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000594
Ted Kremenek0c7cd7a2007-12-20 19:47:16 +0000595 // Now emit the sources.
596
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000597 }
Ted Kremenek21189012007-12-19 23:49:37 +0000598};
599
600
Ted Kremenek397de012007-12-13 00:37:31 +0000601} // end anonymous namespace
602
603
Ted Kremenekd890f6a2007-12-19 22:24:34 +0000604ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenek21189012007-12-19 23:49:37 +0000605 const std::string& OutputFile,
Ted Kremenek842126e2008-06-04 15:55:15 +0000606 Diagnostic &Diags) {
Ted Kremenekbde30332007-12-19 17:25:59 +0000607
Ted Kremenek21189012007-12-19 23:49:37 +0000608 if (OutputFile.size()) {
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000609 if (InFile == "-") {
610 llvm::cerr <<
611 "error: Cannot use --serialize with -o for source read from STDIN.\n";
612 return NULL;
613 }
614
Ted Kremenek21189012007-12-19 23:49:37 +0000615 // The user specified an AST-emission directory. Determine if the path
616 // is absolute.
617 llvm::sys::Path EmitDir(OutputFile);
618
619 if (!EmitDir.isAbsolute()) {
620 llvm::cerr <<
621 "error: Output directory for --serialize must be an absolute path.\n";
622
623 return NULL;
624 }
625
626 // Create the directory if it does not exist.
627 EmitDir.createDirectoryOnDisk(true);
628 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
629 llvm::cerr <<
630 "error: Could not create output directory for --serialize.\n";
631
632 return NULL;
633 }
634
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000635 // FIXME: We should probably only allow using BuildSerializer when
636 // the ASTs come from parsed source files, and not from .ast files.
Nico Weber034bd872008-08-09 18:32:11 +0000637 return new BuildSerializer(EmitDir, Diags);
Ted Kremenek21189012007-12-19 23:49:37 +0000638 }
639
640 // The user did not specify an output directory for serialized ASTs.
641 // Serialize the translation to a single file whose name is the same
642 // as the input file with the ".ast" extension appended.
Ted Kremenekab749372007-12-19 19:27:38 +0000643
Ted Kremenek21189012007-12-19 23:49:37 +0000644 llvm::sys::Path FName(InFile.c_str());
Ted Kremenekfc17b8a2007-12-20 00:34:58 +0000645 FName.appendSuffix("ast");
Nico Weber034bd872008-08-09 18:32:11 +0000646 return new SingleFileSerializer(FName, Diags);
Ted Kremenek397de012007-12-13 00:37:31 +0000647}
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000648
649class LLVMCodeGenWriter : public ASTConsumer {
650 llvm::OwningPtr<CodeGenerator> Gen;
651 const std::string &InFile;
652 const std::string &OutputFile;
653 bool EmitBitcode;
654public:
655
656 LLVMCodeGenWriter(bool EmitBC, Diagnostic &Diags, const LangOptions &Features,
657 const std::string& infile, const std::string& outfile,
658 bool GenerateDebugInfo)
659 : Gen(CreateLLVMCodeGen(Diags, Features, infile, GenerateDebugInfo)),
660 InFile(infile), OutputFile(outfile), EmitBitcode(EmitBC) {}
661
662 virtual void Initialize(ASTContext &Context) {
663 Gen->Initialize(Context);
664 }
Matthijs Kooijman211dbe22008-08-07 16:04:15 +0000665
666 virtual void InitializeTU(TranslationUnit& TU) {
667 Gen->InitializeTU(TU);
668 }
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000669
670 virtual void HandleTopLevelDecl(Decl *D) {
671 Gen->HandleTopLevelDecl(D);
672 }
673
Matthijs Kooijman211dbe22008-08-07 16:04:15 +0000674 virtual void HandleTranslationUnit(TranslationUnit& TU) {
675 Gen->HandleTranslationUnit(TU);
676 }
677
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000678 virtual void HandleTagDeclDefinition(TagDecl *D) {
679 Gen->HandleTagDeclDefinition(D);
680 }
Matthijs Kooijman211dbe22008-08-07 16:04:15 +0000681
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000682 virtual ~LLVMCodeGenWriter() {
683 llvm::OwningPtr<llvm::Module> CodeGenModule(Gen->ReleaseModule());
684
685 if (!CodeGenModule)
686 return;
687
688 std::ostream *Out;
689
690 if (OutputFile == "-") {
691 Out = llvm::cout.stream();
692 } else if (!OutputFile.empty()) {
693 Out = new std::ofstream(OutputFile.c_str(),
694 std::ios_base::binary|std::ios_base::out);
695 } else if (InFile == "-") {
696 Out = llvm::cout.stream();
697 } else {
698 llvm::sys::Path Path(InFile);
699 Path.eraseSuffix();
700 if (!EmitBitcode)
701 Path.appendSuffix("ll");
702 else
703 Path.appendSuffix("bc");
704
705 Out = new std::ofstream(Path.toString().c_str(),
706 std::ios_base::binary|std::ios_base::out);
707 }
708
709 if (!EmitBitcode)
710 CodeGenModule->print(*Out);
711 else
712 llvm::WriteBitcodeToFile(CodeGenModule.get(), *Out);
713
714 if (Out != llvm::cout.stream())
715 delete Out;
716 }
717};
718
719ASTConsumer *clang::CreateLLVMCodeGenWriter(bool EmitBC, Diagnostic &Diags,
720 const LangOptions &Features,
721 const std::string& InFile,
722 const std::string& OutFile,
723 bool GenerateDebugInfo) {
724
725 return new LLVMCodeGenWriter(EmitBC, Diags, Features, InFile, OutFile,
726 GenerateDebugInfo);
727}