DeclPrinter.cpp revision 40902d817e5a73850045d8a0c9795bc5047ee000
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/Attr.h" 16#include "clang/AST/Decl.h" 17#include "clang/AST/DeclCXX.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/DeclVisitor.h" 20#include "clang/AST/Expr.h" 21#include "clang/AST/ExprCXX.h" 22#include "clang/AST/PrettyPrinter.h" 23#include "clang/Basic/Module.h" 24#include "llvm/Support/raw_ostream.h" 25using namespace clang; 26 27namespace { 28 class DeclPrinter : public DeclVisitor<DeclPrinter> { 29 raw_ostream &Out; 30 PrintingPolicy Policy; 31 unsigned Indentation; 32 bool PrintInstantiation; 33 34 raw_ostream& Indent() { return Indent(Indentation); } 35 raw_ostream& Indent(unsigned Indentation); 36 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls); 37 38 void Print(AccessSpecifier AS); 39 40 public: 41 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy, 42 unsigned Indentation = 0, bool PrintInstantiation = false) 43 : Out(Out), Policy(Policy), Indentation(Indentation), 44 PrintInstantiation(PrintInstantiation) { } 45 46 void VisitDeclContext(DeclContext *DC, bool Indent = true); 47 48 void VisitTranslationUnitDecl(TranslationUnitDecl *D); 49 void VisitTypedefDecl(TypedefDecl *D); 50 void VisitTypeAliasDecl(TypeAliasDecl *D); 51 void VisitEnumDecl(EnumDecl *D); 52 void VisitRecordDecl(RecordDecl *D); 53 void VisitEnumConstantDecl(EnumConstantDecl *D); 54 void VisitFunctionDecl(FunctionDecl *D); 55 void VisitFriendDecl(FriendDecl *D); 56 void VisitFieldDecl(FieldDecl *D); 57 void VisitVarDecl(VarDecl *D); 58 void VisitLabelDecl(LabelDecl *D); 59 void VisitParmVarDecl(ParmVarDecl *D); 60 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); 61 void VisitImportDecl(ImportDecl *D); 62 void VisitStaticAssertDecl(StaticAssertDecl *D); 63 void VisitNamespaceDecl(NamespaceDecl *D); 64 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 65 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 66 void VisitCXXRecordDecl(CXXRecordDecl *D); 67 void VisitLinkageSpecDecl(LinkageSpecDecl *D); 68 void VisitTemplateDecl(const TemplateDecl *D); 69 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 70 void VisitClassTemplateDecl(ClassTemplateDecl *D); 71 void VisitObjCMethodDecl(ObjCMethodDecl *D); 72 void VisitObjCImplementationDecl(ObjCImplementationDecl *D); 73 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 74 void VisitObjCProtocolDecl(ObjCProtocolDecl *D); 75 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 76 void VisitObjCCategoryDecl(ObjCCategoryDecl *D); 77 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); 78 void VisitObjCPropertyDecl(ObjCPropertyDecl *D); 79 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 80 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 81 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 82 void VisitUsingDecl(UsingDecl *D); 83 void VisitUsingShadowDecl(UsingShadowDecl *D); 84 85 void PrintTemplateParameters(const TemplateParameterList *Params, 86 const TemplateArgumentList *Args); 87 void prettyPrintAttributes(Decl *D); 88 }; 89} 90 91void Decl::print(raw_ostream &Out, unsigned Indentation, 92 bool PrintInstantiation) const { 93 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation); 94} 95 96void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy, 97 unsigned Indentation, bool PrintInstantiation) const { 98 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation); 99 Printer.Visit(const_cast<Decl*>(this)); 100} 101 102static QualType GetBaseType(QualType T) { 103 // FIXME: This should be on the Type class! 104 QualType BaseType = T; 105 while (!BaseType->isSpecifierType()) { 106 if (isa<TypedefType>(BaseType)) 107 break; 108 else if (const PointerType* PTy = BaseType->getAs<PointerType>()) 109 BaseType = PTy->getPointeeType(); 110 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>()) 111 BaseType = BPy->getPointeeType(); 112 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) 113 BaseType = ATy->getElementType(); 114 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>()) 115 BaseType = FTy->getResultType(); 116 else if (const VectorType *VTy = BaseType->getAs<VectorType>()) 117 BaseType = VTy->getElementType(); 118 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>()) 119 BaseType = RTy->getPointeeType(); 120 else 121 llvm_unreachable("Unknown declarator!"); 122 } 123 return BaseType; 124} 125 126static QualType getDeclType(Decl* D) { 127 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D)) 128 return TDD->getUnderlyingType(); 129 if (ValueDecl* VD = dyn_cast<ValueDecl>(D)) 130 return VD->getType(); 131 return QualType(); 132} 133 134void Decl::printGroup(Decl** Begin, unsigned NumDecls, 135 raw_ostream &Out, const PrintingPolicy &Policy, 136 unsigned Indentation) { 137 if (NumDecls == 1) { 138 (*Begin)->print(Out, Policy, Indentation); 139 return; 140 } 141 142 Decl** End = Begin + NumDecls; 143 TagDecl* TD = dyn_cast<TagDecl>(*Begin); 144 if (TD) 145 ++Begin; 146 147 PrintingPolicy SubPolicy(Policy); 148 if (TD && TD->isCompleteDefinition()) { 149 TD->print(Out, Policy, Indentation); 150 Out << " "; 151 SubPolicy.SuppressTag = true; 152 } 153 154 bool isFirst = true; 155 for ( ; Begin != End; ++Begin) { 156 if (isFirst) { 157 SubPolicy.SuppressSpecifiers = false; 158 isFirst = false; 159 } else { 160 if (!isFirst) Out << ", "; 161 SubPolicy.SuppressSpecifiers = true; 162 } 163 164 (*Begin)->print(Out, SubPolicy, Indentation); 165 } 166} 167 168void DeclContext::dumpDeclContext() const { 169 // Get the translation unit 170 const DeclContext *DC = this; 171 while (!DC->isTranslationUnit()) 172 DC = DC->getParent(); 173 174 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext(); 175 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0); 176 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false); 177} 178 179void Decl::dump() const { 180 dump(llvm::errs()); 181} 182 183void Decl::dump(raw_ostream &Out) const { 184 PrintingPolicy Policy = getASTContext().getPrintingPolicy(); 185 Policy.DumpSourceManager = &getASTContext().getSourceManager(); 186 print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true); 187} 188 189raw_ostream& DeclPrinter::Indent(unsigned Indentation) { 190 for (unsigned i = 0; i != Indentation; ++i) 191 Out << " "; 192 return Out; 193} 194 195void DeclPrinter::prettyPrintAttributes(Decl *D) { 196 if (Policy.PolishForDeclaration) 197 return; 198 199 if (D->hasAttrs()) { 200 AttrVec &Attrs = D->getAttrs(); 201 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) { 202 Attr *A = *i; 203 A->printPretty(Out, Policy); 204 } 205 } 206} 207 208void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) { 209 this->Indent(); 210 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); 211 Out << ";\n"; 212 Decls.clear(); 213 214} 215 216void DeclPrinter::Print(AccessSpecifier AS) { 217 switch(AS) { 218 case AS_none: llvm_unreachable("No access specifier!"); 219 case AS_public: Out << "public"; break; 220 case AS_protected: Out << "protected"; break; 221 case AS_private: Out << "private"; break; 222 } 223} 224 225//---------------------------------------------------------------------------- 226// Common C declarations 227//---------------------------------------------------------------------------- 228 229void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { 230 if (Policy.TerseOutput) 231 return; 232 233 if (Indent) 234 Indentation += Policy.Indentation; 235 236 SmallVector<Decl*, 2> Decls; 237 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); 238 D != DEnd; ++D) { 239 240 // Don't print ObjCIvarDecls, as they are printed when visiting the 241 // containing ObjCInterfaceDecl. 242 if (isa<ObjCIvarDecl>(*D)) 243 continue; 244 245 if (!Policy.DumpSourceManager) { 246 // Skip over implicit declarations in pretty-printing mode. 247 if (D->isImplicit()) continue; 248 // FIXME: Ugly hack so we don't pretty-print the builtin declaration 249 // of __builtin_va_list or __[u]int128_t. There should be some other way 250 // to check that. 251 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) { 252 if (IdentifierInfo *II = ND->getIdentifier()) { 253 if (II->isStr("__builtin_va_list") || 254 II->isStr("__int128_t") || II->isStr("__uint128_t")) 255 continue; 256 } 257 } 258 } 259 260 // The next bits of code handles stuff like "struct {int x;} a,b"; we're 261 // forced to merge the declarations because there's no other way to 262 // refer to the struct in question. This limited merging is safe without 263 // a bunch of other checks because it only merges declarations directly 264 // referring to the tag, not typedefs. 265 // 266 // Check whether the current declaration should be grouped with a previous 267 // unnamed struct. 268 QualType CurDeclType = getDeclType(*D); 269 if (!Decls.empty() && !CurDeclType.isNull()) { 270 QualType BaseType = GetBaseType(CurDeclType); 271 if (!BaseType.isNull() && isa<TagType>(BaseType) && 272 cast<TagType>(BaseType)->getDecl() == Decls[0]) { 273 Decls.push_back(*D); 274 continue; 275 } 276 } 277 278 // If we have a merged group waiting to be handled, handle it now. 279 if (!Decls.empty()) 280 ProcessDeclGroup(Decls); 281 282 // If the current declaration is an unnamed tag type, save it 283 // so we can merge it with the subsequent declaration(s) using it. 284 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) { 285 Decls.push_back(*D); 286 continue; 287 } 288 289 if (isa<AccessSpecDecl>(*D)) { 290 Indentation -= Policy.Indentation; 291 this->Indent(); 292 Print(D->getAccess()); 293 Out << ":\n"; 294 Indentation += Policy.Indentation; 295 continue; 296 } 297 298 this->Indent(); 299 Visit(*D); 300 301 // FIXME: Need to be able to tell the DeclPrinter when 302 const char *Terminator = 0; 303 if (isa<FunctionDecl>(*D) && 304 cast<FunctionDecl>(*D)->isThisDeclarationADefinition()) 305 Terminator = 0; 306 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody()) 307 Terminator = 0; 308 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) || 309 isa<ObjCImplementationDecl>(*D) || 310 isa<ObjCInterfaceDecl>(*D) || 311 isa<ObjCProtocolDecl>(*D) || 312 isa<ObjCCategoryImplDecl>(*D) || 313 isa<ObjCCategoryDecl>(*D)) 314 Terminator = 0; 315 else if (isa<EnumConstantDecl>(*D)) { 316 DeclContext::decl_iterator Next = D; 317 ++Next; 318 if (Next != DEnd) 319 Terminator = ","; 320 } else 321 Terminator = ";"; 322 323 if (Terminator) 324 Out << Terminator; 325 Out << "\n"; 326 } 327 328 if (!Decls.empty()) 329 ProcessDeclGroup(Decls); 330 331 if (Indent) 332 Indentation -= Policy.Indentation; 333} 334 335void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 336 VisitDeclContext(D, false); 337} 338 339void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { 340 if (!Policy.SuppressSpecifiers) { 341 Out << "typedef "; 342 343 if (D->isModulePrivate()) 344 Out << "__module_private__ "; 345 } 346 D->getUnderlyingType().print(Out, Policy, D->getName()); 347 prettyPrintAttributes(D); 348} 349 350void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) { 351 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy); 352} 353 354void DeclPrinter::VisitEnumDecl(EnumDecl *D) { 355 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 356 Out << "__module_private__ "; 357 Out << "enum "; 358 if (D->isScoped()) { 359 if (D->isScopedUsingClassTag()) 360 Out << "class "; 361 else 362 Out << "struct "; 363 } 364 Out << *D; 365 366 if (D->isFixed()) 367 Out << " : " << D->getIntegerType().stream(Policy); 368 369 if (D->isCompleteDefinition()) { 370 Out << " {\n"; 371 VisitDeclContext(D); 372 Indent() << "}"; 373 } 374 prettyPrintAttributes(D); 375} 376 377void DeclPrinter::VisitRecordDecl(RecordDecl *D) { 378 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 379 Out << "__module_private__ "; 380 Out << D->getKindName(); 381 if (D->getIdentifier()) 382 Out << ' ' << *D; 383 384 if (D->isCompleteDefinition()) { 385 Out << " {\n"; 386 VisitDeclContext(D); 387 Indent() << "}"; 388 } 389} 390 391void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { 392 Out << *D; 393 if (Expr *Init = D->getInitExpr()) { 394 Out << " = "; 395 Init->printPretty(Out, 0, Policy, Indentation); 396 } 397} 398 399void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { 400 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D); 401 if (!Policy.SuppressSpecifiers) { 402 switch (D->getStorageClassAsWritten()) { 403 case SC_None: break; 404 case SC_Extern: Out << "extern "; break; 405 case SC_Static: Out << "static "; break; 406 case SC_PrivateExtern: Out << "__private_extern__ "; break; 407 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal: 408 llvm_unreachable("invalid for functions"); 409 } 410 411 if (D->isInlineSpecified()) Out << "inline "; 412 if (D->isVirtualAsWritten()) Out << "virtual "; 413 if (D->isModulePrivate()) Out << "__module_private__ "; 414 if (CDecl && CDecl->isExplicitSpecified()) 415 Out << "explicit "; 416 } 417 418 PrintingPolicy SubPolicy(Policy); 419 SubPolicy.SuppressSpecifiers = false; 420 std::string Proto = D->getNameInfo().getAsString(); 421 422 QualType Ty = D->getType(); 423 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 424 Proto = '(' + Proto + ')'; 425 Ty = PT->getInnerType(); 426 } 427 428 if (isa<FunctionType>(Ty)) { 429 const FunctionType *AFT = Ty->getAs<FunctionType>(); 430 const FunctionProtoType *FT = 0; 431 if (D->hasWrittenPrototype()) 432 FT = dyn_cast<FunctionProtoType>(AFT); 433 434 Proto += "("; 435 if (FT) { 436 llvm::raw_string_ostream POut(Proto); 437 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation); 438 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 439 if (i) POut << ", "; 440 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 441 } 442 443 if (FT->isVariadic()) { 444 if (D->getNumParams()) POut << ", "; 445 POut << "..."; 446 } 447 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { 448 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 449 if (i) 450 Proto += ", "; 451 Proto += D->getParamDecl(i)->getNameAsString(); 452 } 453 } 454 455 Proto += ")"; 456 457 if (FT) { 458 if (FT->isConst()) 459 Proto += " const"; 460 if (FT->isVolatile()) 461 Proto += " volatile"; 462 if (FT->isRestrict()) 463 Proto += " restrict"; 464 } 465 466 if (FT && FT->hasDynamicExceptionSpec()) { 467 Proto += " throw("; 468 if (FT->getExceptionSpecType() == EST_MSAny) 469 Proto += "..."; 470 else 471 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { 472 if (I) 473 Proto += ", "; 474 475 Proto += FT->getExceptionType(I).getAsString(SubPolicy); 476 } 477 Proto += ")"; 478 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { 479 Proto += " noexcept"; 480 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) { 481 Proto += "("; 482 llvm::raw_string_ostream EOut(Proto); 483 FT->getNoexceptExpr()->printPretty(EOut, 0, SubPolicy, 484 Indentation); 485 EOut.flush(); 486 Proto += EOut.str(); 487 Proto += ")"; 488 } 489 } 490 491 if (CDecl) { 492 bool HasInitializerList = false; 493 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(), 494 E = CDecl->init_end(); 495 B != E; ++B) { 496 CXXCtorInitializer * BMInitializer = (*B); 497 if (BMInitializer->isInClassMemberInitializer()) 498 continue; 499 500 if (!HasInitializerList) { 501 Proto += " : "; 502 Out << Proto; 503 Proto.clear(); 504 HasInitializerList = true; 505 } else 506 Out << ", "; 507 508 if (BMInitializer->isAnyMemberInitializer()) { 509 FieldDecl *FD = BMInitializer->getAnyMember(); 510 Out << *FD; 511 } else { 512 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); 513 } 514 515 Out << "("; 516 if (!BMInitializer->getInit()) { 517 // Nothing to print 518 } else { 519 Expr *Init = BMInitializer->getInit(); 520 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) 521 Init = Tmp->getSubExpr(); 522 523 Init = Init->IgnoreParens(); 524 525 Expr *SimpleInit = 0; 526 Expr **Args = 0; 527 unsigned NumArgs = 0; 528 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { 529 Args = ParenList->getExprs(); 530 NumArgs = ParenList->getNumExprs(); 531 } else if (CXXConstructExpr *Construct 532 = dyn_cast<CXXConstructExpr>(Init)) { 533 Args = Construct->getArgs(); 534 NumArgs = Construct->getNumArgs(); 535 } else 536 SimpleInit = Init; 537 538 if (SimpleInit) 539 SimpleInit->printPretty(Out, 0, Policy, Indentation); 540 else { 541 for (unsigned I = 0; I != NumArgs; ++I) { 542 if (isa<CXXDefaultArgExpr>(Args[I])) 543 break; 544 545 if (I) 546 Out << ", "; 547 Args[I]->printPretty(Out, 0, Policy, Indentation); 548 } 549 } 550 } 551 Out << ")"; 552 } 553 if (!Proto.empty()) 554 Out << Proto; 555 } 556 else 557 AFT->getResultType().print(Out, Policy, Proto); 558 } else { 559 Ty.print(Out, Policy, Proto); 560 } 561 562 prettyPrintAttributes(D); 563 564 if (D->isPure()) 565 Out << " = 0"; 566 else if (D->isDeletedAsWritten()) 567 Out << " = delete"; 568 else if (D->isExplicitlyDefaulted()) 569 Out << " = default"; 570 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) { 571 if (!D->hasPrototype() && D->getNumParams()) { 572 // This is a K&R function definition, so we need to print the 573 // parameters. 574 Out << '\n'; 575 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation); 576 Indentation += Policy.Indentation; 577 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 578 Indent(); 579 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 580 Out << ";\n"; 581 } 582 Indentation -= Policy.Indentation; 583 } else 584 Out << ' '; 585 586 D->getBody()->printPretty(Out, 0, SubPolicy, Indentation); 587 Out << '\n'; 588 } 589} 590 591void DeclPrinter::VisitFriendDecl(FriendDecl *D) { 592 if (TypeSourceInfo *TSI = D->getFriendType()) { 593 if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl()) { 594 Out << "friend "; 595 VisitCXXRecordDecl(FriendD); 596 } 597 } 598 else if (FunctionDecl *FD = 599 dyn_cast<FunctionDecl>(D->getFriendDecl())) { 600 Out << "friend "; 601 VisitFunctionDecl(FD); 602 } 603 else if (FunctionTemplateDecl *FTD = 604 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) { 605 Out << "friend "; 606 VisitFunctionTemplateDecl(FTD); 607 } 608 else if (ClassTemplateDecl *CTD = 609 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) { 610 Out << "friend "; 611 VisitClassTemplateDecl(CTD); 612 } 613} 614 615void DeclPrinter::VisitFieldDecl(FieldDecl *D) { 616 if (!Policy.SuppressSpecifiers && D->isMutable()) 617 Out << "mutable "; 618 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 619 Out << "__module_private__ "; 620 621 Out << D->getType().stream(Policy, D->getName()); 622 623 if (D->isBitField()) { 624 Out << " : "; 625 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation); 626 } 627 628 Expr *Init = D->getInClassInitializer(); 629 if (!Policy.SuppressInitializers && Init) { 630 if (D->getInClassInitStyle() == ICIS_ListInit) 631 Out << " "; 632 else 633 Out << " = "; 634 Init->printPretty(Out, 0, Policy, Indentation); 635 } 636 prettyPrintAttributes(D); 637} 638 639void DeclPrinter::VisitLabelDecl(LabelDecl *D) { 640 Out << *D << ":"; 641} 642 643 644void DeclPrinter::VisitVarDecl(VarDecl *D) { 645 StorageClass SCAsWritten = D->getStorageClassAsWritten(); 646 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None) 647 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " "; 648 649 if (!Policy.SuppressSpecifiers && D->isThreadSpecified()) 650 Out << "__thread "; 651 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 652 Out << "__module_private__ "; 653 654 QualType T = D->getType(); 655 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) 656 T = Parm->getOriginalType(); 657 T.print(Out, Policy, D->getName()); 658 Expr *Init = D->getInit(); 659 if (!Policy.SuppressInitializers && Init) { 660 bool ImplicitInit = false; 661 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) 662 ImplicitInit = D->getInitStyle() == VarDecl::CallInit && 663 Construct->getNumArgs() == 0 && !Construct->isListInitialization(); 664 if (!ImplicitInit) { 665 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 666 Out << "("; 667 else if (D->getInitStyle() == VarDecl::CInit) { 668 Out << " = "; 669 } 670 Init->printPretty(Out, 0, Policy, Indentation); 671 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 672 Out << ")"; 673 } 674 } 675 prettyPrintAttributes(D); 676} 677 678void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { 679 VisitVarDecl(D); 680} 681 682void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 683 Out << "__asm ("; 684 D->getAsmString()->printPretty(Out, 0, Policy, Indentation); 685 Out << ")"; 686} 687 688void DeclPrinter::VisitImportDecl(ImportDecl *D) { 689 Out << "@import " << D->getImportedModule()->getFullModuleName() 690 << ";\n"; 691} 692 693void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { 694 Out << "static_assert("; 695 D->getAssertExpr()->printPretty(Out, 0, Policy, Indentation); 696 Out << ", "; 697 D->getMessage()->printPretty(Out, 0, Policy, Indentation); 698 Out << ")"; 699} 700 701//---------------------------------------------------------------------------- 702// C++ declarations 703//---------------------------------------------------------------------------- 704void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { 705 if (D->isInline()) 706 Out << "inline "; 707 Out << "namespace " << *D << " {\n"; 708 VisitDeclContext(D); 709 Indent() << "}"; 710} 711 712void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 713 Out << "using namespace "; 714 if (D->getQualifier()) 715 D->getQualifier()->print(Out, Policy); 716 Out << *D->getNominatedNamespaceAsWritten(); 717} 718 719void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 720 Out << "namespace " << *D << " = "; 721 if (D->getQualifier()) 722 D->getQualifier()->print(Out, Policy); 723 Out << *D->getAliasedNamespace(); 724} 725 726void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { 727 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 728 Out << "__module_private__ "; 729 Out << D->getKindName(); 730 if (D->getIdentifier()) 731 Out << ' ' << *D; 732 733 if (D->isCompleteDefinition()) { 734 // Print the base classes 735 if (D->getNumBases()) { 736 Out << " : "; 737 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), 738 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { 739 if (Base != D->bases_begin()) 740 Out << ", "; 741 742 if (Base->isVirtual()) 743 Out << "virtual "; 744 745 AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); 746 if (AS != AS_none) 747 Print(AS); 748 Out << " " << Base->getType().getAsString(Policy); 749 750 if (Base->isPackExpansion()) 751 Out << "..."; 752 } 753 } 754 755 // Print the class definition 756 // FIXME: Doesn't print access specifiers, e.g., "public:" 757 Out << " {\n"; 758 VisitDeclContext(D); 759 Indent() << "}"; 760 } 761} 762 763void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 764 const char *l; 765 if (D->getLanguage() == LinkageSpecDecl::lang_c) 766 l = "C"; 767 else { 768 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && 769 "unknown language in linkage specification"); 770 l = "C++"; 771 } 772 773 Out << "extern \"" << l << "\" "; 774 if (D->hasBraces()) { 775 Out << "{\n"; 776 VisitDeclContext(D); 777 Indent() << "}"; 778 } else 779 Visit(*D->decls_begin()); 780} 781 782void DeclPrinter::PrintTemplateParameters( 783 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) { 784 assert(Params); 785 assert(!Args || Params->size() == Args->size()); 786 787 Out << "template <"; 788 789 for (unsigned i = 0, e = Params->size(); i != e; ++i) { 790 if (i != 0) 791 Out << ", "; 792 793 const Decl *Param = Params->getParam(i); 794 if (const TemplateTypeParmDecl *TTP = 795 dyn_cast<TemplateTypeParmDecl>(Param)) { 796 797 if (TTP->wasDeclaredWithTypename()) 798 Out << "typename "; 799 else 800 Out << "class "; 801 802 if (TTP->isParameterPack()) 803 Out << "... "; 804 805 Out << *TTP; 806 807 if (Args) { 808 Out << " = "; 809 Args->get(i).print(Policy, Out); 810 } else if (TTP->hasDefaultArgument()) { 811 Out << " = "; 812 Out << TTP->getDefaultArgument().getAsString(Policy); 813 }; 814 } else if (const NonTypeTemplateParmDecl *NTTP = 815 dyn_cast<NonTypeTemplateParmDecl>(Param)) { 816 Out << NTTP->getType().getAsString(Policy); 817 818 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType())) 819 Out << "..."; 820 821 if (IdentifierInfo *Name = NTTP->getIdentifier()) { 822 Out << ' '; 823 Out << Name->getName(); 824 } 825 826 if (Args) { 827 Out << " = "; 828 Args->get(i).print(Policy, Out); 829 } else if (NTTP->hasDefaultArgument()) { 830 Out << " = "; 831 NTTP->getDefaultArgument()->printPretty(Out, 0, Policy, Indentation); 832 } 833 } else if (const TemplateTemplateParmDecl *TTPD = 834 dyn_cast<TemplateTemplateParmDecl>(Param)) { 835 VisitTemplateDecl(TTPD); 836 // FIXME: print the default argument, if present. 837 } 838 } 839 840 Out << "> "; 841} 842 843void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 844 PrintTemplateParameters(D->getTemplateParameters()); 845 846 if (const TemplateTemplateParmDecl *TTP = 847 dyn_cast<TemplateTemplateParmDecl>(D)) { 848 Out << "class "; 849 if (TTP->isParameterPack()) 850 Out << "..."; 851 Out << D->getName(); 852 } else { 853 Visit(D->getTemplatedDecl()); 854 } 855} 856 857void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 858 if (PrintInstantiation) { 859 TemplateParameterList *Params = D->getTemplateParameters(); 860 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); 861 I != E; ++I) { 862 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs()); 863 Visit(*I); 864 } 865 } 866 867 return VisitRedeclarableTemplateDecl(D); 868} 869 870void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 871 if (PrintInstantiation) { 872 TemplateParameterList *Params = D->getTemplateParameters(); 873 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); 874 I != E; ++I) { 875 PrintTemplateParameters(Params, &(*I)->getTemplateArgs()); 876 Visit(*I); 877 Out << '\n'; 878 } 879 } 880 881 return VisitRedeclarableTemplateDecl(D); 882} 883 884//---------------------------------------------------------------------------- 885// Objective-C declarations 886//---------------------------------------------------------------------------- 887 888void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { 889 if (OMD->isInstanceMethod()) 890 Out << "- "; 891 else 892 Out << "+ "; 893 if (!OMD->getResultType().isNull()) 894 Out << '(' << OMD->getResultType().getAsString(Policy) << ")"; 895 896 std::string name = OMD->getSelector().getAsString(); 897 std::string::size_type pos, lastPos = 0; 898 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), 899 E = OMD->param_end(); PI != E; ++PI) { 900 // FIXME: selector is missing here! 901 pos = name.find_first_of(':', lastPos); 902 Out << " " << name.substr(lastPos, pos - lastPos); 903 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI; 904 lastPos = pos + 1; 905 } 906 907 if (OMD->param_begin() == OMD->param_end()) 908 Out << " " << name; 909 910 if (OMD->isVariadic()) 911 Out << ", ..."; 912 913 if (OMD->getBody() && !Policy.TerseOutput) { 914 Out << ' '; 915 OMD->getBody()->printPretty(Out, 0, Policy); 916 Out << '\n'; 917 } 918 else if (Policy.PolishForDeclaration) 919 Out << ';'; 920} 921 922void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { 923 std::string I = OID->getNameAsString(); 924 ObjCInterfaceDecl *SID = OID->getSuperClass(); 925 926 if (SID) 927 Out << "@implementation " << I << " : " << *SID; 928 else 929 Out << "@implementation " << I; 930 931 if (OID->ivar_size() > 0) { 932 Out << "{\n"; 933 Indentation += Policy.Indentation; 934 for (ObjCImplementationDecl::ivar_iterator I = OID->ivar_begin(), 935 E = OID->ivar_end(); I != E; ++I) { 936 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n"; 937 } 938 Indentation -= Policy.Indentation; 939 Out << "}\n"; 940 } 941 VisitDeclContext(OID, false); 942 Out << "@end"; 943} 944 945void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { 946 std::string I = OID->getNameAsString(); 947 ObjCInterfaceDecl *SID = OID->getSuperClass(); 948 949 if (!OID->isThisDeclarationADefinition()) { 950 Out << "@class " << I << ";"; 951 return; 952 } 953 bool eolnOut = false; 954 if (SID) 955 Out << "@interface " << I << " : " << *SID; 956 else 957 Out << "@interface " << I; 958 959 // Protocols? 960 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); 961 if (!Protocols.empty()) { 962 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 963 E = Protocols.end(); I != E; ++I) 964 Out << (I == Protocols.begin() ? '<' : ',') << **I; 965 Out << "> "; 966 } 967 968 if (OID->ivar_size() > 0) { 969 Out << "{\n"; 970 eolnOut = true; 971 Indentation += Policy.Indentation; 972 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), 973 E = OID->ivar_end(); I != E; ++I) { 974 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n"; 975 } 976 Indentation -= Policy.Indentation; 977 Out << "}\n"; 978 } 979 else if (SID) { 980 Out << "\n"; 981 eolnOut = true; 982 } 983 984 VisitDeclContext(OID, false); 985 if (!eolnOut) 986 Out << ' '; 987 Out << "@end"; 988 // FIXME: implement the rest... 989} 990 991void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 992 if (!PID->isThisDeclarationADefinition()) { 993 Out << "@protocol " << *PID << ";\n"; 994 return; 995 } 996 // Protocols? 997 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols(); 998 if (!Protocols.empty()) { 999 Out << "@protocol " << *PID; 1000 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1001 E = Protocols.end(); I != E; ++I) 1002 Out << (I == Protocols.begin() ? '<' : ',') << **I; 1003 Out << ">\n"; 1004 } else 1005 Out << "@protocol " << *PID << '\n'; 1006 VisitDeclContext(PID, false); 1007 Out << "@end"; 1008} 1009 1010void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { 1011 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; 1012 1013 VisitDeclContext(PID, false); 1014 Out << "@end"; 1015 // FIXME: implement the rest... 1016} 1017 1018void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { 1019 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n"; 1020 if (PID->ivar_size() > 0) { 1021 Out << "{\n"; 1022 Indentation += Policy.Indentation; 1023 for (ObjCCategoryDecl::ivar_iterator I = PID->ivar_begin(), 1024 E = PID->ivar_end(); I != E; ++I) { 1025 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n"; 1026 } 1027 Indentation -= Policy.Indentation; 1028 Out << "}\n"; 1029 } 1030 1031 VisitDeclContext(PID, false); 1032 Out << "@end"; 1033 1034 // FIXME: implement the rest... 1035} 1036 1037void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { 1038 Out << "@compatibility_alias " << *AID 1039 << ' ' << *AID->getClassInterface() << ";\n"; 1040} 1041 1042/// PrintObjCPropertyDecl - print a property declaration. 1043/// 1044void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { 1045 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) 1046 Out << "@required\n"; 1047 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) 1048 Out << "@optional\n"; 1049 1050 Out << "@property"; 1051 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { 1052 bool first = true; 1053 Out << " ("; 1054 if (PDecl->getPropertyAttributes() & 1055 ObjCPropertyDecl::OBJC_PR_readonly) { 1056 Out << (first ? ' ' : ',') << "readonly"; 1057 first = false; 1058 } 1059 1060 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 1061 Out << (first ? ' ' : ',') << "getter = " 1062 << PDecl->getGetterName().getAsString(); 1063 first = false; 1064 } 1065 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 1066 Out << (first ? ' ' : ',') << "setter = " 1067 << PDecl->getSetterName().getAsString(); 1068 first = false; 1069 } 1070 1071 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { 1072 Out << (first ? ' ' : ',') << "assign"; 1073 first = false; 1074 } 1075 1076 if (PDecl->getPropertyAttributes() & 1077 ObjCPropertyDecl::OBJC_PR_readwrite) { 1078 Out << (first ? ' ' : ',') << "readwrite"; 1079 first = false; 1080 } 1081 1082 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { 1083 Out << (first ? ' ' : ',') << "retain"; 1084 first = false; 1085 } 1086 1087 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { 1088 Out << (first ? ' ' : ',') << "strong"; 1089 first = false; 1090 } 1091 1092 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { 1093 Out << (first ? ' ' : ',') << "copy"; 1094 first = false; 1095 } 1096 1097 if (PDecl->getPropertyAttributes() & 1098 ObjCPropertyDecl::OBJC_PR_nonatomic) { 1099 Out << (first ? ' ' : ',') << "nonatomic"; 1100 first = false; 1101 } 1102 if (PDecl->getPropertyAttributes() & 1103 ObjCPropertyDecl::OBJC_PR_atomic) { 1104 Out << (first ? ' ' : ',') << "atomic"; 1105 first = false; 1106 } 1107 1108 (void) first; // Silence dead store warning due to idiomatic code. 1109 Out << " )"; 1110 } 1111 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl; 1112 if (Policy.PolishForDeclaration) 1113 Out << ';'; 1114} 1115 1116void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { 1117 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 1118 Out << "@synthesize "; 1119 else 1120 Out << "@dynamic "; 1121 Out << *PID->getPropertyDecl(); 1122 if (PID->getPropertyIvarDecl()) 1123 Out << '=' << *PID->getPropertyIvarDecl(); 1124} 1125 1126void DeclPrinter::VisitUsingDecl(UsingDecl *D) { 1127 Out << "using "; 1128 D->getQualifier()->print(Out, Policy); 1129 Out << *D; 1130} 1131 1132void 1133DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 1134 Out << "using typename "; 1135 D->getQualifier()->print(Out, Policy); 1136 Out << D->getDeclName(); 1137} 1138 1139void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1140 Out << "using "; 1141 D->getQualifier()->print(Out, Policy); 1142 Out << D->getName(); 1143} 1144 1145void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { 1146 // ignore 1147} 1148