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