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