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