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