DeclPrinter.cpp 55 KB

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