DeclPrinter.cpp 56 KB

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