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