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" 25 using namespace clang; 26 27 namespace { 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 /// Print an Objective-C method type in parentheses. 41 /// 42 /// \param Quals The Objective-C declaration qualifiers. 43 /// \param T The type to print. 44 void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals, 45 QualType T); 46 47 void PrintObjCTypeParams(ObjCTypeParamList *Params); 48 49 public: 50 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy, 51 unsigned Indentation = 0, bool PrintInstantiation = false) 52 : Out(Out), Policy(Policy), Indentation(Indentation), 53 PrintInstantiation(PrintInstantiation) { } 54 55 void VisitDeclContext(DeclContext *DC, bool Indent = true); 56 57 void VisitTranslationUnitDecl(TranslationUnitDecl *D); 58 void VisitTypedefDecl(TypedefDecl *D); 59 void VisitTypeAliasDecl(TypeAliasDecl *D); 60 void VisitEnumDecl(EnumDecl *D); 61 void VisitRecordDecl(RecordDecl *D); 62 void VisitEnumConstantDecl(EnumConstantDecl *D); 63 void VisitEmptyDecl(EmptyDecl *D); 64 void VisitFunctionDecl(FunctionDecl *D); 65 void VisitFriendDecl(FriendDecl *D); 66 void VisitFieldDecl(FieldDecl *D); 67 void VisitVarDecl(VarDecl *D); 68 void VisitLabelDecl(LabelDecl *D); 69 void VisitParmVarDecl(ParmVarDecl *D); 70 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); 71 void VisitImportDecl(ImportDecl *D); 72 void VisitStaticAssertDecl(StaticAssertDecl *D); 73 void VisitNamespaceDecl(NamespaceDecl *D); 74 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 75 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 76 void VisitCXXRecordDecl(CXXRecordDecl *D); 77 void VisitLinkageSpecDecl(LinkageSpecDecl *D); 78 void VisitTemplateDecl(const TemplateDecl *D); 79 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 80 void VisitClassTemplateDecl(ClassTemplateDecl *D); 81 void VisitClassTemplateSpecializationDecl( 82 ClassTemplateSpecializationDecl *D); 83 void VisitClassTemplatePartialSpecializationDecl( 84 ClassTemplatePartialSpecializationDecl *D); 85 void VisitObjCMethodDecl(ObjCMethodDecl *D); 86 void VisitObjCImplementationDecl(ObjCImplementationDecl *D); 87 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 88 void VisitObjCProtocolDecl(ObjCProtocolDecl *D); 89 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 90 void VisitObjCCategoryDecl(ObjCCategoryDecl *D); 91 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); 92 void VisitObjCPropertyDecl(ObjCPropertyDecl *D); 93 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 94 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 95 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 96 void VisitUsingDecl(UsingDecl *D); 97 void VisitUsingShadowDecl(UsingShadowDecl *D); 98 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D); 99 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D); 100 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D); 101 102 void printTemplateParameters(const TemplateParameterList *Params); 103 void printTemplateArguments(const TemplateArgumentList &Args, 104 const TemplateParameterList *Params = nullptr); 105 void prettyPrintAttributes(Decl *D); 106 void prettyPrintPragmas(Decl *D); 107 void printDeclType(QualType T, StringRef DeclName, bool Pack = false); 108 }; 109 } 110 111 void Decl::print(raw_ostream &Out, unsigned Indentation, 112 bool PrintInstantiation) const { 113 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation); 114 } 115 116 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy, 117 unsigned Indentation, bool PrintInstantiation) const { 118 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation); 119 Printer.Visit(const_cast<Decl*>(this)); 120 } 121 122 static QualType GetBaseType(QualType T) { 123 // FIXME: This should be on the Type class! 124 QualType BaseType = T; 125 while (!BaseType->isSpecifierType()) { 126 if (isa<TypedefType>(BaseType)) 127 break; 128 else if (const PointerType* PTy = BaseType->getAs<PointerType>()) 129 BaseType = PTy->getPointeeType(); 130 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>()) 131 BaseType = BPy->getPointeeType(); 132 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) 133 BaseType = ATy->getElementType(); 134 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>()) 135 BaseType = FTy->getReturnType(); 136 else if (const VectorType *VTy = BaseType->getAs<VectorType>()) 137 BaseType = VTy->getElementType(); 138 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>()) 139 BaseType = RTy->getPointeeType(); 140 else if (const AutoType *ATy = BaseType->getAs<AutoType>()) 141 BaseType = ATy->getDeducedType(); 142 else 143 llvm_unreachable("Unknown declarator!"); 144 } 145 return BaseType; 146 } 147 148 static QualType getDeclType(Decl* D) { 149 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D)) 150 return TDD->getUnderlyingType(); 151 if (ValueDecl* VD = dyn_cast<ValueDecl>(D)) 152 return VD->getType(); 153 return QualType(); 154 } 155 156 void Decl::printGroup(Decl** Begin, unsigned NumDecls, 157 raw_ostream &Out, const PrintingPolicy &Policy, 158 unsigned Indentation) { 159 if (NumDecls == 1) { 160 (*Begin)->print(Out, Policy, Indentation); 161 return; 162 } 163 164 Decl** End = Begin + NumDecls; 165 TagDecl* TD = dyn_cast<TagDecl>(*Begin); 166 if (TD) 167 ++Begin; 168 169 PrintingPolicy SubPolicy(Policy); 170 171 bool isFirst = true; 172 for ( ; Begin != End; ++Begin) { 173 if (isFirst) { 174 if(TD) 175 SubPolicy.IncludeTagDefinition = true; 176 SubPolicy.SuppressSpecifiers = false; 177 isFirst = false; 178 } else { 179 if (!isFirst) Out << ", "; 180 SubPolicy.IncludeTagDefinition = false; 181 SubPolicy.SuppressSpecifiers = true; 182 } 183 184 (*Begin)->print(Out, SubPolicy, Indentation); 185 } 186 } 187 188 LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const { 189 // Get the translation unit 190 const DeclContext *DC = this; 191 while (!DC->isTranslationUnit()) 192 DC = DC->getParent(); 193 194 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext(); 195 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0); 196 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false); 197 } 198 199 raw_ostream& DeclPrinter::Indent(unsigned Indentation) { 200 for (unsigned i = 0; i != Indentation; ++i) 201 Out << " "; 202 return Out; 203 } 204 205 void DeclPrinter::prettyPrintAttributes(Decl *D) { 206 if (Policy.PolishForDeclaration) 207 return; 208 209 if (D->hasAttrs()) { 210 AttrVec &Attrs = D->getAttrs(); 211 for (auto *A : Attrs) { 212 switch (A->getKind()) { 213 #define ATTR(X) 214 #define PRAGMA_SPELLING_ATTR(X) case attr::X: 215 #include "clang/Basic/AttrList.inc" 216 break; 217 default: 218 A->printPretty(Out, Policy); 219 break; 220 } 221 } 222 } 223 } 224 225 void DeclPrinter::prettyPrintPragmas(Decl *D) { 226 if (Policy.PolishForDeclaration) 227 return; 228 229 if (D->hasAttrs()) { 230 AttrVec &Attrs = D->getAttrs(); 231 for (auto *A : Attrs) { 232 switch (A->getKind()) { 233 #define ATTR(X) 234 #define PRAGMA_SPELLING_ATTR(X) case attr::X: 235 #include "clang/Basic/AttrList.inc" 236 A->printPretty(Out, Policy); 237 Indent(); 238 break; 239 default: 240 break; 241 } 242 } 243 } 244 } 245 246 void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) { 247 // Normally, a PackExpansionType is written as T[3]... (for instance, as a 248 // template argument), but if it is the type of a declaration, the ellipsis 249 // is placed before the name being declared. 250 if (auto *PET = T->getAs<PackExpansionType>()) { 251 Pack = true; 252 T = PET->getPattern(); 253 } 254 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation); 255 } 256 257 void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) { 258 this->Indent(); 259 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); 260 Out << ";\n"; 261 Decls.clear(); 262 263 } 264 265 void DeclPrinter::Print(AccessSpecifier AS) { 266 switch(AS) { 267 case AS_none: llvm_unreachable("No access specifier!"); 268 case AS_public: Out << "public"; break; 269 case AS_protected: Out << "protected"; break; 270 case AS_private: Out << "private"; break; 271 } 272 } 273 274 //---------------------------------------------------------------------------- 275 // Common C declarations 276 //---------------------------------------------------------------------------- 277 278 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { 279 if (Policy.TerseOutput) 280 return; 281 282 if (Indent) 283 Indentation += Policy.Indentation; 284 285 SmallVector<Decl*, 2> Decls; 286 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); 287 D != DEnd; ++D) { 288 289 // Don't print ObjCIvarDecls, as they are printed when visiting the 290 // containing ObjCInterfaceDecl. 291 if (isa<ObjCIvarDecl>(*D)) 292 continue; 293 294 // Skip over implicit declarations in pretty-printing mode. 295 if (D->isImplicit()) 296 continue; 297 298 // Don't print implicit specializations, as they are printed when visiting 299 // corresponding templates. 300 if (auto FD = dyn_cast<FunctionDecl>(*D)) 301 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && 302 !isa<ClassTemplateSpecializationDecl>(DC)) 303 continue; 304 305 // The next bits of code handles stuff like "struct {int x;} a,b"; we're 306 // forced to merge the declarations because there's no other way to 307 // refer to the struct in question. This limited merging is safe without 308 // a bunch of other checks because it only merges declarations directly 309 // referring to the tag, not typedefs. 310 // 311 // Check whether the current declaration should be grouped with a previous 312 // unnamed struct. 313 QualType CurDeclType = getDeclType(*D); 314 if (!Decls.empty() && !CurDeclType.isNull()) { 315 QualType BaseType = GetBaseType(CurDeclType); 316 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType)) 317 BaseType = cast<ElaboratedType>(BaseType)->getNamedType(); 318 if (!BaseType.isNull() && isa<TagType>(BaseType) && 319 cast<TagType>(BaseType)->getDecl() == Decls[0]) { 320 Decls.push_back(*D); 321 continue; 322 } 323 } 324 325 // If we have a merged group waiting to be handled, handle it now. 326 if (!Decls.empty()) 327 ProcessDeclGroup(Decls); 328 329 // If the current declaration is an unnamed tag type, save it 330 // so we can merge it with the subsequent declaration(s) using it. 331 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) { 332 Decls.push_back(*D); 333 continue; 334 } 335 336 if (isa<AccessSpecDecl>(*D)) { 337 Indentation -= Policy.Indentation; 338 this->Indent(); 339 Print(D->getAccess()); 340 Out << ":\n"; 341 Indentation += Policy.Indentation; 342 continue; 343 } 344 345 this->Indent(); 346 Visit(*D); 347 348 // FIXME: Need to be able to tell the DeclPrinter when 349 const char *Terminator = nullptr; 350 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D)) 351 Terminator = nullptr; 352 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody()) 353 Terminator = nullptr; 354 else if (auto FD = dyn_cast<FunctionDecl>(*D)) { 355 if (FD->isThisDeclarationADefinition()) 356 Terminator = nullptr; 357 else 358 Terminator = ";"; 359 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) { 360 if (TD->getTemplatedDecl()->isThisDeclarationADefinition()) 361 Terminator = nullptr; 362 else 363 Terminator = ";"; 364 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) || 365 isa<ObjCImplementationDecl>(*D) || 366 isa<ObjCInterfaceDecl>(*D) || 367 isa<ObjCProtocolDecl>(*D) || 368 isa<ObjCCategoryImplDecl>(*D) || 369 isa<ObjCCategoryDecl>(*D)) 370 Terminator = nullptr; 371 else if (isa<EnumConstantDecl>(*D)) { 372 DeclContext::decl_iterator Next = D; 373 ++Next; 374 if (Next != DEnd) 375 Terminator = ","; 376 } else 377 Terminator = ";"; 378 379 if (Terminator) 380 Out << Terminator; 381 if (!Policy.TerseOutput && 382 ((isa<FunctionDecl>(*D) && 383 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) || 384 (isa<FunctionTemplateDecl>(*D) && 385 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody()))) 386 ; // StmtPrinter already added '\n' after CompoundStmt. 387 else 388 Out << "\n"; 389 390 // Declare target attribute is special one, natural spelling for the pragma 391 // assumes "ending" construct so print it here. 392 if (D->hasAttr<OMPDeclareTargetDeclAttr>()) 393 Out << "#pragma omp end declare target\n"; 394 } 395 396 if (!Decls.empty()) 397 ProcessDeclGroup(Decls); 398 399 if (Indent) 400 Indentation -= Policy.Indentation; 401 } 402 403 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 404 VisitDeclContext(D, false); 405 } 406 407 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { 408 if (!Policy.SuppressSpecifiers) { 409 Out << "typedef "; 410 411 if (D->isModulePrivate()) 412 Out << "__module_private__ "; 413 } 414 QualType Ty = D->getTypeSourceInfo()->getType(); 415 Ty.print(Out, Policy, D->getName(), Indentation); 416 prettyPrintAttributes(D); 417 } 418 419 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) { 420 Out << "using " << *D; 421 prettyPrintAttributes(D); 422 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy); 423 } 424 425 void DeclPrinter::VisitEnumDecl(EnumDecl *D) { 426 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 427 Out << "__module_private__ "; 428 Out << "enum "; 429 if (D->isScoped()) { 430 if (D->isScopedUsingClassTag()) 431 Out << "class "; 432 else 433 Out << "struct "; 434 } 435 Out << *D; 436 437 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11) 438 Out << " : " << D->getIntegerType().stream(Policy); 439 440 if (D->isCompleteDefinition()) { 441 Out << " {\n"; 442 VisitDeclContext(D); 443 Indent() << "}"; 444 } 445 prettyPrintAttributes(D); 446 } 447 448 void DeclPrinter::VisitRecordDecl(RecordDecl *D) { 449 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 450 Out << "__module_private__ "; 451 Out << D->getKindName(); 452 453 prettyPrintAttributes(D); 454 455 if (D->getIdentifier()) 456 Out << ' ' << *D; 457 458 if (D->isCompleteDefinition()) { 459 Out << " {\n"; 460 VisitDeclContext(D); 461 Indent() << "}"; 462 } 463 } 464 465 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { 466 Out << *D; 467 prettyPrintAttributes(D); 468 if (Expr *Init = D->getInitExpr()) { 469 Out << " = "; 470 Init->printPretty(Out, nullptr, Policy, Indentation); 471 } 472 } 473 474 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { 475 if (!D->getDescribedFunctionTemplate() && 476 !D->isFunctionTemplateSpecialization()) 477 prettyPrintPragmas(D); 478 479 if (D->isFunctionTemplateSpecialization()) 480 Out << "template<> "; 481 482 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D); 483 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D); 484 if (!Policy.SuppressSpecifiers) { 485 switch (D->getStorageClass()) { 486 case SC_None: break; 487 case SC_Extern: Out << "extern "; break; 488 case SC_Static: Out << "static "; break; 489 case SC_PrivateExtern: Out << "__private_extern__ "; break; 490 case SC_Auto: case SC_Register: 491 llvm_unreachable("invalid for functions"); 492 } 493 494 if (D->isInlineSpecified()) Out << "inline "; 495 if (D->isVirtualAsWritten()) Out << "virtual "; 496 if (D->isModulePrivate()) Out << "__module_private__ "; 497 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr "; 498 if ((CDecl && CDecl->isExplicitSpecified()) || 499 (ConversionDecl && ConversionDecl->isExplicit())) 500 Out << "explicit "; 501 } 502 503 PrintingPolicy SubPolicy(Policy); 504 SubPolicy.SuppressSpecifiers = false; 505 std::string Proto = D->getNameInfo().getAsString(); 506 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) { 507 llvm::raw_string_ostream POut(Proto); 508 DeclPrinter TArgPrinter(POut, SubPolicy, Indentation); 509 TArgPrinter.printTemplateArguments(*TArgs); 510 } 511 512 QualType Ty = D->getType(); 513 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 514 Proto = '(' + Proto + ')'; 515 Ty = PT->getInnerType(); 516 } 517 518 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) { 519 const FunctionProtoType *FT = nullptr; 520 if (D->hasWrittenPrototype()) 521 FT = dyn_cast<FunctionProtoType>(AFT); 522 523 Proto += "("; 524 if (FT) { 525 llvm::raw_string_ostream POut(Proto); 526 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation); 527 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 528 if (i) POut << ", "; 529 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 530 } 531 532 if (FT->isVariadic()) { 533 if (D->getNumParams()) POut << ", "; 534 POut << "..."; 535 } 536 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { 537 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 538 if (i) 539 Proto += ", "; 540 Proto += D->getParamDecl(i)->getNameAsString(); 541 } 542 } 543 544 Proto += ")"; 545 546 if (FT) { 547 if (FT->isConst()) 548 Proto += " const"; 549 if (FT->isVolatile()) 550 Proto += " volatile"; 551 if (FT->isRestrict()) 552 Proto += " restrict"; 553 554 switch (FT->getRefQualifier()) { 555 case RQ_None: 556 break; 557 case RQ_LValue: 558 Proto += " &"; 559 break; 560 case RQ_RValue: 561 Proto += " &&"; 562 break; 563 } 564 } 565 566 if (FT && FT->hasDynamicExceptionSpec()) { 567 Proto += " throw("; 568 if (FT->getExceptionSpecType() == EST_MSAny) 569 Proto += "..."; 570 else 571 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { 572 if (I) 573 Proto += ", "; 574 575 Proto += FT->getExceptionType(I).getAsString(SubPolicy); 576 } 577 Proto += ")"; 578 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { 579 Proto += " noexcept"; 580 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) { 581 Proto += "("; 582 llvm::raw_string_ostream EOut(Proto); 583 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy, 584 Indentation); 585 EOut.flush(); 586 Proto += EOut.str(); 587 Proto += ")"; 588 } 589 } 590 591 if (CDecl) { 592 bool HasInitializerList = false; 593 for (const auto *BMInitializer : CDecl->inits()) { 594 if (BMInitializer->isInClassMemberInitializer()) 595 continue; 596 597 if (!HasInitializerList) { 598 Proto += " : "; 599 Out << Proto; 600 Proto.clear(); 601 HasInitializerList = true; 602 } else 603 Out << ", "; 604 605 if (BMInitializer->isAnyMemberInitializer()) { 606 FieldDecl *FD = BMInitializer->getAnyMember(); 607 Out << *FD; 608 } else { 609 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); 610 } 611 612 Out << "("; 613 if (!BMInitializer->getInit()) { 614 // Nothing to print 615 } else { 616 Expr *Init = BMInitializer->getInit(); 617 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) 618 Init = Tmp->getSubExpr(); 619 620 Init = Init->IgnoreParens(); 621 622 Expr *SimpleInit = nullptr; 623 Expr **Args = nullptr; 624 unsigned NumArgs = 0; 625 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { 626 Args = ParenList->getExprs(); 627 NumArgs = ParenList->getNumExprs(); 628 } else if (CXXConstructExpr *Construct 629 = dyn_cast<CXXConstructExpr>(Init)) { 630 Args = Construct->getArgs(); 631 NumArgs = Construct->getNumArgs(); 632 } else 633 SimpleInit = Init; 634 635 if (SimpleInit) 636 SimpleInit->printPretty(Out, nullptr, Policy, Indentation); 637 else { 638 for (unsigned I = 0; I != NumArgs; ++I) { 639 assert(Args[I] != nullptr && "Expected non-null Expr"); 640 if (isa<CXXDefaultArgExpr>(Args[I])) 641 break; 642 643 if (I) 644 Out << ", "; 645 Args[I]->printPretty(Out, nullptr, Policy, Indentation); 646 } 647 } 648 } 649 Out << ")"; 650 if (BMInitializer->isPackExpansion()) 651 Out << "..."; 652 } 653 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) { 654 if (FT && FT->hasTrailingReturn()) { 655 Out << "auto " << Proto << " -> "; 656 Proto.clear(); 657 } 658 AFT->getReturnType().print(Out, Policy, Proto); 659 Proto.clear(); 660 } 661 Out << Proto; 662 } else { 663 Ty.print(Out, Policy, Proto); 664 } 665 666 prettyPrintAttributes(D); 667 668 if (D->isPure()) 669 Out << " = 0"; 670 else if (D->isDeletedAsWritten()) 671 Out << " = delete"; 672 else if (D->isExplicitlyDefaulted()) 673 Out << " = default"; 674 else if (D->doesThisDeclarationHaveABody()) { 675 if (!Policy.TerseOutput) { 676 if (!D->hasPrototype() && D->getNumParams()) { 677 // This is a K&R function definition, so we need to print the 678 // parameters. 679 Out << '\n'; 680 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation); 681 Indentation += Policy.Indentation; 682 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 683 Indent(); 684 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 685 Out << ";\n"; 686 } 687 Indentation -= Policy.Indentation; 688 } else 689 Out << ' '; 690 691 if (D->getBody()) 692 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation); 693 } else { 694 if (isa<CXXConstructorDecl>(*D)) 695 Out << " {}"; 696 } 697 } 698 } 699 700 void DeclPrinter::VisitFriendDecl(FriendDecl *D) { 701 if (TypeSourceInfo *TSI = D->getFriendType()) { 702 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists(); 703 for (unsigned i = 0; i < NumTPLists; ++i) 704 printTemplateParameters(D->getFriendTypeTemplateParameterList(i)); 705 Out << "friend "; 706 Out << " " << TSI->getType().getAsString(Policy); 707 } 708 else if (FunctionDecl *FD = 709 dyn_cast<FunctionDecl>(D->getFriendDecl())) { 710 Out << "friend "; 711 VisitFunctionDecl(FD); 712 } 713 else if (FunctionTemplateDecl *FTD = 714 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) { 715 Out << "friend "; 716 VisitFunctionTemplateDecl(FTD); 717 } 718 else if (ClassTemplateDecl *CTD = 719 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) { 720 Out << "friend "; 721 VisitRedeclarableTemplateDecl(CTD); 722 } 723 } 724 725 void DeclPrinter::VisitFieldDecl(FieldDecl *D) { 726 // FIXME: add printing of pragma attributes if required. 727 if (!Policy.SuppressSpecifiers && D->isMutable()) 728 Out << "mutable "; 729 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 730 Out << "__module_private__ "; 731 732 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()). 733 stream(Policy, D->getName(), Indentation); 734 735 if (D->isBitField()) { 736 Out << " : "; 737 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation); 738 } 739 740 Expr *Init = D->getInClassInitializer(); 741 if (!Policy.SuppressInitializers && Init) { 742 if (D->getInClassInitStyle() == ICIS_ListInit) 743 Out << " "; 744 else 745 Out << " = "; 746 Init->printPretty(Out, nullptr, Policy, Indentation); 747 } 748 prettyPrintAttributes(D); 749 } 750 751 void DeclPrinter::VisitLabelDecl(LabelDecl *D) { 752 Out << *D << ":"; 753 } 754 755 void DeclPrinter::VisitVarDecl(VarDecl *D) { 756 prettyPrintPragmas(D); 757 758 QualType T = D->getTypeSourceInfo() 759 ? D->getTypeSourceInfo()->getType() 760 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType()); 761 762 if (!Policy.SuppressSpecifiers) { 763 StorageClass SC = D->getStorageClass(); 764 if (SC != SC_None) 765 Out << VarDecl::getStorageClassSpecifierString(SC) << " "; 766 767 switch (D->getTSCSpec()) { 768 case TSCS_unspecified: 769 break; 770 case TSCS___thread: 771 Out << "__thread "; 772 break; 773 case TSCS__Thread_local: 774 Out << "_Thread_local "; 775 break; 776 case TSCS_thread_local: 777 Out << "thread_local "; 778 break; 779 } 780 781 if (D->isModulePrivate()) 782 Out << "__module_private__ "; 783 784 if (D->isConstexpr()) { 785 Out << "constexpr "; 786 T.removeLocalConst(); 787 } 788 } 789 790 printDeclType(T, D->getName()); 791 Expr *Init = D->getInit(); 792 if (!Policy.SuppressInitializers && Init) { 793 bool ImplicitInit = false; 794 if (CXXConstructExpr *Construct = 795 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) { 796 if (D->getInitStyle() == VarDecl::CallInit && 797 !Construct->isListInitialization()) { 798 ImplicitInit = Construct->getNumArgs() == 0 || 799 Construct->getArg(0)->isDefaultArgument(); 800 } 801 } 802 if (!ImplicitInit) { 803 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 804 Out << "("; 805 else if (D->getInitStyle() == VarDecl::CInit) { 806 Out << " = "; 807 } 808 PrintingPolicy SubPolicy(Policy); 809 SubPolicy.SuppressSpecifiers = false; 810 SubPolicy.IncludeTagDefinition = false; 811 Init->printPretty(Out, nullptr, SubPolicy, Indentation); 812 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 813 Out << ")"; 814 } 815 } 816 prettyPrintAttributes(D); 817 } 818 819 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { 820 VisitVarDecl(D); 821 } 822 823 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 824 Out << "__asm ("; 825 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation); 826 Out << ")"; 827 } 828 829 void DeclPrinter::VisitImportDecl(ImportDecl *D) { 830 Out << "@import " << D->getImportedModule()->getFullModuleName() 831 << ";\n"; 832 } 833 834 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { 835 Out << "static_assert("; 836 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation); 837 if (StringLiteral *SL = D->getMessage()) { 838 Out << ", "; 839 SL->printPretty(Out, nullptr, Policy, Indentation); 840 } 841 Out << ")"; 842 } 843 844 //---------------------------------------------------------------------------- 845 // C++ declarations 846 //---------------------------------------------------------------------------- 847 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { 848 if (D->isInline()) 849 Out << "inline "; 850 Out << "namespace " << *D << " {\n"; 851 VisitDeclContext(D); 852 Indent() << "}"; 853 } 854 855 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 856 Out << "using namespace "; 857 if (D->getQualifier()) 858 D->getQualifier()->print(Out, Policy); 859 Out << *D->getNominatedNamespaceAsWritten(); 860 } 861 862 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 863 Out << "namespace " << *D << " = "; 864 if (D->getQualifier()) 865 D->getQualifier()->print(Out, Policy); 866 Out << *D->getAliasedNamespace(); 867 } 868 869 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) { 870 prettyPrintAttributes(D); 871 } 872 873 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { 874 // FIXME: add printing of pragma attributes if required. 875 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 876 Out << "__module_private__ "; 877 Out << D->getKindName(); 878 879 prettyPrintAttributes(D); 880 881 if (D->getIdentifier()) { 882 Out << ' ' << *D; 883 884 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) 885 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters()); 886 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) 887 printTemplateArguments(S->getTemplateArgs()); 888 } 889 890 if (D->isCompleteDefinition()) { 891 // Print the base classes 892 if (D->getNumBases()) { 893 Out << " : "; 894 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), 895 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { 896 if (Base != D->bases_begin()) 897 Out << ", "; 898 899 if (Base->isVirtual()) 900 Out << "virtual "; 901 902 AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); 903 if (AS != AS_none) { 904 Print(AS); 905 Out << " "; 906 } 907 Out << Base->getType().getAsString(Policy); 908 909 if (Base->isPackExpansion()) 910 Out << "..."; 911 } 912 } 913 914 // Print the class definition 915 // FIXME: Doesn't print access specifiers, e.g., "public:" 916 if (Policy.TerseOutput) { 917 Out << " {}"; 918 } else { 919 Out << " {\n"; 920 VisitDeclContext(D); 921 Indent() << "}"; 922 } 923 } 924 } 925 926 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 927 const char *l; 928 if (D->getLanguage() == LinkageSpecDecl::lang_c) 929 l = "C"; 930 else { 931 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && 932 "unknown language in linkage specification"); 933 l = "C++"; 934 } 935 936 Out << "extern \"" << l << "\" "; 937 if (D->hasBraces()) { 938 Out << "{\n"; 939 VisitDeclContext(D); 940 Indent() << "}"; 941 } else 942 Visit(*D->decls_begin()); 943 } 944 945 void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params) { 946 assert(Params); 947 948 Out << "template <"; 949 950 for (unsigned i = 0, e = Params->size(); i != e; ++i) { 951 if (i != 0) 952 Out << ", "; 953 954 const Decl *Param = Params->getParam(i); 955 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { 956 957 if (TTP->wasDeclaredWithTypename()) 958 Out << "typename "; 959 else 960 Out << "class "; 961 962 if (TTP->isParameterPack()) 963 Out << "..."; 964 965 Out << *TTP; 966 967 if (TTP->hasDefaultArgument()) { 968 Out << " = "; 969 Out << TTP->getDefaultArgument().getAsString(Policy); 970 }; 971 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 972 StringRef Name; 973 if (IdentifierInfo *II = NTTP->getIdentifier()) 974 Name = II->getName(); 975 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack()); 976 977 if (NTTP->hasDefaultArgument()) { 978 Out << " = "; 979 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, 980 Indentation); 981 } 982 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) { 983 VisitTemplateDecl(TTPD); 984 // FIXME: print the default argument, if present. 985 } 986 } 987 988 Out << "> "; 989 } 990 991 void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args, 992 const TemplateParameterList *Params) { 993 Out << "<"; 994 for (size_t I = 0, E = Args.size(); I < E; ++I) { 995 const TemplateArgument &A = Args[I]; 996 if (I) 997 Out << ", "; 998 if (Params) { 999 if (A.getKind() == TemplateArgument::Type) 1000 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) { 1001 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex())); 1002 Out << *P; 1003 continue; 1004 } 1005 if (A.getKind() == TemplateArgument::Template) { 1006 if (auto T = A.getAsTemplate().getAsTemplateDecl()) 1007 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) { 1008 auto P = cast<TemplateTemplateParmDecl>( 1009 Params->getParam(TD->getIndex())); 1010 Out << *P; 1011 continue; 1012 } 1013 } 1014 if (A.getKind() == TemplateArgument::Expression) { 1015 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr())) 1016 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { 1017 auto P = cast<NonTypeTemplateParmDecl>( 1018 Params->getParam(N->getIndex())); 1019 Out << *P; 1020 continue; 1021 } 1022 } 1023 } 1024 A.print(Policy, Out); 1025 } 1026 Out << ">"; 1027 } 1028 1029 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 1030 printTemplateParameters(D->getTemplateParameters()); 1031 1032 if (const TemplateTemplateParmDecl *TTP = 1033 dyn_cast<TemplateTemplateParmDecl>(D)) { 1034 Out << "class "; 1035 if (TTP->isParameterPack()) 1036 Out << "..."; 1037 Out << D->getName(); 1038 } else { 1039 Visit(D->getTemplatedDecl()); 1040 } 1041 } 1042 1043 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 1044 prettyPrintPragmas(D->getTemplatedDecl()); 1045 VisitRedeclarableTemplateDecl(D); 1046 1047 if (PrintInstantiation) { 1048 FunctionDecl *PrevDecl = D->getTemplatedDecl(); 1049 const FunctionDecl *Def; 1050 if (PrevDecl->isDefined(Def) && Def != PrevDecl) 1051 return; 1052 for (auto *I : D->specializations()) 1053 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) { 1054 if (!PrevDecl->isThisDeclarationADefinition()) 1055 Out << ";\n"; 1056 Indent(); 1057 prettyPrintPragmas(I); 1058 Visit(I); 1059 } 1060 } 1061 } 1062 1063 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 1064 VisitRedeclarableTemplateDecl(D); 1065 1066 if (PrintInstantiation) { 1067 for (auto *I : D->specializations()) 1068 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) { 1069 if (D->isThisDeclarationADefinition()) 1070 Out << ";"; 1071 Out << "\n"; 1072 Visit(I); 1073 } 1074 } 1075 } 1076 1077 void DeclPrinter::VisitClassTemplateSpecializationDecl( 1078 ClassTemplateSpecializationDecl *D) { 1079 Out << "template<> "; 1080 VisitCXXRecordDecl(D); 1081 } 1082 1083 void DeclPrinter::VisitClassTemplatePartialSpecializationDecl( 1084 ClassTemplatePartialSpecializationDecl *D) { 1085 printTemplateParameters(D->getTemplateParameters()); 1086 VisitCXXRecordDecl(D); 1087 } 1088 1089 //---------------------------------------------------------------------------- 1090 // Objective-C declarations 1091 //---------------------------------------------------------------------------- 1092 1093 void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx, 1094 Decl::ObjCDeclQualifier Quals, 1095 QualType T) { 1096 Out << '('; 1097 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In) 1098 Out << "in "; 1099 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout) 1100 Out << "inout "; 1101 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out) 1102 Out << "out "; 1103 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy) 1104 Out << "bycopy "; 1105 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref) 1106 Out << "byref "; 1107 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway) 1108 Out << "oneway "; 1109 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) { 1110 if (auto nullability = AttributedType::stripOuterNullability(T)) 1111 Out << getNullabilitySpelling(*nullability, true) << ' '; 1112 } 1113 1114 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy); 1115 Out << ')'; 1116 } 1117 1118 void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) { 1119 Out << "<"; 1120 unsigned First = true; 1121 for (auto *Param : *Params) { 1122 if (First) { 1123 First = false; 1124 } else { 1125 Out << ", "; 1126 } 1127 1128 switch (Param->getVariance()) { 1129 case ObjCTypeParamVariance::Invariant: 1130 break; 1131 1132 case ObjCTypeParamVariance::Covariant: 1133 Out << "__covariant "; 1134 break; 1135 1136 case ObjCTypeParamVariance::Contravariant: 1137 Out << "__contravariant "; 1138 break; 1139 } 1140 1141 Out << Param->getDeclName().getAsString(); 1142 1143 if (Param->hasExplicitBound()) { 1144 Out << " : " << Param->getUnderlyingType().getAsString(Policy); 1145 } 1146 } 1147 Out << ">"; 1148 } 1149 1150 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { 1151 if (OMD->isInstanceMethod()) 1152 Out << "- "; 1153 else 1154 Out << "+ "; 1155 if (!OMD->getReturnType().isNull()) { 1156 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(), 1157 OMD->getReturnType()); 1158 } 1159 1160 std::string name = OMD->getSelector().getAsString(); 1161 std::string::size_type pos, lastPos = 0; 1162 for (const auto *PI : OMD->parameters()) { 1163 // FIXME: selector is missing here! 1164 pos = name.find_first_of(':', lastPos); 1165 Out << " " << name.substr(lastPos, pos - lastPos) << ':'; 1166 PrintObjCMethodType(OMD->getASTContext(), 1167 PI->getObjCDeclQualifier(), 1168 PI->getType()); 1169 Out << *PI; 1170 lastPos = pos + 1; 1171 } 1172 1173 if (OMD->param_begin() == OMD->param_end()) 1174 Out << " " << name; 1175 1176 if (OMD->isVariadic()) 1177 Out << ", ..."; 1178 1179 prettyPrintAttributes(OMD); 1180 1181 if (OMD->getBody() && !Policy.TerseOutput) { 1182 Out << ' '; 1183 OMD->getBody()->printPretty(Out, nullptr, Policy); 1184 } 1185 else if (Policy.PolishForDeclaration) 1186 Out << ';'; 1187 } 1188 1189 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { 1190 std::string I = OID->getNameAsString(); 1191 ObjCInterfaceDecl *SID = OID->getSuperClass(); 1192 1193 bool eolnOut = false; 1194 if (SID) 1195 Out << "@implementation " << I << " : " << *SID; 1196 else 1197 Out << "@implementation " << I; 1198 1199 if (OID->ivar_size() > 0) { 1200 Out << "{\n"; 1201 eolnOut = true; 1202 Indentation += Policy.Indentation; 1203 for (const auto *I : OID->ivars()) { 1204 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). 1205 getAsString(Policy) << ' ' << *I << ";\n"; 1206 } 1207 Indentation -= Policy.Indentation; 1208 Out << "}\n"; 1209 } 1210 else if (SID || (OID->decls_begin() != OID->decls_end())) { 1211 Out << "\n"; 1212 eolnOut = true; 1213 } 1214 VisitDeclContext(OID, false); 1215 if (!eolnOut) 1216 Out << "\n"; 1217 Out << "@end"; 1218 } 1219 1220 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { 1221 std::string I = OID->getNameAsString(); 1222 ObjCInterfaceDecl *SID = OID->getSuperClass(); 1223 1224 if (!OID->isThisDeclarationADefinition()) { 1225 Out << "@class " << I; 1226 1227 if (auto TypeParams = OID->getTypeParamListAsWritten()) { 1228 PrintObjCTypeParams(TypeParams); 1229 } 1230 1231 Out << ";"; 1232 return; 1233 } 1234 bool eolnOut = false; 1235 Out << "@interface " << I; 1236 1237 if (auto TypeParams = OID->getTypeParamListAsWritten()) { 1238 PrintObjCTypeParams(TypeParams); 1239 } 1240 1241 if (SID) 1242 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy); 1243 1244 // Protocols? 1245 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); 1246 if (!Protocols.empty()) { 1247 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1248 E = Protocols.end(); I != E; ++I) 1249 Out << (I == Protocols.begin() ? '<' : ',') << **I; 1250 Out << "> "; 1251 } 1252 1253 if (OID->ivar_size() > 0) { 1254 Out << "{\n"; 1255 eolnOut = true; 1256 Indentation += Policy.Indentation; 1257 for (const auto *I : OID->ivars()) { 1258 Indent() << I->getASTContext() 1259 .getUnqualifiedObjCPointerType(I->getType()) 1260 .getAsString(Policy) << ' ' << *I << ";\n"; 1261 } 1262 Indentation -= Policy.Indentation; 1263 Out << "}\n"; 1264 } 1265 else if (SID || (OID->decls_begin() != OID->decls_end())) { 1266 Out << "\n"; 1267 eolnOut = true; 1268 } 1269 1270 VisitDeclContext(OID, false); 1271 if (!eolnOut) 1272 Out << "\n"; 1273 Out << "@end"; 1274 // FIXME: implement the rest... 1275 } 1276 1277 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 1278 if (!PID->isThisDeclarationADefinition()) { 1279 Out << "@protocol " << *PID << ";\n"; 1280 return; 1281 } 1282 // Protocols? 1283 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols(); 1284 if (!Protocols.empty()) { 1285 Out << "@protocol " << *PID; 1286 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1287 E = Protocols.end(); I != E; ++I) 1288 Out << (I == Protocols.begin() ? '<' : ',') << **I; 1289 Out << ">\n"; 1290 } else 1291 Out << "@protocol " << *PID << '\n'; 1292 VisitDeclContext(PID, false); 1293 Out << "@end"; 1294 } 1295 1296 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { 1297 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; 1298 1299 VisitDeclContext(PID, false); 1300 Out << "@end"; 1301 // FIXME: implement the rest... 1302 } 1303 1304 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { 1305 Out << "@interface " << *PID->getClassInterface(); 1306 if (auto TypeParams = PID->getTypeParamList()) { 1307 PrintObjCTypeParams(TypeParams); 1308 } 1309 Out << "(" << *PID << ")\n"; 1310 if (PID->ivar_size() > 0) { 1311 Out << "{\n"; 1312 Indentation += Policy.Indentation; 1313 for (const auto *I : PID->ivars()) 1314 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). 1315 getAsString(Policy) << ' ' << *I << ";\n"; 1316 Indentation -= Policy.Indentation; 1317 Out << "}\n"; 1318 } 1319 1320 VisitDeclContext(PID, false); 1321 Out << "@end"; 1322 1323 // FIXME: implement the rest... 1324 } 1325 1326 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { 1327 Out << "@compatibility_alias " << *AID 1328 << ' ' << *AID->getClassInterface() << ";\n"; 1329 } 1330 1331 /// PrintObjCPropertyDecl - print a property declaration. 1332 /// 1333 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { 1334 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) 1335 Out << "@required\n"; 1336 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) 1337 Out << "@optional\n"; 1338 1339 QualType T = PDecl->getType(); 1340 1341 Out << "@property"; 1342 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { 1343 bool first = true; 1344 Out << " ("; 1345 if (PDecl->getPropertyAttributes() & 1346 ObjCPropertyDecl::OBJC_PR_readonly) { 1347 Out << (first ? ' ' : ',') << "readonly"; 1348 first = false; 1349 } 1350 1351 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 1352 Out << (first ? ' ' : ',') << "getter = "; 1353 PDecl->getGetterName().print(Out); 1354 first = false; 1355 } 1356 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 1357 Out << (first ? ' ' : ',') << "setter = "; 1358 PDecl->getSetterName().print(Out); 1359 first = false; 1360 } 1361 1362 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { 1363 Out << (first ? ' ' : ',') << "assign"; 1364 first = false; 1365 } 1366 1367 if (PDecl->getPropertyAttributes() & 1368 ObjCPropertyDecl::OBJC_PR_readwrite) { 1369 Out << (first ? ' ' : ',') << "readwrite"; 1370 first = false; 1371 } 1372 1373 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { 1374 Out << (first ? ' ' : ',') << "retain"; 1375 first = false; 1376 } 1377 1378 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { 1379 Out << (first ? ' ' : ',') << "strong"; 1380 first = false; 1381 } 1382 1383 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { 1384 Out << (first ? ' ' : ',') << "copy"; 1385 first = false; 1386 } 1387 1388 if (PDecl->getPropertyAttributes() & 1389 ObjCPropertyDecl::OBJC_PR_nonatomic) { 1390 Out << (first ? ' ' : ',') << "nonatomic"; 1391 first = false; 1392 } 1393 if (PDecl->getPropertyAttributes() & 1394 ObjCPropertyDecl::OBJC_PR_atomic) { 1395 Out << (first ? ' ' : ',') << "atomic"; 1396 first = false; 1397 } 1398 1399 if (PDecl->getPropertyAttributes() & 1400 ObjCPropertyDecl::OBJC_PR_nullability) { 1401 if (auto nullability = AttributedType::stripOuterNullability(T)) { 1402 if (*nullability == NullabilityKind::Unspecified && 1403 (PDecl->getPropertyAttributes() & 1404 ObjCPropertyDecl::OBJC_PR_null_resettable)) { 1405 Out << (first ? ' ' : ',') << "null_resettable"; 1406 } else { 1407 Out << (first ? ' ' : ',') 1408 << getNullabilitySpelling(*nullability, true); 1409 } 1410 first = false; 1411 } 1412 } 1413 1414 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) { 1415 Out << (first ? ' ' : ',') << "class"; 1416 first = false; 1417 } 1418 1419 (void) first; // Silence dead store warning due to idiomatic code. 1420 Out << " )"; 1421 } 1422 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T). 1423 getAsString(Policy) << ' ' << *PDecl; 1424 if (Policy.PolishForDeclaration) 1425 Out << ';'; 1426 } 1427 1428 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { 1429 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 1430 Out << "@synthesize "; 1431 else 1432 Out << "@dynamic "; 1433 Out << *PID->getPropertyDecl(); 1434 if (PID->getPropertyIvarDecl()) 1435 Out << '=' << *PID->getPropertyIvarDecl(); 1436 } 1437 1438 void DeclPrinter::VisitUsingDecl(UsingDecl *D) { 1439 if (!D->isAccessDeclaration()) 1440 Out << "using "; 1441 if (D->hasTypename()) 1442 Out << "typename "; 1443 D->getQualifier()->print(Out, Policy); 1444 1445 // Use the correct record name when the using declaration is used for 1446 // inheriting constructors. 1447 for (const auto *Shadow : D->shadows()) { 1448 if (const auto *ConstructorShadow = 1449 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) { 1450 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext()); 1451 Out << *ConstructorShadow->getNominatedBaseClass(); 1452 return; 1453 } 1454 } 1455 Out << *D; 1456 } 1457 1458 void 1459 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 1460 Out << "using typename "; 1461 D->getQualifier()->print(Out, Policy); 1462 Out << D->getDeclName(); 1463 } 1464 1465 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1466 if (!D->isAccessDeclaration()) 1467 Out << "using "; 1468 D->getQualifier()->print(Out, Policy); 1469 Out << D->getDeclName(); 1470 } 1471 1472 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { 1473 // ignore 1474 } 1475 1476 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { 1477 Out << "#pragma omp threadprivate"; 1478 if (!D->varlist_empty()) { 1479 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(), 1480 E = D->varlist_end(); 1481 I != E; ++I) { 1482 Out << (I == D->varlist_begin() ? '(' : ','); 1483 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl()); 1484 ND->printQualifiedName(Out); 1485 } 1486 Out << ")"; 1487 } 1488 } 1489 1490 void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { 1491 if (!D->isInvalidDecl()) { 1492 Out << "#pragma omp declare reduction ("; 1493 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) { 1494 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = { 1495 nullptr, 1496 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ 1497 Spelling, 1498 #include "clang/Basic/OperatorKinds.def" 1499 }; 1500 const char *OpName = 1501 OperatorNames[D->getDeclName().getCXXOverloadedOperator()]; 1502 assert(OpName && "not an overloaded operator"); 1503 Out << OpName; 1504 } else { 1505 assert(D->getDeclName().isIdentifier()); 1506 D->printName(Out); 1507 } 1508 Out << " : "; 1509 D->getType().print(Out, Policy); 1510 Out << " : "; 1511 D->getCombiner()->printPretty(Out, nullptr, Policy, 0); 1512 Out << ")"; 1513 if (auto *Init = D->getInitializer()) { 1514 Out << " initializer("; 1515 Init->printPretty(Out, nullptr, Policy, 0); 1516 Out << ")"; 1517 } 1518 } 1519 } 1520 1521 void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) { 1522 D->getInit()->printPretty(Out, nullptr, Policy, Indentation); 1523 } 1524 1525