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