Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1 | //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Decl::dump method, which pretty print the |
| 11 | // AST back out to C/Objective-C/C++/Objective-C++ code. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/DeclVisitor.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclCXX.h" |
| 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/PrettyPrinter.h" |
| 21 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
| 27 | class VISIBILITY_HIDDEN DeclPrinter : public DeclVisitor<DeclPrinter> { |
| 28 | llvm::raw_ostream &Out; |
| 29 | ASTContext &Context; |
| 30 | PrintingPolicy Policy; |
| 31 | unsigned Indentation; |
| 32 | |
| 33 | llvm::raw_ostream& Indent(); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 34 | void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 35 | |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 36 | void Print(AccessSpecifier AS); |
| 37 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 38 | public: |
| 39 | DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context, |
| 40 | const PrintingPolicy &Policy, |
| 41 | unsigned Indentation = 0) |
| 42 | : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { } |
| 43 | |
| 44 | void VisitDeclContext(DeclContext *DC, bool Indent = true); |
| 45 | |
| 46 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 47 | void VisitTypedefDecl(TypedefDecl *D); |
| 48 | void VisitEnumDecl(EnumDecl *D); |
| 49 | void VisitRecordDecl(RecordDecl *D); |
| 50 | void VisitEnumConstantDecl(EnumConstantDecl *D); |
| 51 | void VisitFunctionDecl(FunctionDecl *D); |
| 52 | void VisitFieldDecl(FieldDecl *D); |
| 53 | void VisitVarDecl(VarDecl *D); |
| 54 | void VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 55 | void VisitOriginalParmVarDecl(OriginalParmVarDecl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 56 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 57 | void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D); |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 58 | void VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 59 | void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 60 | void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 61 | void VisitCXXRecordDecl(CXXRecordDecl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 62 | void VisitLinkageSpecDecl(LinkageSpecDecl *D); |
| 63 | void VisitTemplateDecl(TemplateDecl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 64 | void VisitObjCMethodDecl(ObjCMethodDecl *D); |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 65 | void VisitObjCClassDecl(ObjCClassDecl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 66 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 67 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 68 | void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 69 | void VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| 70 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 71 | void VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| 72 | void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); |
| 73 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 74 | void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
Anders Carlsson | f53eaa5 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 75 | void VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D); |
| 76 | void VisitUsingDecl(UsingDecl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 77 | }; |
| 78 | } |
| 79 | |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 80 | void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) { |
| 81 | print(Out, getASTContext().PrintingPolicy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 84 | void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy, |
| 85 | unsigned Indentation) { |
| 86 | DeclPrinter Printer(Out, getASTContext(), Policy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 87 | Printer.Visit(this); |
| 88 | } |
| 89 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 90 | static QualType GetBaseType(QualType T) { |
| 91 | // FIXME: This should be on the Type class! |
| 92 | QualType BaseType = T; |
| 93 | while (!BaseType->isSpecifierType()) { |
| 94 | if (isa<TypedefType>(BaseType)) |
| 95 | break; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 96 | else if (const PointerType* PTy = BaseType->getAs<PointerType>()) |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 97 | BaseType = PTy->getPointeeType(); |
| 98 | else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) |
| 99 | BaseType = ATy->getElementType(); |
| 100 | else if (const FunctionType* FTy = BaseType->getAsFunctionType()) |
| 101 | BaseType = FTy->getResultType(); |
Douglas Gregor | 5068ab6 | 2009-07-01 23:58:14 +0000 | [diff] [blame] | 102 | else if (const VectorType *VTy = BaseType->getAsVectorType()) |
| 103 | BaseType = VTy->getElementType(); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 104 | else |
| 105 | assert(0 && "Unknown declarator!"); |
| 106 | } |
| 107 | return BaseType; |
| 108 | } |
| 109 | |
| 110 | static QualType getDeclType(Decl* D) { |
| 111 | if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D)) |
| 112 | return TDD->getUnderlyingType(); |
| 113 | if (ValueDecl* VD = dyn_cast<ValueDecl>(D)) |
| 114 | return VD->getType(); |
| 115 | return QualType(); |
| 116 | } |
| 117 | |
| 118 | void Decl::printGroup(Decl** Begin, unsigned NumDecls, |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 119 | llvm::raw_ostream &Out, const PrintingPolicy &Policy, |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 120 | unsigned Indentation) { |
| 121 | if (NumDecls == 1) { |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 122 | (*Begin)->print(Out, Policy, Indentation); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
| 126 | Decl** End = Begin + NumDecls; |
| 127 | TagDecl* TD = dyn_cast<TagDecl>(*Begin); |
| 128 | if (TD) |
| 129 | ++Begin; |
| 130 | |
| 131 | PrintingPolicy SubPolicy(Policy); |
| 132 | if (TD && TD->isDefinition()) { |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 133 | TD->print(Out, Policy, Indentation); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 134 | Out << " "; |
| 135 | SubPolicy.SuppressTag = true; |
| 136 | } |
| 137 | |
| 138 | bool isFirst = true; |
| 139 | for ( ; Begin != End; ++Begin) { |
| 140 | if (isFirst) { |
| 141 | SubPolicy.SuppressSpecifiers = false; |
| 142 | isFirst = false; |
| 143 | } else { |
| 144 | if (!isFirst) Out << ", "; |
| 145 | SubPolicy.SuppressSpecifiers = true; |
| 146 | } |
| 147 | |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 148 | (*Begin)->print(Out, SubPolicy, Indentation); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 152 | void Decl::dump() { |
| 153 | print(llvm::errs()); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | llvm::raw_ostream& DeclPrinter::Indent() { |
| 157 | for (unsigned i = 0; i < Indentation; ++i) |
| 158 | Out << " "; |
| 159 | return Out; |
| 160 | } |
| 161 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 162 | void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) { |
| 163 | this->Indent(); |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 164 | Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 165 | Out << ";\n"; |
| 166 | Decls.clear(); |
| 167 | |
| 168 | } |
| 169 | |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 170 | void DeclPrinter::Print(AccessSpecifier AS) { |
| 171 | switch(AS) { |
Anders Carlsson | 018e9fe | 2009-08-29 20:36:12 +0000 | [diff] [blame^] | 172 | case AS_none: assert(0 && "No access specifier!"); break; |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 173 | case AS_public: Out << "public"; break; |
| 174 | case AS_protected: Out << "protected"; break; |
| 175 | case AS_private: Out << " private"; break; |
| 176 | } |
| 177 | } |
| 178 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 179 | //---------------------------------------------------------------------------- |
| 180 | // Common C declarations |
| 181 | //---------------------------------------------------------------------------- |
| 182 | |
| 183 | void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { |
| 184 | if (Indent) |
| 185 | Indentation += Policy.Indentation; |
| 186 | |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 187 | bool PrintAccess = isa<CXXRecordDecl>(DC); |
| 188 | AccessSpecifier CurAS = AS_none; |
| 189 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 190 | llvm::SmallVector<Decl*, 2> Decls; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 191 | for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 192 | D != DEnd; ++D) { |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 193 | if (!Policy.Dump) { |
| 194 | // Skip over implicit declarations in pretty-printing mode. |
| 195 | if (D->isImplicit()) continue; |
Eli Friedman | 3d4a7c9 | 2009-05-30 06:35:22 +0000 | [diff] [blame] | 196 | // FIXME: Ugly hack so we don't pretty-print the builtin declaration |
| 197 | // of __builtin_va_list. There should be some other way to check that. |
| 198 | if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() == |
| 199 | "__builtin_va_list") |
| 200 | continue; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 203 | if (PrintAccess) { |
| 204 | AccessSpecifier AS = D->getAccess(); |
| 205 | if (AS != CurAS) { |
| 206 | Print(AS); |
| 207 | Out << ":\n"; |
| 208 | CurAS = AS; |
| 209 | } |
| 210 | } |
| 211 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 212 | // The next bits of code handles stuff like "struct {int x;} a,b"; we're |
| 213 | // forced to merge the declarations because there's no other way to |
| 214 | // refer to the struct in question. This limited merging is safe without |
| 215 | // a bunch of other checks because it only merges declarations directly |
| 216 | // referring to the tag, not typedefs. |
| 217 | // |
| 218 | // Check whether the current declaration should be grouped with a previous |
| 219 | // unnamed struct. |
| 220 | QualType CurDeclType = getDeclType(*D); |
| 221 | if (!Decls.empty() && !CurDeclType.isNull()) { |
| 222 | QualType BaseType = GetBaseType(CurDeclType); |
| 223 | if (!BaseType.isNull() && isa<TagType>(BaseType) && |
| 224 | cast<TagType>(BaseType)->getDecl() == Decls[0]) { |
| 225 | Decls.push_back(*D); |
| 226 | continue; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // If we have a merged group waiting to be handled, handle it now. |
| 231 | if (!Decls.empty()) |
| 232 | ProcessDeclGroup(Decls); |
| 233 | |
| 234 | // If the current declaration is an unnamed tag type, save it |
| 235 | // so we can merge it with the subsequent declaration(s) using it. |
| 236 | if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) { |
| 237 | Decls.push_back(*D); |
| 238 | continue; |
| 239 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 240 | this->Indent(); |
| 241 | Visit(*D); |
| 242 | |
| 243 | // FIXME: Need to be able to tell the DeclPrinter when |
| 244 | const char *Terminator = 0; |
| 245 | if (isa<FunctionDecl>(*D) && |
| 246 | cast<FunctionDecl>(*D)->isThisDeclarationADefinition()) |
| 247 | Terminator = 0; |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 248 | else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody()) |
| 249 | Terminator = 0; |
| 250 | else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) || |
| 251 | isa<ObjCImplementationDecl>(*D) || |
| 252 | isa<ObjCInterfaceDecl>(*D) || |
| 253 | isa<ObjCProtocolDecl>(*D) || |
| 254 | isa<ObjCCategoryImplDecl>(*D) || |
| 255 | isa<ObjCCategoryDecl>(*D)) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 256 | Terminator = 0; |
| 257 | else if (isa<EnumConstantDecl>(*D)) { |
| 258 | DeclContext::decl_iterator Next = D; |
| 259 | ++Next; |
| 260 | if (Next != DEnd) |
| 261 | Terminator = ","; |
| 262 | } else |
| 263 | Terminator = ";"; |
| 264 | |
| 265 | if (Terminator) |
| 266 | Out << Terminator; |
| 267 | Out << "\n"; |
| 268 | } |
| 269 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 270 | if (!Decls.empty()) |
| 271 | ProcessDeclGroup(Decls); |
| 272 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 273 | if (Indent) |
| 274 | Indentation -= Policy.Indentation; |
| 275 | } |
| 276 | |
| 277 | void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 278 | VisitDeclContext(D, false); |
| 279 | } |
| 280 | |
| 281 | void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { |
| 282 | std::string S = D->getNameAsString(); |
| 283 | D->getUnderlyingType().getAsStringInternal(S, Policy); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 284 | if (!Policy.SuppressSpecifiers) |
| 285 | Out << "typedef "; |
| 286 | Out << S; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void DeclPrinter::VisitEnumDecl(EnumDecl *D) { |
| 290 | Out << "enum " << D->getNameAsString() << " {\n"; |
| 291 | VisitDeclContext(D); |
| 292 | Indent() << "}"; |
| 293 | } |
| 294 | |
| 295 | void DeclPrinter::VisitRecordDecl(RecordDecl *D) { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 296 | Out << D->getKindName(); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 297 | if (D->getIdentifier()) { |
| 298 | Out << " "; |
| 299 | Out << D->getNameAsString(); |
| 300 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 301 | |
| 302 | if (D->isDefinition()) { |
| 303 | Out << " {\n"; |
| 304 | VisitDeclContext(D); |
| 305 | Indent() << "}"; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 310 | Out << D->getNameAsString(); |
| 311 | if (Expr *Init = D->getInitExpr()) { |
| 312 | Out << " = "; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 313 | Init->printPretty(Out, Context, 0, Policy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 318 | if (!Policy.SuppressSpecifiers) { |
| 319 | switch (D->getStorageClass()) { |
| 320 | case FunctionDecl::None: break; |
| 321 | case FunctionDecl::Extern: Out << "extern "; break; |
| 322 | case FunctionDecl::Static: Out << "static "; break; |
| 323 | case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break; |
| 324 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 325 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 326 | if (D->isInline()) Out << "inline "; |
| 327 | if (D->isVirtualAsWritten()) Out << "virtual "; |
| 328 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 329 | |
Douglas Gregor | 6620a62 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 330 | PrintingPolicy SubPolicy(Policy); |
| 331 | SubPolicy.SuppressSpecifiers = false; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 332 | std::string Proto = D->getNameAsString(); |
| 333 | if (isa<FunctionType>(D->getType().getTypePtr())) { |
| 334 | const FunctionType *AFT = D->getType()->getAsFunctionType(); |
| 335 | |
| 336 | const FunctionProtoType *FT = 0; |
| 337 | if (D->hasWrittenPrototype()) |
| 338 | FT = dyn_cast<FunctionProtoType>(AFT); |
| 339 | |
| 340 | Proto += "("; |
| 341 | if (FT) { |
| 342 | llvm::raw_string_ostream POut(Proto); |
Douglas Gregor | 6620a62 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 343 | DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 344 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 345 | if (i) POut << ", "; |
| 346 | ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); |
| 347 | } |
| 348 | |
| 349 | if (FT->isVariadic()) { |
| 350 | if (D->getNumParams()) POut << ", "; |
| 351 | POut << "..."; |
| 352 | } |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 353 | } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) { |
| 354 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 355 | if (i) |
| 356 | Proto += ", "; |
| 357 | Proto += D->getParamDecl(i)->getNameAsString(); |
| 358 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | Proto += ")"; |
Mike Stump | fd350b5 | 2009-07-27 21:33:40 +0000 | [diff] [blame] | 362 | if (D->hasAttr<NoReturnAttr>()) |
| 363 | Proto += " __attribute((noreturn))"; |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 364 | if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) { |
| 365 | if (CDecl->getNumBaseOrMemberInitializers() > 0) { |
| 366 | Proto += " : "; |
| 367 | Out << Proto; |
| 368 | Proto.clear(); |
| 369 | for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(), |
| 370 | E = CDecl->init_end(); |
| 371 | B != E; ++B) { |
| 372 | CXXBaseOrMemberInitializer * BMInitializer = (*B); |
| 373 | if (B != CDecl->init_begin()) |
| 374 | Out << ", "; |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 375 | bool hasArguments = (BMInitializer->arg_begin() != |
| 376 | BMInitializer->arg_end()); |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 377 | if (BMInitializer->isMemberInitializer()) { |
| 378 | FieldDecl *FD = BMInitializer->getMember(); |
| 379 | Out << FD->getNameAsString(); |
| 380 | } |
Fariborz Jahanian | 29146ad | 2009-07-14 23:41:35 +0000 | [diff] [blame] | 381 | else // FIXME. skip dependent types for now. |
| 382 | if (const RecordType *RT = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 383 | BMInitializer->getBaseClass()->getAs<RecordType>()) { |
Fariborz Jahanian | 29146ad | 2009-07-14 23:41:35 +0000 | [diff] [blame] | 384 | const CXXRecordDecl *BaseDecl = |
| 385 | cast<CXXRecordDecl>(RT->getDecl()); |
| 386 | Out << BaseDecl->getNameAsString(); |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 387 | } |
| 388 | if (hasArguments) { |
| 389 | Out << "("; |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 390 | for (CXXBaseOrMemberInitializer::const_arg_iterator BE = |
| 391 | BMInitializer->const_arg_begin(), |
| 392 | EE = BMInitializer->const_arg_end(); BE != EE; ++BE) { |
| 393 | if (BE != BMInitializer->const_arg_begin()) |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 394 | Out<< ", "; |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 395 | const Expr *Exp = (*BE); |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 396 | Exp->printPretty(Out, Context, 0, Policy, Indentation); |
| 397 | } |
| 398 | Out << ")"; |
Fariborz Jahanian | 939afd8 | 2009-07-13 23:31:10 +0000 | [diff] [blame] | 399 | } else |
| 400 | Out << "()"; |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 404 | else if (CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(D)) { |
| 405 | if (DDecl->getNumBaseOrMemberDestructions() > 0) { |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 406 | // List order of base/member destruction for visualization purposes. |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 407 | assert (D->isThisDeclarationADefinition() && "Destructor with dtor-list"); |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 408 | Proto += "/* : "; |
| 409 | for (CXXDestructorDecl::destr_const_iterator *B = DDecl->destr_begin(), |
| 410 | *E = DDecl->destr_end(); |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 411 | B != E; ++B) { |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 412 | uintptr_t BaseOrMember = (*B); |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 413 | if (B != DDecl->destr_begin()) |
| 414 | Proto += ", "; |
| 415 | |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 416 | if (DDecl->isMemberToDestroy(BaseOrMember)) { |
| 417 | FieldDecl *FD = DDecl->getMemberToDestroy(BaseOrMember); |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 418 | Proto += "~"; |
| 419 | Proto += FD->getNameAsString(); |
| 420 | } |
| 421 | else // FIXME. skip dependent types for now. |
| 422 | if (const RecordType *RT = |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 423 | DDecl->getAnyBaseClassToDestroy(BaseOrMember) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 424 | ->getAs<RecordType>()) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 425 | const CXXRecordDecl *BaseDecl = |
| 426 | cast<CXXRecordDecl>(RT->getDecl()); |
| 427 | Proto += "~"; |
| 428 | Proto += BaseDecl->getNameAsString(); |
| 429 | } |
| 430 | Proto += "()"; |
| 431 | } |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 432 | Proto += " */"; |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 435 | else |
| 436 | AFT->getResultType().getAsStringInternal(Proto, Policy); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 437 | } else { |
| 438 | D->getType().getAsStringInternal(Proto, Policy); |
| 439 | } |
| 440 | |
| 441 | Out << Proto; |
| 442 | |
| 443 | if (D->isPure()) |
| 444 | Out << " = 0"; |
| 445 | else if (D->isDeleted()) |
| 446 | Out << " = delete"; |
| 447 | else if (D->isThisDeclarationADefinition()) { |
| 448 | if (!D->hasPrototype() && D->getNumParams()) { |
| 449 | // This is a K&R function definition, so we need to print the |
| 450 | // parameters. |
| 451 | Out << '\n'; |
Douglas Gregor | 6620a62 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 452 | DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 453 | Indentation += Policy.Indentation; |
| 454 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 455 | Indent(); |
Douglas Gregor | 6620a62 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 456 | ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 457 | Out << ";\n"; |
| 458 | } |
| 459 | Indentation -= Policy.Indentation; |
| 460 | } else |
| 461 | Out << ' '; |
| 462 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 463 | D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 464 | Out << '\n'; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void DeclPrinter::VisitFieldDecl(FieldDecl *D) { |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 469 | if (!Policy.SuppressSpecifiers && D->isMutable()) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 470 | Out << "mutable "; |
| 471 | |
| 472 | std::string Name = D->getNameAsString(); |
| 473 | D->getType().getAsStringInternal(Name, Policy); |
| 474 | Out << Name; |
| 475 | |
| 476 | if (D->isBitField()) { |
| 477 | Out << " : "; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 478 | D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | void DeclPrinter::VisitVarDecl(VarDecl *D) { |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 483 | if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 484 | Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " "; |
| 485 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 486 | if (!Policy.SuppressSpecifiers && D->isThreadSpecified()) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 487 | Out << "__thread "; |
| 488 | |
| 489 | std::string Name = D->getNameAsString(); |
| 490 | QualType T = D->getType(); |
| 491 | if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D)) |
| 492 | T = Parm->getOriginalType(); |
| 493 | T.getAsStringInternal(Name, Policy); |
| 494 | Out << Name; |
| 495 | if (D->getInit()) { |
| 496 | if (D->hasCXXDirectInitializer()) |
| 497 | Out << "("; |
| 498 | else |
| 499 | Out << " = "; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 500 | D->getInit()->printPretty(Out, Context, 0, Policy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 501 | if (D->hasCXXDirectInitializer()) |
| 502 | Out << ")"; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { |
| 507 | VisitVarDecl(D); |
| 508 | } |
| 509 | |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 510 | void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 511 | VisitVarDecl(D); |
| 512 | } |
| 513 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 514 | void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 515 | Out << "__asm ("; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 516 | D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 517 | Out << ")"; |
| 518 | } |
| 519 | |
| 520 | //---------------------------------------------------------------------------- |
| 521 | // C++ declarations |
| 522 | //---------------------------------------------------------------------------- |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 523 | void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) { |
| 524 | assert(false && |
| 525 | "OverloadedFunctionDecls aren't really decls and are never printed"); |
| 526 | } |
| 527 | |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 528 | void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 529 | Out << "namespace " << D->getNameAsString() << " {\n"; |
| 530 | VisitDeclContext(D); |
| 531 | Indent() << "}"; |
| 532 | } |
| 533 | |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 534 | void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 535 | Out << "using namespace "; |
| 536 | if (D->getQualifier()) |
| 537 | D->getQualifier()->print(Out, Policy); |
| 538 | Out << D->getNominatedNamespace()->getNameAsString(); |
| 539 | } |
| 540 | |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 541 | void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 542 | Out << "namespace " << D->getNameAsString() << " = "; |
| 543 | if (D->getQualifier()) |
| 544 | D->getQualifier()->print(Out, Policy); |
| 545 | Out << D->getAliasedNamespace()->getNameAsString(); |
| 546 | } |
| 547 | |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 548 | void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 549 | Out << D->getKindName(); |
| 550 | if (D->getIdentifier()) { |
| 551 | Out << " "; |
| 552 | Out << D->getNameAsString(); |
| 553 | } |
| 554 | |
| 555 | if (D->isDefinition()) { |
| 556 | // Print the base classes |
| 557 | if (D->getNumBases()) { |
| 558 | Out << " : "; |
| 559 | for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(), |
| 560 | BaseEnd = D->bases_end(); |
| 561 | Base != BaseEnd; ++Base) { |
| 562 | if (Base != D->bases_begin()) |
| 563 | Out << ", "; |
| 564 | |
| 565 | if (Base->isVirtual()) |
| 566 | Out << "virtual "; |
| 567 | |
Anders Carlsson | 018e9fe | 2009-08-29 20:36:12 +0000 | [diff] [blame^] | 568 | AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); |
| 569 | if (AS != AS_none) |
| 570 | Print(AS); |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 571 | Out << " " << Base->getType().getAsString(Policy); |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | // Print the class definition |
Douglas Gregor | f757ae7 | 2009-05-31 07:13:39 +0000 | [diff] [blame] | 576 | // FIXME: Doesn't print access specifiers, e.g., "public:" |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 577 | Out << " {\n"; |
| 578 | VisitDeclContext(D); |
| 579 | Indent() << "}"; |
| 580 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 584 | const char *l; |
| 585 | if (D->getLanguage() == LinkageSpecDecl::lang_c) |
| 586 | l = "C"; |
| 587 | else { |
| 588 | assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && |
| 589 | "unknown language in linkage specification"); |
| 590 | l = "C++"; |
| 591 | } |
| 592 | |
| 593 | Out << "extern \"" << l << "\" "; |
| 594 | if (D->hasBraces()) { |
| 595 | Out << "{\n"; |
| 596 | VisitDeclContext(D); |
| 597 | Indent() << "}"; |
| 598 | } else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 599 | Visit(*D->decls_begin()); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) { |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 603 | Out << "template <"; |
| 604 | |
| 605 | TemplateParameterList *Params = D->getTemplateParameters(); |
| 606 | for (unsigned i = 0, e = Params->size(); i != e; ++i) { |
| 607 | if (i != 0) |
| 608 | Out << ", "; |
| 609 | |
| 610 | const Decl *Param = Params->getParam(i); |
| 611 | if (const TemplateTypeParmDecl *TTP = |
| 612 | dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 613 | |
| 614 | QualType ParamType = |
| 615 | Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP)); |
| 616 | |
| 617 | if (TTP->wasDeclaredWithTypename()) |
| 618 | Out << "typename "; |
| 619 | else |
| 620 | Out << "class "; |
| 621 | |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 622 | if (TTP->isParameterPack()) |
| 623 | Out << "... "; |
| 624 | |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 625 | Out << ParamType.getAsString(Policy); |
| 626 | |
| 627 | if (TTP->hasDefaultArgument()) { |
| 628 | Out << " = "; |
| 629 | Out << TTP->getDefaultArgument().getAsString(Policy); |
| 630 | }; |
| 631 | } else if (const NonTypeTemplateParmDecl *NTTP = |
| 632 | dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 633 | Out << NTTP->getType().getAsString(Policy); |
| 634 | |
| 635 | if (IdentifierInfo *Name = NTTP->getIdentifier()) { |
| 636 | Out << ' '; |
| 637 | Out << Name->getName(); |
| 638 | } |
| 639 | |
| 640 | if (NTTP->hasDefaultArgument()) { |
| 641 | Out << " = "; |
| 642 | NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy, |
| 643 | Indentation); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | Out << "> "; |
| 649 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 650 | Visit(D->getTemplatedDecl()); |
| 651 | } |
| 652 | |
| 653 | //---------------------------------------------------------------------------- |
| 654 | // Objective-C declarations |
| 655 | //---------------------------------------------------------------------------- |
| 656 | |
| 657 | void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 658 | Out << "@class "; |
| 659 | for (ObjCClassDecl::iterator I = D->begin(), E = D->end(); |
| 660 | I != E; ++I) { |
| 661 | if (I != D->begin()) Out << ", "; |
| 662 | Out << (*I)->getNameAsString(); |
| 663 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { |
| 667 | if (OMD->isInstanceMethod()) |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 668 | Out << "- "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 669 | else |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 670 | Out << "+ "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 671 | if (!OMD->getResultType().isNull()) |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 672 | Out << '(' << OMD->getResultType().getAsString(Policy) << ")"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 673 | |
| 674 | std::string name = OMD->getSelector().getAsString(); |
| 675 | std::string::size_type pos, lastPos = 0; |
| 676 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 677 | E = OMD->param_end(); PI != E; ++PI) { |
| 678 | // FIXME: selector is missing here! |
| 679 | pos = name.find_first_of(":", lastPos); |
| 680 | Out << " " << name.substr(lastPos, pos - lastPos); |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 681 | Out << ":(" << (*PI)->getType().getAsString(Policy) << ")" |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 682 | << (*PI)->getNameAsString(); |
| 683 | lastPos = pos + 1; |
| 684 | } |
| 685 | |
| 686 | if (OMD->param_begin() == OMD->param_end()) |
| 687 | Out << " " << name; |
| 688 | |
| 689 | if (OMD->isVariadic()) |
| 690 | Out << ", ..."; |
| 691 | |
| 692 | if (OMD->getBody()) { |
| 693 | Out << ' '; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 694 | OMD->getBody()->printPretty(Out, Context, 0, Policy); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 695 | Out << '\n'; |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 696 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { |
| 700 | std::string I = OID->getNameAsString(); |
| 701 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
| 702 | |
| 703 | if (SID) |
| 704 | Out << "@implementation " << I << " : " << SID->getNameAsString(); |
| 705 | else |
| 706 | Out << "@implementation " << I; |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 707 | Out << "\n"; |
| 708 | VisitDeclContext(OID, false); |
| 709 | Out << "@end"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
| 713 | std::string I = OID->getNameAsString(); |
| 714 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
| 715 | |
| 716 | if (SID) |
| 717 | Out << "@interface " << I << " : " << SID->getNameAsString(); |
| 718 | else |
| 719 | Out << "@interface " << I; |
| 720 | |
| 721 | // Protocols? |
| 722 | const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); |
| 723 | if (!Protocols.empty()) { |
| 724 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 725 | E = Protocols.end(); I != E; ++I) |
| 726 | Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString(); |
| 727 | } |
| 728 | |
| 729 | if (!Protocols.empty()) |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 730 | Out << "> "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 731 | |
| 732 | if (OID->ivar_size() > 0) { |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 733 | Out << "{\n"; |
| 734 | Indentation += Policy.Indentation; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 735 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 736 | E = OID->ivar_end(); I != E; ++I) { |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 737 | Indent() << (*I)->getType().getAsString(Policy) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 738 | << ' ' << (*I)->getNameAsString() << ";\n"; |
| 739 | } |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 740 | Indentation -= Policy.Indentation; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 741 | Out << "}\n"; |
| 742 | } |
| 743 | |
| 744 | VisitDeclContext(OID, false); |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 745 | Out << "@end"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 746 | // FIXME: implement the rest... |
| 747 | } |
| 748 | |
| 749 | void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 750 | Out << "@protocol "; |
| 751 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 752 | E = D->protocol_end(); |
| 753 | I != E; ++I) { |
| 754 | if (I != D->protocol_begin()) Out << ", "; |
| 755 | Out << (*I)->getNameAsString(); |
| 756 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 760 | Out << "@protocol " << PID->getNameAsString() << '\n'; |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 761 | VisitDeclContext(PID, false); |
| 762 | Out << "@end"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
| 766 | Out << "@implementation " |
| 767 | << PID->getClassInterface()->getNameAsString() |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 768 | << '(' << PID->getNameAsString() << ")\n"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 769 | |
| 770 | VisitDeclContext(PID, false); |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 771 | Out << "@end"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 772 | // FIXME: implement the rest... |
| 773 | } |
| 774 | |
| 775 | void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { |
| 776 | Out << "@interface " |
| 777 | << PID->getClassInterface()->getNameAsString() |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 778 | << '(' << PID->getNameAsString() << ")\n"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 779 | VisitDeclContext(PID, false); |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 780 | Out << "@end"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 781 | |
| 782 | // FIXME: implement the rest... |
| 783 | } |
| 784 | |
| 785 | void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
| 786 | Out << "@compatibility_alias " << AID->getNameAsString() |
| 787 | << ' ' << AID->getClassInterface()->getNameAsString() << ";\n"; |
| 788 | } |
| 789 | |
| 790 | /// PrintObjCPropertyDecl - print a property declaration. |
| 791 | /// |
| 792 | void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { |
| 793 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 794 | Out << "@required\n"; |
| 795 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 796 | Out << "@optional\n"; |
| 797 | |
| 798 | Out << "@property"; |
| 799 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 800 | bool first = true; |
| 801 | Out << " ("; |
| 802 | if (PDecl->getPropertyAttributes() & |
| 803 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 804 | Out << (first ? ' ' : ',') << "readonly"; |
| 805 | first = false; |
| 806 | } |
| 807 | |
| 808 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 809 | Out << (first ? ' ' : ',') << "getter = " |
| 810 | << PDecl->getGetterName().getAsString(); |
| 811 | first = false; |
| 812 | } |
| 813 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 814 | Out << (first ? ' ' : ',') << "setter = " |
| 815 | << PDecl->getSetterName().getAsString(); |
| 816 | first = false; |
| 817 | } |
| 818 | |
| 819 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 820 | Out << (first ? ' ' : ',') << "assign"; |
| 821 | first = false; |
| 822 | } |
| 823 | |
| 824 | if (PDecl->getPropertyAttributes() & |
| 825 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 826 | Out << (first ? ' ' : ',') << "readwrite"; |
| 827 | first = false; |
| 828 | } |
| 829 | |
| 830 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
| 831 | Out << (first ? ' ' : ',') << "retain"; |
| 832 | first = false; |
| 833 | } |
| 834 | |
| 835 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 836 | Out << (first ? ' ' : ',') << "copy"; |
| 837 | first = false; |
| 838 | } |
| 839 | |
| 840 | if (PDecl->getPropertyAttributes() & |
| 841 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
| 842 | Out << (first ? ' ' : ',') << "nonatomic"; |
| 843 | first = false; |
| 844 | } |
| 845 | Out << " )"; |
| 846 | } |
| 847 | Out << ' ' << PDecl->getType().getAsString(Policy) |
| 848 | << ' ' << PDecl->getNameAsString(); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
| 852 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 853 | Out << "@synthesize "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 854 | else |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 855 | Out << "@dynamic "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 856 | Out << PID->getPropertyDecl()->getNameAsString(); |
| 857 | if (PID->getPropertyIvarDecl()) |
| 858 | Out << "=" << PID->getPropertyIvarDecl()->getNameAsString(); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 859 | } |
Anders Carlsson | f53eaa5 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 860 | |
| 861 | void DeclPrinter::VisitUsingDecl(UsingDecl *D) { |
| 862 | Out << "using "; |
| 863 | D->getTargetNestedNameDecl()->print(Out, Policy); |
| 864 | Out << D->getTargetDecl()->getNameAsString(); |
| 865 | } |
| 866 | |
| 867 | void DeclPrinter::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) { |
| 868 | Out << "using "; |
| 869 | D->getTargetNestedNameSpecifier()->print(Out, Policy); |
| 870 | Out << D->getTargetName().getAsString(); |
| 871 | } |