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 96 void PrintTemplateParameters(const TemplateParameterList *Params, 97 const TemplateArgumentList *Args = nullptr); 98 void prettyPrintAttributes(Decl *D); 99 void prettyPrintPragmas(Decl *D); 100 void printDeclType(QualType T, StringRef DeclName, bool Pack = false); 101 }; 102 } 103 104 void Decl::print(raw_ostream &Out, unsigned Indentation, 105 bool PrintInstantiation) const { 106 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation); 107 } 108 109 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy, 110 unsigned Indentation, bool PrintInstantiation) const { 111 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation); 112 Printer.Visit(const_cast<Decl*>(this)); 113 } 114 115 static QualType GetBaseType(QualType T) { 116 // FIXME: This should be on the Type class! 117 QualType BaseType = T; 118 while (!BaseType->isSpecifierType()) { 119 if (isa<TypedefType>(BaseType)) 120 break; 121 else if (const PointerType* PTy = BaseType->getAs<PointerType>()) 122 BaseType = PTy->getPointeeType(); 123 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>()) 124 BaseType = BPy->getPointeeType(); 125 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) 126 BaseType = ATy->getElementType(); 127 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>()) 128 BaseType = FTy->getReturnType(); 129 else if (const VectorType *VTy = BaseType->getAs<VectorType>()) 130 BaseType = VTy->getElementType(); 131 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>()) 132 BaseType = RTy->getPointeeType(); 133 else 134 llvm_unreachable("Unknown declarator!"); 135 } 136 return BaseType; 137 } 138 139 static QualType getDeclType(Decl* D) { 140 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D)) 141 return TDD->getUnderlyingType(); 142 if (ValueDecl* VD = dyn_cast<ValueDecl>(D)) 143 return VD->getType(); 144 return QualType(); 145 } 146 147 void Decl::printGroup(Decl** Begin, unsigned NumDecls, 148 raw_ostream &Out, const PrintingPolicy &Policy, 149 unsigned Indentation) { 150 if (NumDecls == 1) { 151 (*Begin)->print(Out, Policy, Indentation); 152 return; 153 } 154 155 Decl** End = Begin + NumDecls; 156 TagDecl* TD = dyn_cast<TagDecl>(*Begin); 157 if (TD) 158 ++Begin; 159 160 PrintingPolicy SubPolicy(Policy); 161 if (TD && TD->isCompleteDefinition()) { 162 TD->print(Out, Policy, Indentation); 163 Out << " "; 164 SubPolicy.SuppressTag = true; 165 } 166 167 bool isFirst = true; 168 for ( ; Begin != End; ++Begin) { 169 if (isFirst) { 170 SubPolicy.SuppressSpecifiers = false; 171 isFirst = false; 172 } else { 173 if (!isFirst) Out << ", "; 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); 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)) 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 363 if (!Decls.empty()) 364 ProcessDeclGroup(Decls); 365 366 if (Indent) 367 Indentation -= Policy.Indentation; 368 } 369 370 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 371 VisitDeclContext(D, false); 372 } 373 374 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { 375 if (!Policy.SuppressSpecifiers) { 376 Out << "typedef "; 377 378 if (D->isModulePrivate()) 379 Out << "__module_private__ "; 380 } 381 D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName()); 382 prettyPrintAttributes(D); 383 } 384 385 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) { 386 Out << "using " << *D; 387 prettyPrintAttributes(D); 388 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy); 389 } 390 391 void DeclPrinter::VisitEnumDecl(EnumDecl *D) { 392 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 393 Out << "__module_private__ "; 394 Out << "enum "; 395 if (D->isScoped()) { 396 if (D->isScopedUsingClassTag()) 397 Out << "class "; 398 else 399 Out << "struct "; 400 } 401 Out << *D; 402 403 if (D->isFixed()) 404 Out << " : " << D->getIntegerType().stream(Policy); 405 406 if (D->isCompleteDefinition()) { 407 Out << " {\n"; 408 VisitDeclContext(D); 409 Indent() << "}"; 410 } 411 prettyPrintAttributes(D); 412 } 413 414 void DeclPrinter::VisitRecordDecl(RecordDecl *D) { 415 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 416 Out << "__module_private__ "; 417 Out << D->getKindName(); 418 419 prettyPrintAttributes(D); 420 421 if (D->getIdentifier()) 422 Out << ' ' << *D; 423 424 if (D->isCompleteDefinition()) { 425 Out << " {\n"; 426 VisitDeclContext(D); 427 Indent() << "}"; 428 } 429 } 430 431 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { 432 Out << *D; 433 if (Expr *Init = D->getInitExpr()) { 434 Out << " = "; 435 Init->printPretty(Out, nullptr, Policy, Indentation); 436 } 437 } 438 439 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { 440 if (!D->getDescribedFunctionTemplate() && 441 !D->isFunctionTemplateSpecialization()) 442 prettyPrintPragmas(D); 443 444 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D); 445 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D); 446 if (!Policy.SuppressSpecifiers) { 447 switch (D->getStorageClass()) { 448 case SC_None: break; 449 case SC_Extern: Out << "extern "; break; 450 case SC_Static: Out << "static "; break; 451 case SC_PrivateExtern: Out << "__private_extern__ "; break; 452 case SC_Auto: case SC_Register: 453 llvm_unreachable("invalid for functions"); 454 } 455 456 if (D->isInlineSpecified()) Out << "inline "; 457 if (D->isVirtualAsWritten()) Out << "virtual "; 458 if (D->isModulePrivate()) Out << "__module_private__ "; 459 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr "; 460 if ((CDecl && CDecl->isExplicitSpecified()) || 461 (ConversionDecl && ConversionDecl->isExplicit())) 462 Out << "explicit "; 463 } 464 465 PrintingPolicy SubPolicy(Policy); 466 SubPolicy.SuppressSpecifiers = false; 467 std::string Proto = D->getNameInfo().getAsString(); 468 469 QualType Ty = D->getType(); 470 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 471 Proto = '(' + Proto + ')'; 472 Ty = PT->getInnerType(); 473 } 474 475 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) { 476 const FunctionProtoType *FT = nullptr; 477 if (D->hasWrittenPrototype()) 478 FT = dyn_cast<FunctionProtoType>(AFT); 479 480 Proto += "("; 481 if (FT) { 482 llvm::raw_string_ostream POut(Proto); 483 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation); 484 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 485 if (i) POut << ", "; 486 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 487 } 488 489 if (FT->isVariadic()) { 490 if (D->getNumParams()) POut << ", "; 491 POut << "..."; 492 } 493 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { 494 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 495 if (i) 496 Proto += ", "; 497 Proto += D->getParamDecl(i)->getNameAsString(); 498 } 499 } 500 501 Proto += ")"; 502 503 if (FT) { 504 if (FT->isConst()) 505 Proto += " const"; 506 if (FT->isVolatile()) 507 Proto += " volatile"; 508 if (FT->isRestrict()) 509 Proto += " restrict"; 510 511 switch (FT->getRefQualifier()) { 512 case RQ_None: 513 break; 514 case RQ_LValue: 515 Proto += " &"; 516 break; 517 case RQ_RValue: 518 Proto += " &&"; 519 break; 520 } 521 } 522 523 if (FT && FT->hasDynamicExceptionSpec()) { 524 Proto += " throw("; 525 if (FT->getExceptionSpecType() == EST_MSAny) 526 Proto += "..."; 527 else 528 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { 529 if (I) 530 Proto += ", "; 531 532 Proto += FT->getExceptionType(I).getAsString(SubPolicy); 533 } 534 Proto += ")"; 535 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { 536 Proto += " noexcept"; 537 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) { 538 Proto += "("; 539 llvm::raw_string_ostream EOut(Proto); 540 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy, 541 Indentation); 542 EOut.flush(); 543 Proto += EOut.str(); 544 Proto += ")"; 545 } 546 } 547 548 if (CDecl) { 549 bool HasInitializerList = false; 550 for (const auto *BMInitializer : CDecl->inits()) { 551 if (BMInitializer->isInClassMemberInitializer()) 552 continue; 553 554 if (!HasInitializerList) { 555 Proto += " : "; 556 Out << Proto; 557 Proto.clear(); 558 HasInitializerList = true; 559 } else 560 Out << ", "; 561 562 if (BMInitializer->isAnyMemberInitializer()) { 563 FieldDecl *FD = BMInitializer->getAnyMember(); 564 Out << *FD; 565 } else { 566 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); 567 } 568 569 Out << "("; 570 if (!BMInitializer->getInit()) { 571 // Nothing to print 572 } else { 573 Expr *Init = BMInitializer->getInit(); 574 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) 575 Init = Tmp->getSubExpr(); 576 577 Init = Init->IgnoreParens(); 578 579 Expr *SimpleInit = nullptr; 580 Expr **Args = nullptr; 581 unsigned NumArgs = 0; 582 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { 583 Args = ParenList->getExprs(); 584 NumArgs = ParenList->getNumExprs(); 585 } else if (CXXConstructExpr *Construct 586 = dyn_cast<CXXConstructExpr>(Init)) { 587 Args = Construct->getArgs(); 588 NumArgs = Construct->getNumArgs(); 589 } else 590 SimpleInit = Init; 591 592 if (SimpleInit) 593 SimpleInit->printPretty(Out, nullptr, Policy, Indentation); 594 else { 595 for (unsigned I = 0; I != NumArgs; ++I) { 596 assert(Args[I] != nullptr && "Expected non-null Expr"); 597 if (isa<CXXDefaultArgExpr>(Args[I])) 598 break; 599 600 if (I) 601 Out << ", "; 602 Args[I]->printPretty(Out, nullptr, Policy, Indentation); 603 } 604 } 605 } 606 Out << ")"; 607 if (BMInitializer->isPackExpansion()) 608 Out << "..."; 609 } 610 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) { 611 if (FT && FT->hasTrailingReturn()) { 612 Out << "auto " << Proto << " -> "; 613 Proto.clear(); 614 } 615 AFT->getReturnType().print(Out, Policy, Proto); 616 Proto.clear(); 617 } 618 Out << Proto; 619 } else { 620 Ty.print(Out, Policy, Proto); 621 } 622 623 prettyPrintAttributes(D); 624 625 if (D->isPure()) 626 Out << " = 0"; 627 else if (D->isDeletedAsWritten()) 628 Out << " = delete"; 629 else if (D->isExplicitlyDefaulted()) 630 Out << " = default"; 631 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) { 632 if (!D->hasPrototype() && D->getNumParams()) { 633 // This is a K&R function definition, so we need to print the 634 // parameters. 635 Out << '\n'; 636 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation); 637 Indentation += Policy.Indentation; 638 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 639 Indent(); 640 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 641 Out << ";\n"; 642 } 643 Indentation -= Policy.Indentation; 644 } else 645 Out << ' '; 646 647 if (D->getBody()) 648 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation); 649 Out << '\n'; 650 } 651 } 652 653 void DeclPrinter::VisitFriendDecl(FriendDecl *D) { 654 if (TypeSourceInfo *TSI = D->getFriendType()) { 655 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists(); 656 for (unsigned i = 0; i < NumTPLists; ++i) 657 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i)); 658 Out << "friend "; 659 Out << " " << TSI->getType().getAsString(Policy); 660 } 661 else if (FunctionDecl *FD = 662 dyn_cast<FunctionDecl>(D->getFriendDecl())) { 663 Out << "friend "; 664 VisitFunctionDecl(FD); 665 } 666 else if (FunctionTemplateDecl *FTD = 667 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) { 668 Out << "friend "; 669 VisitFunctionTemplateDecl(FTD); 670 } 671 else if (ClassTemplateDecl *CTD = 672 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) { 673 Out << "friend "; 674 VisitRedeclarableTemplateDecl(CTD); 675 } 676 } 677 678 void DeclPrinter::VisitFieldDecl(FieldDecl *D) { 679 // FIXME: add printing of pragma attributes if required. 680 if (!Policy.SuppressSpecifiers && D->isMutable()) 681 Out << "mutable "; 682 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 683 Out << "__module_private__ "; 684 685 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()). 686 stream(Policy, D->getName()); 687 688 if (D->isBitField()) { 689 Out << " : "; 690 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation); 691 } 692 693 Expr *Init = D->getInClassInitializer(); 694 if (!Policy.SuppressInitializers && Init) { 695 if (D->getInClassInitStyle() == ICIS_ListInit) 696 Out << " "; 697 else 698 Out << " = "; 699 Init->printPretty(Out, nullptr, Policy, Indentation); 700 } 701 prettyPrintAttributes(D); 702 } 703 704 void DeclPrinter::VisitLabelDecl(LabelDecl *D) { 705 Out << *D << ":"; 706 } 707 708 void DeclPrinter::VisitVarDecl(VarDecl *D) { 709 prettyPrintPragmas(D); 710 if (!Policy.SuppressSpecifiers) { 711 StorageClass SC = D->getStorageClass(); 712 if (SC != SC_None) 713 Out << VarDecl::getStorageClassSpecifierString(SC) << " "; 714 715 switch (D->getTSCSpec()) { 716 case TSCS_unspecified: 717 break; 718 case TSCS___thread: 719 Out << "__thread "; 720 break; 721 case TSCS__Thread_local: 722 Out << "_Thread_local "; 723 break; 724 case TSCS_thread_local: 725 Out << "thread_local "; 726 break; 727 } 728 729 if (D->isModulePrivate()) 730 Out << "__module_private__ "; 731 } 732 733 QualType T = D->getTypeSourceInfo() 734 ? D->getTypeSourceInfo()->getType() 735 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType()); 736 printDeclType(T, D->getName()); 737 Expr *Init = D->getInit(); 738 if (!Policy.SuppressInitializers && Init) { 739 bool ImplicitInit = false; 740 if (CXXConstructExpr *Construct = 741 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) { 742 if (D->getInitStyle() == VarDecl::CallInit && 743 !Construct->isListInitialization()) { 744 ImplicitInit = Construct->getNumArgs() == 0 || 745 Construct->getArg(0)->isDefaultArgument(); 746 } 747 } 748 if (!ImplicitInit) { 749 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 750 Out << "("; 751 else if (D->getInitStyle() == VarDecl::CInit) { 752 Out << " = "; 753 } 754 PrintingPolicy SubPolicy(Policy); 755 SubPolicy.SuppressSpecifiers = false; 756 SubPolicy.SuppressTag = false; 757 Init->printPretty(Out, nullptr, SubPolicy, Indentation); 758 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 759 Out << ")"; 760 } 761 } 762 prettyPrintAttributes(D); 763 } 764 765 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { 766 VisitVarDecl(D); 767 } 768 769 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 770 Out << "__asm ("; 771 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation); 772 Out << ")"; 773 } 774 775 void DeclPrinter::VisitImportDecl(ImportDecl *D) { 776 Out << "@import " << D->getImportedModule()->getFullModuleName() 777 << ";\n"; 778 } 779 780 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { 781 Out << "static_assert("; 782 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation); 783 if (StringLiteral *SL = D->getMessage()) { 784 Out << ", "; 785 SL->printPretty(Out, nullptr, Policy, Indentation); 786 } 787 Out << ")"; 788 } 789 790 //---------------------------------------------------------------------------- 791 // C++ declarations 792 //---------------------------------------------------------------------------- 793 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { 794 if (D->isInline()) 795 Out << "inline "; 796 Out << "namespace " << *D << " {\n"; 797 VisitDeclContext(D); 798 Indent() << "}"; 799 } 800 801 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 802 Out << "using namespace "; 803 if (D->getQualifier()) 804 D->getQualifier()->print(Out, Policy); 805 Out << *D->getNominatedNamespaceAsWritten(); 806 } 807 808 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 809 Out << "namespace " << *D << " = "; 810 if (D->getQualifier()) 811 D->getQualifier()->print(Out, Policy); 812 Out << *D->getAliasedNamespace(); 813 } 814 815 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) { 816 prettyPrintAttributes(D); 817 } 818 819 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { 820 // FIXME: add printing of pragma attributes if required. 821 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 822 Out << "__module_private__ "; 823 Out << D->getKindName(); 824 825 prettyPrintAttributes(D); 826 827 if (D->getIdentifier()) 828 Out << ' ' << *D; 829 830 if (D->isCompleteDefinition()) { 831 // Print the base classes 832 if (D->getNumBases()) { 833 Out << " : "; 834 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), 835 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { 836 if (Base != D->bases_begin()) 837 Out << ", "; 838 839 if (Base->isVirtual()) 840 Out << "virtual "; 841 842 AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); 843 if (AS != AS_none) { 844 Print(AS); 845 Out << " "; 846 } 847 Out << Base->getType().getAsString(Policy); 848 849 if (Base->isPackExpansion()) 850 Out << "..."; 851 } 852 } 853 854 // Print the class definition 855 // FIXME: Doesn't print access specifiers, e.g., "public:" 856 Out << " {\n"; 857 VisitDeclContext(D); 858 Indent() << "}"; 859 } 860 } 861 862 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 863 const char *l; 864 if (D->getLanguage() == LinkageSpecDecl::lang_c) 865 l = "C"; 866 else { 867 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && 868 "unknown language in linkage specification"); 869 l = "C++"; 870 } 871 872 Out << "extern \"" << l << "\" "; 873 if (D->hasBraces()) { 874 Out << "{\n"; 875 VisitDeclContext(D); 876 Indent() << "}"; 877 } else 878 Visit(*D->decls_begin()); 879 } 880 881 void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params, 882 const TemplateArgumentList *Args) { 883 assert(Params); 884 assert(!Args || Params->size() == Args->size()); 885 886 Out << "template <"; 887 888 for (unsigned i = 0, e = Params->size(); i != e; ++i) { 889 if (i != 0) 890 Out << ", "; 891 892 const Decl *Param = Params->getParam(i); 893 if (const TemplateTypeParmDecl *TTP = 894 dyn_cast<TemplateTypeParmDecl>(Param)) { 895 896 if (TTP->wasDeclaredWithTypename()) 897 Out << "typename "; 898 else 899 Out << "class "; 900 901 if (TTP->isParameterPack()) 902 Out << "..."; 903 904 Out << *TTP; 905 906 if (Args) { 907 Out << " = "; 908 Args->get(i).print(Policy, Out); 909 } else if (TTP->hasDefaultArgument()) { 910 Out << " = "; 911 Out << TTP->getDefaultArgument().getAsString(Policy); 912 }; 913 } else if (const NonTypeTemplateParmDecl *NTTP = 914 dyn_cast<NonTypeTemplateParmDecl>(Param)) { 915 StringRef Name; 916 if (IdentifierInfo *II = NTTP->getIdentifier()) 917 Name = II->getName(); 918 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack()); 919 920 if (Args) { 921 Out << " = "; 922 Args->get(i).print(Policy, Out); 923 } else if (NTTP->hasDefaultArgument()) { 924 Out << " = "; 925 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, 926 Indentation); 927 } 928 } else if (const TemplateTemplateParmDecl *TTPD = 929 dyn_cast<TemplateTemplateParmDecl>(Param)) { 930 VisitTemplateDecl(TTPD); 931 // FIXME: print the default argument, if present. 932 } 933 } 934 935 Out << "> "; 936 } 937 938 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 939 PrintTemplateParameters(D->getTemplateParameters()); 940 941 if (const TemplateTemplateParmDecl *TTP = 942 dyn_cast<TemplateTemplateParmDecl>(D)) { 943 Out << "class "; 944 if (TTP->isParameterPack()) 945 Out << "..."; 946 Out << D->getName(); 947 } else { 948 Visit(D->getTemplatedDecl()); 949 } 950 } 951 952 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 953 if (PrintInstantiation) { 954 TemplateParameterList *Params = D->getTemplateParameters(); 955 for (auto *I : D->specializations()) { 956 prettyPrintPragmas(I); 957 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs()); 958 Visit(I); 959 } 960 } 961 962 prettyPrintPragmas(D->getTemplatedDecl()); 963 return VisitRedeclarableTemplateDecl(D); 964 } 965 966 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 967 if (PrintInstantiation) { 968 TemplateParameterList *Params = D->getTemplateParameters(); 969 for (auto *I : D->specializations()) { 970 PrintTemplateParameters(Params, &I->getTemplateArgs()); 971 Visit(I); 972 Out << '\n'; 973 } 974 } 975 976 return VisitRedeclarableTemplateDecl(D); 977 } 978 979 //---------------------------------------------------------------------------- 980 // Objective-C declarations 981 //---------------------------------------------------------------------------- 982 983 void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx, 984 Decl::ObjCDeclQualifier Quals, 985 QualType T) { 986 Out << '('; 987 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In) 988 Out << "in "; 989 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout) 990 Out << "inout "; 991 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out) 992 Out << "out "; 993 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy) 994 Out << "bycopy "; 995 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref) 996 Out << "byref "; 997 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway) 998 Out << "oneway "; 999 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) { 1000 if (auto nullability = AttributedType::stripOuterNullability(T)) 1001 Out << getNullabilitySpelling(*nullability, true) << ' '; 1002 } 1003 1004 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy); 1005 Out << ')'; 1006 } 1007 1008 void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) { 1009 Out << "<"; 1010 unsigned First = true; 1011 for (auto *Param : *Params) { 1012 if (First) { 1013 First = false; 1014 } else { 1015 Out << ", "; 1016 } 1017 1018 switch (Param->getVariance()) { 1019 case ObjCTypeParamVariance::Invariant: 1020 break; 1021 1022 case ObjCTypeParamVariance::Covariant: 1023 Out << "__covariant "; 1024 break; 1025 1026 case ObjCTypeParamVariance::Contravariant: 1027 Out << "__contravariant "; 1028 break; 1029 } 1030 1031 Out << Param->getDeclName().getAsString(); 1032 1033 if (Param->hasExplicitBound()) { 1034 Out << " : " << Param->getUnderlyingType().getAsString(Policy); 1035 } 1036 } 1037 Out << ">"; 1038 } 1039 1040 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { 1041 if (OMD->isInstanceMethod()) 1042 Out << "- "; 1043 else 1044 Out << "+ "; 1045 if (!OMD->getReturnType().isNull()) { 1046 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(), 1047 OMD->getReturnType()); 1048 } 1049 1050 std::string name = OMD->getSelector().getAsString(); 1051 std::string::size_type pos, lastPos = 0; 1052 for (const auto *PI : OMD->params()) { 1053 // FIXME: selector is missing here! 1054 pos = name.find_first_of(':', lastPos); 1055 Out << " " << name.substr(lastPos, pos - lastPos) << ':'; 1056 PrintObjCMethodType(OMD->getASTContext(), 1057 PI->getObjCDeclQualifier(), 1058 PI->getType()); 1059 Out << *PI; 1060 lastPos = pos + 1; 1061 } 1062 1063 if (OMD->param_begin() == OMD->param_end()) 1064 Out << " " << name; 1065 1066 if (OMD->isVariadic()) 1067 Out << ", ..."; 1068 1069 prettyPrintAttributes(OMD); 1070 1071 if (OMD->getBody() && !Policy.TerseOutput) { 1072 Out << ' '; 1073 OMD->getBody()->printPretty(Out, nullptr, Policy); 1074 } 1075 else if (Policy.PolishForDeclaration) 1076 Out << ';'; 1077 } 1078 1079 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { 1080 std::string I = OID->getNameAsString(); 1081 ObjCInterfaceDecl *SID = OID->getSuperClass(); 1082 1083 bool eolnOut = false; 1084 if (SID) 1085 Out << "@implementation " << I << " : " << *SID; 1086 else 1087 Out << "@implementation " << I; 1088 1089 if (OID->ivar_size() > 0) { 1090 Out << "{\n"; 1091 eolnOut = true; 1092 Indentation += Policy.Indentation; 1093 for (const auto *I : OID->ivars()) { 1094 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). 1095 getAsString(Policy) << ' ' << *I << ";\n"; 1096 } 1097 Indentation -= Policy.Indentation; 1098 Out << "}\n"; 1099 } 1100 else if (SID || (OID->decls_begin() != OID->decls_end())) { 1101 Out << "\n"; 1102 eolnOut = true; 1103 } 1104 VisitDeclContext(OID, false); 1105 if (!eolnOut) 1106 Out << "\n"; 1107 Out << "@end"; 1108 } 1109 1110 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { 1111 std::string I = OID->getNameAsString(); 1112 ObjCInterfaceDecl *SID = OID->getSuperClass(); 1113 1114 if (!OID->isThisDeclarationADefinition()) { 1115 Out << "@class " << I; 1116 1117 if (auto TypeParams = OID->getTypeParamListAsWritten()) { 1118 PrintObjCTypeParams(TypeParams); 1119 } 1120 1121 Out << ";"; 1122 return; 1123 } 1124 bool eolnOut = false; 1125 Out << "@interface " << I; 1126 1127 if (auto TypeParams = OID->getTypeParamListAsWritten()) { 1128 PrintObjCTypeParams(TypeParams); 1129 } 1130 1131 if (SID) 1132 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy); 1133 1134 // Protocols? 1135 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); 1136 if (!Protocols.empty()) { 1137 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1138 E = Protocols.end(); I != E; ++I) 1139 Out << (I == Protocols.begin() ? '<' : ',') << **I; 1140 Out << "> "; 1141 } 1142 1143 if (OID->ivar_size() > 0) { 1144 Out << "{\n"; 1145 eolnOut = true; 1146 Indentation += Policy.Indentation; 1147 for (const auto *I : OID->ivars()) { 1148 Indent() << I->getASTContext() 1149 .getUnqualifiedObjCPointerType(I->getType()) 1150 .getAsString(Policy) << ' ' << *I << ";\n"; 1151 } 1152 Indentation -= Policy.Indentation; 1153 Out << "}\n"; 1154 } 1155 else if (SID || (OID->decls_begin() != OID->decls_end())) { 1156 Out << "\n"; 1157 eolnOut = true; 1158 } 1159 1160 VisitDeclContext(OID, false); 1161 if (!eolnOut) 1162 Out << "\n"; 1163 Out << "@end"; 1164 // FIXME: implement the rest... 1165 } 1166 1167 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 1168 if (!PID->isThisDeclarationADefinition()) { 1169 Out << "@protocol " << *PID << ";\n"; 1170 return; 1171 } 1172 // Protocols? 1173 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols(); 1174 if (!Protocols.empty()) { 1175 Out << "@protocol " << *PID; 1176 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1177 E = Protocols.end(); I != E; ++I) 1178 Out << (I == Protocols.begin() ? '<' : ',') << **I; 1179 Out << ">\n"; 1180 } else 1181 Out << "@protocol " << *PID << '\n'; 1182 VisitDeclContext(PID, false); 1183 Out << "@end"; 1184 } 1185 1186 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { 1187 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; 1188 1189 VisitDeclContext(PID, false); 1190 Out << "@end"; 1191 // FIXME: implement the rest... 1192 } 1193 1194 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { 1195 Out << "@interface " << *PID->getClassInterface(); 1196 if (auto TypeParams = PID->getTypeParamList()) { 1197 PrintObjCTypeParams(TypeParams); 1198 } 1199 Out << "(" << *PID << ")\n"; 1200 if (PID->ivar_size() > 0) { 1201 Out << "{\n"; 1202 Indentation += Policy.Indentation; 1203 for (const auto *I : PID->ivars()) 1204 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). 1205 getAsString(Policy) << ' ' << *I << ";\n"; 1206 Indentation -= Policy.Indentation; 1207 Out << "}\n"; 1208 } 1209 1210 VisitDeclContext(PID, false); 1211 Out << "@end"; 1212 1213 // FIXME: implement the rest... 1214 } 1215 1216 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { 1217 Out << "@compatibility_alias " << *AID 1218 << ' ' << *AID->getClassInterface() << ";\n"; 1219 } 1220 1221 /// PrintObjCPropertyDecl - print a property declaration. 1222 /// 1223 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { 1224 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) 1225 Out << "@required\n"; 1226 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) 1227 Out << "@optional\n"; 1228 1229 QualType T = PDecl->getType(); 1230 1231 Out << "@property"; 1232 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { 1233 bool first = true; 1234 Out << " ("; 1235 if (PDecl->getPropertyAttributes() & 1236 ObjCPropertyDecl::OBJC_PR_readonly) { 1237 Out << (first ? ' ' : ',') << "readonly"; 1238 first = false; 1239 } 1240 1241 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 1242 Out << (first ? ' ' : ',') << "getter = "; 1243 PDecl->getGetterName().print(Out); 1244 first = false; 1245 } 1246 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 1247 Out << (first ? ' ' : ',') << "setter = "; 1248 PDecl->getSetterName().print(Out); 1249 first = false; 1250 } 1251 1252 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { 1253 Out << (first ? ' ' : ',') << "assign"; 1254 first = false; 1255 } 1256 1257 if (PDecl->getPropertyAttributes() & 1258 ObjCPropertyDecl::OBJC_PR_readwrite) { 1259 Out << (first ? ' ' : ',') << "readwrite"; 1260 first = false; 1261 } 1262 1263 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { 1264 Out << (first ? ' ' : ',') << "retain"; 1265 first = false; 1266 } 1267 1268 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { 1269 Out << (first ? ' ' : ',') << "strong"; 1270 first = false; 1271 } 1272 1273 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { 1274 Out << (first ? ' ' : ',') << "copy"; 1275 first = false; 1276 } 1277 1278 if (PDecl->getPropertyAttributes() & 1279 ObjCPropertyDecl::OBJC_PR_nonatomic) { 1280 Out << (first ? ' ' : ',') << "nonatomic"; 1281 first = false; 1282 } 1283 if (PDecl->getPropertyAttributes() & 1284 ObjCPropertyDecl::OBJC_PR_atomic) { 1285 Out << (first ? ' ' : ',') << "atomic"; 1286 first = false; 1287 } 1288 1289 if (PDecl->getPropertyAttributes() & 1290 ObjCPropertyDecl::OBJC_PR_nullability) { 1291 if (auto nullability = AttributedType::stripOuterNullability(T)) { 1292 if (*nullability == NullabilityKind::Unspecified && 1293 (PDecl->getPropertyAttributes() & 1294 ObjCPropertyDecl::OBJC_PR_null_resettable)) { 1295 Out << (first ? ' ' : ',') << "null_resettable"; 1296 } else { 1297 Out << (first ? ' ' : ',') 1298 << getNullabilitySpelling(*nullability, true); 1299 } 1300 first = false; 1301 } 1302 } 1303 1304 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) { 1305 Out << (first ? ' ' : ',') << "class"; 1306 first = false; 1307 } 1308 1309 (void) first; // Silence dead store warning due to idiomatic code. 1310 Out << " )"; 1311 } 1312 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T). 1313 getAsString(Policy) << ' ' << *PDecl; 1314 if (Policy.PolishForDeclaration) 1315 Out << ';'; 1316 } 1317 1318 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { 1319 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 1320 Out << "@synthesize "; 1321 else 1322 Out << "@dynamic "; 1323 Out << *PID->getPropertyDecl(); 1324 if (PID->getPropertyIvarDecl()) 1325 Out << '=' << *PID->getPropertyIvarDecl(); 1326 } 1327 1328 void DeclPrinter::VisitUsingDecl(UsingDecl *D) { 1329 if (!D->isAccessDeclaration()) 1330 Out << "using "; 1331 if (D->hasTypename()) 1332 Out << "typename "; 1333 D->getQualifier()->print(Out, Policy); 1334 Out << *D; 1335 } 1336 1337 void 1338 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 1339 Out << "using typename "; 1340 D->getQualifier()->print(Out, Policy); 1341 Out << D->getDeclName(); 1342 } 1343 1344 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1345 if (!D->isAccessDeclaration()) 1346 Out << "using "; 1347 D->getQualifier()->print(Out, Policy); 1348 Out << D->getDeclName(); 1349 } 1350 1351 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { 1352 // ignore 1353 } 1354 1355 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { 1356 Out << "#pragma omp threadprivate"; 1357 if (!D->varlist_empty()) { 1358 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(), 1359 E = D->varlist_end(); 1360 I != E; ++I) { 1361 Out << (I == D->varlist_begin() ? '(' : ','); 1362 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl()); 1363 ND->printQualifiedName(Out); 1364 } 1365 Out << ")"; 1366 } 1367 } 1368 1369