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