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