blob: e59792710e752b489f614b263044e47262aaa1bb [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);
Sam McCall87054ec2019-11-14 14:16:14 +0100111 void printTemplateArguments(llvm::ArrayRef<TemplateArgument> Args);
112 void printTemplateArguments(llvm::ArrayRef<TemplateArgumentLoc> Args);
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 {
Sam McCall575e09d2019-11-15 19:19:17 +0100628 llvm::raw_string_ostream OS(Proto);
Serge Pavlov842022a2017-11-23 05:38:20 +0000629 if (!Policy.SuppressScope) {
630 if (const NestedNameSpecifier *NS = D->getQualifier()) {
Serge Pavlov842022a2017-11-23 05:38:20 +0000631 NS->print(OS, Policy);
632 }
Alex Lorenza981c7d2017-04-11 16:46:03 +0000633 }
Sam McCall575e09d2019-11-15 19:19:17 +0100634 D->getNameInfo().printName(OS, Policy);
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();
Sam McCall87054ec2019-11-14 14:16:14 +0100639 if (D->isFunctionTemplateSpecialization()) {
Serge Pavlova67a4d22016-11-10 08:49:37 +0000640 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000641 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
Sam McCall87054ec2019-11-14 14:16:14 +0100642 const auto *TArgAsWritten = D->getTemplateSpecializationArgsAsWritten();
643 if (TArgAsWritten && !Policy.PrintCanonicalTypes)
644 TArgPrinter.printTemplateArguments(TArgAsWritten->arguments());
645 else if (const TemplateArgumentList *TArgs =
646 D->getTemplateSpecializationArgs())
647 TArgPrinter.printTemplateArguments(TArgs->asArray());
Serge Pavlova67a4d22016-11-10 08:49:37 +0000648 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000649
Abramo Bagnara6d810632010-12-14 22:11:44 +0000650 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000651 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000652 Proto = '(' + Proto + ')';
653 Ty = PT->getInnerType();
654 }
655
Reid Kleckner0503a872013-12-05 01:23:43 +0000656 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000657 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000658 if (D->hasWrittenPrototype())
659 FT = dyn_cast<FunctionProtoType>(AFT);
660
661 Proto += "(";
662 if (FT) {
663 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000664 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000665 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
666 if (i) POut << ", ";
667 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
668 }
Mike Stump11289f42009-09-09 15:08:12 +0000669
Douglas Gregor278f52e2009-05-30 00:08:05 +0000670 if (FT->isVariadic()) {
671 if (D->getNumParams()) POut << ", ";
672 POut << "...";
673 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000674 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000675 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
676 if (i)
677 Proto += ", ";
678 Proto += D->getParamDecl(i)->getNameAsString();
679 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000680 }
681
682 Proto += ")";
Fangrui Song6907ce22018-07-30 19:24:48 +0000683
David Blaikief5697e52012-08-10 00:55:35 +0000684 if (FT) {
685 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000686 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000687 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000688 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000689 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000690 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000691
692 switch (FT->getRefQualifier()) {
693 case RQ_None:
694 break;
695 case RQ_LValue:
696 Proto += " &";
697 break;
698 case RQ_RValue:
699 Proto += " &&";
700 break;
701 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000702 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000703
704 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000705 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000706 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000707 Proto += "...";
Fangrui Song6907ce22018-07-30 19:24:48 +0000708 else
Douglas Gregor049bdca2009-12-08 17:45:32 +0000709 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
710 if (I)
711 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000712
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000713 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000714 }
715 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000716 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
717 Proto += " noexcept";
Richard Smitheaf11ad2018-05-03 03:58:32 +0000718 if (isComputedNoexcept(FT->getExceptionSpecType())) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000719 Proto += "(";
720 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000721 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000722 Indentation);
723 EOut.flush();
724 Proto += EOut.str();
725 Proto += ")";
726 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000727 }
728
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000729 if (CDecl) {
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000730 if (!Policy.TerseOutput)
731 PrintConstructorInitializers(CDecl, Proto);
Benjamin Kramer2907b082014-02-25 18:49:49 +0000732 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000733 if (FT && FT->hasTrailingReturn()) {
Richard Smith057ec502017-02-18 01:01:48 +0000734 if (!GuideDecl)
735 Out << "auto ";
736 Out << Proto << " -> ";
Richard Smithe6560762013-02-22 05:54:51 +0000737 Proto.clear();
738 }
Alp Toker314cc812014-01-25 16:55:45 +0000739 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000740 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000741 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000742 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000743 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000744 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000745 }
746
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000747 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000748
749 if (D->isPure())
750 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000751 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000752 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000753 else if (D->isExplicitlyDefaulted())
754 Out << " = default";
Serge Pavlova67a4d22016-11-10 08:49:37 +0000755 else if (D->doesThisDeclarationHaveABody()) {
756 if (!Policy.TerseOutput) {
757 if (!D->hasPrototype() && D->getNumParams()) {
758 // This is a K&R function definition, so we need to print the
759 // parameters.
760 Out << '\n';
Alex Lorenz36070ed2017-08-17 13:41:55 +0000761 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000762 Indentation += Policy.Indentation;
763 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
764 Indent();
765 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
766 Out << ";\n";
767 }
768 Indentation -= Policy.Indentation;
769 } else
770 Out << ' ';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000771
Serge Pavlova67a4d22016-11-10 08:49:37 +0000772 if (D->getBody())
773 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
774 } else {
Alex Lorenz35019db2017-11-16 01:28:25 +0000775 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
Serge Pavlova67a4d22016-11-10 08:49:37 +0000776 Out << " {}";
777 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000778 }
779}
780
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000781void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
782 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000783 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
784 for (unsigned i = 0; i < NumTPLists; ++i)
Serge Pavlova67a4d22016-11-10 08:49:37 +0000785 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000786 Out << "friend ";
787 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000788 }
789 else if (FunctionDecl *FD =
790 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
791 Out << "friend ";
792 VisitFunctionDecl(FD);
793 }
794 else if (FunctionTemplateDecl *FTD =
795 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
796 Out << "friend ";
797 VisitFunctionTemplateDecl(FTD);
798 }
799 else if (ClassTemplateDecl *CTD =
800 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
801 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000802 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000803 }
804}
805
Douglas Gregor278f52e2009-05-30 00:08:05 +0000806void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000807 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000808 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000809 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000810 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
811 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000812
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000813 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000814 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000815
816 if (D->isBitField()) {
817 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000818 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000819 }
Richard Smith938f40b2011-06-11 17:19:42 +0000820
821 Expr *Init = D->getInClassInitializer();
822 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000823 if (D->getInClassInitStyle() == ICIS_ListInit)
824 Out << " ";
825 else
826 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000827 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000828 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000829 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000830}
831
Chris Lattnercab02a62011-02-17 20:34:02 +0000832void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000833 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000834}
835
Douglas Gregor278f52e2009-05-30 00:08:05 +0000836void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000837 prettyPrintPragmas(D);
Vassil Vassilev10023732016-07-08 21:09:08 +0000838
839 QualType T = D->getTypeSourceInfo()
840 ? D->getTypeSourceInfo()->getType()
841 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
842
Richard Smithfd3834f2013-04-13 02:43:54 +0000843 if (!Policy.SuppressSpecifiers) {
844 StorageClass SC = D->getStorageClass();
845 if (SC != SC_None)
846 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000847
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000848 switch (D->getTSCSpec()) {
849 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000850 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000851 case TSCS___thread:
852 Out << "__thread ";
853 break;
854 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000855 Out << "_Thread_local ";
856 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000857 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000858 Out << "thread_local ";
859 break;
860 }
861
862 if (D->isModulePrivate())
863 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000864
865 if (D->isConstexpr()) {
866 Out << "constexpr ";
867 T.removeLocalConst();
868 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000869 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000870
Richard Smitha4bb2922014-07-23 03:17:06 +0000871 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000872 Expr *Init = D->getInit();
873 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000874 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000875 if (CXXConstructExpr *Construct =
876 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000877 if (D->getInitStyle() == VarDecl::CallInit &&
878 !Construct->isListInitialization()) {
879 ImplicitInit = Construct->getNumArgs() == 0 ||
880 Construct->getArg(0)->isDefaultArgument();
881 }
882 }
Sebastian Redla9351792012-02-11 23:51:47 +0000883 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000884 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000885 Out << "(";
886 else if (D->getInitStyle() == VarDecl::CInit) {
887 Out << " = ";
888 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000889 PrintingPolicy SubPolicy(Policy);
890 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000891 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000892 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000893 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000894 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000895 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000896 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000897 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000898}
899
900void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
901 VisitVarDecl(D);
902}
903
904void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
905 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000906 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000907 Out << ")";
908}
909
Douglas Gregorba345522011-12-02 23:23:56 +0000910void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000911 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000912 << ";\n";
913}
914
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000915void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
916 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000917 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000918 if (StringLiteral *SL = D->getMessage()) {
919 Out << ", ";
920 SL->printPretty(Out, nullptr, Policy, Indentation);
921 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000922 Out << ")";
923}
924
Douglas Gregor278f52e2009-05-30 00:08:05 +0000925//----------------------------------------------------------------------------
926// C++ declarations
927//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000928void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000929 if (D->isInline())
930 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000931 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000932 VisitDeclContext(D);
933 Indent() << "}";
934}
935
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000936void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
937 Out << "using namespace ";
938 if (D->getQualifier())
939 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000940 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000941}
942
Douglas Gregor18231932009-05-30 06:48:27 +0000943void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000944 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000945 if (D->getQualifier())
946 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000947 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000948}
949
Michael Han84324352013-02-22 17:15:32 +0000950void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
951 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000952}
953
Douglas Gregor5f478b72009-05-30 06:58:37 +0000954void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000955 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000956 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
957 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000958 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000959
960 prettyPrintAttributes(D);
961
Serge Pavlova67a4d22016-11-10 08:49:37 +0000962 if (D->getIdentifier()) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000963 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000964
Sam McCall87054ec2019-11-14 14:16:14 +0100965 if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
966 ArrayRef<TemplateArgument> Args = S->getTemplateArgs().asArray();
967 if (!Policy.PrintCanonicalTypes)
968 if (const auto* TSI = S->getTypeAsWritten())
969 if (const auto *TST =
970 dyn_cast<TemplateSpecializationType>(TSI->getType()))
971 Args = TST->template_arguments();
972 printTemplateArguments(Args);
973 }
Serge Pavlova67a4d22016-11-10 08:49:37 +0000974 }
975
John McCallf937c022011-10-07 06:10:15 +0000976 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000977 // Print the base classes
978 if (D->getNumBases()) {
979 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000980 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
981 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000982 if (Base != D->bases_begin())
983 Out << ", ";
984
985 if (Base->isVirtual())
986 Out << "virtual ";
987
Anders Carlsson6df9e072009-08-29 20:36:12 +0000988 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000989 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000990 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000991 Out << " ";
992 }
993 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000994
995 if (Base->isPackExpansion())
996 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000997 }
998 }
999
1000 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +00001001 // FIXME: Doesn't print access specifiers, e.g., "public:"
Serge Pavlova67a4d22016-11-10 08:49:37 +00001002 if (Policy.TerseOutput) {
1003 Out << " {}";
1004 } else {
1005 Out << " {\n";
1006 VisitDeclContext(D);
1007 Indent() << "}";
1008 }
Mike Stump11289f42009-09-09 15:08:12 +00001009 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001010}
1011
1012void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1013 const char *l;
Adrian Prantl350de4f2019-09-24 00:38:49 +00001014 switch (D->getLanguage()) {
1015 case LinkageSpecDecl::lang_c:
Douglas Gregor278f52e2009-05-30 00:08:05 +00001016 l = "C";
Adrian Prantl350de4f2019-09-24 00:38:49 +00001017 break;
1018 case LinkageSpecDecl::lang_cxx_14:
1019 l = "C++14";
1020 break;
1021 case LinkageSpecDecl::lang_cxx_11:
1022 l = "C++11";
1023 break;
1024 case LinkageSpecDecl::lang_cxx:
Douglas Gregor278f52e2009-05-30 00:08:05 +00001025 l = "C++";
Adrian Prantl350de4f2019-09-24 00:38:49 +00001026 break;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001027 }
1028
1029 Out << "extern \"" << l << "\" ";
1030 if (D->hasBraces()) {
1031 Out << "{\n";
1032 VisitDeclContext(D);
1033 Indent() << "}";
1034 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001035 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001036}
1037
Hamza Sood8205a812019-05-04 10:49:46 +00001038void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
1039 bool OmitTemplateKW) {
Richard Trieu82398f92011-07-28 00:19:05 +00001040 assert(Params);
Richard Trieu82398f92011-07-28 00:19:05 +00001041
Hamza Sood8205a812019-05-04 10:49:46 +00001042 if (!OmitTemplateKW)
1043 Out << "template ";
1044 Out << '<';
Mike Stump11289f42009-09-09 15:08:12 +00001045
Hamza Sood8205a812019-05-04 10:49:46 +00001046 bool NeedComma = false;
1047 for (const Decl *Param : *Params) {
1048 if (Param->isImplicit())
1049 continue;
1050
1051 if (NeedComma)
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001052 Out << ", ";
Hamza Sood8205a812019-05-04 10:49:46 +00001053 else
1054 NeedComma = true;
Mike Stump11289f42009-09-09 15:08:12 +00001055
Serge Pavlova67a4d22016-11-10 08:49:37 +00001056 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +00001057
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001058 if (TTP->wasDeclaredWithTypename())
Hamza Sood8205a812019-05-04 10:49:46 +00001059 Out << "typename";
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001060 else
Hamza Sood8205a812019-05-04 10:49:46 +00001061 Out << "class";
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001062
Anders Carlssonfb1d7762009-06-12 22:23:22 +00001063 if (TTP->isParameterPack())
Hamza Sood8205a812019-05-04 10:49:46 +00001064 Out << " ...";
1065 else if (!TTP->getName().empty())
1066 Out << ' ';
Mike Stump11289f42009-09-09 15:08:12 +00001067
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001068 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001069
Serge Pavlova67a4d22016-11-10 08:49:37 +00001070 if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001071 Out << " = ";
1072 Out << TTP->getDefaultArgument().getAsString(Policy);
1073 };
Serge Pavlova67a4d22016-11-10 08:49:37 +00001074 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +00001075 StringRef Name;
1076 if (IdentifierInfo *II = NTTP->getIdentifier())
1077 Name = II->getName();
1078 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +00001079
Serge Pavlova67a4d22016-11-10 08:49:37 +00001080 if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001081 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +00001082 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1083 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001084 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001085 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
Richard Smith030f4992011-04-15 13:38:57 +00001086 VisitTemplateDecl(TTPD);
1087 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001088 }
1089 }
Mike Stump11289f42009-09-09 15:08:12 +00001090
Hamza Sood8205a812019-05-04 10:49:46 +00001091 Out << '>';
1092 if (!OmitTemplateKW)
1093 Out << ' ';
Richard Trieu82398f92011-07-28 00:19:05 +00001094}
1095
Sam McCall87054ec2019-11-14 14:16:14 +01001096void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgument> Args) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001097 Out << "<";
1098 for (size_t I = 0, E = Args.size(); I < E; ++I) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001099 if (I)
1100 Out << ", ";
Sam McCall87054ec2019-11-14 14:16:14 +01001101 Args[I].print(Policy, Out);
1102 }
1103 Out << ">";
1104}
1105
1106void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgumentLoc> Args) {
1107 Out << "<";
1108 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1109 if (I)
1110 Out << ", ";
1111 Args[I].getArgument().print(Policy, Out);
Serge Pavlova67a4d22016-11-10 08:49:37 +00001112 }
1113 Out << ">";
1114}
1115
Richard Trieu82398f92011-07-28 00:19:05 +00001116void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001117 printTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001118
Richard Smith030f4992011-04-15 13:38:57 +00001119 if (const TemplateTemplateParmDecl *TTP =
1120 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +00001121 Out << "class ";
1122 if (TTP->isParameterPack())
1123 Out << "...";
1124 Out << D->getName();
Saar Razd7aae332019-07-10 21:25:49 +00001125 } else if (auto *TD = D->getTemplatedDecl())
1126 Visit(TD);
1127 else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) {
1128 Out << "concept " << Concept->getName() << " = " ;
1129 Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy,
1130 Indentation);
1131 Out << ";";
Craig Silverstein4a5d0862010-07-09 20:25:10 +00001132 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001133}
1134
Richard Trieu82398f92011-07-28 00:19:05 +00001135void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +00001136 prettyPrintPragmas(D->getTemplatedDecl());
Alex Lorenz47fd10c2017-04-18 15:12:34 +00001137 // Print any leading template parameter lists.
1138 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1139 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1140 I < NumTemplateParams; ++I)
1141 printTemplateParameters(FD->getTemplateParameterList(I));
1142 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001143 VisitRedeclarableTemplateDecl(D);
Alexey Bataev97b72212018-08-14 18:31:20 +00001144 // Declare target attribute is special one, natural spelling for the pragma
1145 // assumes "ending" construct so print it here.
1146 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1147 Out << "#pragma omp end declare target\n";
Serge Pavlova67a4d22016-11-10 08:49:37 +00001148
Richard Smith057ec502017-02-18 01:01:48 +00001149 // Never print "instantiations" for deduction guides (they don't really
1150 // have them).
1151 if (PrintInstantiation &&
1152 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001153 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1154 const FunctionDecl *Def;
1155 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1156 return;
1157 for (auto *I : D->specializations())
1158 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1159 if (!PrevDecl->isThisDeclarationADefinition())
1160 Out << ";\n";
1161 Indent();
1162 prettyPrintPragmas(I);
1163 Visit(I);
1164 }
1165 }
Richard Trieu82398f92011-07-28 00:19:05 +00001166}
1167
1168void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001169 VisitRedeclarableTemplateDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001170
Serge Pavlova67a4d22016-11-10 08:49:37 +00001171 if (PrintInstantiation) {
1172 for (auto *I : D->specializations())
1173 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1174 if (D->isThisDeclarationADefinition())
1175 Out << ";";
1176 Out << "\n";
1177 Visit(I);
1178 }
1179 }
1180}
1181
1182void DeclPrinter::VisitClassTemplateSpecializationDecl(
1183 ClassTemplateSpecializationDecl *D) {
1184 Out << "template<> ";
1185 VisitCXXRecordDecl(D);
1186}
1187
1188void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1189 ClassTemplatePartialSpecializationDecl *D) {
1190 printTemplateParameters(D->getTemplateParameters());
1191 VisitCXXRecordDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001192}
1193
Douglas Gregor278f52e2009-05-30 00:08:05 +00001194//----------------------------------------------------------------------------
1195// Objective-C declarations
1196//----------------------------------------------------------------------------
1197
Fangrui Song6907ce22018-07-30 19:24:48 +00001198void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1199 Decl::ObjCDeclQualifier Quals,
Douglas Gregor813a0662015-06-19 18:14:38 +00001200 QualType T) {
1201 Out << '(';
1202 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1203 Out << "in ";
1204 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1205 Out << "inout ";
1206 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1207 Out << "out ";
1208 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1209 Out << "bycopy ";
1210 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1211 Out << "byref ";
1212 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1213 Out << "oneway ";
1214 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001215 if (auto nullability = AttributedType::stripOuterNullability(T))
1216 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001217 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001218
Douglas Gregor813a0662015-06-19 18:14:38 +00001219 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1220 Out << ')';
1221}
1222
Douglas Gregor85f3f952015-07-07 03:57:15 +00001223void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1224 Out << "<";
1225 unsigned First = true;
1226 for (auto *Param : *Params) {
1227 if (First) {
1228 First = false;
1229 } else {
1230 Out << ", ";
1231 }
1232
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001233 switch (Param->getVariance()) {
1234 case ObjCTypeParamVariance::Invariant:
1235 break;
1236
1237 case ObjCTypeParamVariance::Covariant:
1238 Out << "__covariant ";
1239 break;
1240
1241 case ObjCTypeParamVariance::Contravariant:
1242 Out << "__contravariant ";
1243 break;
1244 }
1245
Douglas Gregor85f3f952015-07-07 03:57:15 +00001246 Out << Param->getDeclName().getAsString();
1247
1248 if (Param->hasExplicitBound()) {
1249 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1250 }
1251 }
1252 Out << ">";
1253}
1254
Douglas Gregor278f52e2009-05-30 00:08:05 +00001255void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1256 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001257 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001258 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001259 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001260 if (!OMD->getReturnType().isNull()) {
1261 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1262 OMD->getReturnType());
1263 }
Mike Stump11289f42009-09-09 15:08:12 +00001264
Douglas Gregor278f52e2009-05-30 00:08:05 +00001265 std::string name = OMD->getSelector().getAsString();
1266 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001267 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001268 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001269 pos = name.find_first_of(':', lastPos);
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001270 if (lastPos != 0)
1271 Out << " ";
1272 Out << name.substr(lastPos, pos - lastPos) << ':';
Fangrui Song6907ce22018-07-30 19:24:48 +00001273 PrintObjCMethodType(OMD->getASTContext(),
Douglas Gregor813a0662015-06-19 18:14:38 +00001274 PI->getObjCDeclQualifier(),
1275 PI->getType());
1276 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001277 lastPos = pos + 1;
1278 }
Mike Stump11289f42009-09-09 15:08:12 +00001279
Douglas Gregor278f52e2009-05-30 00:08:05 +00001280 if (OMD->param_begin() == OMD->param_end())
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001281 Out << name;
Mike Stump11289f42009-09-09 15:08:12 +00001282
Douglas Gregor278f52e2009-05-30 00:08:05 +00001283 if (OMD->isVariadic())
1284 Out << ", ...";
Fangrui Song6907ce22018-07-30 19:24:48 +00001285
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001286 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001287
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001288 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001289 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001290 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001291 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001292 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001293 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001294}
1295
1296void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1297 std::string I = OID->getNameAsString();
1298 ObjCInterfaceDecl *SID = OID->getSuperClass();
1299
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001300 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001301 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001302 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001303 else
1304 Out << "@implementation " << I;
Fangrui Song6907ce22018-07-30 19:24:48 +00001305
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001306 if (OID->ivar_size() > 0) {
1307 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001308 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001309 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001310 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001311 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001312 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001313 }
1314 Indentation -= Policy.Indentation;
1315 Out << "}\n";
1316 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001317 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1318 Out << "\n";
1319 eolnOut = true;
1320 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001321 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001322 if (!eolnOut)
1323 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001324 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001325}
1326
1327void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1328 std::string I = OID->getNameAsString();
1329 ObjCInterfaceDecl *SID = OID->getSuperClass();
1330
Douglas Gregordc9166c2011-12-15 20:29:51 +00001331 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001332 Out << "@class " << I;
1333
1334 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1335 PrintObjCTypeParams(TypeParams);
1336 }
1337
1338 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001339 return;
1340 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001341 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001342 Out << "@interface " << I;
1343
1344 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1345 PrintObjCTypeParams(TypeParams);
1346 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001347
Douglas Gregor278f52e2009-05-30 00:08:05 +00001348 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001349 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001350
Douglas Gregor278f52e2009-05-30 00:08:05 +00001351 // Protocols?
1352 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1353 if (!Protocols.empty()) {
1354 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1355 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001356 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001357 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001358 }
Mike Stump11289f42009-09-09 15:08:12 +00001359
Douglas Gregor278f52e2009-05-30 00:08:05 +00001360 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001361 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001362 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001363 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001364 for (const auto *I : OID->ivars()) {
1365 Indent() << I->getASTContext()
1366 .getUnqualifiedObjCPointerType(I->getType())
1367 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001368 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001369 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001370 Out << "}\n";
1371 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001372 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001373 Out << "\n";
1374 eolnOut = true;
1375 }
Mike Stump11289f42009-09-09 15:08:12 +00001376
Douglas Gregor278f52e2009-05-30 00:08:05 +00001377 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001378 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001379 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001380 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001381 // FIXME: implement the rest...
1382}
1383
Douglas Gregor278f52e2009-05-30 00:08:05 +00001384void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001385 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001386 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001387 return;
1388 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001389 // Protocols?
1390 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1391 if (!Protocols.empty()) {
1392 Out << "@protocol " << *PID;
1393 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1394 E = Protocols.end(); I != E; ++I)
1395 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1396 Out << ">\n";
1397 } else
1398 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001399 VisitDeclContext(PID, false);
1400 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001401}
1402
1403void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001404 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001405
1406 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001407 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001408 // FIXME: implement the rest...
1409}
1410
1411void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001412 Out << "@interface " << *PID->getClassInterface();
1413 if (auto TypeParams = PID->getTypeParamList()) {
1414 PrintObjCTypeParams(TypeParams);
1415 }
1416 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001417 if (PID->ivar_size() > 0) {
1418 Out << "{\n";
1419 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001420 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001421 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001422 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001423 Indentation -= Policy.Indentation;
1424 Out << "}\n";
1425 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001426
Douglas Gregor278f52e2009-05-30 00:08:05 +00001427 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001428 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001429
Douglas Gregor278f52e2009-05-30 00:08:05 +00001430 // FIXME: implement the rest...
1431}
1432
1433void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001434 Out << "@compatibility_alias " << *AID
1435 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001436}
1437
1438/// PrintObjCPropertyDecl - print a property declaration.
1439///
David Goldmanfa8185c2019-04-08 19:52:45 +00001440/// Print attributes in the following order:
1441/// - class
1442/// - nonatomic | atomic
1443/// - assign | retain | strong | copy | weak | unsafe_unretained
1444/// - readwrite | readonly
1445/// - getter & setter
1446/// - nullability
Douglas Gregor278f52e2009-05-30 00:08:05 +00001447void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1448 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1449 Out << "@required\n";
1450 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1451 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001452
Douglas Gregor813a0662015-06-19 18:14:38 +00001453 QualType T = PDecl->getType();
1454
Douglas Gregor278f52e2009-05-30 00:08:05 +00001455 Out << "@property";
1456 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1457 bool first = true;
David Goldmanfa8185c2019-04-08 19:52:45 +00001458 Out << "(";
1459 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1460 Out << (first ? "" : ", ") << "class";
Ted Kremenek897af912011-08-17 21:09:35 +00001461 first = false;
1462 }
Mike Stump11289f42009-09-09 15:08:12 +00001463
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08001464 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_direct) {
1465 Out << (first ? "" : ", ") << "direct";
1466 first = false;
1467 }
1468
Ted Kremenek897af912011-08-17 21:09:35 +00001469 if (PDecl->getPropertyAttributes() &
1470 ObjCPropertyDecl::OBJC_PR_nonatomic) {
David Goldmanfa8185c2019-04-08 19:52:45 +00001471 Out << (first ? "" : ", ") << "nonatomic";
Ted Kremenek897af912011-08-17 21:09:35 +00001472 first = false;
1473 }
1474 if (PDecl->getPropertyAttributes() &
1475 ObjCPropertyDecl::OBJC_PR_atomic) {
David Goldmanfa8185c2019-04-08 19:52:45 +00001476 Out << (first ? "" : ", ") << "atomic";
1477 first = false;
1478 }
1479
1480 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1481 Out << (first ? "" : ", ") << "assign";
1482 first = false;
1483 }
1484 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1485 Out << (first ? "" : ", ") << "retain";
1486 first = false;
1487 }
1488
1489 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1490 Out << (first ? "" : ", ") << "strong";
1491 first = false;
1492 }
1493 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1494 Out << (first ? "" : ", ") << "copy";
1495 first = false;
1496 }
1497 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) {
1498 Out << (first ? "" : ", ") << "weak";
1499 first = false;
1500 }
1501 if (PDecl->getPropertyAttributes()
1502 & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
1503 Out << (first ? "" : ", ") << "unsafe_unretained";
1504 first = false;
1505 }
1506
1507 if (PDecl->getPropertyAttributes() &
1508 ObjCPropertyDecl::OBJC_PR_readwrite) {
1509 Out << (first ? "" : ", ") << "readwrite";
1510 first = false;
1511 }
1512 if (PDecl->getPropertyAttributes() &
1513 ObjCPropertyDecl::OBJC_PR_readonly) {
1514 Out << (first ? "" : ", ") << "readonly";
1515 first = false;
1516 }
1517
1518 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1519 Out << (first ? "" : ", ") << "getter = ";
1520 PDecl->getGetterName().print(Out);
1521 first = false;
1522 }
1523 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1524 Out << (first ? "" : ", ") << "setter = ";
1525 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001526 first = false;
1527 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001528
Douglas Gregor813a0662015-06-19 18:14:38 +00001529 if (PDecl->getPropertyAttributes() &
1530 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001531 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001532 if (*nullability == NullabilityKind::Unspecified &&
1533 (PDecl->getPropertyAttributes() &
1534 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
David Goldmanfa8185c2019-04-08 19:52:45 +00001535 Out << (first ? "" : ", ") << "null_resettable";
Douglas Gregor849ebc22015-06-19 18:14:46 +00001536 } else {
David Goldmanfa8185c2019-04-08 19:52:45 +00001537 Out << (first ? "" : ", ")
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001538 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001539 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001540 first = false;
1541 }
1542 }
1543
Ted Kremenek897af912011-08-17 21:09:35 +00001544 (void) first; // Silence dead store warning due to idiomatic code.
David Goldmanfa8185c2019-04-08 19:52:45 +00001545 Out << ")";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001546 }
David Goldmanfa8185c2019-04-08 19:52:45 +00001547 std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1548 getAsString(Policy);
1549 Out << ' ' << TypeStr;
1550 if (!StringRef(TypeStr).endswith("*"))
1551 Out << ' ';
1552 Out << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001553 if (Policy.PolishForDeclaration)
1554 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001555}
1556
1557void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1558 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001559 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001560 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001561 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001562 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001563 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001564 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001565}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001566
1567void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001568 if (!D->isAccessDeclaration())
1569 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001570 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001571 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001572 D->getQualifier()->print(Out, Policy);
Alex Lorenzea9b59a2016-10-03 12:22:17 +00001573
1574 // Use the correct record name when the using declaration is used for
1575 // inheriting constructors.
1576 for (const auto *Shadow : D->shadows()) {
1577 if (const auto *ConstructorShadow =
1578 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1579 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1580 Out << *ConstructorShadow->getNominatedBaseClass();
1581 return;
1582 }
1583 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001584 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001585}
1586
John McCalle61f2ba2009-11-18 02:36:19 +00001587void
1588DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1589 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001590 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001591 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001592}
1593
1594void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001595 if (!D->isAccessDeclaration())
1596 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001597 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001598 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001599}
John McCall3f746822009-11-17 05:59:44 +00001600
1601void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1602 // ignore
1603}
Alexey Bataeva769e072013-03-22 06:34:35 +00001604
1605void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1606 Out << "#pragma omp threadprivate";
1607 if (!D->varlist_empty()) {
1608 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1609 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001610 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001611 Out << (I == D->varlist_begin() ? '(' : ',');
George Burgess IV00f70bd2018-03-01 05:43:23 +00001612 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001613 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001614 }
1615 Out << ")";
1616 }
1617}
1618
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001619void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1620 Out << "#pragma omp allocate";
1621 if (!D->varlist_empty()) {
1622 for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(),
1623 E = D->varlist_end();
1624 I != E; ++I) {
1625 Out << (I == D->varlist_begin() ? '(' : ',');
1626 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1627 ND->printQualifiedName(Out);
1628 }
1629 Out << ")";
1630 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001631 if (!D->clauselist_empty()) {
1632 Out << " ";
1633 OMPClausePrinter Printer(Out, Policy);
1634 for (OMPClause *C : D->clauselists())
1635 Printer.Visit(C);
1636 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001637}
1638
Kelvin Li1408f912018-09-26 04:28:39 +00001639void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1640 Out << "#pragma omp requires ";
1641 if (!D->clauselist_empty()) {
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001642 OMPClausePrinter Printer(Out, Policy);
1643 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I)
1644 Printer.Visit(*I);
Kelvin Li1408f912018-09-26 04:28:39 +00001645 }
1646}
1647
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001648void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1649 if (!D->isInvalidDecl()) {
1650 Out << "#pragma omp declare reduction (";
1651 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001652 const char *OpName =
Richard Smith7fa2b742019-06-14 20:01:51 +00001653 getOperatorSpelling(D->getDeclName().getCXXOverloadedOperator());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001654 assert(OpName && "not an overloaded operator");
1655 Out << OpName;
1656 } else {
1657 assert(D->getDeclName().isIdentifier());
1658 D->printName(Out);
1659 }
1660 Out << " : ";
1661 D->getType().print(Out, Policy);
1662 Out << " : ";
1663 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1664 Out << ")";
1665 if (auto *Init = D->getInitializer()) {
1666 Out << " initializer(";
Alexey Bataev070f43a2017-09-06 14:49:58 +00001667 switch (D->getInitializerKind()) {
1668 case OMPDeclareReductionDecl::DirectInit:
1669 Out << "omp_priv(";
1670 break;
1671 case OMPDeclareReductionDecl::CopyInit:
1672 Out << "omp_priv = ";
1673 break;
1674 case OMPDeclareReductionDecl::CallInit:
1675 break;
1676 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001677 Init->printPretty(Out, nullptr, Policy, 0);
Alexey Bataev070f43a2017-09-06 14:49:58 +00001678 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1679 Out << ")";
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001680 Out << ")";
1681 }
1682 }
1683}
1684
Michael Kruse251e1482019-02-01 20:25:04 +00001685void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1686 if (!D->isInvalidDecl()) {
1687 Out << "#pragma omp declare mapper (";
1688 D->printName(Out);
1689 Out << " : ";
1690 D->getType().print(Out, Policy);
1691 Out << " ";
1692 Out << D->getVarName();
1693 Out << ")";
1694 if (!D->clauselist_empty()) {
1695 OMPClausePrinter Printer(Out, Policy);
1696 for (auto *C : D->clauselists()) {
1697 Out << " ";
1698 Printer.Visit(C);
1699 }
1700 }
1701 }
1702}
1703
Alexey Bataev4244be22016-02-11 05:35:55 +00001704void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001705 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1706}
1707