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