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