DeclPrinter.cpp 52 KB

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