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