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(Policy, Out, 1109 TemplateParameterList::shouldIncludeTypeForArgument( 1110 Policy, 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(Policy, Params, 1128 I)); 1129 } 1130 Out << ">"; 1131 } 1132 1133 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 1134 printTemplateParameters(D->getTemplateParameters()); 1135 1136 if (const TemplateTemplateParmDecl *TTP = 1137 dyn_cast<TemplateTemplateParmDecl>(D)) { 1138 Out << "class"; 1139 1140 if (TTP->isParameterPack()) 1141 Out << " ..."; 1142 else if (TTP->getDeclName()) 1143 Out << ' '; 1144 1145 if (TTP->getDeclName()) 1146 Out << TTP->getDeclName(); 1147 } else if (auto *TD = D->getTemplatedDecl()) 1148 Visit(TD); 1149 else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) { 1150 Out << "concept " << Concept->getName() << " = " ; 1151 Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy, Indentation, 1152 "\n", &Context); 1153 } 1154 } 1155 1156 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 1157 prettyPrintPragmas(D->getTemplatedDecl()); 1158 // Print any leading template parameter lists. 1159 if (const FunctionDecl *FD = D->getTemplatedDecl()) { 1160 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists(); 1161 I < NumTemplateParams; ++I) 1162 printTemplateParameters(FD->getTemplateParameterList(I)); 1163 } 1164 VisitRedeclarableTemplateDecl(D); 1165 // Declare target attribute is special one, natural spelling for the pragma 1166 // assumes "ending" construct so print it here. 1167 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>()) 1168 Out << "#pragma omp end declare target\n"; 1169 1170 // Never print "instantiations" for deduction guides (they don't really 1171 // have them). 1172 if (PrintInstantiation && 1173 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) { 1174 FunctionDecl *PrevDecl = D->getTemplatedDecl(); 1175 const FunctionDecl *Def; 1176 if (PrevDecl->isDefined(Def) && Def != PrevDecl) 1177 return; 1178 for (auto *I : D->specializations()) 1179 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) { 1180 if (!PrevDecl->isThisDeclarationADefinition()) 1181 Out << ";\n"; 1182 Indent(); 1183 prettyPrintPragmas(I); 1184 Visit(I); 1185 } 1186 } 1187 } 1188 1189 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 1190 VisitRedeclarableTemplateDecl(D); 1191 1192 if (PrintInstantiation) { 1193 for (auto *I : D->specializations()) 1194 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) { 1195 if (D->isThisDeclarationADefinition()) 1196 Out << ";"; 1197 Out << "\n"; 1198 Visit(I); 1199 } 1200 } 1201 } 1202 1203 void DeclPrinter::VisitClassTemplateSpecializationDecl( 1204 ClassTemplateSpecializationDecl *D) { 1205 Out << "template<> "; 1206 VisitCXXRecordDecl(D); 1207 } 1208 1209 void DeclPrinter::VisitClassTemplatePartialSpecializationDecl( 1210 ClassTemplatePartialSpecializationDecl *D) { 1211 printTemplateParameters(D->getTemplateParameters()); 1212 VisitCXXRecordDecl(D); 1213 } 1214 1215 //---------------------------------------------------------------------------- 1216 // Objective-C declarations 1217 //---------------------------------------------------------------------------- 1218 1219 void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx, 1220 Decl::ObjCDeclQualifier Quals, 1221 QualType T) { 1222 Out << '('; 1223 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In) 1224 Out << "in "; 1225 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout) 1226 Out << "inout "; 1227 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out) 1228 Out << "out "; 1229 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy) 1230 Out << "bycopy "; 1231 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref) 1232 Out << "byref "; 1233 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway) 1234 Out << "oneway "; 1235 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) { 1236 if (auto nullability = AttributedType::stripOuterNullability(T)) 1237 Out << getNullabilitySpelling(*nullability, true) << ' '; 1238 } 1239 1240 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy); 1241 Out << ')'; 1242 } 1243 1244 void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) { 1245 Out << "<"; 1246 unsigned First = true; 1247 for (auto *Param : *Params) { 1248 if (First) { 1249 First = false; 1250 } else { 1251 Out << ", "; 1252 } 1253 1254 switch (Param->getVariance()) { 1255 case ObjCTypeParamVariance::Invariant: 1256 break; 1257 1258 case ObjCTypeParamVariance::Covariant: 1259 Out << "__covariant "; 1260 break; 1261 1262 case ObjCTypeParamVariance::Contravariant: 1263 Out << "__contravariant "; 1264 break; 1265 } 1266 1267 Out << Param->getDeclName(); 1268 1269 if (Param->hasExplicitBound()) { 1270 Out << " : " << Param->getUnderlyingType().getAsString(Policy); 1271 } 1272 } 1273 Out << ">"; 1274 } 1275 1276 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { 1277 if (OMD->isInstanceMethod()) 1278 Out << "- "; 1279 else 1280 Out << "+ "; 1281 if (!OMD->getReturnType().isNull()) { 1282 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(), 1283 OMD->getReturnType()); 1284 } 1285 1286 std::string name = OMD->getSelector().getAsString(); 1287 std::string::size_type pos, lastPos = 0; 1288 for (const auto *PI : OMD->parameters()) { 1289 // FIXME: selector is missing here! 1290 pos = name.find_first_of(':', lastPos); 1291 if (lastPos != 0) 1292 Out << " "; 1293 Out << name.substr(lastPos, pos - lastPos) << ':'; 1294 PrintObjCMethodType(OMD->getASTContext(), 1295 PI->getObjCDeclQualifier(), 1296 PI->getType()); 1297 Out << *PI; 1298 lastPos = pos + 1; 1299 } 1300 1301 if (OMD->param_begin() == OMD->param_end()) 1302 Out << name; 1303 1304 if (OMD->isVariadic()) 1305 Out << ", ..."; 1306 1307 prettyPrintAttributes(OMD); 1308 1309 if (OMD->getBody() && !Policy.TerseOutput) { 1310 Out << ' '; 1311 OMD->getBody()->printPretty(Out, nullptr, Policy, Indentation, "\n", 1312 &Context); 1313 } 1314 else if (Policy.PolishForDeclaration) 1315 Out << ';'; 1316 } 1317 1318 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { 1319 std::string I = OID->getNameAsString(); 1320 ObjCInterfaceDecl *SID = OID->getSuperClass(); 1321 1322 bool eolnOut = false; 1323 if (SID) 1324 Out << "@implementation " << I << " : " << *SID; 1325 else 1326 Out << "@implementation " << I; 1327 1328 if (OID->ivar_size() > 0) { 1329 Out << "{\n"; 1330 eolnOut = true; 1331 Indentation += Policy.Indentation; 1332 for (const auto *I : OID->ivars()) { 1333 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). 1334 getAsString(Policy) << ' ' << *I << ";\n"; 1335 } 1336 Indentation -= Policy.Indentation; 1337 Out << "}\n"; 1338 } 1339 else if (SID || (OID->decls_begin() != OID->decls_end())) { 1340 Out << "\n"; 1341 eolnOut = true; 1342 } 1343 VisitDeclContext(OID, false); 1344 if (!eolnOut) 1345 Out << "\n"; 1346 Out << "@end"; 1347 } 1348 1349 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { 1350 std::string I = OID->getNameAsString(); 1351 ObjCInterfaceDecl *SID = OID->getSuperClass(); 1352 1353 if (!OID->isThisDeclarationADefinition()) { 1354 Out << "@class " << I; 1355 1356 if (auto TypeParams = OID->getTypeParamListAsWritten()) { 1357 PrintObjCTypeParams(TypeParams); 1358 } 1359 1360 Out << ";"; 1361 return; 1362 } 1363 bool eolnOut = false; 1364 Out << "@interface " << I; 1365 1366 if (auto TypeParams = OID->getTypeParamListAsWritten()) { 1367 PrintObjCTypeParams(TypeParams); 1368 } 1369 1370 if (SID) 1371 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy); 1372 1373 // Protocols? 1374 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); 1375 if (!Protocols.empty()) { 1376 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1377 E = Protocols.end(); I != E; ++I) 1378 Out << (I == Protocols.begin() ? '<' : ',') << **I; 1379 Out << "> "; 1380 } 1381 1382 if (OID->ivar_size() > 0) { 1383 Out << "{\n"; 1384 eolnOut = true; 1385 Indentation += Policy.Indentation; 1386 for (const auto *I : OID->ivars()) { 1387 Indent() << I->getASTContext() 1388 .getUnqualifiedObjCPointerType(I->getType()) 1389 .getAsString(Policy) << ' ' << *I << ";\n"; 1390 } 1391 Indentation -= Policy.Indentation; 1392 Out << "}\n"; 1393 } 1394 else if (SID || (OID->decls_begin() != OID->decls_end())) { 1395 Out << "\n"; 1396 eolnOut = true; 1397 } 1398 1399 VisitDeclContext(OID, false); 1400 if (!eolnOut) 1401 Out << "\n"; 1402 Out << "@end"; 1403 // FIXME: implement the rest... 1404 } 1405 1406 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 1407 if (!PID->isThisDeclarationADefinition()) { 1408 Out << "@protocol " << *PID << ";\n"; 1409 return; 1410 } 1411 // Protocols? 1412 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols(); 1413 if (!Protocols.empty()) { 1414 Out << "@protocol " << *PID; 1415 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1416 E = Protocols.end(); I != E; ++I) 1417 Out << (I == Protocols.begin() ? '<' : ',') << **I; 1418 Out << ">\n"; 1419 } else 1420 Out << "@protocol " << *PID << '\n'; 1421 VisitDeclContext(PID, false); 1422 Out << "@end"; 1423 } 1424 1425 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { 1426 Out << "@implementation "; 1427 if (const auto *CID = PID->getClassInterface()) 1428 Out << *CID; 1429 else 1430 Out << "<<error-type>>"; 1431 Out << '(' << *PID << ")\n"; 1432 1433 VisitDeclContext(PID, false); 1434 Out << "@end"; 1435 // FIXME: implement the rest... 1436 } 1437 1438 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { 1439 Out << "@interface "; 1440 if (const auto *CID = PID->getClassInterface()) 1441 Out << *CID; 1442 else 1443 Out << "<<error-type>>"; 1444 if (auto TypeParams = PID->getTypeParamList()) { 1445 PrintObjCTypeParams(TypeParams); 1446 } 1447 Out << "(" << *PID << ")\n"; 1448 if (PID->ivar_size() > 0) { 1449 Out << "{\n"; 1450 Indentation += Policy.Indentation; 1451 for (const auto *I : PID->ivars()) 1452 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). 1453 getAsString(Policy) << ' ' << *I << ";\n"; 1454 Indentation -= Policy.Indentation; 1455 Out << "}\n"; 1456 } 1457 1458 VisitDeclContext(PID, false); 1459 Out << "@end"; 1460 1461 // FIXME: implement the rest... 1462 } 1463 1464 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { 1465 Out << "@compatibility_alias " << *AID 1466 << ' ' << *AID->getClassInterface() << ";\n"; 1467 } 1468 1469 /// PrintObjCPropertyDecl - print a property declaration. 1470 /// 1471 /// Print attributes in the following order: 1472 /// - class 1473 /// - nonatomic | atomic 1474 /// - assign | retain | strong | copy | weak | unsafe_unretained 1475 /// - readwrite | readonly 1476 /// - getter & setter 1477 /// - nullability 1478 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { 1479 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) 1480 Out << "@required\n"; 1481 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) 1482 Out << "@optional\n"; 1483 1484 QualType T = PDecl->getType(); 1485 1486 Out << "@property"; 1487 if (PDecl->getPropertyAttributes() != ObjCPropertyAttribute::kind_noattr) { 1488 bool first = true; 1489 Out << "("; 1490 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_class) { 1491 Out << (first ? "" : ", ") << "class"; 1492 first = false; 1493 } 1494 1495 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_direct) { 1496 Out << (first ? "" : ", ") << "direct"; 1497 first = false; 1498 } 1499 1500 if (PDecl->getPropertyAttributes() & 1501 ObjCPropertyAttribute::kind_nonatomic) { 1502 Out << (first ? "" : ", ") << "nonatomic"; 1503 first = false; 1504 } 1505 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic) { 1506 Out << (first ? "" : ", ") << "atomic"; 1507 first = false; 1508 } 1509 1510 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_assign) { 1511 Out << (first ? "" : ", ") << "assign"; 1512 first = false; 1513 } 1514 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_retain) { 1515 Out << (first ? "" : ", ") << "retain"; 1516 first = false; 1517 } 1518 1519 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_strong) { 1520 Out << (first ? "" : ", ") << "strong"; 1521 first = false; 1522 } 1523 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy) { 1524 Out << (first ? "" : ", ") << "copy"; 1525 first = false; 1526 } 1527 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak) { 1528 Out << (first ? "" : ", ") << "weak"; 1529 first = false; 1530 } 1531 if (PDecl->getPropertyAttributes() & 1532 ObjCPropertyAttribute::kind_unsafe_unretained) { 1533 Out << (first ? "" : ", ") << "unsafe_unretained"; 1534 first = false; 1535 } 1536 1537 if (PDecl->getPropertyAttributes() & 1538 ObjCPropertyAttribute::kind_readwrite) { 1539 Out << (first ? "" : ", ") << "readwrite"; 1540 first = false; 1541 } 1542 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly) { 1543 Out << (first ? "" : ", ") << "readonly"; 1544 first = false; 1545 } 1546 1547 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_getter) { 1548 Out << (first ? "" : ", ") << "getter = "; 1549 PDecl->getGetterName().print(Out); 1550 first = false; 1551 } 1552 if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_setter) { 1553 Out << (first ? "" : ", ") << "setter = "; 1554 PDecl->getSetterName().print(Out); 1555 first = false; 1556 } 1557 1558 if (PDecl->getPropertyAttributes() & 1559 ObjCPropertyAttribute::kind_nullability) { 1560 if (auto nullability = AttributedType::stripOuterNullability(T)) { 1561 if (*nullability == NullabilityKind::Unspecified && 1562 (PDecl->getPropertyAttributes() & 1563 ObjCPropertyAttribute::kind_null_resettable)) { 1564 Out << (first ? "" : ", ") << "null_resettable"; 1565 } else { 1566 Out << (first ? "" : ", ") 1567 << getNullabilitySpelling(*nullability, true); 1568 } 1569 first = false; 1570 } 1571 } 1572 1573 (void) first; // Silence dead store warning due to idiomatic code. 1574 Out << ")"; 1575 } 1576 std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T). 1577 getAsString(Policy); 1578 Out << ' ' << TypeStr; 1579 if (!StringRef(TypeStr).endswith("*")) 1580 Out << ' '; 1581 Out << *PDecl; 1582 if (Policy.PolishForDeclaration) 1583 Out << ';'; 1584 } 1585 1586 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { 1587 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 1588 Out << "@synthesize "; 1589 else 1590 Out << "@dynamic "; 1591 Out << *PID->getPropertyDecl(); 1592 if (PID->getPropertyIvarDecl()) 1593 Out << '=' << *PID->getPropertyIvarDecl(); 1594 } 1595 1596 void DeclPrinter::VisitUsingDecl(UsingDecl *D) { 1597 if (!D->isAccessDeclaration()) 1598 Out << "using "; 1599 if (D->hasTypename()) 1600 Out << "typename "; 1601 D->getQualifier()->print(Out, Policy); 1602 1603 // Use the correct record name when the using declaration is used for 1604 // inheriting constructors. 1605 for (const auto *Shadow : D->shadows()) { 1606 if (const auto *ConstructorShadow = 1607 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) { 1608 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext()); 1609 Out << *ConstructorShadow->getNominatedBaseClass(); 1610 return; 1611 } 1612 } 1613 Out << *D; 1614 } 1615 1616 void DeclPrinter::VisitUsingEnumDecl(UsingEnumDecl *D) { 1617 Out << "using enum " << D->getEnumDecl(); 1618 } 1619 1620 void 1621 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 1622 Out << "using typename "; 1623 D->getQualifier()->print(Out, Policy); 1624 Out << D->getDeclName(); 1625 } 1626 1627 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1628 if (!D->isAccessDeclaration()) 1629 Out << "using "; 1630 D->getQualifier()->print(Out, Policy); 1631 Out << D->getDeclName(); 1632 } 1633 1634 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { 1635 // ignore 1636 } 1637 1638 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { 1639 Out << "#pragma omp threadprivate"; 1640 if (!D->varlist_empty()) { 1641 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(), 1642 E = D->varlist_end(); 1643 I != E; ++I) { 1644 Out << (I == D->varlist_begin() ? '(' : ','); 1645 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl(); 1646 ND->printQualifiedName(Out); 1647 } 1648 Out << ")"; 1649 } 1650 } 1651 1652 void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) { 1653 Out << "#pragma omp allocate"; 1654 if (!D->varlist_empty()) { 1655 for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(), 1656 E = D->varlist_end(); 1657 I != E; ++I) { 1658 Out << (I == D->varlist_begin() ? '(' : ','); 1659 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl(); 1660 ND->printQualifiedName(Out); 1661 } 1662 Out << ")"; 1663 } 1664 if (!D->clauselist_empty()) { 1665 Out << " "; 1666 OMPClausePrinter Printer(Out, Policy); 1667 for (OMPClause *C : D->clauselists()) 1668 Printer.Visit(C); 1669 } 1670 } 1671 1672 void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) { 1673 Out << "#pragma omp requires "; 1674 if (!D->clauselist_empty()) { 1675 OMPClausePrinter Printer(Out, Policy); 1676 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I) 1677 Printer.Visit(*I); 1678 } 1679 } 1680 1681 void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { 1682 if (!D->isInvalidDecl()) { 1683 Out << "#pragma omp declare reduction ("; 1684 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) { 1685 const char *OpName = 1686 getOperatorSpelling(D->getDeclName().getCXXOverloadedOperator()); 1687 assert(OpName && "not an overloaded operator"); 1688 Out << OpName; 1689 } else { 1690 assert(D->getDeclName().isIdentifier()); 1691 D->printName(Out); 1692 } 1693 Out << " : "; 1694 D->getType().print(Out, Policy); 1695 Out << " : "; 1696 D->getCombiner()->printPretty(Out, nullptr, Policy, 0, "\n", &Context); 1697 Out << ")"; 1698 if (auto *Init = D->getInitializer()) { 1699 Out << " initializer("; 1700 switch (D->getInitializerKind()) { 1701 case OMPDeclareReductionDecl::DirectInit: 1702 Out << "omp_priv("; 1703 break; 1704 case OMPDeclareReductionDecl::CopyInit: 1705 Out << "omp_priv = "; 1706 break; 1707 case OMPDeclareReductionDecl::CallInit: 1708 break; 1709 } 1710 Init->printPretty(Out, nullptr, Policy, 0, "\n", &Context); 1711 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit) 1712 Out << ")"; 1713 Out << ")"; 1714 } 1715 } 1716 } 1717 1718 void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) { 1719 if (!D->isInvalidDecl()) { 1720 Out << "#pragma omp declare mapper ("; 1721 D->printName(Out); 1722 Out << " : "; 1723 D->getType().print(Out, Policy); 1724 Out << " "; 1725 Out << D->getVarName(); 1726 Out << ")"; 1727 if (!D->clauselist_empty()) { 1728 OMPClausePrinter Printer(Out, Policy); 1729 for (auto *C : D->clauselists()) { 1730 Out << " "; 1731 Printer.Visit(C); 1732 } 1733 } 1734 } 1735 } 1736 1737 void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) { 1738 D->getInit()->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context); 1739 } 1740 1741 void DeclPrinter::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP) { 1742 if (const TypeConstraint *TC = TTP->getTypeConstraint()) 1743 TC->print(Out, Policy); 1744 else if (TTP->wasDeclaredWithTypename()) 1745 Out << "typename"; 1746 else 1747 Out << "class"; 1748 1749 if (TTP->isParameterPack()) 1750 Out << " ..."; 1751 else if (TTP->getDeclName()) 1752 Out << ' '; 1753 1754 if (TTP->getDeclName()) 1755 Out << TTP->getDeclName(); 1756 1757 if (TTP->hasDefaultArgument()) { 1758 Out << " = "; 1759 Out << TTP->getDefaultArgument().getAsString(Policy); 1760 } 1761 } 1762 1763 void DeclPrinter::VisitNonTypeTemplateParmDecl( 1764 const NonTypeTemplateParmDecl *NTTP) { 1765 StringRef Name; 1766 if (IdentifierInfo *II = NTTP->getIdentifier()) 1767 Name = II->getName(); 1768 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack()); 1769 1770 if (NTTP->hasDefaultArgument()) { 1771 Out << " = "; 1772 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, Indentation, 1773 "\n", &Context); 1774 } 1775 } 1776