blob: 78b199680ae2d606868a70b7f2ed6e5e5087d0cd [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 Kremenekad99dbf2008-11-03 22:31:48 +000015#include "clang/Driver/PathDiagnosticClients.h"
Ted Kremenek77cda502007-12-18 21:34:28 +000016#include "clang/AST/TranslationUnit.h"
Nico Weberdae86962008-08-09 18:32:11 +000017#include "clang/Basic/Diagnostic.h"
Ted Kremenek54117722007-12-20 00:34:58 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/AST/AST.h"
Chris Lattner3d4997d2007-09-15 23:02:28 +000021#include "clang/AST/ASTConsumer.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000022#include "clang/CodeGen/ModuleBuilder.h"
23#include "llvm/Module.h"
Daniel Dunbard46075f2008-10-21 23:54:00 +000024#include "llvm/Support/Streams.h"
Ted Kremenekcb330932008-02-18 21:21:23 +000025#include "llvm/Support/Timer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000027
Chris Lattner6000dac2007-08-08 22:51:59 +000028using namespace clang;
Reid Spencer5f016e22007-07-11 17:01:13 +000029
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000030//===----------------------------------------------------------------------===//
31/// DeclPrinter - Utility class for printing top-level decls.
Chris Lattner6000dac2007-08-08 22:51:59 +000032
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000033namespace {
34 class DeclPrinter {
35 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +000036 llvm::raw_ostream& Out;
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000037
Ted Kremeneka95d3752008-09-13 05:16:45 +000038 DeclPrinter(llvm::raw_ostream* out) : Out(out ? *out : llvm::errs()) {}
39 DeclPrinter() : Out(llvm::errs()) {}
40 virtual ~DeclPrinter();
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000041
Chris Lattneref5a85d2008-01-02 21:04:16 +000042 void PrintDecl(Decl *D);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000043 void PrintFunctionDeclStart(FunctionDecl *FD);
44 void PrintTypeDefDecl(TypedefDecl *TD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +000045 void PrintLinkageSpec(LinkageSpecDecl *LS);
Ted Kremeneka526c5c2008-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 Jahanian3dd4ba42008-04-17 18:25:18 +000053 void PrintObjCPropertyDecl(ObjCPropertyDecl *PD);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +000054 void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID);
Douglas Gregoraaba5e32009-02-04 19:02:06 +000055
56 void PrintTemplateDecl(TemplateDecl *TD);
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +000057 };
58} // end anonymous namespace
59
Ted Kremeneka95d3752008-09-13 05:16:45 +000060DeclPrinter::~DeclPrinter() {
61 Out.flush();
62}
63
Chris Lattneref5a85d2008-01-02 21:04:16 +000064void DeclPrinter:: PrintDecl(Decl *D) {
65 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
66 PrintFunctionDeclStart(FD);
67
68 if (FD->getBody()) {
69 Out << ' ';
70 FD->getBody()->printPretty(Out);
71 Out << '\n';
72 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000073 } else if (isa<ObjCMethodDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000074 // Do nothing, methods definitions are printed in
Ted Kremeneka526c5c2008-01-07 19:49:32 +000075 // PrintObjCImplementationDecl.
Chris Lattneref5a85d2008-01-02 21:04:16 +000076 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
77 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000078 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
79 PrintObjCInterfaceDecl(OID);
80 } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) {
81 PrintObjCProtocolDecl(PID);
82 } else if (ObjCForwardProtocolDecl *OFPD =
Chris Lattnerc81c8142008-02-25 21:04:36 +000083 dyn_cast<ObjCForwardProtocolDecl>(D)) {
Chris Lattneref5a85d2008-01-02 21:04:16 +000084 Out << "@protocol ";
85 for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000086 const ObjCProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
Chris Lattneref5a85d2008-01-02 21:04:16 +000087 if (i) Out << ", ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000088 Out << D->getNameAsString();
Chris Lattneref5a85d2008-01-02 21:04:16 +000089 }
90 Out << ";\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +000091 } else if (ObjCImplementationDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000092 dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000093 PrintObjCImplementationDecl(OID);
94 } else if (ObjCCategoryImplDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000095 dyn_cast<ObjCCategoryImplDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000096 PrintObjCCategoryImplDecl(OID);
97 } else if (ObjCCategoryDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +000098 dyn_cast<ObjCCategoryDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000099 PrintObjCCategoryDecl(OID);
100 } else if (ObjCCompatibleAliasDecl *OID =
Chris Lattnerc81c8142008-02-25 21:04:36 +0000101 dyn_cast<ObjCCompatibleAliasDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000102 PrintObjCCompatibleAliasDecl(OID);
Chris Lattner23a0e452008-06-21 21:40:20 +0000103 } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) {
104 Out << "@class ";
105 ObjCInterfaceDecl **ForwardDecls = OFCD->getForwardDecls();
106 for (unsigned i = 0, e = OFCD->getNumForwardDecls(); i != e; ++i) {
107 const ObjCInterfaceDecl *D = ForwardDecls[i];
108 if (i) Out << ", ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000109 Out << D->getNameAsString();
Chris Lattner23a0e452008-06-21 21:40:20 +0000110 }
111 Out << ";\n";
Douglas Gregor45579f52008-12-17 02:04:30 +0000112 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
113 Out << "enum " << ED->getNameAsString() << " {\n";
114 for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(),
115 EEnd = ED->enumerator_end();
116 E != EEnd; ++E)
117 Out << " " << (*E)->getNameAsString() << ",\n";
118 Out << "};\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000119 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000120 Out << "Read top-level tag decl: '" << TD->getNameAsString() << "'\n";
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000121 } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) {
122 PrintTemplateDecl(TempD);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000123 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
124 PrintLinkageSpec(LSD);
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000125 } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
126 Out << "asm(";
127 AD->getAsmString()->printPretty(Out);
128 Out << ")\n";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000129 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
130 Out << "Read top-level variable decl: '" << ND->getNameAsString() << "'\n";
Chris Lattneref5a85d2008-01-02 21:04:16 +0000131 } else {
132 assert(0 && "Unknown decl type!");
133 }
134}
135
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000136void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 bool HasBody = FD->getBody();
138
Ted Kremenekea75c552007-11-28 21:32:21 +0000139 Out << '\n';
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000140
141 switch (FD->getStorageClass()) {
142 default: assert(0 && "Unknown storage class");
143 case FunctionDecl::None: break;
Ted Kremenekea75c552007-11-28 21:32:21 +0000144 case FunctionDecl::Extern: Out << "extern "; break;
145 case FunctionDecl::Static: Out << "static "; break;
Ted Kremenek24bd3c42008-04-15 03:57:09 +0000146 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000147 }
148
149 if (FD->isInline())
Ted Kremenekea75c552007-11-28 21:32:21 +0000150 Out << "inline ";
Chris Lattner70c8b2e2007-08-26 04:02:13 +0000151
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000152 std::string Proto = FD->getNameAsString();
Chris Lattner0d6ca112007-12-03 21:43:25 +0000153 const FunctionType *AFT = FD->getType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000154
Chris Lattner0d6ca112007-12-03 21:43:25 +0000155 if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 Proto += "(";
157 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
158 if (i) Proto += ", ";
159 std::string ParamStr;
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000160 if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000161
162 FT->getArgType(i).getAsStringInternal(ParamStr);
163 Proto += ParamStr;
164 }
165
166 if (FT->isVariadic()) {
167 if (FD->getNumParams()) Proto += ", ";
168 Proto += "...";
169 }
170 Proto += ")";
171 } else {
172 assert(isa<FunctionTypeNoProto>(AFT));
173 Proto += "()";
174 }
175
176 AFT->getResultType().getAsStringInternal(Proto);
Ted Kremenekea75c552007-11-28 21:32:21 +0000177 Out << Proto;
Reid Spencer5f016e22007-07-11 17:01:13 +0000178
Chris Lattner6000dac2007-08-08 22:51:59 +0000179 if (!FD->getBody())
Ted Kremenekea75c552007-11-28 21:32:21 +0000180 Out << ";\n";
Chris Lattner6000dac2007-08-08 22:51:59 +0000181 // Doesn't print the body.
Reid Spencer5f016e22007-07-11 17:01:13 +0000182}
183
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000184void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000185 std::string S = TD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 TD->getUnderlyingType().getAsStringInternal(S);
Ted Kremenekea75c552007-11-28 21:32:21 +0000187 Out << "typedef " << S << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000188}
189
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000190void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) {
191 const char *l;
192 if (LS->getLanguage() == LinkageSpecDecl::lang_c)
193 l = "C";
Chris Lattner06767512008-04-08 05:52:18 +0000194 else {
195 assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx &&
196 "unknown language in linkage specification");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000197 l = "C++";
Chris Lattner06767512008-04-08 05:52:18 +0000198 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000199
200 Out << "extern \"" << l << "\" ";
201 if (LS->hasBraces())
202 Out << "{\n";
203
Douglas Gregor074149e2009-01-05 19:45:36 +0000204 for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(),
205 DEnd = LS->decls_end();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000206 D != DEnd; ++D)
207 PrintDecl(*D);
208
209 if (LS->hasBraces())
210 Out << "}";
211 Out << "\n";
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000212}
213
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000214void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000215 if (OMD->isInstanceMethod())
Ted Kremenekea75c552007-11-28 21:32:21 +0000216 Out << "\n- ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000217 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000218 Out << "\n+ ";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000219 if (!OMD->getResultType().isNull())
Chris Lattnerefe8a962008-08-22 06:59:15 +0000220 Out << '(' << OMD->getResultType().getAsString() << ")";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000221
Chris Lattner077bf5e2008-11-24 03:33:13 +0000222 std::string name = OMD->getSelector().getAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000223 std::string::size_type pos, lastPos = 0;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000224 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i) {
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000225 ParmVarDecl *PDecl = OMD->getParamDecl(i);
Ted Kremenekea75c552007-11-28 21:32:21 +0000226 // FIXME: selector is missing here!
Chris Lattnerefe8a962008-08-22 06:59:15 +0000227 pos = name.find_first_of(":", lastPos);
228 Out << " " << name.substr(lastPos, pos - lastPos);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000229 Out << ":(" << PDecl->getType().getAsString() << ")"
230 << PDecl->getNameAsString();
Chris Lattnerefe8a962008-08-22 06:59:15 +0000231 lastPos = pos + 1;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000232 }
Chris Lattnerefe8a962008-08-22 06:59:15 +0000233
234 if (OMD->getNumParams() == 0)
235 Out << " " << name;
236
237 if (OMD->isVariadic())
238 Out << ", ...";
239
240 Out << ";";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000241}
242
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000243void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000244 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000245 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000246
247 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000248 Out << "@implementation " << I << " : " << SID->getNameAsString();
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000249 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000250 Out << "@implementation " << I;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000251
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000252 for (ObjCImplementationDecl::instmeth_iterator I = OID->instmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000253 E = OID->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000254 ObjCMethodDecl *OMD = *I;
255 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000256 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000257 Out << ' ';
258 OMD->getBody()->printPretty(Out);
259 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000260 }
261 }
262
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000263 for (ObjCImplementationDecl::classmeth_iterator I = OID->classmeth_begin(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000264 E = OID->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000265 ObjCMethodDecl *OMD = *I;
266 PrintObjCMethodDecl(OMD);
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000267 if (OMD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000268 Out << ' ';
269 OMD->getBody()->printPretty(Out);
270 Out << '\n';
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000271 }
272 }
273
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000274 for (ObjCImplementationDecl::propimpl_iterator I = OID->propimpl_begin(),
275 E = OID->propimpl_end(); I != E; ++I)
276 PrintObjCPropertyImplDecl(*I);
277
Ted Kremenekea75c552007-11-28 21:32:21 +0000278 Out << "@end\n";
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000279}
280
281
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000282void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000283 std::string I = OID->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000284 ObjCInterfaceDecl *SID = OID->getSuperClass();
Ted Kremenekea75c552007-11-28 21:32:21 +0000285
286 if (SID)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000287 Out << "@interface " << I << " : " << SID->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000288 else
Ted Kremenekea75c552007-11-28 21:32:21 +0000289 Out << "@interface " << I;
290
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000291 // Protocols?
Chris Lattner3db6cae2008-07-21 18:19:38 +0000292 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
293 if (!Protocols.empty()) {
294 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
295 E = Protocols.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000296 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000297 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000298
Chris Lattner3db6cae2008-07-21 18:19:38 +0000299 if (!Protocols.empty())
300 Out << ">";
301 Out << '\n';
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000302
Chris Lattnerf3a7af92008-03-16 21:08:55 +0000303 if (OID->ivar_size() > 0) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000304 Out << '{';
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000305 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
Chris Lattnerbe6df082007-12-12 07:56:42 +0000306 E = OID->ivar_end(); I != E; ++I) {
307 Out << '\t' << (*I)->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000308 << ' ' << (*I)->getNameAsString() << ";\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000309 }
Ted Kremenekea75c552007-11-28 21:32:21 +0000310 Out << "}\n";
Fariborz Jahanianedcfb422007-10-26 16:29:12 +0000311 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000312
Steve Naroff09c47192009-01-09 15:36:25 +0000313 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(),
314 E = OID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000315 PrintObjCPropertyDecl(*I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000316 bool eol_needed = false;
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000317 for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
318 E = OID->classmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000319 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000320
321 for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
322 E = OID->instmeth_end(); I != E; ++I)
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000323 eol_needed = true, PrintObjCMethodDecl(*I);
Fariborz Jahanianb89ca232008-05-06 23:14:25 +0000324
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000325 Out << (eol_needed ? "\n@end\n" : "@end\n");
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000326 // FIXME: implement the rest...
327}
328
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000329void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000330 Out << "@protocol " << PID->getNameAsString() << '\n';
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000331
Steve Naroff09c47192009-01-09 15:36:25 +0000332 for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(),
333 E = PID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000334 PrintObjCPropertyDecl(*I);
335 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000336 // FIXME: implement the rest...
337}
338
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000339void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000340 Out << "@implementation "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000341 << PID->getClassInterface()->getNameAsString()
342 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000343 for (ObjCCategoryImplDecl::propimpl_iterator I = PID->propimpl_begin(),
344 E = PID->propimpl_end(); I != E; ++I)
345 PrintObjCPropertyImplDecl(*I);
346 Out << "@end\n";
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000347 // FIXME: implement the rest...
348}
349
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000350void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000351 Out << "@interface "
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000352 << PID->getClassInterface()->getNameAsString()
353 << '(' << PID->getNameAsString() << ");\n";
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000354 // Output property declarations.
Steve Naroff09c47192009-01-09 15:36:25 +0000355 for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(),
356 E = PID->prop_end(); I != E; ++I)
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000357 PrintObjCPropertyDecl(*I);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000358 Out << "@end\n";
359
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000360 // FIXME: implement the rest...
361}
362
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000363void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000364 Out << "@compatibility_alias " << AID->getNameAsString()
365 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000366}
367
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000368/// PrintObjCPropertyDecl - print a property declaration.
369///
370void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000371 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
372 Out << "@required\n";
373 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
374 Out << "@optional\n";
375
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000376 Out << "@property";
377 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
378 bool first = true;
379 Out << " (";
380 if (PDecl->getPropertyAttributes() &
381 ObjCPropertyDecl::OBJC_PR_readonly) {
382 Out << (first ? ' ' : ',') << "readonly";
383 first = false;
384 }
385
386 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
387 Out << (first ? ' ' : ',') << "getter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000388 << PDecl->getGetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000389 first = false;
390 }
391 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
392 Out << (first ? ' ' : ',') << "setter = "
Chris Lattner077bf5e2008-11-24 03:33:13 +0000393 << PDecl->getSetterName().getAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000394 first = false;
395 }
396
397 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
398 Out << (first ? ' ' : ',') << "assign";
399 first = false;
400 }
401
402 if (PDecl->getPropertyAttributes() &
403 ObjCPropertyDecl::OBJC_PR_readwrite) {
404 Out << (first ? ' ' : ',') << "readwrite";
405 first = false;
406 }
407
408 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
409 Out << (first ? ' ' : ',') << "retain";
410 first = false;
411 }
412
413 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
414 Out << (first ? ' ' : ',') << "copy";
415 first = false;
416 }
417
418 if (PDecl->getPropertyAttributes() &
419 ObjCPropertyDecl::OBJC_PR_nonatomic) {
420 Out << (first ? ' ' : ',') << "nonatomic";
421 first = false;
422 }
423 Out << " )";
424 }
425 Out << ' ' << PDecl->getType().getAsString()
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000426 << ' ' << PDecl->getNameAsString();
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000427
428 Out << ";\n";
429}
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000430
431/// PrintObjCPropertyImplDecl - Print an objective-c property implementation
432/// declaration syntax.
433///
434void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
Daniel Dunbar9f0afd42008-08-26 04:47:31 +0000435 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000436 Out << "\n@synthesize ";
437 else
438 Out << "\n@dynamic ";
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000439 Out << PID->getPropertyDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000440 if (PID->getPropertyIvarDecl())
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000441 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Fariborz Jahanian628b96f2008-04-23 00:06:01 +0000442 Out << ";\n";
443}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000444
445/// PrintTemplateParams - Print a template parameter list and recursively print
446/// it's underlying top-level definition.
447void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) {
448 // TODO: Write template parameters.
449 Out << "template <...> ";
450 PrintDecl(TD->getTemplatedDecl());
451}
452
453
454
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000455//===----------------------------------------------------------------------===//
456/// ASTPrinter - Pretty-printer of ASTs
457
Chris Lattner3d4997d2007-09-15 23:02:28 +0000458namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000459 class ASTPrinter : public ASTConsumer, public DeclPrinter {
460 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +0000461 ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000462
Chris Lattner3d4997d2007-09-15 23:02:28 +0000463 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattneref5a85d2008-01-02 21:04:16 +0000464 PrintDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000466 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000467}
Chris Lattner6000dac2007-08-08 22:51:59 +0000468
Ted Kremeneka95d3752008-09-13 05:16:45 +0000469ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000470 return new ASTPrinter(out);
471}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000472
473//===----------------------------------------------------------------------===//
474/// ASTDumper - Low-level dumper of ASTs
Chris Lattner3d4997d2007-09-15 23:02:28 +0000475
476namespace {
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000477 class ASTDumper : public ASTConsumer, public DeclPrinter {
Chris Lattner3d4997d2007-09-15 23:02:28 +0000478 SourceManager *SM;
479 public:
Ted Kremenekea75c552007-11-28 21:32:21 +0000480 ASTDumper() : DeclPrinter() {}
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000481
Ted Kremenek95041a22007-12-19 22:51:13 +0000482 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000483 SM = &Context.getSourceManager();
Chris Lattner6000dac2007-08-08 22:51:59 +0000484 }
Chris Lattner3d4997d2007-09-15 23:02:28 +0000485
486 virtual void HandleTopLevelDecl(Decl *D) {
487 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
488 PrintFunctionDeclStart(FD);
489
490 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000491 Out << '\n';
492 // FIXME: convert dumper to use std::ostream?
Chris Lattner3d4997d2007-09-15 23:02:28 +0000493 FD->getBody()->dumpAll(*SM);
Ted Kremenekea75c552007-11-28 21:32:21 +0000494 Out << '\n';
Chris Lattner3d4997d2007-09-15 23:02:28 +0000495 }
496 } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
497 PrintTypeDefDecl(TD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000498 } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000499 Out << "Read objc interface '" << OID->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000500 } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000501 Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000502 } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000503 Out << "Read objc category '" << OCD->getNameAsString() << "'\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000504 } else if (isa<ObjCForwardProtocolDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000505 Out << "Read objc fwd protocol decl\n";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000506 } else if (isa<ObjCClassDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000507 Out << "Read objc fwd class decl\n";
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000508 } else if (isa<FileScopeAsmDecl>(D)) {
509 Out << "Read file scope asm decl\n";
Ted Kremenek63bbe532008-03-14 17:31:00 +0000510 } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000511 Out << "Read objc method decl: '" << MD->getSelector().getAsString()
Ted Kremenek63bbe532008-03-14 17:31:00 +0000512 << "'\n";
Steve Naroff1a2b90d2008-05-23 18:50:58 +0000513 if (MD->getBody()) {
514 // FIXME: convert dumper to use std::ostream?
515 MD->getBody()->dumpAll(*SM);
516 Out << '\n';
517 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000518 } else if (isa<ObjCImplementationDecl>(D)) {
519 Out << "Read objc implementation decl\n";
Daniel Dunbar539ced12008-10-05 00:31:15 +0000520 } else if (isa<ObjCCategoryImplDecl>(D)) {
521 Out << "Read objc category implementation decl\n";
Eli Friedmanf595eb02008-12-16 22:14:15 +0000522 } else if (isa<LinkageSpecDecl>(D)) {
523 Out << "Read linkage spec decl\n";
Douglas Gregor305c22e2009-01-23 01:10:18 +0000524 } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
525 Out << "Read top-level variable decl: '" << ND->getNameAsString()
526 << "'\n";
Eli Friedmanf595eb02008-12-16 22:14:15 +0000527 } else {
Chris Lattner9fa5e652007-10-06 18:52:10 +0000528 assert(0 && "Unknown decl type!");
Chris Lattner3d4997d2007-09-15 23:02:28 +0000529 }
530 }
531 };
Chris Lattner6000dac2007-08-08 22:51:59 +0000532}
533
Chris Lattner3d4997d2007-09-15 23:02:28 +0000534ASTConsumer *clang::CreateASTDumper() { return new ASTDumper(); }
535
Ted Kremenek1b5a4bd2007-11-27 21:46:50 +0000536//===----------------------------------------------------------------------===//
537/// ASTViewer - AST Visualization
538
Ted Kremenek80de08f2007-09-19 21:29:43 +0000539namespace {
540 class ASTViewer : public ASTConsumer {
541 SourceManager *SM;
542 public:
Ted Kremenek95041a22007-12-19 22:51:13 +0000543 void Initialize(ASTContext &Context) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000544 SM = &Context.getSourceManager();
Ted Kremenek80de08f2007-09-19 21:29:43 +0000545 }
546
547 virtual void HandleTopLevelDecl(Decl *D) {
548 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000549 DeclPrinter().PrintFunctionDeclStart(FD);
Ted Kremenek80de08f2007-09-19 21:29:43 +0000550
551 if (FD->getBody()) {
Ted Kremenekea75c552007-11-28 21:32:21 +0000552 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000553 FD->getBody()->viewAST();
Ted Kremenekea75c552007-11-28 21:32:21 +0000554 llvm::cerr << '\n';
Ted Kremenek80de08f2007-09-19 21:29:43 +0000555 }
556 }
Ted Kremenek63bbe532008-03-14 17:31:00 +0000557 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
558 DeclPrinter().PrintObjCMethodDecl(MD);
559
560 if (MD->getBody()) {
561 llvm::cerr << '\n';
562 MD->getBody()->viewAST();
563 llvm::cerr << '\n';
564 }
565 }
Ted Kremenek80de08f2007-09-19 21:29:43 +0000566 }
567 };
568}
569
570ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); }
571
Ted Kremenek74bf2c92007-09-07 23:47:56 +0000572//===----------------------------------------------------------------------===//
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000573/// DeclContextPrinter - Decl and DeclContext Visualization
574
575namespace {
576
577class DeclContextPrinter : public ASTConsumer {
578 llvm::raw_ostream& Out;
579public:
580 DeclContextPrinter() : Out(llvm::errs()) {}
581
582 void HandleTranslationUnit(TranslationUnit& TU) {
583 TranslationUnitDecl* TUD = TU.getContext().getTranslationUnitDecl();
584 PrintDeclContext(TUD, 4);
585 }
586
587 void PrintDeclContext(const DeclContext* DC, unsigned Indentation);
588};
589
590void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
591 unsigned Indentation) {
592 // Print DeclContext name.
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000593 switch (DC->getDeclKind()) {
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000594 case Decl::TranslationUnit:
595 Out << "[translation unit] " << DC;
596 break;
597 case Decl::Namespace: {
598 Out << "[namespace] ";
599 NamespaceDecl* ND = NamespaceDecl::castFromDeclContext(DC);
600 Out << ND->getNameAsString();
601 break;
602 }
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000603 case Decl::Enum: {
604 EnumDecl* ED = EnumDecl::castFromDeclContext(DC);
605 if (ED->isDefinition())
606 Out << "[enum] ";
607 else
608 Out << "<enum> ";
609 Out << ED->getNameAsString();
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000610 break;
Zhongxing Xu867c39e2009-01-13 02:41:08 +0000611 }
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000612 case Decl::Record: {
613 RecordDecl* RD = RecordDecl::castFromDeclContext(DC);
614 if (RD->isDefinition())
615 Out << "[struct] ";
616 else
617 Out << "<struct> ";
618 Out << RD->getNameAsString();
619 break;
620 }
621 case Decl::CXXRecord: {
622 CXXRecordDecl* RD = CXXRecordDecl::castFromDeclContext(DC);
623 if (RD->isDefinition())
624 Out << "[class] ";
625 else
626 Out << "<class> ";
627 Out << RD->getNameAsString() << " " << DC;
628 break;
629 }
630 case Decl::ObjCMethod:
631 Out << "[objc method]";
632 break;
633 case Decl::ObjCInterface:
634 Out << "[objc interface]";
635 break;
636 case Decl::ObjCCategory:
637 Out << "[objc category]";
638 break;
639 case Decl::ObjCProtocol:
640 Out << "[objc protocol]";
641 break;
642 case Decl::ObjCImplementation:
643 Out << "[objc implementation]";
644 break;
645 case Decl::ObjCCategoryImpl:
646 Out << "[objc categoryimpl]";
647 break;
648 case Decl::LinkageSpec:
649 Out << "[linkage spec]";
650 break;
651 case Decl::Block:
652 Out << "[block]";
653 break;
654 case Decl::Function: {
655 FunctionDecl* FD = FunctionDecl::castFromDeclContext(DC);
656 if (FD->isThisDeclarationADefinition())
657 Out << "[function] ";
658 else
659 Out << "<function> ";
660 Out << FD->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000661 // Print the parameters.
662 Out << "(";
663 bool PrintComma = false;
664 for (FunctionDecl::param_const_iterator I = FD->param_begin(),
665 E = FD->param_end(); I != E; ++I) {
666 if (PrintComma)
667 Out << ", ";
668 else
669 PrintComma = true;
670 Out << (*I)->getNameAsString();
671 }
672 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000673 break;
674 }
675 case Decl::CXXMethod: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000676 CXXMethodDecl* D = CXXMethodDecl::castFromDeclContext(DC);
677 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000678 Out << "[c++ method] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000679 else if (D->isImplicit())
680 Out << "(c++ method) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000681 else
682 Out << "<c++ method> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000683 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000684 // Print the parameters.
685 Out << "(";
686 bool PrintComma = false;
687 for (FunctionDecl::param_const_iterator I = D->param_begin(),
688 E = D->param_end(); I != E; ++I) {
689 if (PrintComma)
690 Out << ", ";
691 else
692 PrintComma = true;
693 Out << (*I)->getNameAsString();
694 }
695 Out << ")";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000696
697 // Check the semantic DeclContext.
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000698 DeclContext* SemaDC = D->getDeclContext();
699 DeclContext* LexicalDC = D->getLexicalDeclContext();
700 if (SemaDC != LexicalDC)
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000701 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000702
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000703 break;
704 }
705 case Decl::CXXConstructor: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000706 CXXConstructorDecl* D = CXXConstructorDecl::castFromDeclContext(DC);
707 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000708 Out << "[c++ ctor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000709 else if (D->isImplicit())
710 Out << "(c++ ctor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000711 else
712 Out << "<c++ ctor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000713 Out << D->getNameAsString();
Zhongxing Xuca04ce42009-01-13 06:25:33 +0000714 // Print the parameters.
715 Out << "(";
716 bool PrintComma = false;
717 for (FunctionDecl::param_const_iterator I = D->param_begin(),
718 E = D->param_end(); I != E; ++I) {
719 if (PrintComma)
720 Out << ", ";
721 else
722 PrintComma = true;
723 Out << (*I)->getNameAsString();
724 }
725 Out << ")";
726
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000727 // Check the semantic DC.
728 DeclContext* SemaDC = D->getDeclContext();
729 DeclContext* LexicalDC = D->getLexicalDeclContext();
730 if (SemaDC != LexicalDC)
731 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000732 break;
733 }
734 case Decl::CXXDestructor: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000735 CXXDestructorDecl* D = CXXDestructorDecl::castFromDeclContext(DC);
736 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000737 Out << "[c++ dtor] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000738 else if (D->isImplicit())
739 Out << "(c++ dtor) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000740 else
741 Out << "<c++ dtor> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000742 Out << D->getNameAsString();
743 // Check the semantic DC.
744 DeclContext* SemaDC = D->getDeclContext();
745 DeclContext* LexicalDC = D->getLexicalDeclContext();
746 if (SemaDC != LexicalDC)
747 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000748 break;
749 }
750 case Decl::CXXConversion: {
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000751 CXXConversionDecl* D = CXXConversionDecl::castFromDeclContext(DC);
752 if (D->isOutOfLineDefinition())
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000753 Out << "[c++ conversion] ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000754 else if (D->isImplicit())
755 Out << "(c++ conversion) ";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000756 else
757 Out << "<c++ conversion> ";
Zhongxing Xu2a3eb0e2009-01-13 03:26:38 +0000758 Out << D->getNameAsString();
759 // Check the semantic DC.
760 DeclContext* SemaDC = D->getDeclContext();
761 DeclContext* LexicalDC = D->getLexicalDeclContext();
762 if (SemaDC != LexicalDC)
763 Out << " [[" << SemaDC << "]]";
Zhongxing Xu2d75d6f2009-01-13 01:29:24 +0000764 break;
765 }
766
767 default:
768 assert(0 && "a decl that inherits DeclContext isn't handled");
769 }
770
771 Out << "\n";
772
773 // Print decls in the DeclContext.
774 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
775 I != E; ++I) {
776 for (unsigned i = 0; i < Indentation; ++i)
777 Out << " ";
778
779 Decl::Kind DK = I->getKind();
780 switch (DK) {
781 case Decl::Namespace:
782 case Decl::Enum:
783 case Decl::Record:
784 case Decl::CXXRecord:
785 case Decl::ObjCMethod:
786 case Decl::ObjCInterface:
787 case Decl::ObjCCategory:
788 case Decl::ObjCProtocol:
789 case Decl::ObjCImplementation:
790 case Decl::ObjCCategoryImpl:
791 case Decl::LinkageSpec:
792 case Decl::Block:
793 case Decl::Function:
794 case Decl::CXXMethod:
795 case Decl::CXXConstructor:
796 case Decl::CXXDestructor:
797 case Decl::CXXConversion:
798 {
799 DeclContext* DC = Decl::castToDeclContext(*I);
800 PrintDeclContext(DC, Indentation+4);
801 break;
802 }
803 case Decl::Field: {
804 FieldDecl* FD = cast<FieldDecl>(*I);
805 Out << "<field> " << FD->getNameAsString() << "\n";
806 break;
807 }
808 case Decl::Typedef: {
809 TypedefDecl* TD = cast<TypedefDecl>(*I);
810 Out << "<typedef> " << TD->getNameAsString() << "\n";
811 break;
812 }
813 case Decl::EnumConstant: {
814 EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I);
815 Out << "<enum constant> " << ECD->getNameAsString() << "\n";
816 break;
817 }
818 case Decl::Var: {
819 VarDecl* VD = cast<VarDecl>(*I);
820 Out << "<var> " << VD->getNameAsString() << "\n";
821 break;
822 }
823 case Decl::ImplicitParam: {
824 ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I);
825 Out << "<implicit parameter> " << IPD->getNameAsString() << "\n";
826 break;
827 }
828 case Decl::CXXClassVar: {
829 CXXClassVarDecl* CVD = cast<CXXClassVarDecl>(*I);
830 Out << "<static member var> " << CVD->getNameAsString() << "\n";
831 break;
832 }
833 case Decl::ParmVar: {
834 ParmVarDecl* PVD = cast<ParmVarDecl>(*I);
835 Out << "<parameter> " << PVD->getNameAsString() << "\n";
836 break;
837 }
838 case Decl::ObjCProperty: {
839 ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I);
840 Out << "<objc property> " << OPD->getNameAsString() << "\n";
841 break;
842 }
843 default:
844 fprintf(stderr, "DeclKind: %d\n", DK);
845 assert(0 && "decl unhandled");
846 }
847 }
848}
849
850}
851
852ASTConsumer *clang::CreateDeclContextPrinter() {
853 return new DeclContextPrinter();
854}
855
856//===----------------------------------------------------------------------===//
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000857/// InheritanceViewer - C++ Inheritance Visualization
858
859namespace {
860class InheritanceViewer : public ASTConsumer {
861 const std::string clsname;
862public:
863 InheritanceViewer(const std::string& cname) : clsname(cname) {}
864
865 void HandleTranslationUnit(TranslationUnit& TU) {
866 ASTContext& C = TU.getContext();
867 for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I)
868 if (CXXRecordType *T = dyn_cast<CXXRecordType>(*I)) {
869 CXXRecordDecl* D = T->getDecl();
870 // FIXME: This lookup needs to be generalized to handle namespaces and
871 // (when we support them) templates.
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000872 if (D->getNameAsString() == clsname) {
Douglas Gregor1f812302008-10-24 19:53:54 +0000873 D->viewInheritance(C);
Ted Kremenek7cae2f62008-10-23 23:36:29 +0000874 }
875 }
876 }
877};
878}
879
880ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) {
881 return new InheritanceViewer(clsname);
882}
883
884//===----------------------------------------------------------------------===//
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000885// AST Serializer
886
887namespace {
Ted Kremenekf06c9282007-12-19 23:49:37 +0000888
889class ASTSerializer : public ASTConsumer {
890protected:
Nico Weberdae86962008-08-09 18:32:11 +0000891 Diagnostic& Diags;
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000892
Ted Kremenekf06c9282007-12-19 23:49:37 +0000893public:
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000894 ASTSerializer(Diagnostic& diags) : Diags(diags) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000895};
Eli Friedmand8a65c12008-06-09 20:02:51 +0000896
Ted Kremenekf06c9282007-12-19 23:49:37 +0000897class SingleFileSerializer : public ASTSerializer {
898 const llvm::sys::Path FName;
899public:
Nico Weberdae86962008-08-09 18:32:11 +0000900 SingleFileSerializer(const llvm::sys::Path& F, Diagnostic& diags)
901 : ASTSerializer(diags), FName(F) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000902
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000903 virtual void HandleTranslationUnit(TranslationUnit& TU) {
Nico Weberdae86962008-08-09 18:32:11 +0000904 if (Diags.hasErrorOccurred())
905 return;
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000906 EmitASTBitcodeFile(&TU, FName);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000907 }
908};
909
910class BuildSerializer : public ASTSerializer {
911 llvm::sys::Path EmitDir;
912public:
Nico Weberdae86962008-08-09 18:32:11 +0000913 BuildSerializer(const llvm::sys::Path& dir, Diagnostic& diags)
914 : ASTSerializer(diags), EmitDir(dir) {}
Ted Kremenekf06c9282007-12-19 23:49:37 +0000915
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000916 virtual void HandleTranslationUnit(TranslationUnit& TU) {
917 if (Diags.hasErrorOccurred())
Ted Kremenekc1e9dea2008-04-23 16:25:39 +0000918 return;
919
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000920 SourceManager& SourceMgr = TU.getContext().getSourceManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000921 FileID ID = SourceMgr.getMainFileID();
922 assert(!ID.isInvalid() && "MainFileID not set!");
Ted Kremenek54117722007-12-20 00:34:58 +0000923 const FileEntry* FE = SourceMgr.getFileEntryForID(ID);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000924 assert(FE && "No FileEntry for main file.");
Ted Kremenek54117722007-12-20 00:34:58 +0000925
926 // FIXME: This is not portable to Windows.
927 // FIXME: This logic should probably be moved elsewhere later.
928
Ted Kremenekee533642007-12-20 19:47:16 +0000929 llvm::sys::Path FName(EmitDir);
Ted Kremenek54117722007-12-20 00:34:58 +0000930
931 std::vector<char> buf;
932 buf.reserve(strlen(FE->getName())+100);
933
934 sprintf(&buf[0], "dev_%llx", (uint64_t) FE->getDevice());
Ted Kremenekee533642007-12-20 19:47:16 +0000935 FName.appendComponent(&buf[0]);
936 FName.createDirectoryOnDisk(true);
937 if (!FName.canWrite() || !FName.isDirectory()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000938 assert (false && "Could not create 'device' serialization directory.");
939 return;
940 }
Ted Kremenekee533642007-12-20 19:47:16 +0000941
Ted Kremenek54117722007-12-20 00:34:58 +0000942 sprintf(&buf[0], "%s-%llX.ast", FE->getName(), (uint64_t) FE->getInode());
Ted Kremenekee533642007-12-20 19:47:16 +0000943 FName.appendComponent(&buf[0]);
Nico Weberbe1eb5c2008-08-10 19:20:05 +0000944 EmitASTBitcodeFile(&TU, FName);
Ted Kremenek54117722007-12-20 00:34:58 +0000945
Ted Kremenekee533642007-12-20 19:47:16 +0000946 // Now emit the sources.
947
Ted Kremenek54117722007-12-20 00:34:58 +0000948 }
Ted Kremenekf06c9282007-12-19 23:49:37 +0000949};
950
951
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000952} // end anonymous namespace
953
954
Ted Kremenekfdfc1982007-12-19 22:24:34 +0000955ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
Ted Kremenekf06c9282007-12-19 23:49:37 +0000956 const std::string& OutputFile,
Ted Kremeneke7d07d12008-06-04 15:55:15 +0000957 Diagnostic &Diags) {
Ted Kremenek3910c7c2007-12-19 17:25:59 +0000958
Ted Kremenekf06c9282007-12-19 23:49:37 +0000959 if (OutputFile.size()) {
Ted Kremenek54117722007-12-20 00:34:58 +0000960 if (InFile == "-") {
961 llvm::cerr <<
962 "error: Cannot use --serialize with -o for source read from STDIN.\n";
963 return NULL;
964 }
965
Ted Kremenekf06c9282007-12-19 23:49:37 +0000966 // The user specified an AST-emission directory. Determine if the path
967 // is absolute.
968 llvm::sys::Path EmitDir(OutputFile);
969
970 if (!EmitDir.isAbsolute()) {
971 llvm::cerr <<
972 "error: Output directory for --serialize must be an absolute path.\n";
973
974 return NULL;
975 }
976
977 // Create the directory if it does not exist.
978 EmitDir.createDirectoryOnDisk(true);
979 if (!EmitDir.canWrite() || !EmitDir.isDirectory()) {
980 llvm::cerr <<
981 "error: Could not create output directory for --serialize.\n";
982
983 return NULL;
984 }
985
Ted Kremenek54117722007-12-20 00:34:58 +0000986 // FIXME: We should probably only allow using BuildSerializer when
987 // the ASTs come from parsed source files, and not from .ast files.
Nico Weberdae86962008-08-09 18:32:11 +0000988 return new BuildSerializer(EmitDir, Diags);
Ted Kremenekf06c9282007-12-19 23:49:37 +0000989 }
990
991 // The user did not specify an output directory for serialized ASTs.
992 // Serialize the translation to a single file whose name is the same
993 // as the input file with the ".ast" extension appended.
Ted Kremenek63ea8632007-12-19 19:27:38 +0000994
Ted Kremenekf06c9282007-12-19 23:49:37 +0000995 llvm::sys::Path FName(InFile.c_str());
Ted Kremenek54117722007-12-20 00:34:58 +0000996 FName.appendSuffix("ast");
Nico Weberdae86962008-08-09 18:32:11 +0000997 return new SingleFileSerializer(FName, Diags);
Ted Kremeneka1fa3a12007-12-13 00:37:31 +0000998}