Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 1 | //===--- ASTConsumers.cpp - ASTConsumer implementations -------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 10 | // AST Consumer Implementations. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Friedman | 39d7c4d | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTConsumers.h" |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/DocumentXML.h" |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/PathDiagnosticClients.h" |
Nico Weber | dae8696 | 2008-08-09 18:32:11 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | 5411772 | 2007-12-20 00:34:58 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/AST.h" |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 23 | #include "clang/AST/PrettyPrinter.h" |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 24 | #include "clang/CodeGen/ModuleBuilder.h" |
| 25 | #include "llvm/Module.h" |
Daniel Dunbar | d46075f | 2008-10-21 23:54:00 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Streams.h" |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Timer.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 29 | #include "llvm/System/Path.h" |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 30 | using namespace clang; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | /// DeclPrinter - Utility class for printing top-level decls. |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | class DeclPrinter { |
| 37 | public: |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 38 | llvm::raw_ostream& Out; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 39 | PrintingPolicy Policy; |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 40 | unsigned Indentation; |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 42 | DeclPrinter(llvm::raw_ostream* out, |
| 43 | const PrintingPolicy &Policy = PrintingPolicy()) |
| 44 | : Out(out ? *out : llvm::errs()), Policy(Policy), |
| 45 | Indentation(0) {} |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 46 | DeclPrinter() : Out(llvm::errs()), Indentation(0) {} |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 47 | virtual ~DeclPrinter(); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 48 | |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 49 | void ChangeIndent(int I) { |
| 50 | Indentation += I; |
| 51 | } |
| 52 | |
| 53 | llvm::raw_ostream& Indent() { |
| 54 | for (unsigned i = 0; i < Indentation; ++i) |
| 55 | Out << " "; |
| 56 | return Out; |
| 57 | } |
| 58 | |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 59 | void PrintDecl(Decl *D); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 60 | void Print(NamedDecl *ND); |
| 61 | void Print(NamespaceDecl *NS); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 62 | void PrintFunctionDeclStart(FunctionDecl *FD); |
| 63 | void PrintTypeDefDecl(TypedefDecl *TD); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 64 | void PrintLinkageSpec(LinkageSpecDecl *LS); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 65 | void PrintObjCMethodDecl(ObjCMethodDecl *OMD); |
| 66 | void PrintObjCImplementationDecl(ObjCImplementationDecl *OID); |
| 67 | void PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID); |
| 68 | void PrintObjCProtocolDecl(ObjCProtocolDecl *PID); |
| 69 | void PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID); |
| 70 | void PrintObjCCategoryDecl(ObjCCategoryDecl *PID); |
| 71 | void PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 72 | void PrintObjCPropertyDecl(ObjCPropertyDecl *PD); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 73 | void PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 74 | |
| 75 | void PrintTemplateDecl(TemplateDecl *TD); |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 76 | }; |
| 77 | } // end anonymous namespace |
| 78 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 79 | DeclPrinter::~DeclPrinter() { |
| 80 | Out.flush(); |
| 81 | } |
| 82 | |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 83 | void DeclPrinter:: PrintDecl(Decl *D) { |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 84 | Indent(); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 85 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 86 | PrintFunctionDeclStart(FD); |
| 87 | |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 88 | // FIXME: Pass a context here so we can use getBody() |
| 89 | if (FD->getBodyIfAvailable()) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 90 | Out << ' '; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 91 | FD->getBodyIfAvailable()->printPretty(Out, 0, Policy); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 92 | Out << '\n'; |
| 93 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 94 | } else if (isa<ObjCMethodDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 95 | // Do nothing, methods definitions are printed in |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 96 | // PrintObjCImplementationDecl. |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 97 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 98 | PrintTypeDefDecl(TD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 99 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 100 | PrintObjCInterfaceDecl(OID); |
| 101 | } else if (ObjCProtocolDecl *PID = dyn_cast<ObjCProtocolDecl>(D)) { |
| 102 | PrintObjCProtocolDecl(PID); |
| 103 | } else if (ObjCForwardProtocolDecl *OFPD = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 104 | dyn_cast<ObjCForwardProtocolDecl>(D)) { |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 105 | Out << "@protocol "; |
Steve Naroff | 30833f8 | 2009-04-21 15:12:33 +0000 | [diff] [blame] | 106 | for (ObjCForwardProtocolDecl::protocol_iterator I = OFPD->protocol_begin(), |
| 107 | E = OFPD->protocol_end(); |
Chris Lattner | 07fa774 | 2009-02-20 18:10:37 +0000 | [diff] [blame] | 108 | I != E; ++I) { |
Steve Naroff | 30833f8 | 2009-04-21 15:12:33 +0000 | [diff] [blame] | 109 | if (I != OFPD->protocol_begin()) Out << ", "; |
Chris Lattner | 07fa774 | 2009-02-20 18:10:37 +0000 | [diff] [blame] | 110 | Out << (*I)->getNameAsString(); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 111 | } |
| 112 | Out << ";\n"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 113 | } else if (ObjCImplementationDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 114 | dyn_cast<ObjCImplementationDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 115 | PrintObjCImplementationDecl(OID); |
| 116 | } else if (ObjCCategoryImplDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 117 | dyn_cast<ObjCCategoryImplDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 118 | PrintObjCCategoryImplDecl(OID); |
| 119 | } else if (ObjCCategoryDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 120 | dyn_cast<ObjCCategoryDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 121 | PrintObjCCategoryDecl(OID); |
| 122 | } else if (ObjCCompatibleAliasDecl *OID = |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 123 | dyn_cast<ObjCCompatibleAliasDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 124 | PrintObjCCompatibleAliasDecl(OID); |
Chris Lattner | 23a0e45 | 2008-06-21 21:40:20 +0000 | [diff] [blame] | 125 | } else if (ObjCClassDecl *OFCD = dyn_cast<ObjCClassDecl>(D)) { |
| 126 | Out << "@class "; |
Chris Lattner | 6795605 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 127 | for (ObjCClassDecl::iterator I = OFCD->begin(), E = OFCD->end(); |
| 128 | I != E; ++I) { |
| 129 | if (I != OFCD->begin()) Out << ", "; |
| 130 | Out << (*I)->getNameAsString(); |
Chris Lattner | 23a0e45 | 2008-06-21 21:40:20 +0000 | [diff] [blame] | 131 | } |
| 132 | Out << ";\n"; |
Douglas Gregor | 45579f5 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 133 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 134 | Out << "enum " << ED->getNameAsString() << " {\n"; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 135 | // FIXME: Shouldn't pass a NULL context |
| 136 | ASTContext *Context = 0; |
| 137 | for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(*Context), |
| 138 | EEnd = ED->enumerator_end(*Context); |
Douglas Gregor | 45579f5 | 2008-12-17 02:04:30 +0000 | [diff] [blame] | 139 | E != EEnd; ++E) |
| 140 | Out << " " << (*E)->getNameAsString() << ",\n"; |
| 141 | Out << "};\n"; |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 142 | } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 143 | // print a free standing tag decl (e.g. "struct x;"). |
| 144 | Out << TD->getKindName(); |
| 145 | Out << " "; |
| 146 | if (const IdentifierInfo *II = TD->getIdentifier()) |
| 147 | Out << II->getName(); |
| 148 | |
Eli Friedman | b3e2296 | 2009-05-17 01:05:34 +0000 | [diff] [blame] | 149 | if (TD->isDefinition()) { |
| 150 | Out << " {\n"; |
| 151 | ChangeIndent(1); |
| 152 | // FIXME: Shouldn't pass a NULL context |
| 153 | ASTContext *Context = 0; |
| 154 | for (DeclContext::decl_iterator i = TD->decls_begin(*Context); |
| 155 | i != TD->decls_end(*Context); |
| 156 | ++i) |
| 157 | PrintDecl(*i); |
| 158 | ChangeIndent(-1); |
| 159 | Indent(); |
| 160 | Out << "}"; |
| 161 | } |
| 162 | Out << ";\n"; |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 163 | } else if (TemplateDecl *TempD = dyn_cast<TemplateDecl>(D)) { |
| 164 | PrintTemplateDecl(TempD); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 165 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 166 | PrintLinkageSpec(LSD); |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 167 | } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) { |
| 168 | Out << "asm("; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 169 | AD->getAsmString()->printPretty(Out, 0, Policy); |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 170 | Out << ")\n"; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 171 | } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 172 | Print(ND); |
Chris Lattner | ef5a85d | 2008-01-02 21:04:16 +0000 | [diff] [blame] | 173 | } else { |
| 174 | assert(0 && "Unknown decl type!"); |
| 175 | } |
| 176 | } |
| 177 | |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 178 | void DeclPrinter::Print(NamedDecl *ND) { |
| 179 | switch (ND->getKind()) { |
| 180 | default: |
| 181 | // FIXME: Handle the rest of the NamedDecls. |
| 182 | Out << "### NamedDecl " << ND->getNameAsString() << "\n"; |
| 183 | break; |
| 184 | case Decl::Field: |
| 185 | case Decl::Var: { |
| 186 | // Emit storage class for vardecls. |
| 187 | if (VarDecl *V = dyn_cast<VarDecl>(ND)) { |
| 188 | switch (V->getStorageClass()) { |
| 189 | default: assert(0 && "Unknown storage class!"); |
Mike Stump | c5840c0 | 2009-02-10 23:49:50 +0000 | [diff] [blame] | 190 | case VarDecl::None: break; |
| 191 | case VarDecl::Auto: Out << "auto "; break; |
| 192 | case VarDecl::Register: Out << "register "; break; |
| 193 | case VarDecl::Extern: Out << "extern "; break; |
| 194 | case VarDecl::Static: Out << "static "; break; |
Daniel Dunbar | 7ab41f7 | 2009-02-13 22:49:34 +0000 | [diff] [blame] | 195 | case VarDecl::PrivateExtern: Out << "__private_extern__ "; break; |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | std::string Name = ND->getNameAsString(); |
| 199 | // This forms: "int a". |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 200 | dyn_cast<ValueDecl>(ND)->getType().getAsStringInternal(Name, Policy); |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 201 | Out << Name; |
| 202 | if (VarDecl *Var = dyn_cast<VarDecl>(ND)) { |
| 203 | if (Var->getInit()) { |
| 204 | Out << " = "; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 205 | Var->getInit()->printPretty(Out, 0, Policy); |
Douglas Gregor | 087fd53 | 2009-04-14 23:32:43 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | Out << ";\n"; |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 209 | break; |
| 210 | } |
| 211 | case Decl::Namespace: |
| 212 | Print(dyn_cast<NamespaceDecl>(ND)); |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void DeclPrinter::Print(NamespaceDecl *NS) { |
| 218 | Out << "namespace " << NS->getNameAsString() << " {\n"; |
| 219 | ChangeIndent(1); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 220 | // FIXME: Shouldn't pass a NULL context |
| 221 | ASTContext *Context = 0; |
| 222 | for (DeclContext::decl_iterator i = NS->decls_begin(*Context); |
| 223 | i != NS->decls_end(*Context); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 224 | ++i) |
| 225 | PrintDecl(*i); |
| 226 | ChangeIndent(-1); |
| 227 | Indent(); |
| 228 | Out << "}\n"; |
| 229 | } |
| 230 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 231 | void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 232 | // FIXME: pass a context so that we can use getBody. |
| 233 | bool HasBody = FD->getBodyIfAvailable(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 235 | Out << '\n'; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 236 | |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 237 | Indent(); |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 238 | switch (FD->getStorageClass()) { |
| 239 | default: assert(0 && "Unknown storage class"); |
| 240 | case FunctionDecl::None: break; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 241 | case FunctionDecl::Extern: Out << "extern "; break; |
| 242 | case FunctionDecl::Static: Out << "static "; break; |
Ted Kremenek | 24bd3c4 | 2008-04-15 03:57:09 +0000 | [diff] [blame] | 243 | case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | if (FD->isInline()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 247 | Out << "inline "; |
Chris Lattner | 70c8b2e | 2007-08-26 04:02:13 +0000 | [diff] [blame] | 248 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 249 | std::string Proto = FD->getNameAsString(); |
Chris Lattner | 0d6ca11 | 2007-12-03 21:43:25 +0000 | [diff] [blame] | 250 | const FunctionType *AFT = FD->getType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 252 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | Proto += "("; |
| 254 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 255 | if (i) Proto += ", "; |
| 256 | std::string ParamStr; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 257 | if (HasBody) ParamStr = FD->getParamDecl(i)->getNameAsString(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 259 | FT->getArgType(i).getAsStringInternal(ParamStr, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | Proto += ParamStr; |
| 261 | } |
| 262 | |
| 263 | if (FT->isVariadic()) { |
| 264 | if (FD->getNumParams()) Proto += ", "; |
| 265 | Proto += "..."; |
| 266 | } |
| 267 | Proto += ")"; |
| 268 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 269 | assert(isa<FunctionNoProtoType>(AFT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | Proto += "()"; |
| 271 | } |
| 272 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 273 | AFT->getResultType().getAsStringInternal(Proto, Policy); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 274 | Out << Proto; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 276 | if (!FD->getBodyIfAvailable()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 277 | Out << ";\n"; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 278 | // Doesn't print the body. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 281 | void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 282 | std::string S = TD->getNameAsString(); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 283 | TD->getUnderlyingType().getAsStringInternal(S, Policy); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 284 | Out << "typedef " << S << ";\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 287 | void DeclPrinter::PrintLinkageSpec(LinkageSpecDecl *LS) { |
| 288 | const char *l; |
| 289 | if (LS->getLanguage() == LinkageSpecDecl::lang_c) |
| 290 | l = "C"; |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 291 | else { |
| 292 | assert(LS->getLanguage() == LinkageSpecDecl::lang_cxx && |
| 293 | "unknown language in linkage specification"); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 294 | l = "C++"; |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 295 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 296 | |
| 297 | Out << "extern \"" << l << "\" "; |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 298 | if (LS->hasBraces()) { |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 299 | Out << "{\n"; |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 300 | ChangeIndent(1); |
| 301 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 302 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 303 | // FIXME: Should not use a NULL DeclContext! |
| 304 | ASTContext *Context = 0; |
| 305 | for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context), |
| 306 | DEnd = LS->decls_end(*Context); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 307 | D != DEnd; ++D) |
| 308 | PrintDecl(*D); |
| 309 | |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 310 | if (LS->hasBraces()) { |
| 311 | ChangeIndent(-1); |
| 312 | Indent() << "}"; |
| 313 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 314 | Out << "\n"; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 317 | void DeclPrinter::PrintObjCMethodDecl(ObjCMethodDecl *OMD) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 318 | if (OMD->isInstanceMethod()) |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 319 | Out << "\n- "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 320 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 321 | Out << "\n+ "; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 322 | if (!OMD->getResultType().isNull()) |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 323 | Out << '(' << OMD->getResultType().getAsString() << ")"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 325 | std::string name = OMD->getSelector().getAsString(); |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 326 | std::string::size_type pos, lastPos = 0; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 327 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 328 | E = OMD->param_end(); PI != E; ++PI) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 329 | // FIXME: selector is missing here! |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 330 | pos = name.find_first_of(":", lastPos); |
| 331 | Out << " " << name.substr(lastPos, pos - lastPos); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 332 | Out << ":(" << (*PI)->getType().getAsString() << ")" |
| 333 | << (*PI)->getNameAsString(); |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 334 | lastPos = pos + 1; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 335 | } |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 337 | if (OMD->param_begin() == OMD->param_end()) |
Chris Lattner | efe8a96 | 2008-08-22 06:59:15 +0000 | [diff] [blame] | 338 | Out << " " << name; |
| 339 | |
| 340 | if (OMD->isVariadic()) |
| 341 | Out << ", ..."; |
| 342 | |
| 343 | Out << ";"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 346 | void DeclPrinter::PrintObjCImplementationDecl(ObjCImplementationDecl *OID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 347 | std::string I = OID->getNameAsString(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 348 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 349 | |
| 350 | if (SID) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 351 | Out << "@implementation " << I << " : " << SID->getNameAsString(); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 352 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 353 | Out << "@implementation " << I; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 354 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 355 | // FIXME: Don't use a NULL context |
| 356 | ASTContext *Context = 0; |
| 357 | for (ObjCImplementationDecl::instmeth_iterator |
| 358 | I = OID->instmeth_begin(*Context), |
| 359 | E = OID->instmeth_end(*Context); |
| 360 | I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 361 | ObjCMethodDecl *OMD = *I; |
| 362 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 363 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 364 | Out << ' '; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 365 | OMD->getBody()->printPretty(Out, 0, Policy); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 366 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 370 | for (ObjCImplementationDecl::classmeth_iterator |
| 371 | I = OID->classmeth_begin(*Context), |
| 372 | E = OID->classmeth_end(*Context); |
| 373 | I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 374 | ObjCMethodDecl *OMD = *I; |
| 375 | PrintObjCMethodDecl(OMD); |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 376 | if (OMD->getBody()) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 377 | Out << ' '; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 378 | OMD->getBody()->printPretty(Out, 0, Policy); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 379 | Out << '\n'; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 383 | for (ObjCImplementationDecl::propimpl_iterator |
| 384 | I = OID->propimpl_begin(*Context), |
| 385 | E = OID->propimpl_end(*Context); I != E; ++I) |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 386 | PrintObjCPropertyImplDecl(*I); |
| 387 | |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 388 | Out << "@end\n"; |
Fariborz Jahanian | db8f3d3 | 2007-11-10 20:59:13 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 392 | void DeclPrinter::PrintObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 393 | std::string I = OID->getNameAsString(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 394 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 395 | |
| 396 | if (SID) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 397 | Out << "@interface " << I << " : " << SID->getNameAsString(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 398 | else |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 399 | Out << "@interface " << I; |
| 400 | |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 401 | // Protocols? |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 402 | const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); |
| 403 | if (!Protocols.empty()) { |
| 404 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 405 | E = Protocols.end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 406 | Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString(); |
Fariborz Jahanian | e37882a | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 407 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 409 | if (!Protocols.empty()) |
| 410 | Out << ">"; |
| 411 | Out << '\n'; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 412 | |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 413 | if (OID->ivar_size() > 0) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 414 | Out << '{'; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 415 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 416 | E = OID->ivar_end(); I != E; ++I) { |
| 417 | Out << '\t' << (*I)->getType().getAsString() |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 418 | << ' ' << (*I)->getNameAsString() << ";\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 419 | } |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 420 | Out << "}\n"; |
Fariborz Jahanian | edcfb42 | 2007-10-26 16:29:12 +0000 | [diff] [blame] | 421 | } |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 422 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 423 | // FIXME: Should not use a NULL DeclContext! |
| 424 | ASTContext *Context = 0; |
| 425 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context), |
| 426 | E = OID->prop_end(*Context); I != E; ++I) |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 427 | PrintObjCPropertyDecl(*I); |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 428 | bool eol_needed = false; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 429 | for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context), |
| 430 | E = OID->classmeth_end(*Context); I != E; ++I) |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 431 | eol_needed = true, PrintObjCMethodDecl(*I); |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 432 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 433 | for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context), |
| 434 | E = OID->instmeth_end(*Context); I != E; ++I) |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 435 | eol_needed = true, PrintObjCMethodDecl(*I); |
Fariborz Jahanian | b89ca23 | 2008-05-06 23:14:25 +0000 | [diff] [blame] | 436 | |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 437 | Out << (eol_needed ? "\n@end\n" : "@end\n"); |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 438 | // FIXME: implement the rest... |
| 439 | } |
| 440 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 441 | void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 442 | Out << "@protocol " << PID->getNameAsString() << '\n'; |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 444 | // FIXME: Should not use a NULL DeclContext! |
| 445 | ASTContext *Context = 0; |
| 446 | for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context), |
| 447 | E = PID->prop_end(*Context); I != E; ++I) |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 448 | PrintObjCPropertyDecl(*I); |
| 449 | Out << "@end\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 450 | // FIXME: implement the rest... |
| 451 | } |
| 452 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 453 | void DeclPrinter::PrintObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 454 | Out << "@implementation " |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 455 | << PID->getClassInterface()->getNameAsString() |
| 456 | << '(' << PID->getNameAsString() << ");\n"; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 457 | |
| 458 | // FIXME: Don't use a NULL context here |
| 459 | ASTContext *Context = 0; |
| 460 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 461 | I = PID->propimpl_begin(*Context), |
| 462 | E = PID->propimpl_end(*Context); I != E; ++I) |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 463 | PrintObjCPropertyImplDecl(*I); |
| 464 | Out << "@end\n"; |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 465 | // FIXME: implement the rest... |
| 466 | } |
| 467 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 468 | void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 469 | // FIXME: Should not use a NULL DeclContext! |
| 470 | ASTContext *Context = 0; |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 471 | Out << "@interface " |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 472 | << PID->getClassInterface()->getNameAsString() |
| 473 | << '(' << PID->getNameAsString() << ");\n"; |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 474 | // Output property declarations. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 475 | for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context), |
| 476 | E = PID->prop_end(*Context); I != E; ++I) |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 477 | PrintObjCPropertyDecl(*I); |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 478 | Out << "@end\n"; |
| 479 | |
Fariborz Jahanian | ab0aeb0 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 480 | // FIXME: implement the rest... |
| 481 | } |
| 482 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 483 | void DeclPrinter::PrintObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 484 | Out << "@compatibility_alias " << AID->getNameAsString() |
| 485 | << ' ' << AID->getClassInterface()->getNameAsString() << ";\n"; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 488 | /// PrintObjCPropertyDecl - print a property declaration. |
| 489 | /// |
| 490 | void DeclPrinter::PrintObjCPropertyDecl(ObjCPropertyDecl *PDecl) { |
Fariborz Jahanian | 46b55e5 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 491 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 492 | Out << "@required\n"; |
| 493 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 494 | Out << "@optional\n"; |
| 495 | |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 496 | Out << "@property"; |
| 497 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 498 | bool first = true; |
| 499 | Out << " ("; |
| 500 | if (PDecl->getPropertyAttributes() & |
| 501 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 502 | Out << (first ? ' ' : ',') << "readonly"; |
| 503 | first = false; |
| 504 | } |
| 505 | |
| 506 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 507 | Out << (first ? ' ' : ',') << "getter = " |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 508 | << PDecl->getGetterName().getAsString(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 509 | first = false; |
| 510 | } |
| 511 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 512 | Out << (first ? ' ' : ',') << "setter = " |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 513 | << PDecl->getSetterName().getAsString(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 514 | first = false; |
| 515 | } |
| 516 | |
| 517 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 518 | Out << (first ? ' ' : ',') << "assign"; |
| 519 | first = false; |
| 520 | } |
| 521 | |
| 522 | if (PDecl->getPropertyAttributes() & |
| 523 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 524 | Out << (first ? ' ' : ',') << "readwrite"; |
| 525 | first = false; |
| 526 | } |
| 527 | |
| 528 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
| 529 | Out << (first ? ' ' : ',') << "retain"; |
| 530 | first = false; |
| 531 | } |
| 532 | |
| 533 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 534 | Out << (first ? ' ' : ',') << "copy"; |
| 535 | first = false; |
| 536 | } |
| 537 | |
| 538 | if (PDecl->getPropertyAttributes() & |
| 539 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
| 540 | Out << (first ? ' ' : ',') << "nonatomic"; |
| 541 | first = false; |
| 542 | } |
| 543 | Out << " )"; |
| 544 | } |
| 545 | Out << ' ' << PDecl->getType().getAsString() |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 546 | << ' ' << PDecl->getNameAsString(); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 547 | |
| 548 | Out << ";\n"; |
| 549 | } |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 550 | |
| 551 | /// PrintObjCPropertyImplDecl - Print an objective-c property implementation |
| 552 | /// declaration syntax. |
| 553 | /// |
| 554 | void DeclPrinter::PrintObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | 9f0afd4 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 555 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 556 | Out << "\n@synthesize "; |
| 557 | else |
| 558 | Out << "\n@dynamic "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 559 | Out << PID->getPropertyDecl()->getNameAsString(); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 560 | if (PID->getPropertyIvarDecl()) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 561 | Out << "=" << PID->getPropertyIvarDecl()->getNameAsString(); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 562 | Out << ";\n"; |
| 563 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 564 | |
| 565 | /// PrintTemplateParams - Print a template parameter list and recursively print |
| 566 | /// it's underlying top-level definition. |
| 567 | void DeclPrinter::PrintTemplateDecl(TemplateDecl *TD) { |
| 568 | // TODO: Write template parameters. |
| 569 | Out << "template <...> "; |
| 570 | PrintDecl(TD->getTemplatedDecl()); |
| 571 | } |
| 572 | |
| 573 | |
| 574 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 575 | //===----------------------------------------------------------------------===// |
| 576 | /// ASTPrinter - Pretty-printer of ASTs |
| 577 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 578 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 579 | class ASTPrinter : public ASTConsumer, public DeclPrinter { |
| 580 | public: |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 581 | ASTPrinter(llvm::raw_ostream* o = NULL) : DeclPrinter(o) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 583 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 584 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 585 | PrintDecl(*I); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 586 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 587 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 588 | } // end anonymous namespace |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 589 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 590 | ASTConsumer *clang::CreateASTPrinter(llvm::raw_ostream* out) { |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 591 | return new ASTPrinter(out); |
| 592 | } |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 593 | |
| 594 | //===----------------------------------------------------------------------===// |
Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 595 | /// ASTPrinterXML - XML-printer of ASTs |
| 596 | |
| 597 | namespace { |
| 598 | class ASTPrinterXML : public ASTConsumer { |
| 599 | DocumentXML Doc; |
| 600 | |
| 601 | public: |
| 602 | ASTPrinterXML(llvm::raw_ostream& o) : Doc("CLANG_XML", o) {} |
| 603 | |
| 604 | void Initialize(ASTContext &Context) { |
| 605 | Doc.initialize(Context); |
| 606 | } |
| 607 | |
| 608 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 609 | Doc.addSubNode("TranslationUnit"); |
| 610 | for (DeclContext::decl_iterator |
| 611 | D = Ctx.getTranslationUnitDecl()->decls_begin(Ctx), |
| 612 | DEnd = Ctx.getTranslationUnitDecl()->decls_end(Ctx); |
| 613 | D != DEnd; |
| 614 | ++D) |
| 615 | { |
| 616 | Doc.PrintDecl(*D); |
| 617 | } |
| 618 | Doc.toParent(); |
| 619 | Doc.finalize(); |
| 620 | } |
| 621 | }; |
| 622 | } // end anonymous namespace |
| 623 | |
| 624 | |
| 625 | ASTConsumer *clang::CreateASTPrinterXML(llvm::raw_ostream* out) { |
| 626 | return new ASTPrinterXML(out ? *out : llvm::outs()); |
| 627 | } |
| 628 | |
| 629 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 630 | /// ASTDumper - Low-level dumper of ASTs |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 631 | |
| 632 | namespace { |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 633 | class ASTDumper : public ASTConsumer, public DeclPrinter { |
Chris Lattner | c50a280 | 2009-04-25 23:31:28 +0000 | [diff] [blame] | 634 | ASTContext *Ctx; |
Douglas Gregor | 609e72f | 2009-04-26 02:02:08 +0000 | [diff] [blame] | 635 | bool FullDump; |
| 636 | |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 637 | public: |
Douglas Gregor | 609e72f | 2009-04-26 02:02:08 +0000 | [diff] [blame] | 638 | explicit ASTDumper(bool FullDump) : DeclPrinter(), FullDump(FullDump) {} |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 639 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 640 | void Initialize(ASTContext &Context) { |
Chris Lattner | c50a280 | 2009-04-25 23:31:28 +0000 | [diff] [blame] | 641 | Ctx = &Context; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 642 | } |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 644 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
Douglas Gregor | 609e72f | 2009-04-26 02:02:08 +0000 | [diff] [blame] | 645 | if (FullDump) |
| 646 | return; |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 647 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 648 | HandleTopLevelSingleDecl(*I); |
| 649 | } |
| 650 | void HandleTopLevelSingleDecl(Decl *D); |
Douglas Gregor | 609e72f | 2009-04-26 02:02:08 +0000 | [diff] [blame] | 651 | |
| 652 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 653 | if (!FullDump) |
| 654 | return; |
| 655 | |
| 656 | for (DeclContext::decl_iterator |
| 657 | D = Ctx.getTranslationUnitDecl()->decls_begin(Ctx), |
| 658 | DEnd = Ctx.getTranslationUnitDecl()->decls_end(Ctx); |
| 659 | D != DEnd; |
| 660 | ++D) |
| 661 | HandleTopLevelSingleDecl(*D); |
| 662 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 663 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 664 | } // end anonymous namespace |
| 665 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 666 | void ASTDumper::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 667 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 668 | PrintFunctionDeclStart(FD); |
| 669 | |
Chris Lattner | c50a280 | 2009-04-25 23:31:28 +0000 | [diff] [blame] | 670 | if (Stmt *Body = FD->getBody(*Ctx)) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 671 | Out << '\n'; |
Chris Lattner | c50a280 | 2009-04-25 23:31:28 +0000 | [diff] [blame] | 672 | // FIXME: convert dumper to use raw_ostream. |
| 673 | Body->dumpAll(Ctx->getSourceManager()); |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 674 | Out << '\n'; |
| 675 | } |
| 676 | } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 677 | PrintTypeDefDecl(TD); |
| 678 | } else if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 679 | Out << "Read objc interface '" << OID->getNameAsString() << "'\n"; |
| 680 | } else if (ObjCProtocolDecl *OPD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 681 | Out << "Read objc protocol '" << OPD->getNameAsString() << "'\n"; |
| 682 | } else if (ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 683 | Out << "Read objc category '" << OCD->getNameAsString() << "'\n"; |
| 684 | } else if (isa<ObjCForwardProtocolDecl>(D)) { |
| 685 | Out << "Read objc fwd protocol decl\n"; |
| 686 | } else if (isa<ObjCClassDecl>(D)) { |
| 687 | Out << "Read objc fwd class decl\n"; |
| 688 | } else if (isa<FileScopeAsmDecl>(D)) { |
| 689 | Out << "Read file scope asm decl\n"; |
| 690 | } else if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 691 | Out << "Read objc method decl: '" << MD->getSelector().getAsString() |
| 692 | << "'\n"; |
Chris Lattner | c50a280 | 2009-04-25 23:31:28 +0000 | [diff] [blame] | 693 | if (Stmt *S = MD->getBody()) { |
| 694 | // FIXME: convert dumper to use raw_ostream. |
| 695 | S->dumpAll(Ctx->getSourceManager()); |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 696 | Out << '\n'; |
| 697 | } |
| 698 | } else if (isa<ObjCImplementationDecl>(D)) { |
| 699 | Out << "Read objc implementation decl\n"; |
| 700 | } else if (isa<ObjCCategoryImplDecl>(D)) { |
| 701 | Out << "Read objc category implementation decl\n"; |
| 702 | } else if (isa<LinkageSpecDecl>(D)) { |
| 703 | Out << "Read linkage spec decl\n"; |
| 704 | } else if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 705 | Out << "Read top-level variable decl: '" << ND->getNameAsString() |
| 706 | << "'\n"; |
| 707 | } else { |
| 708 | assert(0 && "Unknown decl type!"); |
| 709 | } |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Douglas Gregor | 609e72f | 2009-04-26 02:02:08 +0000 | [diff] [blame] | 712 | ASTConsumer *clang::CreateASTDumper(bool FullDump) { |
| 713 | return new ASTDumper(FullDump); |
| 714 | } |
Chris Lattner | 3d4997d | 2007-09-15 23:02:28 +0000 | [diff] [blame] | 715 | |
Ted Kremenek | 1b5a4bd | 2007-11-27 21:46:50 +0000 | [diff] [blame] | 716 | //===----------------------------------------------------------------------===// |
| 717 | /// ASTViewer - AST Visualization |
| 718 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 719 | namespace { |
| 720 | class ASTViewer : public ASTConsumer { |
| 721 | SourceManager *SM; |
| 722 | public: |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 723 | void Initialize(ASTContext &Context) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 724 | SM = &Context.getSourceManager(); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 725 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 726 | |
| 727 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 728 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 729 | HandleTopLevelSingleDecl(*I); |
| 730 | } |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 731 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 732 | void HandleTopLevelSingleDecl(Decl *D); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 733 | }; |
| 734 | } |
| 735 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 736 | void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 737 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 738 | DeclPrinter().PrintFunctionDeclStart(FD); |
| 739 | |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 740 | if (FD->getBodyIfAvailable()) { |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 741 | llvm::cerr << '\n'; |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 742 | FD->getBodyIfAvailable()->viewAST(); |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 743 | llvm::cerr << '\n'; |
| 744 | } |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 749 | DeclPrinter().PrintObjCMethodDecl(MD); |
| 750 | |
| 751 | if (MD->getBody()) { |
| 752 | llvm::cerr << '\n'; |
| 753 | MD->getBody()->viewAST(); |
| 754 | llvm::cerr << '\n'; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 760 | ASTConsumer *clang::CreateASTViewer() { return new ASTViewer(); } |
| 761 | |
Ted Kremenek | 74bf2c9 | 2007-09-07 23:47:56 +0000 | [diff] [blame] | 762 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 763 | /// DeclContextPrinter - Decl and DeclContext Visualization |
| 764 | |
| 765 | namespace { |
| 766 | |
| 767 | class DeclContextPrinter : public ASTConsumer { |
| 768 | llvm::raw_ostream& Out; |
| 769 | public: |
| 770 | DeclContextPrinter() : Out(llvm::errs()) {} |
| 771 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 772 | void HandleTranslationUnit(ASTContext &C) { |
| 773 | PrintDeclContext(C.getTranslationUnitDecl(), 4); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | void PrintDeclContext(const DeclContext* DC, unsigned Indentation); |
| 777 | }; |
Chris Lattner | b23ff6b | 2009-03-28 05:44:17 +0000 | [diff] [blame] | 778 | } // end anonymous namespace |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 779 | |
| 780 | void DeclContextPrinter::PrintDeclContext(const DeclContext* DC, |
| 781 | unsigned Indentation) { |
| 782 | // Print DeclContext name. |
Argyrios Kyrtzidis | 9b9ca01 | 2009-01-13 13:11:58 +0000 | [diff] [blame] | 783 | switch (DC->getDeclKind()) { |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 784 | case Decl::TranslationUnit: |
| 785 | Out << "[translation unit] " << DC; |
| 786 | break; |
| 787 | case Decl::Namespace: { |
| 788 | Out << "[namespace] "; |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 789 | const NamespaceDecl* ND = cast<NamespaceDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 790 | Out << ND->getNameAsString(); |
| 791 | break; |
| 792 | } |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 793 | case Decl::Enum: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 794 | const EnumDecl* ED = cast<EnumDecl>(DC); |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 795 | if (ED->isDefinition()) |
| 796 | Out << "[enum] "; |
| 797 | else |
| 798 | Out << "<enum> "; |
| 799 | Out << ED->getNameAsString(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 800 | break; |
Zhongxing Xu | 867c39e | 2009-01-13 02:41:08 +0000 | [diff] [blame] | 801 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 802 | case Decl::Record: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 803 | const RecordDecl* RD = cast<RecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 804 | if (RD->isDefinition()) |
| 805 | Out << "[struct] "; |
| 806 | else |
| 807 | Out << "<struct> "; |
| 808 | Out << RD->getNameAsString(); |
| 809 | break; |
| 810 | } |
| 811 | case Decl::CXXRecord: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 812 | const CXXRecordDecl* RD = cast<CXXRecordDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 813 | if (RD->isDefinition()) |
| 814 | Out << "[class] "; |
| 815 | else |
| 816 | Out << "<class> "; |
| 817 | Out << RD->getNameAsString() << " " << DC; |
| 818 | break; |
| 819 | } |
| 820 | case Decl::ObjCMethod: |
| 821 | Out << "[objc method]"; |
| 822 | break; |
| 823 | case Decl::ObjCInterface: |
| 824 | Out << "[objc interface]"; |
| 825 | break; |
| 826 | case Decl::ObjCCategory: |
| 827 | Out << "[objc category]"; |
| 828 | break; |
| 829 | case Decl::ObjCProtocol: |
| 830 | Out << "[objc protocol]"; |
| 831 | break; |
| 832 | case Decl::ObjCImplementation: |
| 833 | Out << "[objc implementation]"; |
| 834 | break; |
| 835 | case Decl::ObjCCategoryImpl: |
| 836 | Out << "[objc categoryimpl]"; |
| 837 | break; |
| 838 | case Decl::LinkageSpec: |
| 839 | Out << "[linkage spec]"; |
| 840 | break; |
| 841 | case Decl::Block: |
| 842 | Out << "[block]"; |
| 843 | break; |
| 844 | case Decl::Function: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 845 | const FunctionDecl* FD = cast<FunctionDecl>(DC); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 846 | if (FD->isThisDeclarationADefinition()) |
| 847 | Out << "[function] "; |
| 848 | else |
| 849 | Out << "<function> "; |
| 850 | Out << FD->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 851 | // Print the parameters. |
| 852 | Out << "("; |
| 853 | bool PrintComma = false; |
| 854 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
| 855 | E = FD->param_end(); I != E; ++I) { |
| 856 | if (PrintComma) |
| 857 | Out << ", "; |
| 858 | else |
| 859 | PrintComma = true; |
| 860 | Out << (*I)->getNameAsString(); |
| 861 | } |
| 862 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 863 | break; |
| 864 | } |
| 865 | case Decl::CXXMethod: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 866 | const CXXMethodDecl* D = cast<CXXMethodDecl>(DC); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 867 | if (D->isOutOfLineDefinition()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 868 | Out << "[c++ method] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 869 | else if (D->isImplicit()) |
| 870 | Out << "(c++ method) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 871 | else |
| 872 | Out << "<c++ method> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 873 | Out << D->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 874 | // Print the parameters. |
| 875 | Out << "("; |
| 876 | bool PrintComma = false; |
| 877 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
| 878 | E = D->param_end(); I != E; ++I) { |
| 879 | if (PrintComma) |
| 880 | Out << ", "; |
| 881 | else |
| 882 | PrintComma = true; |
| 883 | Out << (*I)->getNameAsString(); |
| 884 | } |
| 885 | Out << ")"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 886 | |
| 887 | // Check the semantic DeclContext. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 888 | const DeclContext* SemaDC = D->getDeclContext(); |
| 889 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 890 | if (SemaDC != LexicalDC) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 891 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 892 | |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 893 | break; |
| 894 | } |
| 895 | case Decl::CXXConstructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 896 | const CXXConstructorDecl* D = cast<CXXConstructorDecl>(DC); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 897 | if (D->isOutOfLineDefinition()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 898 | Out << "[c++ ctor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 899 | else if (D->isImplicit()) |
| 900 | Out << "(c++ ctor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 901 | else |
| 902 | Out << "<c++ ctor> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 903 | Out << D->getNameAsString(); |
Zhongxing Xu | ca04ce4 | 2009-01-13 06:25:33 +0000 | [diff] [blame] | 904 | // Print the parameters. |
| 905 | Out << "("; |
| 906 | bool PrintComma = false; |
| 907 | for (FunctionDecl::param_const_iterator I = D->param_begin(), |
| 908 | E = D->param_end(); I != E; ++I) { |
| 909 | if (PrintComma) |
| 910 | Out << ", "; |
| 911 | else |
| 912 | PrintComma = true; |
| 913 | Out << (*I)->getNameAsString(); |
| 914 | } |
| 915 | Out << ")"; |
| 916 | |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 917 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 918 | const DeclContext* SemaDC = D->getDeclContext(); |
| 919 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 920 | if (SemaDC != LexicalDC) |
| 921 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 922 | break; |
| 923 | } |
| 924 | case Decl::CXXDestructor: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 925 | const CXXDestructorDecl* D = cast<CXXDestructorDecl>(DC); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 926 | if (D->isOutOfLineDefinition()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 927 | Out << "[c++ dtor] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 928 | else if (D->isImplicit()) |
| 929 | Out << "(c++ dtor) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 930 | else |
| 931 | Out << "<c++ dtor> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 932 | Out << D->getNameAsString(); |
| 933 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 934 | const DeclContext* SemaDC = D->getDeclContext(); |
| 935 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 936 | if (SemaDC != LexicalDC) |
| 937 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 938 | break; |
| 939 | } |
| 940 | case Decl::CXXConversion: { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 941 | const CXXConversionDecl* D = cast<CXXConversionDecl>(DC); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 942 | if (D->isOutOfLineDefinition()) |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 943 | Out << "[c++ conversion] "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 944 | else if (D->isImplicit()) |
| 945 | Out << "(c++ conversion) "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 946 | else |
| 947 | Out << "<c++ conversion> "; |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 948 | Out << D->getNameAsString(); |
| 949 | // Check the semantic DC. |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 950 | const DeclContext* SemaDC = D->getDeclContext(); |
| 951 | const DeclContext* LexicalDC = D->getLexicalDeclContext(); |
Zhongxing Xu | 2a3eb0e | 2009-01-13 03:26:38 +0000 | [diff] [blame] | 952 | if (SemaDC != LexicalDC) |
| 953 | Out << " [[" << SemaDC << "]]"; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 954 | break; |
| 955 | } |
| 956 | |
| 957 | default: |
| 958 | assert(0 && "a decl that inherits DeclContext isn't handled"); |
| 959 | } |
| 960 | |
| 961 | Out << "\n"; |
| 962 | |
| 963 | // Print decls in the DeclContext. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 964 | // FIXME: Should not use a NULL DeclContext! |
| 965 | ASTContext *Context = 0; |
| 966 | for (DeclContext::decl_iterator I = DC->decls_begin(*Context), |
| 967 | E = DC->decls_end(*Context); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 968 | I != E; ++I) { |
| 969 | for (unsigned i = 0; i < Indentation; ++i) |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 970 | Out << " "; |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 971 | |
| 972 | Decl::Kind DK = I->getKind(); |
| 973 | switch (DK) { |
| 974 | case Decl::Namespace: |
| 975 | case Decl::Enum: |
| 976 | case Decl::Record: |
| 977 | case Decl::CXXRecord: |
| 978 | case Decl::ObjCMethod: |
| 979 | case Decl::ObjCInterface: |
| 980 | case Decl::ObjCCategory: |
| 981 | case Decl::ObjCProtocol: |
| 982 | case Decl::ObjCImplementation: |
| 983 | case Decl::ObjCCategoryImpl: |
| 984 | case Decl::LinkageSpec: |
| 985 | case Decl::Block: |
| 986 | case Decl::Function: |
| 987 | case Decl::CXXMethod: |
| 988 | case Decl::CXXConstructor: |
| 989 | case Decl::CXXDestructor: |
| 990 | case Decl::CXXConversion: |
| 991 | { |
Argyrios Kyrtzidis | 7ad5bf3 | 2009-02-16 14:29:59 +0000 | [diff] [blame] | 992 | DeclContext* DC = cast<DeclContext>(*I); |
Mike Stump | 071e4da | 2009-02-10 20:16:46 +0000 | [diff] [blame] | 993 | PrintDeclContext(DC, Indentation+2); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 994 | break; |
| 995 | } |
| 996 | case Decl::Field: { |
| 997 | FieldDecl* FD = cast<FieldDecl>(*I); |
| 998 | Out << "<field> " << FD->getNameAsString() << "\n"; |
| 999 | break; |
| 1000 | } |
| 1001 | case Decl::Typedef: { |
| 1002 | TypedefDecl* TD = cast<TypedefDecl>(*I); |
| 1003 | Out << "<typedef> " << TD->getNameAsString() << "\n"; |
| 1004 | break; |
| 1005 | } |
| 1006 | case Decl::EnumConstant: { |
| 1007 | EnumConstantDecl* ECD = cast<EnumConstantDecl>(*I); |
| 1008 | Out << "<enum constant> " << ECD->getNameAsString() << "\n"; |
| 1009 | break; |
| 1010 | } |
| 1011 | case Decl::Var: { |
| 1012 | VarDecl* VD = cast<VarDecl>(*I); |
| 1013 | Out << "<var> " << VD->getNameAsString() << "\n"; |
| 1014 | break; |
| 1015 | } |
| 1016 | case Decl::ImplicitParam: { |
| 1017 | ImplicitParamDecl* IPD = cast<ImplicitParamDecl>(*I); |
| 1018 | Out << "<implicit parameter> " << IPD->getNameAsString() << "\n"; |
| 1019 | break; |
| 1020 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 1021 | case Decl::ParmVar: { |
| 1022 | ParmVarDecl* PVD = cast<ParmVarDecl>(*I); |
| 1023 | Out << "<parameter> " << PVD->getNameAsString() << "\n"; |
| 1024 | break; |
| 1025 | } |
Zhongxing Xu | ba16be9 | 2009-04-05 02:04:38 +0000 | [diff] [blame] | 1026 | case Decl::OriginalParmVar: { |
| 1027 | OriginalParmVarDecl* OPVD = cast<OriginalParmVarDecl>(*I); |
| 1028 | Out << "<original parameter> " << OPVD->getNameAsString() << "\n"; |
| 1029 | break; |
| 1030 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 1031 | case Decl::ObjCProperty: { |
| 1032 | ObjCPropertyDecl* OPD = cast<ObjCPropertyDecl>(*I); |
| 1033 | Out << "<objc property> " << OPD->getNameAsString() << "\n"; |
| 1034 | break; |
| 1035 | } |
| 1036 | default: |
Zhongxing Xu | ba16be9 | 2009-04-05 02:04:38 +0000 | [diff] [blame] | 1037 | fprintf(stderr, "DeclKind: %d \"%s\"\n", DK, I->getDeclKindName()); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 1038 | assert(0 && "decl unhandled"); |
| 1039 | } |
| 1040 | } |
| 1041 | } |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 1042 | ASTConsumer *clang::CreateDeclContextPrinter() { |
| 1043 | return new DeclContextPrinter(); |
| 1044 | } |
| 1045 | |
| 1046 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1047 | /// InheritanceViewer - C++ Inheritance Visualization |
| 1048 | |
| 1049 | namespace { |
| 1050 | class InheritanceViewer : public ASTConsumer { |
| 1051 | const std::string clsname; |
| 1052 | public: |
| 1053 | InheritanceViewer(const std::string& cname) : clsname(cname) {} |
| 1054 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 1055 | void HandleTranslationUnit(ASTContext &C) { |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1056 | for (ASTContext::type_iterator I=C.types_begin(),E=C.types_end(); I!=E; ++I) |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1057 | if (RecordType *T = dyn_cast<RecordType>(*I)) { |
| 1058 | if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(T->getDecl())) { |
| 1059 | // FIXME: This lookup needs to be generalized to handle namespaces and |
| 1060 | // (when we support them) templates. |
| 1061 | if (D->getNameAsString() == clsname) { |
| 1062 | D->viewInheritance(C); |
| 1063 | } |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | }; |
| 1068 | } |
| 1069 | |
| 1070 | ASTConsumer *clang::CreateInheritanceViewer(const std::string& clsname) { |
| 1071 | return new InheritanceViewer(clsname); |
| 1072 | } |