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