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; 508 if (!Policy.SuppressScope) { 509 if (const NestedNameSpecifier *NS = D->getQualifier()) { 510 llvm::raw_string_ostream OS(Proto); 511 NS->print(OS, Policy); 512 } 513 } 514 Proto += D->getNameInfo().getAsString(); 515 if (GuideDecl) 516 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString(); 517 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) { 518 llvm::raw_string_ostream POut(Proto); 519 DeclPrinter TArgPrinter(POut, SubPolicy, Indentation); 520 TArgPrinter.printTemplateArguments(*TArgs); 521 } 522 523 QualType Ty = D->getType(); 524 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 525 Proto = '(' + Proto + ')'; 526 Ty = PT->getInnerType(); 527 } 528 529 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) { 530 const FunctionProtoType *FT = nullptr; 531 if (D->hasWrittenPrototype()) 532 FT = dyn_cast<FunctionProtoType>(AFT); 533 534 Proto += "("; 535 if (FT) { 536 llvm::raw_string_ostream POut(Proto); 537 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation); 538 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 539 if (i) POut << ", "; 540 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 541 } 542 543 if (FT->isVariadic()) { 544 if (D->getNumParams()) POut << ", "; 545 POut << "..."; 546 } 547 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { 548 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 549 if (i) 550 Proto += ", "; 551 Proto += D->getParamDecl(i)->getNameAsString(); 552 } 553 } 554 555 Proto += ")"; 556 557 if (FT) { 558 if (FT->isConst()) 559 Proto += " const"; 560 if (FT->isVolatile()) 561 Proto += " volatile"; 562 if (FT->isRestrict()) 563 Proto += " restrict"; 564 565 switch (FT->getRefQualifier()) { 566 case RQ_None: 567 break; 568 case RQ_LValue: 569 Proto += " &"; 570 break; 571 case RQ_RValue: 572 Proto += " &&"; 573 break; 574 } 575 } 576 577 if (FT && FT->hasDynamicExceptionSpec()) { 578 Proto += " throw("; 579 if (FT->getExceptionSpecType() == EST_MSAny) 580 Proto += "..."; 581 else 582 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { 583 if (I) 584 Proto += ", "; 585 586 Proto += FT->getExceptionType(I).getAsString(SubPolicy); 587 } 588 Proto += ")"; 589 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { 590 Proto += " noexcept"; 591 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) { 592 Proto += "("; 593 llvm::raw_string_ostream EOut(Proto); 594 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy, 595 Indentation); 596 EOut.flush(); 597 Proto += EOut.str(); 598 Proto += ")"; 599 } 600 } 601 602 if (CDecl) { 603 bool HasInitializerList = false; 604 for (const auto *BMInitializer : CDecl->inits()) { 605 if (BMInitializer->isInClassMemberInitializer()) 606 continue; 607 608 if (!HasInitializerList) { 609 Proto += " : "; 610 Out << Proto; 611 Proto.clear(); 612 HasInitializerList = true; 613 } else 614 Out << ", "; 615 616 if (BMInitializer->isAnyMemberInitializer()) { 617 FieldDecl *FD = BMInitializer->getAnyMember(); 618 Out << *FD; 619 } else { 620 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); 621 } 622 623 Out << "("; 624 if (!BMInitializer->getInit()) { 625 // Nothing to print 626 } else { 627 Expr *Init = BMInitializer->getInit(); 628 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) 629 Init = Tmp->getSubExpr(); 630 631 Init = Init->IgnoreParens(); 632 633 Expr *SimpleInit = nullptr; 634 Expr **Args = nullptr; 635 unsigned NumArgs = 0; 636 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { 637 Args = ParenList->getExprs(); 638 NumArgs = ParenList->getNumExprs(); 639 } else if (CXXConstructExpr *Construct 640 = dyn_cast<CXXConstructExpr>(Init)) { 641 Args = Construct->getArgs(); 642 NumArgs = Construct->getNumArgs(); 643 } else 644 SimpleInit = Init; 645 646 if (SimpleInit) 647 SimpleInit->printPretty(Out, nullptr, Policy, Indentation); 648 else { 649 for (unsigned I = 0; I != NumArgs; ++I) { 650 assert(Args[I] != nullptr && "Expected non-null Expr"); 651 if (isa<CXXDefaultArgExpr>(Args[I])) 652 break; 653 654 if (I) 655 Out << ", "; 656 Args[I]->printPretty(Out, nullptr, Policy, Indentation); 657 } 658 } 659 } 660 Out << ")"; 661 if (BMInitializer->isPackExpansion()) 662 Out << "..."; 663 } 664 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) { 665 if (FT && FT->hasTrailingReturn()) { 666 if (!GuideDecl) 667 Out << "auto "; 668 Out << Proto << " -> "; 669 Proto.clear(); 670 } 671 AFT->getReturnType().print(Out, Policy, Proto); 672 Proto.clear(); 673 } 674 Out << Proto; 675 } else { 676 Ty.print(Out, Policy, Proto); 677 } 678 679 prettyPrintAttributes(D); 680 681 if (D->isPure()) 682 Out << " = 0"; 683 else if (D->isDeletedAsWritten()) 684 Out << " = delete"; 685 else if (D->isExplicitlyDefaulted()) 686 Out << " = default"; 687 else if (D->doesThisDeclarationHaveABody()) { 688 if (!Policy.TerseOutput) { 689 if (!D->hasPrototype() && D->getNumParams()) { 690 // This is a K&R function definition, so we need to print the 691 // parameters. 692 Out << '\n'; 693 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation); 694 Indentation += Policy.Indentation; 695 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 696 Indent(); 697 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 698 Out << ";\n"; 699 } 700 Indentation -= Policy.Indentation; 701 } else 702 Out << ' '; 703 704 if (D->getBody()) 705 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation); 706 } else { 707 if (isa<CXXConstructorDecl>(*D)) 708 Out << " {}"; 709 } 710 } 711 } 712 713 void DeclPrinter::VisitFriendDecl(FriendDecl *D) { 714 if (TypeSourceInfo *TSI = D->getFriendType()) { 715 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists(); 716 for (unsigned i = 0; i < NumTPLists; ++i) 717 printTemplateParameters(D->getFriendTypeTemplateParameterList(i)); 718 Out << "friend "; 719 Out << " " << TSI->getType().getAsString(Policy); 720 } 721 else if (FunctionDecl *FD = 722 dyn_cast<FunctionDecl>(D->getFriendDecl())) { 723 Out << "friend "; 724 VisitFunctionDecl(FD); 725 } 726 else if (FunctionTemplateDecl *FTD = 727 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) { 728 Out << "friend "; 729 VisitFunctionTemplateDecl(FTD); 730 } 731 else if (ClassTemplateDecl *CTD = 732 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) { 733 Out << "friend "; 734 VisitRedeclarableTemplateDecl(CTD); 735 } 736 } 737 738 void DeclPrinter::VisitFieldDecl(FieldDecl *D) { 739 // FIXME: add printing of pragma attributes if required. 740 if (!Policy.SuppressSpecifiers && D->isMutable()) 741 Out << "mutable "; 742 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 743 Out << "__module_private__ "; 744 745 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()). 746 stream(Policy, D->getName(), Indentation); 747 748 if (D->isBitField()) { 749 Out << " : "; 750 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation); 751 } 752 753 Expr *Init = D->getInClassInitializer(); 754 if (!Policy.SuppressInitializers && Init) { 755 if (D->getInClassInitStyle() == ICIS_ListInit) 756 Out << " "; 757 else 758 Out << " = "; 759 Init->printPretty(Out, nullptr, Policy, Indentation); 760 } 761 prettyPrintAttributes(D); 762 } 763 764 void DeclPrinter::VisitLabelDecl(LabelDecl *D) { 765 Out << *D << ":"; 766 } 767 768 void DeclPrinter::VisitVarDecl(VarDecl *D) { 769 prettyPrintPragmas(D); 770 771 QualType T = D->getTypeSourceInfo() 772 ? D->getTypeSourceInfo()->getType() 773 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType()); 774 775 if (!Policy.SuppressSpecifiers) { 776 StorageClass SC = D->getStorageClass(); 777 if (SC != SC_None) 778 Out << VarDecl::getStorageClassSpecifierString(SC) << " "; 779 780 switch (D->getTSCSpec()) { 781 case TSCS_unspecified: 782 break; 783 case TSCS___thread: 784 Out << "__thread "; 785 break; 786 case TSCS__Thread_local: 787 Out << "_Thread_local "; 788 break; 789 case TSCS_thread_local: 790 Out << "thread_local "; 791 break; 792 } 793 794 if (D->isModulePrivate()) 795 Out << "__module_private__ "; 796 797 if (D->isConstexpr()) { 798 Out << "constexpr "; 799 T.removeLocalConst(); 800 } 801 } 802 803 printDeclType(T, D->getName()); 804 Expr *Init = D->getInit(); 805 if (!Policy.SuppressInitializers && Init) { 806 bool ImplicitInit = false; 807 if (CXXConstructExpr *Construct = 808 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) { 809 if (D->getInitStyle() == VarDecl::CallInit && 810 !Construct->isListInitialization()) { 811 ImplicitInit = Construct->getNumArgs() == 0 || 812 Construct->getArg(0)->isDefaultArgument(); 813 } 814 } 815 if (!ImplicitInit) { 816 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 817 Out << "("; 818 else if (D->getInitStyle() == VarDecl::CInit) { 819 Out << " = "; 820 } 821 PrintingPolicy SubPolicy(Policy); 822 SubPolicy.SuppressSpecifiers = false; 823 SubPolicy.IncludeTagDefinition = false; 824 Init->printPretty(Out, nullptr, SubPolicy, Indentation); 825 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 826 Out << ")"; 827 } 828 } 829 prettyPrintAttributes(D); 830 } 831 832 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { 833 VisitVarDecl(D); 834 } 835 836 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 837 Out << "__asm ("; 838 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation); 839 Out << ")"; 840 } 841 842 void DeclPrinter::VisitImportDecl(ImportDecl *D) { 843 Out << "@import " << D->getImportedModule()->getFullModuleName() 844 << ";\n"; 845 } 846 847 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { 848 Out << "static_assert("; 849 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation); 850 if (StringLiteral *SL = D->getMessage()) { 851 Out << ", "; 852 SL->printPretty(Out, nullptr, Policy, Indentation); 853 } 854 Out << ")"; 855 } 856 857 //---------------------------------------------------------------------------- 858 // C++ declarations 859 //---------------------------------------------------------------------------- 860 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { 861 if (D->isInline()) 862 Out << "inline "; 863 Out << "namespace " << *D << " {\n"; 864 VisitDeclContext(D); 865 Indent() << "}"; 866 } 867 868 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 869 Out << "using namespace "; 870 if (D->getQualifier()) 871 D->getQualifier()->print(Out, Policy); 872 Out << *D->getNominatedNamespaceAsWritten(); 873 } 874 875 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 876 Out << "namespace " << *D << " = "; 877 if (D->getQualifier()) 878 D->getQualifier()->print(Out, Policy); 879 Out << *D->getAliasedNamespace(); 880 } 881 882 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) { 883 prettyPrintAttributes(D); 884 } 885 886 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { 887 // FIXME: add printing of pragma attributes if required. 888 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 889 Out << "__module_private__ "; 890 Out << D->getKindName(); 891 892 prettyPrintAttributes(D); 893 894 if (D->getIdentifier()) { 895 Out << ' ' << *D; 896 897 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) 898 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters()); 899 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) 900 printTemplateArguments(S->getTemplateArgs()); 901 } 902 903 if (D->isCompleteDefinition()) { 904 // Print the base classes 905 if (D->getNumBases()) { 906 Out << " : "; 907 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), 908 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { 909 if (Base != D->bases_begin()) 910 Out << ", "; 911 912 if (Base->isVirtual()) 913 Out << "virtual "; 914 915 AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); 916 if (AS != AS_none) { 917 Print(AS); 918 Out << " "; 919 } 920 Out << Base->getType().getAsString(Policy); 921 922 if (Base->isPackExpansion()) 923 Out << "..."; 924 } 925 } 926 927 // Print the class definition 928 // FIXME: Doesn't print access specifiers, e.g., "public:" 929 if (Policy.TerseOutput) { 930 Out << " {}"; 931 } else { 932 Out << " {\n"; 933 VisitDeclContext(D); 934 Indent() << "}"; 935 } 936 } 937 } 938 939 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 940 const char *l; 941 if (D->getLanguage() == LinkageSpecDecl::lang_c) 942 l = "C"; 943 else { 944 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && 945 "unknown language in linkage specification"); 946 l = "C++"; 947 } 948 949 Out << "extern \"" << l << "\" "; 950 if (D->hasBraces()) { 951 Out << "{\n"; 952 VisitDeclContext(D); 953 Indent() << "}"; 954 } else 955 Visit(*D->decls_begin()); 956 } 957 958 void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params) { 959 assert(Params); 960 961 Out << "template <"; 962 963 for (unsigned i = 0, e = Params->size(); i != e; ++i) { 964 if (i != 0) 965 Out << ", "; 966 967 const Decl *Param = Params->getParam(i); 968 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { 969 970 if (TTP->wasDeclaredWithTypename()) 971 Out << "typename "; 972 else 973 Out << "class "; 974 975 if (TTP->isParameterPack()) 976 Out << "..."; 977 978 Out << *TTP; 979 980 if (TTP->hasDefaultArgument()) { 981 Out << " = "; 982 Out << TTP->getDefaultArgument().getAsString(Policy); 983 }; 984 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 985 StringRef Name; 986 if (IdentifierInfo *II = NTTP->getIdentifier()) 987 Name = II->getName(); 988 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack()); 989 990 if (NTTP->hasDefaultArgument()) { 991 Out << " = "; 992 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, 993 Indentation); 994 } 995 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) { 996 VisitTemplateDecl(TTPD); 997 // FIXME: print the default argument, if present. 998 } 999 } 1000 1001 Out << "> "; 1002 } 1003 1004 void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args, 1005 const TemplateParameterList *Params) { 1006 Out << "<"; 1007 for (size_t I = 0, E = Args.size(); I < E; ++I) { 1008 const TemplateArgument &A = Args[I]; 1009 if (I) 1010 Out << ", "; 1011 if (Params) { 1012 if (A.getKind() == TemplateArgument::Type) 1013 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) { 1014 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex())); 1015 Out << *P; 1016 continue; 1017 } 1018 if (A.getKind() == TemplateArgument::Template) { 1019 if (auto T = A.getAsTemplate().getAsTemplateDecl()) 1020 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) { 1021 auto P = cast<TemplateTemplateParmDecl>( 1022 Params->getParam(TD->getIndex())); 1023 Out << *P; 1024 continue; 1025 } 1026 } 1027 if (A.getKind() == TemplateArgument::Expression) { 1028 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr())) 1029 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { 1030 auto P = cast<NonTypeTemplateParmDecl>( 1031 Params->getParam(N->getIndex())); 1032 Out << *P; 1033 continue; 1034 } 1035 } 1036 } 1037 A.print(Policy, Out); 1038 } 1039 Out << ">"; 1040 } 1041 1042 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 1043 printTemplateParameters(D->getTemplateParameters()); 1044 1045 if (const TemplateTemplateParmDecl *TTP = 1046 dyn_cast<TemplateTemplateParmDecl>(D)) { 1047 Out << "class "; 1048 if (TTP->isParameterPack()) 1049 Out << "..."; 1050 Out << D->getName(); 1051 } else { 1052 Visit(D->getTemplatedDecl()); 1053 } 1054 } 1055 1056 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 1057 prettyPrintPragmas(D->getTemplatedDecl()); 1058 VisitRedeclarableTemplateDecl(D); 1059 1060 // Never print "instantiations" for deduction guides (they don't really 1061 // have them). 1062 if (PrintInstantiation && 1063 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) { 1064 FunctionDecl *PrevDecl = D->getTemplatedDecl(); 1065 const FunctionDecl *Def; 1066 if (PrevDecl->isDefined(Def) && Def != PrevDecl) 1067 return; 1068 for (auto *I : D->specializations()) 1069 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) { 1070 if (!PrevDecl->isThisDeclarationADefinition()) 1071 Out << ";\n"; 1072 Indent(); 1073 prettyPrintPragmas(I); 1074 Visit(I); 1075 } 1076 } 1077 } 1078 1079 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 1080 VisitRedeclarableTemplateDecl(D); 1081 1082 if (PrintInstantiation) { 1083 for (auto *I : D->specializations()) 1084 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) { 1085 if (D->isThisDeclarationADefinition()) 1086 Out << ";"; 1087 Out << "\n"; 1088 Visit(I); 1089 } 1090 } 1091 } 1092 1093 void DeclPrinter::VisitClassTemplateSpecializationDecl( 1094 ClassTemplateSpecializationDecl *D) { 1095 Out << "template<> "; 1096 VisitCXXRecordDecl(D); 1097 } 1098 1099 void DeclPrinter::VisitClassTemplatePartialSpecializationDecl( 1100 ClassTemplatePartialSpecializationDecl *D) { 1101 printTemplateParameters(D->getTemplateParameters()); 1102 VisitCXXRecordDecl(D); 1103 } 1104 1105 //---------------------------------------------------------------------------- 1106 // Objective-C declarations 1107 //---------------------------------------------------------------------------- 1108 1109 void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx, 1110 Decl::ObjCDeclQualifier Quals, 1111 QualType T) { 1112 Out << '('; 1113 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In) 1114 Out << "in "; 1115 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout) 1116 Out << "inout "; 1117 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out) 1118 Out << "out "; 1119 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy) 1120 Out << "bycopy "; 1121 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref) 1122 Out << "byref "; 1123 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway) 1124 Out << "oneway "; 1125 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) { 1126 if (auto nullability = AttributedType::stripOuterNullability(T)) 1127 Out << getNullabilitySpelling(*nullability, true) << ' '; 1128 } 1129 1130 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy); 1131 Out << ')'; 1132 } 1133 1134 void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) { 1135 Out << "<"; 1136 unsigned First = true; 1137 for (auto *Param : *Params) { 1138 if (First) { 1139 First = false; 1140 } else { 1141 Out << ", "; 1142 } 1143 1144 switch (Param->getVariance()) { 1145 case ObjCTypeParamVariance::Invariant: 1146 break; 1147 1148 case ObjCTypeParamVariance::Covariant: 1149 Out << "__covariant "; 1150 break; 1151 1152 case ObjCTypeParamVariance::Contravariant: 1153 Out << "__contravariant "; 1154 break; 1155 } 1156 1157 Out << Param->getDeclName().getAsString(); 1158 1159 if (Param->hasExplicitBound()) { 1160 Out << " : " << Param->getUnderlyingType().getAsString(Policy); 1161 } 1162 } 1163 Out << ">"; 1164 } 1165 1166 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { 1167 if (OMD->isInstanceMethod()) 1168 Out << "- "; 1169 else 1170 Out << "+ "; 1171 if (!OMD->getReturnType().isNull()) { 1172 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(), 1173 OMD->getReturnType()); 1174 } 1175 1176 std::string name = OMD->getSelector().getAsString(); 1177 std::string::size_type pos, lastPos = 0; 1178 for (const auto *PI : OMD->parameters()) { 1179 // FIXME: selector is missing here! 1180 pos = name.find_first_of(':', lastPos); 1181 Out << " " << name.substr(lastPos, pos - lastPos) << ':'; 1182 PrintObjCMethodType(OMD->getASTContext(), 1183 PI->getObjCDeclQualifier(), 1184 PI->getType()); 1185 Out << *PI; 1186 lastPos = pos + 1; 1187 } 1188 1189 if (OMD->param_begin() == OMD->param_end()) 1190 Out << " " << name; 1191 1192 if (OMD->isVariadic()) 1193 Out << ", ..."; 1194 1195 prettyPrintAttributes(OMD); 1196 1197 if (OMD->getBody() && !Policy.TerseOutput) { 1198 Out << ' '; 1199 OMD->getBody()->printPretty(Out, nullptr, Policy); 1200 } 1201 else if (Policy.PolishForDeclaration) 1202 Out << ';'; 1203 } 1204 1205 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { 1206 std::string I = OID->getNameAsString(); 1207 ObjCInterfaceDecl *SID = OID->getSuperClass(); 1208 1209 bool eolnOut = false; 1210 if (SID) 1211 Out << "@implementation " << I << " : " << *SID; 1212 else 1213 Out << "@implementation " << I; 1214 1215 if (OID->ivar_size() > 0) { 1216 Out << "{\n"; 1217 eolnOut = true; 1218 Indentation += Policy.Indentation; 1219 for (const auto *I : OID->ivars()) { 1220 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). 1221 getAsString(Policy) << ' ' << *I << ";\n"; 1222 } 1223 Indentation -= Policy.Indentation; 1224 Out << "}\n"; 1225 } 1226 else if (SID || (OID->decls_begin() != OID->decls_end())) { 1227 Out << "\n"; 1228 eolnOut = true; 1229 } 1230 VisitDeclContext(OID, false); 1231 if (!eolnOut) 1232 Out << "\n"; 1233 Out << "@end"; 1234 } 1235 1236 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { 1237 std::string I = OID->getNameAsString(); 1238 ObjCInterfaceDecl *SID = OID->getSuperClass(); 1239 1240 if (!OID->isThisDeclarationADefinition()) { 1241 Out << "@class " << I; 1242 1243 if (auto TypeParams = OID->getTypeParamListAsWritten()) { 1244 PrintObjCTypeParams(TypeParams); 1245 } 1246 1247 Out << ";"; 1248 return; 1249 } 1250 bool eolnOut = false; 1251 Out << "@interface " << I; 1252 1253 if (auto TypeParams = OID->getTypeParamListAsWritten()) { 1254 PrintObjCTypeParams(TypeParams); 1255 } 1256 1257 if (SID) 1258 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy); 1259 1260 // Protocols? 1261 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); 1262 if (!Protocols.empty()) { 1263 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1264 E = Protocols.end(); I != E; ++I) 1265 Out << (I == Protocols.begin() ? '<' : ',') << **I; 1266 Out << "> "; 1267 } 1268 1269 if (OID->ivar_size() > 0) { 1270 Out << "{\n"; 1271 eolnOut = true; 1272 Indentation += Policy.Indentation; 1273 for (const auto *I : OID->ivars()) { 1274 Indent() << I->getASTContext() 1275 .getUnqualifiedObjCPointerType(I->getType()) 1276 .getAsString(Policy) << ' ' << *I << ";\n"; 1277 } 1278 Indentation -= Policy.Indentation; 1279 Out << "}\n"; 1280 } 1281 else if (SID || (OID->decls_begin() != OID->decls_end())) { 1282 Out << "\n"; 1283 eolnOut = true; 1284 } 1285 1286 VisitDeclContext(OID, false); 1287 if (!eolnOut) 1288 Out << "\n"; 1289 Out << "@end"; 1290 // FIXME: implement the rest... 1291 } 1292 1293 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 1294 if (!PID->isThisDeclarationADefinition()) { 1295 Out << "@protocol " << *PID << ";\n"; 1296 return; 1297 } 1298 // Protocols? 1299 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols(); 1300 if (!Protocols.empty()) { 1301 Out << "@protocol " << *PID; 1302 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1303 E = Protocols.end(); I != E; ++I) 1304 Out << (I == Protocols.begin() ? '<' : ',') << **I; 1305 Out << ">\n"; 1306 } else 1307 Out << "@protocol " << *PID << '\n'; 1308 VisitDeclContext(PID, false); 1309 Out << "@end"; 1310 } 1311 1312 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { 1313 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; 1314 1315 VisitDeclContext(PID, false); 1316 Out << "@end"; 1317 // FIXME: implement the rest... 1318 } 1319 1320 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { 1321 Out << "@interface " << *PID->getClassInterface(); 1322 if (auto TypeParams = PID->getTypeParamList()) { 1323 PrintObjCTypeParams(TypeParams); 1324 } 1325 Out << "(" << *PID << ")\n"; 1326 if (PID->ivar_size() > 0) { 1327 Out << "{\n"; 1328 Indentation += Policy.Indentation; 1329 for (const auto *I : PID->ivars()) 1330 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). 1331 getAsString(Policy) << ' ' << *I << ";\n"; 1332 Indentation -= Policy.Indentation; 1333 Out << "}\n"; 1334 } 1335 1336 VisitDeclContext(PID, false); 1337 Out << "@end"; 1338 1339 // FIXME: implement the rest... 1340 } 1341 1342 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { 1343 Out << "@compatibility_alias " << *AID 1344 << ' ' << *AID->getClassInterface() << ";\n"; 1345 } 1346 1347 /// PrintObjCPropertyDecl - print a property declaration. 1348 /// 1349 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { 1350 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) 1351 Out << "@required\n"; 1352 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) 1353 Out << "@optional\n"; 1354 1355 QualType T = PDecl->getType(); 1356 1357 Out << "@property"; 1358 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { 1359 bool first = true; 1360 Out << " ("; 1361 if (PDecl->getPropertyAttributes() & 1362 ObjCPropertyDecl::OBJC_PR_readonly) { 1363 Out << (first ? ' ' : ',') << "readonly"; 1364 first = false; 1365 } 1366 1367 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 1368 Out << (first ? ' ' : ',') << "getter = "; 1369 PDecl->getGetterName().print(Out); 1370 first = false; 1371 } 1372 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 1373 Out << (first ? ' ' : ',') << "setter = "; 1374 PDecl->getSetterName().print(Out); 1375 first = false; 1376 } 1377 1378 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { 1379 Out << (first ? ' ' : ',') << "assign"; 1380 first = false; 1381 } 1382 1383 if (PDecl->getPropertyAttributes() & 1384 ObjCPropertyDecl::OBJC_PR_readwrite) { 1385 Out << (first ? ' ' : ',') << "readwrite"; 1386 first = false; 1387 } 1388 1389 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { 1390 Out << (first ? ' ' : ',') << "retain"; 1391 first = false; 1392 } 1393 1394 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { 1395 Out << (first ? ' ' : ',') << "strong"; 1396 first = false; 1397 } 1398 1399 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { 1400 Out << (first ? ' ' : ',') << "copy"; 1401 first = false; 1402 } 1403 1404 if (PDecl->getPropertyAttributes() & 1405 ObjCPropertyDecl::OBJC_PR_nonatomic) { 1406 Out << (first ? ' ' : ',') << "nonatomic"; 1407 first = false; 1408 } 1409 if (PDecl->getPropertyAttributes() & 1410 ObjCPropertyDecl::OBJC_PR_atomic) { 1411 Out << (first ? ' ' : ',') << "atomic"; 1412 first = false; 1413 } 1414 1415 if (PDecl->getPropertyAttributes() & 1416 ObjCPropertyDecl::OBJC_PR_nullability) { 1417 if (auto nullability = AttributedType::stripOuterNullability(T)) { 1418 if (*nullability == NullabilityKind::Unspecified && 1419 (PDecl->getPropertyAttributes() & 1420 ObjCPropertyDecl::OBJC_PR_null_resettable)) { 1421 Out << (first ? ' ' : ',') << "null_resettable"; 1422 } else { 1423 Out << (first ? ' ' : ',') 1424 << getNullabilitySpelling(*nullability, true); 1425 } 1426 first = false; 1427 } 1428 } 1429 1430 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) { 1431 Out << (first ? ' ' : ',') << "class"; 1432 first = false; 1433 } 1434 1435 (void) first; // Silence dead store warning due to idiomatic code. 1436 Out << " )"; 1437 } 1438 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T). 1439 getAsString(Policy) << ' ' << *PDecl; 1440 if (Policy.PolishForDeclaration) 1441 Out << ';'; 1442 } 1443 1444 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { 1445 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 1446 Out << "@synthesize "; 1447 else 1448 Out << "@dynamic "; 1449 Out << *PID->getPropertyDecl(); 1450 if (PID->getPropertyIvarDecl()) 1451 Out << '=' << *PID->getPropertyIvarDecl(); 1452 } 1453 1454 void DeclPrinter::VisitUsingDecl(UsingDecl *D) { 1455 if (!D->isAccessDeclaration()) 1456 Out << "using "; 1457 if (D->hasTypename()) 1458 Out << "typename "; 1459 D->getQualifier()->print(Out, Policy); 1460 1461 // Use the correct record name when the using declaration is used for 1462 // inheriting constructors. 1463 for (const auto *Shadow : D->shadows()) { 1464 if (const auto *ConstructorShadow = 1465 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) { 1466 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext()); 1467 Out << *ConstructorShadow->getNominatedBaseClass(); 1468 return; 1469 } 1470 } 1471 Out << *D; 1472 } 1473 1474 void 1475 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 1476 Out << "using typename "; 1477 D->getQualifier()->print(Out, Policy); 1478 Out << D->getDeclName(); 1479 } 1480 1481 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1482 if (!D->isAccessDeclaration()) 1483 Out << "using "; 1484 D->getQualifier()->print(Out, Policy); 1485 Out << D->getDeclName(); 1486 } 1487 1488 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { 1489 // ignore 1490 } 1491 1492 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { 1493 Out << "#pragma omp threadprivate"; 1494 if (!D->varlist_empty()) { 1495 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(), 1496 E = D->varlist_end(); 1497 I != E; ++I) { 1498 Out << (I == D->varlist_begin() ? '(' : ','); 1499 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl()); 1500 ND->printQualifiedName(Out); 1501 } 1502 Out << ")"; 1503 } 1504 } 1505 1506 void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { 1507 if (!D->isInvalidDecl()) { 1508 Out << "#pragma omp declare reduction ("; 1509 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) { 1510 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = { 1511 nullptr, 1512 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ 1513 Spelling, 1514 #include "clang/Basic/OperatorKinds.def" 1515 }; 1516 const char *OpName = 1517 OperatorNames[D->getDeclName().getCXXOverloadedOperator()]; 1518 assert(OpName && "not an overloaded operator"); 1519 Out << OpName; 1520 } else { 1521 assert(D->getDeclName().isIdentifier()); 1522 D->printName(Out); 1523 } 1524 Out << " : "; 1525 D->getType().print(Out, Policy); 1526 Out << " : "; 1527 D->getCombiner()->printPretty(Out, nullptr, Policy, 0); 1528 Out << ")"; 1529 if (auto *Init = D->getInitializer()) { 1530 Out << " initializer("; 1531 Init->printPretty(Out, nullptr, Policy, 0); 1532 Out << ")"; 1533 } 1534 } 1535 } 1536 1537 void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) { 1538 D->getInit()->printPretty(Out, nullptr, Policy, Indentation); 1539 } 1540 1541