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