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