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