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