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