blob: f5c69944034a5e73c38c095f21edc05b168d6618 [file] [log] [blame]
Douglas Gregor278f52e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Douglas Gregor278f52e2009-05-30 00:08:05 +00006//
7//===----------------------------------------------------------------------===//
8//
Alexander Kornienko90ff6072012-12-20 02:09:13 +00009// This file implements the Decl::print method, which pretty prints the
Douglas Gregor278f52e2009-05-30 00:08:05 +000010// AST back out to C/Objective-C/C++/Objective-C++ code.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000014#include "clang/AST/Attr.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000015#include "clang/AST/Decl.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclObjC.h"
Hamza Sood8205a812019-05-04 10:49:46 +000018#include "clang/AST/DeclTemplate.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000020#include "clang/AST/Expr.h"
Douglas Gregor7ae2d772010-01-31 09:12:51 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000022#include "clang/AST/PrettyPrinter.h"
Douglas Gregorba345522011-12-02 23:23:56 +000023#include "clang/Basic/Module.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000024#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26
27namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000028 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000029 raw_ostream &Out;
Douglas Gregor278f52e2009-05-30 00:08:05 +000030 PrintingPolicy Policy;
Alex Lorenz36070ed2017-08-17 13:41:55 +000031 const ASTContext &Context;
Douglas Gregor278f52e2009-05-30 00:08:05 +000032 unsigned Indentation;
Richard Trieu82398f92011-07-28 00:19:05 +000033 bool PrintInstantiation;
Douglas Gregor278f52e2009-05-30 00:08:05 +000034
Chris Lattner0e62c1c2011-07-23 10:55:15 +000035 raw_ostream& Indent() { return Indent(Indentation); }
36 raw_ostream& Indent(unsigned Indentation);
37 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
Douglas Gregor278f52e2009-05-30 00:08:05 +000038
Anders Carlsson601d6e42009-08-28 22:39:52 +000039 void Print(AccessSpecifier AS);
Alex Lorenzdc616fa2017-11-16 01:31:27 +000040 void PrintConstructorInitializers(CXXConstructorDecl *CDecl,
41 std::string &Proto);
Mike Stump11289f42009-09-09 15:08:12 +000042
Douglas Gregor813a0662015-06-19 18:14:38 +000043 /// Print an Objective-C method type in parentheses.
44 ///
45 /// \param Quals The Objective-C declaration qualifiers.
46 /// \param T The type to print.
Fangrui Song6907ce22018-07-30 19:24:48 +000047 void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
Douglas Gregor813a0662015-06-19 18:14:38 +000048 QualType T);
49
Douglas Gregor85f3f952015-07-07 03:57:15 +000050 void PrintObjCTypeParams(ObjCTypeParamList *Params);
51
Douglas Gregor278f52e2009-05-30 00:08:05 +000052 public:
Richard Smith235341b2012-08-16 03:56:14 +000053 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
Alex Lorenz36070ed2017-08-17 13:41:55 +000054 const ASTContext &Context, unsigned Indentation = 0,
55 bool PrintInstantiation = false)
56 : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation),
57 PrintInstantiation(PrintInstantiation) {}
Douglas Gregor278f52e2009-05-30 00:08:05 +000058
59 void VisitDeclContext(DeclContext *DC, bool Indent = true);
60
61 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
62 void VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +000063 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000064 void VisitEnumDecl(EnumDecl *D);
65 void VisitRecordDecl(RecordDecl *D);
66 void VisitEnumConstantDecl(EnumConstantDecl *D);
Michael Han84324352013-02-22 17:15:32 +000067 void VisitEmptyDecl(EmptyDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000068 void VisitFunctionDecl(FunctionDecl *D);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +000069 void VisitFriendDecl(FriendDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000070 void VisitFieldDecl(FieldDecl *D);
71 void VisitVarDecl(VarDecl *D);
Chris Lattnercab02a62011-02-17 20:34:02 +000072 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000073 void VisitParmVarDecl(ParmVarDecl *D);
74 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregorba345522011-12-02 23:23:56 +000075 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +000076 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000077 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000078 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000079 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000080 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000081 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith030f4992011-04-15 13:38:57 +000082 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000083 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
84 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Serge Pavlova67a4d22016-11-10 08:49:37 +000085 void VisitClassTemplateSpecializationDecl(
86 ClassTemplateSpecializationDecl *D);
87 void VisitClassTemplatePartialSpecializationDecl(
88 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000089 void VisitObjCMethodDecl(ObjCMethodDecl *D);
90 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
91 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000092 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
93 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
94 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
95 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
96 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
97 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000098 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
99 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000100 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +0000101 void VisitUsingShadowDecl(UsingShadowDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000102 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000103 void VisitOMPAllocateDecl(OMPAllocateDecl *D);
Kelvin Li1408f912018-09-26 04:28:39 +0000104 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000105 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Michael Kruse251e1482019-02-01 20:25:04 +0000106 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000107 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +0000108
Hamza Sood8205a812019-05-04 10:49:46 +0000109 void printTemplateParameters(const TemplateParameterList *Params,
110 bool OmitTemplateKW = false);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000111 void printTemplateArguments(const TemplateArgumentList &Args,
112 const TemplateParameterList *Params = nullptr);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000113 void prettyPrintAttributes(Decl *D);
Alexey Bataev6d455322015-10-12 06:59:48 +0000114 void prettyPrintPragmas(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +0000115 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000116 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000117}
Douglas Gregor278f52e2009-05-30 00:08:05 +0000118
Richard Trieu82398f92011-07-28 00:19:05 +0000119void Decl::print(raw_ostream &Out, unsigned Indentation,
120 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +0000121 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000122}
123
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000124void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000125 unsigned Indentation, bool PrintInstantiation) const {
Alex Lorenz36070ed2017-08-17 13:41:55 +0000126 DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
127 PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000128 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000129}
130
Hamza Sood8205a812019-05-04 10:49:46 +0000131void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
132 bool OmitTemplateKW) const {
133 print(Out, Context, Context.getPrintingPolicy(), OmitTemplateKW);
134}
135
136void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
137 const PrintingPolicy &Policy,
138 bool OmitTemplateKW) const {
139 DeclPrinter Printer(Out, Policy, Context);
140 Printer.printTemplateParameters(this, OmitTemplateKW);
141}
142
Eli Friedman79635842009-05-30 04:20:30 +0000143static QualType GetBaseType(QualType T) {
144 // FIXME: This should be on the Type class!
145 QualType BaseType = T;
146 while (!BaseType->isSpecifierType()) {
Artem Belevich224879e2018-01-17 19:29:39 +0000147 if (const PointerType *PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000148 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000149 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
150 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000151 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
152 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000153 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000154 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000155 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000156 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000157 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
158 BaseType = RTy->getPointeeType();
Vassil Vassilevcdaa31f2016-07-08 16:04:22 +0000159 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
160 BaseType = ATy->getDeducedType();
Artem Belevich224879e2018-01-17 19:29:39 +0000161 else if (const ParenType *PTy = BaseType->getAs<ParenType>())
162 BaseType = PTy->desugar();
Eli Friedman79635842009-05-30 04:20:30 +0000163 else
Artem Belevich224879e2018-01-17 19:29:39 +0000164 // This must be a syntax error.
165 break;
Eli Friedman79635842009-05-30 04:20:30 +0000166 }
167 return BaseType;
168}
169
170static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000171 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000172 return TDD->getUnderlyingType();
173 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
174 return VD->getType();
175 return QualType();
176}
177
178void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000179 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000180 unsigned Indentation) {
181 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000182 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000183 return;
184 }
185
186 Decl** End = Begin + NumDecls;
187 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
188 if (TD)
189 ++Begin;
190
191 PrintingPolicy SubPolicy(Policy);
Eli Friedman79635842009-05-30 04:20:30 +0000192
193 bool isFirst = true;
194 for ( ; Begin != End; ++Begin) {
195 if (isFirst) {
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000196 if(TD)
197 SubPolicy.IncludeTagDefinition = true;
Eli Friedman79635842009-05-30 04:20:30 +0000198 SubPolicy.SuppressSpecifiers = false;
199 isFirst = false;
200 } else {
201 if (!isFirst) Out << ", ";
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000202 SubPolicy.IncludeTagDefinition = false;
Eli Friedman79635842009-05-30 04:20:30 +0000203 SubPolicy.SuppressSpecifiers = true;
204 }
205
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000206 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000207 }
208}
209
Alp Tokeref6b0072014-01-04 13:47:14 +0000210LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000211 // Get the translation unit
212 const DeclContext *DC = this;
213 while (!DC->isTranslationUnit())
214 DC = DC->getParent();
Fangrui Song6907ce22018-07-30 19:24:48 +0000215
Anders Carlsson538af092009-12-09 17:27:46 +0000216 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Alex Lorenz36070ed2017-08-17 13:41:55 +0000217 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000218 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
219}
220
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000221raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000222 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000223 Out << " ";
224 return Out;
225}
226
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000227void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000228 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000229 return;
Alexey Bataev6d455322015-10-12 06:59:48 +0000230
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000231 if (D->hasAttrs()) {
232 AttrVec &Attrs = D->getAttrs();
Alexey Bataev6d455322015-10-12 06:59:48 +0000233 for (auto *A : Attrs) {
Joel E. Denny94548642018-05-15 22:16:47 +0000234 if (A->isInherited() || A->isImplicit())
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000235 continue;
Alexey Bataev6d455322015-10-12 06:59:48 +0000236 switch (A->getKind()) {
237#define ATTR(X)
238#define PRAGMA_SPELLING_ATTR(X) case attr::X:
239#include "clang/Basic/AttrList.inc"
240 break;
241 default:
242 A->printPretty(Out, Policy);
243 break;
244 }
245 }
246 }
247}
248
249void DeclPrinter::prettyPrintPragmas(Decl *D) {
250 if (Policy.PolishForDeclaration)
251 return;
252
253 if (D->hasAttrs()) {
254 AttrVec &Attrs = D->getAttrs();
255 for (auto *A : Attrs) {
256 switch (A->getKind()) {
257#define ATTR(X)
258#define PRAGMA_SPELLING_ATTR(X) case attr::X:
259#include "clang/Basic/AttrList.inc"
260 A->printPretty(Out, Policy);
261 Indent();
262 break;
263 default:
264 break;
265 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000266 }
267 }
268}
269
Richard Smitha4bb2922014-07-23 03:17:06 +0000270void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
271 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
272 // template argument), but if it is the type of a declaration, the ellipsis
273 // is placed before the name being declared.
274 if (auto *PET = T->getAs<PackExpansionType>()) {
275 Pack = true;
276 T = PET->getPattern();
277 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000278 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
Richard Smitha4bb2922014-07-23 03:17:06 +0000279}
280
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000281void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000282 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000283 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000284 Out << ";\n";
285 Decls.clear();
286
287}
288
Anders Carlsson601d6e42009-08-28 22:39:52 +0000289void DeclPrinter::Print(AccessSpecifier AS) {
290 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000291 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000292 case AS_public: Out << "public"; break;
293 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000294 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000295 }
296}
297
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000298void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
299 std::string &Proto) {
300 bool HasInitializerList = false;
301 for (const auto *BMInitializer : CDecl->inits()) {
302 if (BMInitializer->isInClassMemberInitializer())
303 continue;
304
305 if (!HasInitializerList) {
306 Proto += " : ";
307 Out << Proto;
308 Proto.clear();
309 HasInitializerList = true;
310 } else
311 Out << ", ";
312
313 if (BMInitializer->isAnyMemberInitializer()) {
314 FieldDecl *FD = BMInitializer->getAnyMember();
315 Out << *FD;
316 } else {
317 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
318 }
319
320 Out << "(";
321 if (!BMInitializer->getInit()) {
322 // Nothing to print
323 } else {
324 Expr *Init = BMInitializer->getInit();
325 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
326 Init = Tmp->getSubExpr();
327
328 Init = Init->IgnoreParens();
329
330 Expr *SimpleInit = nullptr;
331 Expr **Args = nullptr;
332 unsigned NumArgs = 0;
333 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
334 Args = ParenList->getExprs();
335 NumArgs = ParenList->getNumExprs();
336 } else if (CXXConstructExpr *Construct =
337 dyn_cast<CXXConstructExpr>(Init)) {
338 Args = Construct->getArgs();
339 NumArgs = Construct->getNumArgs();
340 } else
341 SimpleInit = Init;
342
343 if (SimpleInit)
344 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
345 else {
346 for (unsigned I = 0; I != NumArgs; ++I) {
347 assert(Args[I] != nullptr && "Expected non-null Expr");
348 if (isa<CXXDefaultArgExpr>(Args[I]))
349 break;
350
351 if (I)
352 Out << ", ";
353 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
354 }
355 }
356 }
357 Out << ")";
358 if (BMInitializer->isPackExpansion())
359 Out << "...";
360 }
361}
362
Douglas Gregor278f52e2009-05-30 00:08:05 +0000363//----------------------------------------------------------------------------
364// Common C declarations
365//----------------------------------------------------------------------------
366
367void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000368 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000369 return;
370
Douglas Gregor278f52e2009-05-30 00:08:05 +0000371 if (Indent)
372 Indentation += Policy.Indentation;
373
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000374 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000375 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000376 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000377
378 // Don't print ObjCIvarDecls, as they are printed when visiting the
379 // containing ObjCInterfaceDecl.
380 if (isa<ObjCIvarDecl>(*D))
381 continue;
382
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000383 // Skip over implicit declarations in pretty-printing mode.
384 if (D->isImplicit())
385 continue;
386
Serge Pavlova67a4d22016-11-10 08:49:37 +0000387 // Don't print implicit specializations, as they are printed when visiting
388 // corresponding templates.
389 if (auto FD = dyn_cast<FunctionDecl>(*D))
390 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
391 !isa<ClassTemplateSpecializationDecl>(DC))
392 continue;
393
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000394 // The next bits of code handle stuff like "struct {int x;} a,b"; we're
Eli Friedman79635842009-05-30 04:20:30 +0000395 // forced to merge the declarations because there's no other way to
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000396 // refer to the struct in question. When that struct is named instead, we
397 // also need to merge to avoid splitting off a stand-alone struct
398 // declaration that produces the warning ext_no_declarators in some
399 // contexts.
400 //
401 // This limited merging is safe without a bunch of other checks because it
402 // only merges declarations directly referring to the tag, not typedefs.
Eli Friedman79635842009-05-30 04:20:30 +0000403 //
404 // Check whether the current declaration should be grouped with a previous
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000405 // non-free-standing tag declaration.
Eli Friedman79635842009-05-30 04:20:30 +0000406 QualType CurDeclType = getDeclType(*D);
407 if (!Decls.empty() && !CurDeclType.isNull()) {
408 QualType BaseType = GetBaseType(CurDeclType);
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000409 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) &&
410 cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]) {
Eli Friedman79635842009-05-30 04:20:30 +0000411 Decls.push_back(*D);
412 continue;
413 }
414 }
415
416 // If we have a merged group waiting to be handled, handle it now.
417 if (!Decls.empty())
418 ProcessDeclGroup(Decls);
419
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000420 // If the current declaration is not a free standing declaration, save it
Eli Friedman79635842009-05-30 04:20:30 +0000421 // so we can merge it with the subsequent declaration(s) using it.
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000422 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->isFreeStanding()) {
Eli Friedman79635842009-05-30 04:20:30 +0000423 Decls.push_back(*D);
424 continue;
425 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000426
427 if (isa<AccessSpecDecl>(*D)) {
428 Indentation -= Policy.Indentation;
429 this->Indent();
430 Print(D->getAccess());
431 Out << ":\n";
432 Indentation += Policy.Indentation;
433 continue;
434 }
435
Douglas Gregor278f52e2009-05-30 00:08:05 +0000436 this->Indent();
437 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000438
439 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000440 const char *Terminator = nullptr;
Kelvin Li1408f912018-09-26 04:28:39 +0000441 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D) ||
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000442 isa<OMPDeclareMapperDecl>(*D) || isa<OMPRequiresDecl>(*D) ||
443 isa<OMPAllocateDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000444 Terminator = nullptr;
Serge Pavlovdc586c42016-10-31 05:11:12 +0000445 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000446 Terminator = nullptr;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000447 else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
448 if (FD->isThisDeclarationADefinition())
449 Terminator = nullptr;
450 else
451 Terminator = ";";
452 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
453 if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
454 Terminator = nullptr;
455 else
456 Terminator = ";";
457 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000458 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000459 isa<ObjCInterfaceDecl>(*D) ||
460 isa<ObjCProtocolDecl>(*D) ||
461 isa<ObjCCategoryImplDecl>(*D) ||
462 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000463 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000464 else if (isa<EnumConstantDecl>(*D)) {
465 DeclContext::decl_iterator Next = D;
466 ++Next;
467 if (Next != DEnd)
468 Terminator = ",";
469 } else
470 Terminator = ";";
471
472 if (Terminator)
473 Out << Terminator;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000474 if (!Policy.TerseOutput &&
475 ((isa<FunctionDecl>(*D) &&
476 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
477 (isa<FunctionTemplateDecl>(*D) &&
478 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
479 ; // StmtPrinter already added '\n' after CompoundStmt.
480 else
481 Out << "\n";
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000482
483 // Declare target attribute is special one, natural spelling for the pragma
484 // assumes "ending" construct so print it here.
485 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
486 Out << "#pragma omp end declare target\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000487 }
488
Eli Friedman79635842009-05-30 04:20:30 +0000489 if (!Decls.empty())
490 ProcessDeclGroup(Decls);
491
Douglas Gregor278f52e2009-05-30 00:08:05 +0000492 if (Indent)
493 Indentation -= Policy.Indentation;
494}
495
496void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
497 VisitDeclContext(D, false);
498}
499
500void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000501 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000502 Out << "typedef ";
Fangrui Song6907ce22018-07-30 19:24:48 +0000503
Douglas Gregor26701a42011-09-09 02:06:17 +0000504 if (D->isModulePrivate())
505 Out << "__module_private__ ";
506 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000507 QualType Ty = D->getTypeSourceInfo()->getType();
508 Ty.print(Out, Policy, D->getName(), Indentation);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000509 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000510}
511
Richard Smithdda56e42011-04-15 14:24:37 +0000512void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000513 Out << "using " << *D;
514 prettyPrintAttributes(D);
515 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000516}
517
Douglas Gregor278f52e2009-05-30 00:08:05 +0000518void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000519 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
520 Out << "__module_private__ ";
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000521 Out << "enum";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000522 if (D->isScoped()) {
523 if (D->isScopedUsingClassTag())
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000524 Out << " class";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000525 else
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000526 Out << " struct";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000527 }
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000528
529 prettyPrintAttributes(D);
530
531 Out << ' ' << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000532
Serge Pavlov08c8de22016-11-04 06:03:34 +0000533 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000534 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000535
John McCallf937c022011-10-07 06:10:15 +0000536 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000537 Out << " {\n";
538 VisitDeclContext(D);
539 Indent() << "}";
540 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000541}
542
543void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000544 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
545 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000546 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000547
548 prettyPrintAttributes(D);
549
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000550 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000551 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000552
John McCallf937c022011-10-07 06:10:15 +0000553 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000554 Out << " {\n";
555 VisitDeclContext(D);
556 Indent() << "}";
557 }
558}
559
560void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000561 Out << *D;
Jordan Roseccca6692017-01-20 03:33:42 +0000562 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000563 if (Expr *Init = D->getInitExpr()) {
564 Out << " = ";
George Karpenkov64885ae2018-09-15 02:02:31 +0000565 Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000566 }
567}
568
Richard Smith76b90272019-05-09 03:59:21 +0000569static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
570 PrintingPolicy &Policy,
571 unsigned Indentation) {
572 std::string Proto = "explicit";
573 llvm::raw_string_ostream EOut(Proto);
574 if (ES.getExpr()) {
575 EOut << "(";
576 ES.getExpr()->printPretty(EOut, nullptr, Policy, Indentation);
577 EOut << ")";
578 }
579 EOut << " ";
580 EOut.flush();
581 Out << EOut.str();
582}
583
Mike Stump11289f42009-09-09 15:08:12 +0000584void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000585 if (!D->getDescribedFunctionTemplate() &&
586 !D->isFunctionTemplateSpecialization())
587 prettyPrintPragmas(D);
588
Serge Pavlova67a4d22016-11-10 08:49:37 +0000589 if (D->isFunctionTemplateSpecialization())
590 Out << "template<> ";
Alex Lorenz47fd10c2017-04-18 15:12:34 +0000591 else if (!D->getDescribedFunctionTemplate()) {
592 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
593 I < NumTemplateParams; ++I)
594 printTemplateParameters(D->getTemplateParameterList(I));
595 }
Serge Pavlova67a4d22016-11-10 08:49:37 +0000596
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000597 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000598 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Richard Smith057ec502017-02-18 01:01:48 +0000599 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000600 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000601 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000602 case SC_None: break;
603 case SC_Extern: Out << "extern "; break;
604 case SC_Static: Out << "static "; break;
605 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000606 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000607 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000608 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000609
Douglas Gregor26701a42011-09-09 02:06:17 +0000610 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000611 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000612 if (D->isModulePrivate()) Out << "__module_private__ ";
Gauthier Harnisch796ed032019-06-14 08:56:20 +0000613 if (D->isConstexprSpecified() && !D->isExplicitlyDefaulted())
614 Out << "constexpr ";
615 if (D->isConsteval()) Out << "consteval ";
Richard Smith76b90272019-05-09 03:59:21 +0000616 ExplicitSpecifier ExplicitSpec = ExplicitSpecifier::getFromDecl(D);
617 if (ExplicitSpec.isSpecified())
618 printExplicitSpecifier(ExplicitSpec, Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000619 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000620
Douglas Gregor2d042f12009-05-30 05:39:39 +0000621 PrintingPolicy SubPolicy(Policy);
622 SubPolicy.SuppressSpecifiers = false;
Alex Lorenza981c7d2017-04-11 16:46:03 +0000623 std::string Proto;
Serge Pavlov842022a2017-11-23 05:38:20 +0000624
625 if (Policy.FullyQualifiedName) {
626 Proto += D->getQualifiedNameAsString();
627 } else {
628 if (!Policy.SuppressScope) {
629 if (const NestedNameSpecifier *NS = D->getQualifier()) {
630 llvm::raw_string_ostream OS(Proto);
631 NS->print(OS, Policy);
632 }
Alex Lorenza981c7d2017-04-11 16:46:03 +0000633 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000634 Proto += D->getNameInfo().getAsString();
Alex Lorenza981c7d2017-04-11 16:46:03 +0000635 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000636
Richard Smith057ec502017-02-18 01:01:48 +0000637 if (GuideDecl)
638 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
Serge Pavlova67a4d22016-11-10 08:49:37 +0000639 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
640 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000641 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000642 TArgPrinter.printTemplateArguments(*TArgs);
643 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000644
Abramo Bagnara6d810632010-12-14 22:11:44 +0000645 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000646 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000647 Proto = '(' + Proto + ')';
648 Ty = PT->getInnerType();
649 }
650
Reid Kleckner0503a872013-12-05 01:23:43 +0000651 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000652 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000653 if (D->hasWrittenPrototype())
654 FT = dyn_cast<FunctionProtoType>(AFT);
655
656 Proto += "(";
657 if (FT) {
658 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000659 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000660 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
661 if (i) POut << ", ";
662 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
663 }
Mike Stump11289f42009-09-09 15:08:12 +0000664
Douglas Gregor278f52e2009-05-30 00:08:05 +0000665 if (FT->isVariadic()) {
666 if (D->getNumParams()) POut << ", ";
667 POut << "...";
668 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000669 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000670 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
671 if (i)
672 Proto += ", ";
673 Proto += D->getParamDecl(i)->getNameAsString();
674 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000675 }
676
677 Proto += ")";
Fangrui Song6907ce22018-07-30 19:24:48 +0000678
David Blaikief5697e52012-08-10 00:55:35 +0000679 if (FT) {
680 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000681 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000682 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000683 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000684 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000685 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000686
687 switch (FT->getRefQualifier()) {
688 case RQ_None:
689 break;
690 case RQ_LValue:
691 Proto += " &";
692 break;
693 case RQ_RValue:
694 Proto += " &&";
695 break;
696 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000697 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000698
699 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000700 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000701 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000702 Proto += "...";
Fangrui Song6907ce22018-07-30 19:24:48 +0000703 else
Douglas Gregor049bdca2009-12-08 17:45:32 +0000704 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
705 if (I)
706 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000707
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000708 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000709 }
710 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000711 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
712 Proto += " noexcept";
Richard Smitheaf11ad2018-05-03 03:58:32 +0000713 if (isComputedNoexcept(FT->getExceptionSpecType())) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000714 Proto += "(";
715 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000716 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000717 Indentation);
718 EOut.flush();
719 Proto += EOut.str();
720 Proto += ")";
721 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000722 }
723
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000724 if (CDecl) {
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000725 if (!Policy.TerseOutput)
726 PrintConstructorInitializers(CDecl, Proto);
Benjamin Kramer2907b082014-02-25 18:49:49 +0000727 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000728 if (FT && FT->hasTrailingReturn()) {
Richard Smith057ec502017-02-18 01:01:48 +0000729 if (!GuideDecl)
730 Out << "auto ";
731 Out << Proto << " -> ";
Richard Smithe6560762013-02-22 05:54:51 +0000732 Proto.clear();
733 }
Alp Toker314cc812014-01-25 16:55:45 +0000734 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000735 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000736 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000737 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000738 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000739 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000740 }
741
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000742 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000743
744 if (D->isPure())
745 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000746 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000747 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000748 else if (D->isExplicitlyDefaulted())
749 Out << " = default";
Serge Pavlova67a4d22016-11-10 08:49:37 +0000750 else if (D->doesThisDeclarationHaveABody()) {
751 if (!Policy.TerseOutput) {
752 if (!D->hasPrototype() && D->getNumParams()) {
753 // This is a K&R function definition, so we need to print the
754 // parameters.
755 Out << '\n';
Alex Lorenz36070ed2017-08-17 13:41:55 +0000756 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000757 Indentation += Policy.Indentation;
758 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
759 Indent();
760 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
761 Out << ";\n";
762 }
763 Indentation -= Policy.Indentation;
764 } else
765 Out << ' ';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000766
Serge Pavlova67a4d22016-11-10 08:49:37 +0000767 if (D->getBody())
768 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
769 } else {
Alex Lorenz35019db2017-11-16 01:28:25 +0000770 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
Serge Pavlova67a4d22016-11-10 08:49:37 +0000771 Out << " {}";
772 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000773 }
774}
775
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000776void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
777 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000778 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
779 for (unsigned i = 0; i < NumTPLists; ++i)
Serge Pavlova67a4d22016-11-10 08:49:37 +0000780 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000781 Out << "friend ";
782 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000783 }
784 else if (FunctionDecl *FD =
785 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
786 Out << "friend ";
787 VisitFunctionDecl(FD);
788 }
789 else if (FunctionTemplateDecl *FTD =
790 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
791 Out << "friend ";
792 VisitFunctionTemplateDecl(FTD);
793 }
794 else if (ClassTemplateDecl *CTD =
795 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
796 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000797 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000798 }
799}
800
Douglas Gregor278f52e2009-05-30 00:08:05 +0000801void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000802 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000803 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000804 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000805 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
806 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000807
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000808 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000809 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000810
811 if (D->isBitField()) {
812 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000813 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000814 }
Richard Smith938f40b2011-06-11 17:19:42 +0000815
816 Expr *Init = D->getInClassInitializer();
817 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000818 if (D->getInClassInitStyle() == ICIS_ListInit)
819 Out << " ";
820 else
821 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000822 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000823 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000824 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000825}
826
Chris Lattnercab02a62011-02-17 20:34:02 +0000827void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000828 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000829}
830
Douglas Gregor278f52e2009-05-30 00:08:05 +0000831void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000832 prettyPrintPragmas(D);
Vassil Vassilev10023732016-07-08 21:09:08 +0000833
834 QualType T = D->getTypeSourceInfo()
835 ? D->getTypeSourceInfo()->getType()
836 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
837
Richard Smithfd3834f2013-04-13 02:43:54 +0000838 if (!Policy.SuppressSpecifiers) {
839 StorageClass SC = D->getStorageClass();
840 if (SC != SC_None)
841 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000842
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000843 switch (D->getTSCSpec()) {
844 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000845 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000846 case TSCS___thread:
847 Out << "__thread ";
848 break;
849 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000850 Out << "_Thread_local ";
851 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000852 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000853 Out << "thread_local ";
854 break;
855 }
856
857 if (D->isModulePrivate())
858 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000859
860 if (D->isConstexpr()) {
861 Out << "constexpr ";
862 T.removeLocalConst();
863 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000864 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000865
Richard Smitha4bb2922014-07-23 03:17:06 +0000866 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000867 Expr *Init = D->getInit();
868 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000869 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000870 if (CXXConstructExpr *Construct =
871 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000872 if (D->getInitStyle() == VarDecl::CallInit &&
873 !Construct->isListInitialization()) {
874 ImplicitInit = Construct->getNumArgs() == 0 ||
875 Construct->getArg(0)->isDefaultArgument();
876 }
877 }
Sebastian Redla9351792012-02-11 23:51:47 +0000878 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000879 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000880 Out << "(";
881 else if (D->getInitStyle() == VarDecl::CInit) {
882 Out << " = ";
883 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000884 PrintingPolicy SubPolicy(Policy);
885 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000886 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000887 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000888 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000889 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000890 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000891 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000892 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000893}
894
895void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
896 VisitVarDecl(D);
897}
898
899void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
900 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000901 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000902 Out << ")";
903}
904
Douglas Gregorba345522011-12-02 23:23:56 +0000905void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000906 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000907 << ";\n";
908}
909
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000910void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
911 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000912 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000913 if (StringLiteral *SL = D->getMessage()) {
914 Out << ", ";
915 SL->printPretty(Out, nullptr, Policy, Indentation);
916 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000917 Out << ")";
918}
919
Douglas Gregor278f52e2009-05-30 00:08:05 +0000920//----------------------------------------------------------------------------
921// C++ declarations
922//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000923void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000924 if (D->isInline())
925 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000926 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000927 VisitDeclContext(D);
928 Indent() << "}";
929}
930
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000931void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
932 Out << "using namespace ";
933 if (D->getQualifier())
934 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000935 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000936}
937
Douglas Gregor18231932009-05-30 06:48:27 +0000938void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000939 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000940 if (D->getQualifier())
941 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000942 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000943}
944
Michael Han84324352013-02-22 17:15:32 +0000945void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
946 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000947}
948
Douglas Gregor5f478b72009-05-30 06:58:37 +0000949void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000950 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000951 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
952 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000953 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000954
955 prettyPrintAttributes(D);
956
Serge Pavlova67a4d22016-11-10 08:49:37 +0000957 if (D->getIdentifier()) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000958 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000959
Serge Pavlova67a4d22016-11-10 08:49:37 +0000960 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
961 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
962 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
963 printTemplateArguments(S->getTemplateArgs());
964 }
965
John McCallf937c022011-10-07 06:10:15 +0000966 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000967 // Print the base classes
968 if (D->getNumBases()) {
969 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000970 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
971 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000972 if (Base != D->bases_begin())
973 Out << ", ";
974
975 if (Base->isVirtual())
976 Out << "virtual ";
977
Anders Carlsson6df9e072009-08-29 20:36:12 +0000978 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000979 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000980 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000981 Out << " ";
982 }
983 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000984
985 if (Base->isPackExpansion())
986 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000987 }
988 }
989
990 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000991 // FIXME: Doesn't print access specifiers, e.g., "public:"
Serge Pavlova67a4d22016-11-10 08:49:37 +0000992 if (Policy.TerseOutput) {
993 Out << " {}";
994 } else {
995 Out << " {\n";
996 VisitDeclContext(D);
997 Indent() << "}";
998 }
Mike Stump11289f42009-09-09 15:08:12 +0000999 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001000}
1001
1002void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1003 const char *l;
Jonas Devlieghere5c49c262019-09-23 23:49:36 +00001004 if (D->getLanguage() == LinkageSpecDecl::lang_c)
Douglas Gregor278f52e2009-05-30 00:08:05 +00001005 l = "C";
Jonas Devlieghere5c49c262019-09-23 23:49:36 +00001006 else {
1007 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
1008 "unknown language in linkage specification");
Douglas Gregor278f52e2009-05-30 00:08:05 +00001009 l = "C++";
1010 }
1011
1012 Out << "extern \"" << l << "\" ";
1013 if (D->hasBraces()) {
1014 Out << "{\n";
1015 VisitDeclContext(D);
1016 Indent() << "}";
1017 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001018 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001019}
1020
Hamza Sood8205a812019-05-04 10:49:46 +00001021void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
1022 bool OmitTemplateKW) {
Richard Trieu82398f92011-07-28 00:19:05 +00001023 assert(Params);
Richard Trieu82398f92011-07-28 00:19:05 +00001024
Hamza Sood8205a812019-05-04 10:49:46 +00001025 if (!OmitTemplateKW)
1026 Out << "template ";
1027 Out << '<';
Mike Stump11289f42009-09-09 15:08:12 +00001028
Hamza Sood8205a812019-05-04 10:49:46 +00001029 bool NeedComma = false;
1030 for (const Decl *Param : *Params) {
1031 if (Param->isImplicit())
1032 continue;
1033
1034 if (NeedComma)
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001035 Out << ", ";
Hamza Sood8205a812019-05-04 10:49:46 +00001036 else
1037 NeedComma = true;
Mike Stump11289f42009-09-09 15:08:12 +00001038
Serge Pavlova67a4d22016-11-10 08:49:37 +00001039 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +00001040
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001041 if (TTP->wasDeclaredWithTypename())
Hamza Sood8205a812019-05-04 10:49:46 +00001042 Out << "typename";
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001043 else
Hamza Sood8205a812019-05-04 10:49:46 +00001044 Out << "class";
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001045
Anders Carlssonfb1d7762009-06-12 22:23:22 +00001046 if (TTP->isParameterPack())
Hamza Sood8205a812019-05-04 10:49:46 +00001047 Out << " ...";
1048 else if (!TTP->getName().empty())
1049 Out << ' ';
Mike Stump11289f42009-09-09 15:08:12 +00001050
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001051 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001052
Serge Pavlova67a4d22016-11-10 08:49:37 +00001053 if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001054 Out << " = ";
1055 Out << TTP->getDefaultArgument().getAsString(Policy);
1056 };
Serge Pavlova67a4d22016-11-10 08:49:37 +00001057 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +00001058 StringRef Name;
1059 if (IdentifierInfo *II = NTTP->getIdentifier())
1060 Name = II->getName();
1061 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +00001062
Serge Pavlova67a4d22016-11-10 08:49:37 +00001063 if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001064 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +00001065 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1066 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001067 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001068 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
Richard Smith030f4992011-04-15 13:38:57 +00001069 VisitTemplateDecl(TTPD);
1070 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001071 }
1072 }
Mike Stump11289f42009-09-09 15:08:12 +00001073
Hamza Sood8205a812019-05-04 10:49:46 +00001074 Out << '>';
1075 if (!OmitTemplateKW)
1076 Out << ' ';
Richard Trieu82398f92011-07-28 00:19:05 +00001077}
1078
Serge Pavlova67a4d22016-11-10 08:49:37 +00001079void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1080 const TemplateParameterList *Params) {
1081 Out << "<";
1082 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1083 const TemplateArgument &A = Args[I];
1084 if (I)
1085 Out << ", ";
1086 if (Params) {
1087 if (A.getKind() == TemplateArgument::Type)
1088 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1089 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1090 Out << *P;
1091 continue;
1092 }
1093 if (A.getKind() == TemplateArgument::Template) {
1094 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1095 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1096 auto P = cast<TemplateTemplateParmDecl>(
1097 Params->getParam(TD->getIndex()));
1098 Out << *P;
1099 continue;
1100 }
1101 }
1102 if (A.getKind() == TemplateArgument::Expression) {
1103 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1104 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1105 auto P = cast<NonTypeTemplateParmDecl>(
1106 Params->getParam(N->getIndex()));
1107 Out << *P;
1108 continue;
1109 }
1110 }
1111 }
1112 A.print(Policy, Out);
1113 }
1114 Out << ">";
1115}
1116
Richard Trieu82398f92011-07-28 00:19:05 +00001117void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001118 printTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001119
Richard Smith030f4992011-04-15 13:38:57 +00001120 if (const TemplateTemplateParmDecl *TTP =
1121 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +00001122 Out << "class ";
1123 if (TTP->isParameterPack())
1124 Out << "...";
1125 Out << D->getName();
Saar Razd7aae332019-07-10 21:25:49 +00001126 } else if (auto *TD = D->getTemplatedDecl())
1127 Visit(TD);
1128 else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) {
1129 Out << "concept " << Concept->getName() << " = " ;
1130 Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy,
1131 Indentation);
1132 Out << ";";
Craig Silverstein4a5d0862010-07-09 20:25:10 +00001133 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001134}
1135
Richard Trieu82398f92011-07-28 00:19:05 +00001136void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +00001137 prettyPrintPragmas(D->getTemplatedDecl());
Alex Lorenz47fd10c2017-04-18 15:12:34 +00001138 // Print any leading template parameter lists.
1139 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1140 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1141 I < NumTemplateParams; ++I)
1142 printTemplateParameters(FD->getTemplateParameterList(I));
1143 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001144 VisitRedeclarableTemplateDecl(D);
Alexey Bataev97b72212018-08-14 18:31:20 +00001145 // Declare target attribute is special one, natural spelling for the pragma
1146 // assumes "ending" construct so print it here.
1147 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1148 Out << "#pragma omp end declare target\n";
Serge Pavlova67a4d22016-11-10 08:49:37 +00001149
Richard Smith057ec502017-02-18 01:01:48 +00001150 // Never print "instantiations" for deduction guides (they don't really
1151 // have them).
1152 if (PrintInstantiation &&
1153 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001154 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1155 const FunctionDecl *Def;
1156 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1157 return;
1158 for (auto *I : D->specializations())
1159 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1160 if (!PrevDecl->isThisDeclarationADefinition())
1161 Out << ";\n";
1162 Indent();
1163 prettyPrintPragmas(I);
1164 Visit(I);
1165 }
1166 }
Richard Trieu82398f92011-07-28 00:19:05 +00001167}
1168
1169void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001170 VisitRedeclarableTemplateDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001171
Serge Pavlova67a4d22016-11-10 08:49:37 +00001172 if (PrintInstantiation) {
1173 for (auto *I : D->specializations())
1174 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1175 if (D->isThisDeclarationADefinition())
1176 Out << ";";
1177 Out << "\n";
1178 Visit(I);
1179 }
1180 }
1181}
1182
1183void DeclPrinter::VisitClassTemplateSpecializationDecl(
1184 ClassTemplateSpecializationDecl *D) {
1185 Out << "template<> ";
1186 VisitCXXRecordDecl(D);
1187}
1188
1189void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1190 ClassTemplatePartialSpecializationDecl *D) {
1191 printTemplateParameters(D->getTemplateParameters());
1192 VisitCXXRecordDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001193}
1194
Douglas Gregor278f52e2009-05-30 00:08:05 +00001195//----------------------------------------------------------------------------
1196// Objective-C declarations
1197//----------------------------------------------------------------------------
1198
Fangrui Song6907ce22018-07-30 19:24:48 +00001199void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1200 Decl::ObjCDeclQualifier Quals,
Douglas Gregor813a0662015-06-19 18:14:38 +00001201 QualType T) {
1202 Out << '(';
1203 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1204 Out << "in ";
1205 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1206 Out << "inout ";
1207 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1208 Out << "out ";
1209 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1210 Out << "bycopy ";
1211 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1212 Out << "byref ";
1213 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1214 Out << "oneway ";
1215 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001216 if (auto nullability = AttributedType::stripOuterNullability(T))
1217 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001218 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001219
Douglas Gregor813a0662015-06-19 18:14:38 +00001220 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1221 Out << ')';
1222}
1223
Douglas Gregor85f3f952015-07-07 03:57:15 +00001224void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1225 Out << "<";
1226 unsigned First = true;
1227 for (auto *Param : *Params) {
1228 if (First) {
1229 First = false;
1230 } else {
1231 Out << ", ";
1232 }
1233
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001234 switch (Param->getVariance()) {
1235 case ObjCTypeParamVariance::Invariant:
1236 break;
1237
1238 case ObjCTypeParamVariance::Covariant:
1239 Out << "__covariant ";
1240 break;
1241
1242 case ObjCTypeParamVariance::Contravariant:
1243 Out << "__contravariant ";
1244 break;
1245 }
1246
Douglas Gregor85f3f952015-07-07 03:57:15 +00001247 Out << Param->getDeclName().getAsString();
1248
1249 if (Param->hasExplicitBound()) {
1250 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1251 }
1252 }
1253 Out << ">";
1254}
1255
Douglas Gregor278f52e2009-05-30 00:08:05 +00001256void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1257 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001258 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001259 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001260 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001261 if (!OMD->getReturnType().isNull()) {
1262 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1263 OMD->getReturnType());
1264 }
Mike Stump11289f42009-09-09 15:08:12 +00001265
Douglas Gregor278f52e2009-05-30 00:08:05 +00001266 std::string name = OMD->getSelector().getAsString();
1267 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001268 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001269 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001270 pos = name.find_first_of(':', lastPos);
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001271 if (lastPos != 0)
1272 Out << " ";
1273 Out << name.substr(lastPos, pos - lastPos) << ':';
Fangrui Song6907ce22018-07-30 19:24:48 +00001274 PrintObjCMethodType(OMD->getASTContext(),
Douglas Gregor813a0662015-06-19 18:14:38 +00001275 PI->getObjCDeclQualifier(),
1276 PI->getType());
1277 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001278 lastPos = pos + 1;
1279 }
Mike Stump11289f42009-09-09 15:08:12 +00001280
Douglas Gregor278f52e2009-05-30 00:08:05 +00001281 if (OMD->param_begin() == OMD->param_end())
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001282 Out << name;
Mike Stump11289f42009-09-09 15:08:12 +00001283
Douglas Gregor278f52e2009-05-30 00:08:05 +00001284 if (OMD->isVariadic())
1285 Out << ", ...";
Fangrui Song6907ce22018-07-30 19:24:48 +00001286
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001287 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001288
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001289 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001290 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001291 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001292 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001293 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001294 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001295}
1296
1297void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1298 std::string I = OID->getNameAsString();
1299 ObjCInterfaceDecl *SID = OID->getSuperClass();
1300
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001301 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001302 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001303 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001304 else
1305 Out << "@implementation " << I;
Fangrui Song6907ce22018-07-30 19:24:48 +00001306
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001307 if (OID->ivar_size() > 0) {
1308 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001309 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001310 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001311 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001312 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001313 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001314 }
1315 Indentation -= Policy.Indentation;
1316 Out << "}\n";
1317 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001318 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1319 Out << "\n";
1320 eolnOut = true;
1321 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001322 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001323 if (!eolnOut)
1324 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001325 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001326}
1327
1328void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1329 std::string I = OID->getNameAsString();
1330 ObjCInterfaceDecl *SID = OID->getSuperClass();
1331
Douglas Gregordc9166c2011-12-15 20:29:51 +00001332 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001333 Out << "@class " << I;
1334
1335 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1336 PrintObjCTypeParams(TypeParams);
1337 }
1338
1339 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001340 return;
1341 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001342 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001343 Out << "@interface " << I;
1344
1345 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1346 PrintObjCTypeParams(TypeParams);
1347 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001348
Douglas Gregor278f52e2009-05-30 00:08:05 +00001349 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001350 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001351
Douglas Gregor278f52e2009-05-30 00:08:05 +00001352 // Protocols?
1353 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1354 if (!Protocols.empty()) {
1355 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1356 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001357 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001358 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001359 }
Mike Stump11289f42009-09-09 15:08:12 +00001360
Douglas Gregor278f52e2009-05-30 00:08:05 +00001361 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001362 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001363 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001364 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001365 for (const auto *I : OID->ivars()) {
1366 Indent() << I->getASTContext()
1367 .getUnqualifiedObjCPointerType(I->getType())
1368 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001369 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001370 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001371 Out << "}\n";
1372 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001373 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001374 Out << "\n";
1375 eolnOut = true;
1376 }
Mike Stump11289f42009-09-09 15:08:12 +00001377
Douglas Gregor278f52e2009-05-30 00:08:05 +00001378 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001379 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001380 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001381 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001382 // FIXME: implement the rest...
1383}
1384
Douglas Gregor278f52e2009-05-30 00:08:05 +00001385void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001386 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001387 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001388 return;
1389 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001390 // Protocols?
1391 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1392 if (!Protocols.empty()) {
1393 Out << "@protocol " << *PID;
1394 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1395 E = Protocols.end(); I != E; ++I)
1396 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1397 Out << ">\n";
1398 } else
1399 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001400 VisitDeclContext(PID, false);
1401 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001402}
1403
1404void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001405 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001406
1407 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001408 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001409 // FIXME: implement the rest...
1410}
1411
1412void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001413 Out << "@interface " << *PID->getClassInterface();
1414 if (auto TypeParams = PID->getTypeParamList()) {
1415 PrintObjCTypeParams(TypeParams);
1416 }
1417 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001418 if (PID->ivar_size() > 0) {
1419 Out << "{\n";
1420 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001421 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001422 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001423 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001424 Indentation -= Policy.Indentation;
1425 Out << "}\n";
1426 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001427
Douglas Gregor278f52e2009-05-30 00:08:05 +00001428 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001429 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001430
Douglas Gregor278f52e2009-05-30 00:08:05 +00001431 // FIXME: implement the rest...
1432}
1433
1434void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001435 Out << "@compatibility_alias " << *AID
1436 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001437}
1438
1439/// PrintObjCPropertyDecl - print a property declaration.
1440///
David Goldmanfa8185c2019-04-08 19:52:45 +00001441/// Print attributes in the following order:
1442/// - class
1443/// - nonatomic | atomic
1444/// - assign | retain | strong | copy | weak | unsafe_unretained
1445/// - readwrite | readonly
1446/// - getter & setter
1447/// - nullability
Douglas Gregor278f52e2009-05-30 00:08:05 +00001448void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1449 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1450 Out << "@required\n";
1451 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1452 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001453
Douglas Gregor813a0662015-06-19 18:14:38 +00001454 QualType T = PDecl->getType();
1455
Douglas Gregor278f52e2009-05-30 00:08:05 +00001456 Out << "@property";
1457 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1458 bool first = true;
David Goldmanfa8185c2019-04-08 19:52:45 +00001459 Out << "(";
1460 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1461 Out << (first ? "" : ", ") << "class";
Ted Kremenek897af912011-08-17 21:09:35 +00001462 first = false;
1463 }
Mike Stump11289f42009-09-09 15:08:12 +00001464
Ted Kremenek897af912011-08-17 21:09:35 +00001465 if (PDecl->getPropertyAttributes() &
1466 ObjCPropertyDecl::OBJC_PR_nonatomic) {
David Goldmanfa8185c2019-04-08 19:52:45 +00001467 Out << (first ? "" : ", ") << "nonatomic";
Ted Kremenek897af912011-08-17 21:09:35 +00001468 first = false;
1469 }
1470 if (PDecl->getPropertyAttributes() &
1471 ObjCPropertyDecl::OBJC_PR_atomic) {
David Goldmanfa8185c2019-04-08 19:52:45 +00001472 Out << (first ? "" : ", ") << "atomic";
1473 first = false;
1474 }
1475
1476 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1477 Out << (first ? "" : ", ") << "assign";
1478 first = false;
1479 }
1480 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1481 Out << (first ? "" : ", ") << "retain";
1482 first = false;
1483 }
1484
1485 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1486 Out << (first ? "" : ", ") << "strong";
1487 first = false;
1488 }
1489 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1490 Out << (first ? "" : ", ") << "copy";
1491 first = false;
1492 }
1493 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) {
1494 Out << (first ? "" : ", ") << "weak";
1495 first = false;
1496 }
1497 if (PDecl->getPropertyAttributes()
1498 & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
1499 Out << (first ? "" : ", ") << "unsafe_unretained";
1500 first = false;
1501 }
1502
1503 if (PDecl->getPropertyAttributes() &
1504 ObjCPropertyDecl::OBJC_PR_readwrite) {
1505 Out << (first ? "" : ", ") << "readwrite";
1506 first = false;
1507 }
1508 if (PDecl->getPropertyAttributes() &
1509 ObjCPropertyDecl::OBJC_PR_readonly) {
1510 Out << (first ? "" : ", ") << "readonly";
1511 first = false;
1512 }
1513
1514 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1515 Out << (first ? "" : ", ") << "getter = ";
1516 PDecl->getGetterName().print(Out);
1517 first = false;
1518 }
1519 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1520 Out << (first ? "" : ", ") << "setter = ";
1521 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001522 first = false;
1523 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001524
Douglas Gregor813a0662015-06-19 18:14:38 +00001525 if (PDecl->getPropertyAttributes() &
1526 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001527 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001528 if (*nullability == NullabilityKind::Unspecified &&
1529 (PDecl->getPropertyAttributes() &
1530 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
David Goldmanfa8185c2019-04-08 19:52:45 +00001531 Out << (first ? "" : ", ") << "null_resettable";
Douglas Gregor849ebc22015-06-19 18:14:46 +00001532 } else {
David Goldmanfa8185c2019-04-08 19:52:45 +00001533 Out << (first ? "" : ", ")
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001534 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001535 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001536 first = false;
1537 }
1538 }
1539
Ted Kremenek897af912011-08-17 21:09:35 +00001540 (void) first; // Silence dead store warning due to idiomatic code.
David Goldmanfa8185c2019-04-08 19:52:45 +00001541 Out << ")";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001542 }
David Goldmanfa8185c2019-04-08 19:52:45 +00001543 std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1544 getAsString(Policy);
1545 Out << ' ' << TypeStr;
1546 if (!StringRef(TypeStr).endswith("*"))
1547 Out << ' ';
1548 Out << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001549 if (Policy.PolishForDeclaration)
1550 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001551}
1552
1553void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1554 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001555 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001556 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001557 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001558 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001559 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001560 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001561}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001562
1563void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001564 if (!D->isAccessDeclaration())
1565 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001566 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001567 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001568 D->getQualifier()->print(Out, Policy);
Alex Lorenzea9b59a2016-10-03 12:22:17 +00001569
1570 // Use the correct record name when the using declaration is used for
1571 // inheriting constructors.
1572 for (const auto *Shadow : D->shadows()) {
1573 if (const auto *ConstructorShadow =
1574 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1575 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1576 Out << *ConstructorShadow->getNominatedBaseClass();
1577 return;
1578 }
1579 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001580 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001581}
1582
John McCalle61f2ba2009-11-18 02:36:19 +00001583void
1584DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1585 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001586 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001587 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001588}
1589
1590void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001591 if (!D->isAccessDeclaration())
1592 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001593 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001594 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001595}
John McCall3f746822009-11-17 05:59:44 +00001596
1597void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1598 // ignore
1599}
Alexey Bataeva769e072013-03-22 06:34:35 +00001600
1601void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1602 Out << "#pragma omp threadprivate";
1603 if (!D->varlist_empty()) {
1604 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1605 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001606 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001607 Out << (I == D->varlist_begin() ? '(' : ',');
George Burgess IV00f70bd2018-03-01 05:43:23 +00001608 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001609 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001610 }
1611 Out << ")";
1612 }
1613}
1614
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001615void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1616 Out << "#pragma omp allocate";
1617 if (!D->varlist_empty()) {
1618 for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(),
1619 E = D->varlist_end();
1620 I != E; ++I) {
1621 Out << (I == D->varlist_begin() ? '(' : ',');
1622 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1623 ND->printQualifiedName(Out);
1624 }
1625 Out << ")";
1626 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001627 if (!D->clauselist_empty()) {
1628 Out << " ";
1629 OMPClausePrinter Printer(Out, Policy);
1630 for (OMPClause *C : D->clauselists())
1631 Printer.Visit(C);
1632 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001633}
1634
Kelvin Li1408f912018-09-26 04:28:39 +00001635void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1636 Out << "#pragma omp requires ";
1637 if (!D->clauselist_empty()) {
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001638 OMPClausePrinter Printer(Out, Policy);
1639 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I)
1640 Printer.Visit(*I);
Kelvin Li1408f912018-09-26 04:28:39 +00001641 }
1642}
1643
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001644void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1645 if (!D->isInvalidDecl()) {
1646 Out << "#pragma omp declare reduction (";
1647 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001648 const char *OpName =
Richard Smith7fa2b742019-06-14 20:01:51 +00001649 getOperatorSpelling(D->getDeclName().getCXXOverloadedOperator());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001650 assert(OpName && "not an overloaded operator");
1651 Out << OpName;
1652 } else {
1653 assert(D->getDeclName().isIdentifier());
1654 D->printName(Out);
1655 }
1656 Out << " : ";
1657 D->getType().print(Out, Policy);
1658 Out << " : ";
1659 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1660 Out << ")";
1661 if (auto *Init = D->getInitializer()) {
1662 Out << " initializer(";
Alexey Bataev070f43a2017-09-06 14:49:58 +00001663 switch (D->getInitializerKind()) {
1664 case OMPDeclareReductionDecl::DirectInit:
1665 Out << "omp_priv(";
1666 break;
1667 case OMPDeclareReductionDecl::CopyInit:
1668 Out << "omp_priv = ";
1669 break;
1670 case OMPDeclareReductionDecl::CallInit:
1671 break;
1672 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001673 Init->printPretty(Out, nullptr, Policy, 0);
Alexey Bataev070f43a2017-09-06 14:49:58 +00001674 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1675 Out << ")";
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001676 Out << ")";
1677 }
1678 }
1679}
1680
Michael Kruse251e1482019-02-01 20:25:04 +00001681void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1682 if (!D->isInvalidDecl()) {
1683 Out << "#pragma omp declare mapper (";
1684 D->printName(Out);
1685 Out << " : ";
1686 D->getType().print(Out, Policy);
1687 Out << " ";
1688 Out << D->getVarName();
1689 Out << ")";
1690 if (!D->clauselist_empty()) {
1691 OMPClausePrinter Printer(Out, Policy);
1692 for (auto *C : D->clauselists()) {
1693 Out << " ";
1694 Printer.Visit(C);
1695 }
1696 }
1697 }
1698}
1699
Alexey Bataev4244be22016-02-11 05:35:55 +00001700void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001701 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1702}
1703