blob: 325400e4630c75fcd2d8c3894e5bd0960c62205f [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
Mike Stump11289f42009-09-09 15:08:12 +0000569void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000570 if (!D->getDescribedFunctionTemplate() &&
571 !D->isFunctionTemplateSpecialization())
572 prettyPrintPragmas(D);
573
Serge Pavlova67a4d22016-11-10 08:49:37 +0000574 if (D->isFunctionTemplateSpecialization())
575 Out << "template<> ";
Alex Lorenz47fd10c2017-04-18 15:12:34 +0000576 else if (!D->getDescribedFunctionTemplate()) {
577 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
578 I < NumTemplateParams; ++I)
579 printTemplateParameters(D->getTemplateParameterList(I));
580 }
Serge Pavlova67a4d22016-11-10 08:49:37 +0000581
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000582 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000583 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Richard Smith057ec502017-02-18 01:01:48 +0000584 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000585 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000586 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000587 case SC_None: break;
588 case SC_Extern: Out << "extern "; break;
589 case SC_Static: Out << "static "; break;
590 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000591 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000592 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000593 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000594
Douglas Gregor26701a42011-09-09 02:06:17 +0000595 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000596 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000597 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000598 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Hans Wennborgd2b9fc82019-05-06 09:51:10 +0000599 if ((CDecl && CDecl->isExplicitSpecified()) ||
600 (ConversionDecl && ConversionDecl->isExplicitSpecified()) ||
601 (GuideDecl && GuideDecl->isExplicitSpecified()))
602 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000603 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000604
Douglas Gregor2d042f12009-05-30 05:39:39 +0000605 PrintingPolicy SubPolicy(Policy);
606 SubPolicy.SuppressSpecifiers = false;
Alex Lorenza981c7d2017-04-11 16:46:03 +0000607 std::string Proto;
Serge Pavlov842022a2017-11-23 05:38:20 +0000608
609 if (Policy.FullyQualifiedName) {
610 Proto += D->getQualifiedNameAsString();
611 } else {
612 if (!Policy.SuppressScope) {
613 if (const NestedNameSpecifier *NS = D->getQualifier()) {
614 llvm::raw_string_ostream OS(Proto);
615 NS->print(OS, Policy);
616 }
Alex Lorenza981c7d2017-04-11 16:46:03 +0000617 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000618 Proto += D->getNameInfo().getAsString();
Alex Lorenza981c7d2017-04-11 16:46:03 +0000619 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000620
Richard Smith057ec502017-02-18 01:01:48 +0000621 if (GuideDecl)
622 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
Serge Pavlova67a4d22016-11-10 08:49:37 +0000623 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
624 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000625 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000626 TArgPrinter.printTemplateArguments(*TArgs);
627 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000628
Abramo Bagnara6d810632010-12-14 22:11:44 +0000629 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000630 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000631 Proto = '(' + Proto + ')';
632 Ty = PT->getInnerType();
633 }
634
Reid Kleckner0503a872013-12-05 01:23:43 +0000635 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000636 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000637 if (D->hasWrittenPrototype())
638 FT = dyn_cast<FunctionProtoType>(AFT);
639
640 Proto += "(";
641 if (FT) {
642 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000643 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000644 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
645 if (i) POut << ", ";
646 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
647 }
Mike Stump11289f42009-09-09 15:08:12 +0000648
Douglas Gregor278f52e2009-05-30 00:08:05 +0000649 if (FT->isVariadic()) {
650 if (D->getNumParams()) POut << ", ";
651 POut << "...";
652 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000653 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000654 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
655 if (i)
656 Proto += ", ";
657 Proto += D->getParamDecl(i)->getNameAsString();
658 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000659 }
660
661 Proto += ")";
Fangrui Song6907ce22018-07-30 19:24:48 +0000662
David Blaikief5697e52012-08-10 00:55:35 +0000663 if (FT) {
664 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000665 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000666 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000667 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000668 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000669 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000670
671 switch (FT->getRefQualifier()) {
672 case RQ_None:
673 break;
674 case RQ_LValue:
675 Proto += " &";
676 break;
677 case RQ_RValue:
678 Proto += " &&";
679 break;
680 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000681 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000682
683 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000684 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000685 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000686 Proto += "...";
Fangrui Song6907ce22018-07-30 19:24:48 +0000687 else
Douglas Gregor049bdca2009-12-08 17:45:32 +0000688 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
689 if (I)
690 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000691
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000692 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000693 }
694 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000695 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
696 Proto += " noexcept";
Richard Smitheaf11ad2018-05-03 03:58:32 +0000697 if (isComputedNoexcept(FT->getExceptionSpecType())) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000698 Proto += "(";
699 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000700 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000701 Indentation);
702 EOut.flush();
703 Proto += EOut.str();
704 Proto += ")";
705 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000706 }
707
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000708 if (CDecl) {
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000709 if (!Policy.TerseOutput)
710 PrintConstructorInitializers(CDecl, Proto);
Benjamin Kramer2907b082014-02-25 18:49:49 +0000711 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000712 if (FT && FT->hasTrailingReturn()) {
Richard Smith057ec502017-02-18 01:01:48 +0000713 if (!GuideDecl)
714 Out << "auto ";
715 Out << Proto << " -> ";
Richard Smithe6560762013-02-22 05:54:51 +0000716 Proto.clear();
717 }
Alp Toker314cc812014-01-25 16:55:45 +0000718 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000719 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000720 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000721 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000722 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000723 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000724 }
725
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000726 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000727
728 if (D->isPure())
729 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000730 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000731 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000732 else if (D->isExplicitlyDefaulted())
733 Out << " = default";
Serge Pavlova67a4d22016-11-10 08:49:37 +0000734 else if (D->doesThisDeclarationHaveABody()) {
735 if (!Policy.TerseOutput) {
736 if (!D->hasPrototype() && D->getNumParams()) {
737 // This is a K&R function definition, so we need to print the
738 // parameters.
739 Out << '\n';
Alex Lorenz36070ed2017-08-17 13:41:55 +0000740 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000741 Indentation += Policy.Indentation;
742 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
743 Indent();
744 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
745 Out << ";\n";
746 }
747 Indentation -= Policy.Indentation;
748 } else
749 Out << ' ';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000750
Serge Pavlova67a4d22016-11-10 08:49:37 +0000751 if (D->getBody())
752 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
753 } else {
Alex Lorenz35019db2017-11-16 01:28:25 +0000754 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
Serge Pavlova67a4d22016-11-10 08:49:37 +0000755 Out << " {}";
756 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000757 }
758}
759
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000760void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
761 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000762 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
763 for (unsigned i = 0; i < NumTPLists; ++i)
Serge Pavlova67a4d22016-11-10 08:49:37 +0000764 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000765 Out << "friend ";
766 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000767 }
768 else if (FunctionDecl *FD =
769 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
770 Out << "friend ";
771 VisitFunctionDecl(FD);
772 }
773 else if (FunctionTemplateDecl *FTD =
774 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
775 Out << "friend ";
776 VisitFunctionTemplateDecl(FTD);
777 }
778 else if (ClassTemplateDecl *CTD =
779 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
780 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000781 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000782 }
783}
784
Douglas Gregor278f52e2009-05-30 00:08:05 +0000785void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000786 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000787 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000788 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000789 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
790 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000791
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000792 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000793 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000794
795 if (D->isBitField()) {
796 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000797 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000798 }
Richard Smith938f40b2011-06-11 17:19:42 +0000799
800 Expr *Init = D->getInClassInitializer();
801 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000802 if (D->getInClassInitStyle() == ICIS_ListInit)
803 Out << " ";
804 else
805 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000806 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000807 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000808 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000809}
810
Chris Lattnercab02a62011-02-17 20:34:02 +0000811void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000812 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000813}
814
Douglas Gregor278f52e2009-05-30 00:08:05 +0000815void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000816 prettyPrintPragmas(D);
Vassil Vassilev10023732016-07-08 21:09:08 +0000817
818 QualType T = D->getTypeSourceInfo()
819 ? D->getTypeSourceInfo()->getType()
820 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
821
Richard Smithfd3834f2013-04-13 02:43:54 +0000822 if (!Policy.SuppressSpecifiers) {
823 StorageClass SC = D->getStorageClass();
824 if (SC != SC_None)
825 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000826
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000827 switch (D->getTSCSpec()) {
828 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000829 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000830 case TSCS___thread:
831 Out << "__thread ";
832 break;
833 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000834 Out << "_Thread_local ";
835 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000836 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000837 Out << "thread_local ";
838 break;
839 }
840
841 if (D->isModulePrivate())
842 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000843
844 if (D->isConstexpr()) {
845 Out << "constexpr ";
846 T.removeLocalConst();
847 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000848 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000849
Richard Smitha4bb2922014-07-23 03:17:06 +0000850 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000851 Expr *Init = D->getInit();
852 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000853 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000854 if (CXXConstructExpr *Construct =
855 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000856 if (D->getInitStyle() == VarDecl::CallInit &&
857 !Construct->isListInitialization()) {
858 ImplicitInit = Construct->getNumArgs() == 0 ||
859 Construct->getArg(0)->isDefaultArgument();
860 }
861 }
Sebastian Redla9351792012-02-11 23:51:47 +0000862 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000863 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000864 Out << "(";
865 else if (D->getInitStyle() == VarDecl::CInit) {
866 Out << " = ";
867 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000868 PrintingPolicy SubPolicy(Policy);
869 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000870 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000871 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000872 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000873 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000874 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000875 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000876 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000877}
878
879void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
880 VisitVarDecl(D);
881}
882
883void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
884 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000885 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000886 Out << ")";
887}
888
Douglas Gregorba345522011-12-02 23:23:56 +0000889void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000890 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000891 << ";\n";
892}
893
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000894void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
895 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000896 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000897 if (StringLiteral *SL = D->getMessage()) {
898 Out << ", ";
899 SL->printPretty(Out, nullptr, Policy, Indentation);
900 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000901 Out << ")";
902}
903
Douglas Gregor278f52e2009-05-30 00:08:05 +0000904//----------------------------------------------------------------------------
905// C++ declarations
906//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000907void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000908 if (D->isInline())
909 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000910 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000911 VisitDeclContext(D);
912 Indent() << "}";
913}
914
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000915void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
916 Out << "using namespace ";
917 if (D->getQualifier())
918 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000919 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000920}
921
Douglas Gregor18231932009-05-30 06:48:27 +0000922void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000923 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000924 if (D->getQualifier())
925 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000926 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000927}
928
Michael Han84324352013-02-22 17:15:32 +0000929void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
930 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000931}
932
Douglas Gregor5f478b72009-05-30 06:58:37 +0000933void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000934 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000935 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
936 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000937 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000938
939 prettyPrintAttributes(D);
940
Serge Pavlova67a4d22016-11-10 08:49:37 +0000941 if (D->getIdentifier()) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000942 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000943
Serge Pavlova67a4d22016-11-10 08:49:37 +0000944 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
945 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
946 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
947 printTemplateArguments(S->getTemplateArgs());
948 }
949
John McCallf937c022011-10-07 06:10:15 +0000950 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000951 // Print the base classes
952 if (D->getNumBases()) {
953 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000954 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
955 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000956 if (Base != D->bases_begin())
957 Out << ", ";
958
959 if (Base->isVirtual())
960 Out << "virtual ";
961
Anders Carlsson6df9e072009-08-29 20:36:12 +0000962 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000963 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000964 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000965 Out << " ";
966 }
967 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000968
969 if (Base->isPackExpansion())
970 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000971 }
972 }
973
974 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000975 // FIXME: Doesn't print access specifiers, e.g., "public:"
Serge Pavlova67a4d22016-11-10 08:49:37 +0000976 if (Policy.TerseOutput) {
977 Out << " {}";
978 } else {
979 Out << " {\n";
980 VisitDeclContext(D);
981 Indent() << "}";
982 }
Mike Stump11289f42009-09-09 15:08:12 +0000983 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000984}
985
986void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
987 const char *l;
988 if (D->getLanguage() == LinkageSpecDecl::lang_c)
989 l = "C";
990 else {
991 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
992 "unknown language in linkage specification");
993 l = "C++";
994 }
995
996 Out << "extern \"" << l << "\" ";
997 if (D->hasBraces()) {
998 Out << "{\n";
999 VisitDeclContext(D);
1000 Indent() << "}";
1001 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001002 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001003}
1004
Hamza Sood8205a812019-05-04 10:49:46 +00001005void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
1006 bool OmitTemplateKW) {
Richard Trieu82398f92011-07-28 00:19:05 +00001007 assert(Params);
Richard Trieu82398f92011-07-28 00:19:05 +00001008
Hamza Sood8205a812019-05-04 10:49:46 +00001009 if (!OmitTemplateKW)
1010 Out << "template ";
1011 Out << '<';
Mike Stump11289f42009-09-09 15:08:12 +00001012
Hamza Sood8205a812019-05-04 10:49:46 +00001013 bool NeedComma = false;
1014 for (const Decl *Param : *Params) {
1015 if (Param->isImplicit())
1016 continue;
1017
1018 if (NeedComma)
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001019 Out << ", ";
Hamza Sood8205a812019-05-04 10:49:46 +00001020 else
1021 NeedComma = true;
Mike Stump11289f42009-09-09 15:08:12 +00001022
Serge Pavlova67a4d22016-11-10 08:49:37 +00001023 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +00001024
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001025 if (TTP->wasDeclaredWithTypename())
Hamza Sood8205a812019-05-04 10:49:46 +00001026 Out << "typename";
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001027 else
Hamza Sood8205a812019-05-04 10:49:46 +00001028 Out << "class";
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001029
Anders Carlssonfb1d7762009-06-12 22:23:22 +00001030 if (TTP->isParameterPack())
Hamza Sood8205a812019-05-04 10:49:46 +00001031 Out << " ...";
1032 else if (!TTP->getName().empty())
1033 Out << ' ';
Mike Stump11289f42009-09-09 15:08:12 +00001034
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001035 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001036
Serge Pavlova67a4d22016-11-10 08:49:37 +00001037 if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001038 Out << " = ";
1039 Out << TTP->getDefaultArgument().getAsString(Policy);
1040 };
Serge Pavlova67a4d22016-11-10 08:49:37 +00001041 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +00001042 StringRef Name;
1043 if (IdentifierInfo *II = NTTP->getIdentifier())
1044 Name = II->getName();
1045 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +00001046
Serge Pavlova67a4d22016-11-10 08:49:37 +00001047 if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001048 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +00001049 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1050 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001051 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001052 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
Richard Smith030f4992011-04-15 13:38:57 +00001053 VisitTemplateDecl(TTPD);
1054 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001055 }
1056 }
Mike Stump11289f42009-09-09 15:08:12 +00001057
Hamza Sood8205a812019-05-04 10:49:46 +00001058 Out << '>';
1059 if (!OmitTemplateKW)
1060 Out << ' ';
Richard Trieu82398f92011-07-28 00:19:05 +00001061}
1062
Serge Pavlova67a4d22016-11-10 08:49:37 +00001063void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1064 const TemplateParameterList *Params) {
1065 Out << "<";
1066 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1067 const TemplateArgument &A = Args[I];
1068 if (I)
1069 Out << ", ";
1070 if (Params) {
1071 if (A.getKind() == TemplateArgument::Type)
1072 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1073 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1074 Out << *P;
1075 continue;
1076 }
1077 if (A.getKind() == TemplateArgument::Template) {
1078 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1079 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1080 auto P = cast<TemplateTemplateParmDecl>(
1081 Params->getParam(TD->getIndex()));
1082 Out << *P;
1083 continue;
1084 }
1085 }
1086 if (A.getKind() == TemplateArgument::Expression) {
1087 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1088 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1089 auto P = cast<NonTypeTemplateParmDecl>(
1090 Params->getParam(N->getIndex()));
1091 Out << *P;
1092 continue;
1093 }
1094 }
1095 }
1096 A.print(Policy, Out);
1097 }
1098 Out << ">";
1099}
1100
Richard Trieu82398f92011-07-28 00:19:05 +00001101void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001102 printTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001103
Richard Smith030f4992011-04-15 13:38:57 +00001104 if (const TemplateTemplateParmDecl *TTP =
1105 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +00001106 Out << "class ";
1107 if (TTP->isParameterPack())
1108 Out << "...";
1109 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +00001110 } else {
1111 Visit(D->getTemplatedDecl());
1112 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001113}
1114
Richard Trieu82398f92011-07-28 00:19:05 +00001115void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +00001116 prettyPrintPragmas(D->getTemplatedDecl());
Alex Lorenz47fd10c2017-04-18 15:12:34 +00001117 // Print any leading template parameter lists.
1118 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1119 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1120 I < NumTemplateParams; ++I)
1121 printTemplateParameters(FD->getTemplateParameterList(I));
1122 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001123 VisitRedeclarableTemplateDecl(D);
Alexey Bataev97b72212018-08-14 18:31:20 +00001124 // Declare target attribute is special one, natural spelling for the pragma
1125 // assumes "ending" construct so print it here.
1126 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1127 Out << "#pragma omp end declare target\n";
Serge Pavlova67a4d22016-11-10 08:49:37 +00001128
Richard Smith057ec502017-02-18 01:01:48 +00001129 // Never print "instantiations" for deduction guides (they don't really
1130 // have them).
1131 if (PrintInstantiation &&
1132 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001133 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1134 const FunctionDecl *Def;
1135 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1136 return;
1137 for (auto *I : D->specializations())
1138 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1139 if (!PrevDecl->isThisDeclarationADefinition())
1140 Out << ";\n";
1141 Indent();
1142 prettyPrintPragmas(I);
1143 Visit(I);
1144 }
1145 }
Richard Trieu82398f92011-07-28 00:19:05 +00001146}
1147
1148void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001149 VisitRedeclarableTemplateDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001150
Serge Pavlova67a4d22016-11-10 08:49:37 +00001151 if (PrintInstantiation) {
1152 for (auto *I : D->specializations())
1153 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1154 if (D->isThisDeclarationADefinition())
1155 Out << ";";
1156 Out << "\n";
1157 Visit(I);
1158 }
1159 }
1160}
1161
1162void DeclPrinter::VisitClassTemplateSpecializationDecl(
1163 ClassTemplateSpecializationDecl *D) {
1164 Out << "template<> ";
1165 VisitCXXRecordDecl(D);
1166}
1167
1168void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1169 ClassTemplatePartialSpecializationDecl *D) {
1170 printTemplateParameters(D->getTemplateParameters());
1171 VisitCXXRecordDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001172}
1173
Douglas Gregor278f52e2009-05-30 00:08:05 +00001174//----------------------------------------------------------------------------
1175// Objective-C declarations
1176//----------------------------------------------------------------------------
1177
Fangrui Song6907ce22018-07-30 19:24:48 +00001178void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1179 Decl::ObjCDeclQualifier Quals,
Douglas Gregor813a0662015-06-19 18:14:38 +00001180 QualType T) {
1181 Out << '(';
1182 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1183 Out << "in ";
1184 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1185 Out << "inout ";
1186 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1187 Out << "out ";
1188 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1189 Out << "bycopy ";
1190 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1191 Out << "byref ";
1192 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1193 Out << "oneway ";
1194 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001195 if (auto nullability = AttributedType::stripOuterNullability(T))
1196 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001197 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001198
Douglas Gregor813a0662015-06-19 18:14:38 +00001199 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1200 Out << ')';
1201}
1202
Douglas Gregor85f3f952015-07-07 03:57:15 +00001203void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1204 Out << "<";
1205 unsigned First = true;
1206 for (auto *Param : *Params) {
1207 if (First) {
1208 First = false;
1209 } else {
1210 Out << ", ";
1211 }
1212
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001213 switch (Param->getVariance()) {
1214 case ObjCTypeParamVariance::Invariant:
1215 break;
1216
1217 case ObjCTypeParamVariance::Covariant:
1218 Out << "__covariant ";
1219 break;
1220
1221 case ObjCTypeParamVariance::Contravariant:
1222 Out << "__contravariant ";
1223 break;
1224 }
1225
Douglas Gregor85f3f952015-07-07 03:57:15 +00001226 Out << Param->getDeclName().getAsString();
1227
1228 if (Param->hasExplicitBound()) {
1229 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1230 }
1231 }
1232 Out << ">";
1233}
1234
Douglas Gregor278f52e2009-05-30 00:08:05 +00001235void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1236 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001237 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001238 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001239 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001240 if (!OMD->getReturnType().isNull()) {
1241 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1242 OMD->getReturnType());
1243 }
Mike Stump11289f42009-09-09 15:08:12 +00001244
Douglas Gregor278f52e2009-05-30 00:08:05 +00001245 std::string name = OMD->getSelector().getAsString();
1246 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001247 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001248 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001249 pos = name.find_first_of(':', lastPos);
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001250 if (lastPos != 0)
1251 Out << " ";
1252 Out << name.substr(lastPos, pos - lastPos) << ':';
Fangrui Song6907ce22018-07-30 19:24:48 +00001253 PrintObjCMethodType(OMD->getASTContext(),
Douglas Gregor813a0662015-06-19 18:14:38 +00001254 PI->getObjCDeclQualifier(),
1255 PI->getType());
1256 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001257 lastPos = pos + 1;
1258 }
Mike Stump11289f42009-09-09 15:08:12 +00001259
Douglas Gregor278f52e2009-05-30 00:08:05 +00001260 if (OMD->param_begin() == OMD->param_end())
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001261 Out << name;
Mike Stump11289f42009-09-09 15:08:12 +00001262
Douglas Gregor278f52e2009-05-30 00:08:05 +00001263 if (OMD->isVariadic())
1264 Out << ", ...";
Fangrui Song6907ce22018-07-30 19:24:48 +00001265
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001266 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001267
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001268 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001269 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001270 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001271 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001272 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001273 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001274}
1275
1276void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1277 std::string I = OID->getNameAsString();
1278 ObjCInterfaceDecl *SID = OID->getSuperClass();
1279
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001280 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001281 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001282 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001283 else
1284 Out << "@implementation " << I;
Fangrui Song6907ce22018-07-30 19:24:48 +00001285
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001286 if (OID->ivar_size() > 0) {
1287 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001288 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001289 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001290 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001291 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001292 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001293 }
1294 Indentation -= Policy.Indentation;
1295 Out << "}\n";
1296 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001297 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1298 Out << "\n";
1299 eolnOut = true;
1300 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001301 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001302 if (!eolnOut)
1303 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001304 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001305}
1306
1307void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1308 std::string I = OID->getNameAsString();
1309 ObjCInterfaceDecl *SID = OID->getSuperClass();
1310
Douglas Gregordc9166c2011-12-15 20:29:51 +00001311 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001312 Out << "@class " << I;
1313
1314 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1315 PrintObjCTypeParams(TypeParams);
1316 }
1317
1318 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001319 return;
1320 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001321 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001322 Out << "@interface " << I;
1323
1324 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1325 PrintObjCTypeParams(TypeParams);
1326 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001327
Douglas Gregor278f52e2009-05-30 00:08:05 +00001328 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001329 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001330
Douglas Gregor278f52e2009-05-30 00:08:05 +00001331 // Protocols?
1332 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1333 if (!Protocols.empty()) {
1334 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1335 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001336 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001337 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001338 }
Mike Stump11289f42009-09-09 15:08:12 +00001339
Douglas Gregor278f52e2009-05-30 00:08:05 +00001340 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001341 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001342 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001343 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001344 for (const auto *I : OID->ivars()) {
1345 Indent() << I->getASTContext()
1346 .getUnqualifiedObjCPointerType(I->getType())
1347 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001348 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001349 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001350 Out << "}\n";
1351 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001352 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001353 Out << "\n";
1354 eolnOut = true;
1355 }
Mike Stump11289f42009-09-09 15:08:12 +00001356
Douglas Gregor278f52e2009-05-30 00:08:05 +00001357 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001358 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001359 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001360 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001361 // FIXME: implement the rest...
1362}
1363
Douglas Gregor278f52e2009-05-30 00:08:05 +00001364void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001365 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001366 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001367 return;
1368 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001369 // Protocols?
1370 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1371 if (!Protocols.empty()) {
1372 Out << "@protocol " << *PID;
1373 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1374 E = Protocols.end(); I != E; ++I)
1375 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1376 Out << ">\n";
1377 } else
1378 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001379 VisitDeclContext(PID, false);
1380 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001381}
1382
1383void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001384 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001385
1386 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001387 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001388 // FIXME: implement the rest...
1389}
1390
1391void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001392 Out << "@interface " << *PID->getClassInterface();
1393 if (auto TypeParams = PID->getTypeParamList()) {
1394 PrintObjCTypeParams(TypeParams);
1395 }
1396 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001397 if (PID->ivar_size() > 0) {
1398 Out << "{\n";
1399 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001400 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001401 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001402 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001403 Indentation -= Policy.Indentation;
1404 Out << "}\n";
1405 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001406
Douglas Gregor278f52e2009-05-30 00:08:05 +00001407 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001408 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001409
Douglas Gregor278f52e2009-05-30 00:08:05 +00001410 // FIXME: implement the rest...
1411}
1412
1413void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001414 Out << "@compatibility_alias " << *AID
1415 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001416}
1417
1418/// PrintObjCPropertyDecl - print a property declaration.
1419///
David Goldmanfa8185c2019-04-08 19:52:45 +00001420/// Print attributes in the following order:
1421/// - class
1422/// - nonatomic | atomic
1423/// - assign | retain | strong | copy | weak | unsafe_unretained
1424/// - readwrite | readonly
1425/// - getter & setter
1426/// - nullability
Douglas Gregor278f52e2009-05-30 00:08:05 +00001427void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1428 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1429 Out << "@required\n";
1430 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1431 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001432
Douglas Gregor813a0662015-06-19 18:14:38 +00001433 QualType T = PDecl->getType();
1434
Douglas Gregor278f52e2009-05-30 00:08:05 +00001435 Out << "@property";
1436 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1437 bool first = true;
David Goldmanfa8185c2019-04-08 19:52:45 +00001438 Out << "(";
1439 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1440 Out << (first ? "" : ", ") << "class";
Ted Kremenek897af912011-08-17 21:09:35 +00001441 first = false;
1442 }
Mike Stump11289f42009-09-09 15:08:12 +00001443
Ted Kremenek897af912011-08-17 21:09:35 +00001444 if (PDecl->getPropertyAttributes() &
1445 ObjCPropertyDecl::OBJC_PR_nonatomic) {
David Goldmanfa8185c2019-04-08 19:52:45 +00001446 Out << (first ? "" : ", ") << "nonatomic";
Ted Kremenek897af912011-08-17 21:09:35 +00001447 first = false;
1448 }
1449 if (PDecl->getPropertyAttributes() &
1450 ObjCPropertyDecl::OBJC_PR_atomic) {
David Goldmanfa8185c2019-04-08 19:52:45 +00001451 Out << (first ? "" : ", ") << "atomic";
1452 first = false;
1453 }
1454
1455 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1456 Out << (first ? "" : ", ") << "assign";
1457 first = false;
1458 }
1459 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1460 Out << (first ? "" : ", ") << "retain";
1461 first = false;
1462 }
1463
1464 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1465 Out << (first ? "" : ", ") << "strong";
1466 first = false;
1467 }
1468 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1469 Out << (first ? "" : ", ") << "copy";
1470 first = false;
1471 }
1472 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) {
1473 Out << (first ? "" : ", ") << "weak";
1474 first = false;
1475 }
1476 if (PDecl->getPropertyAttributes()
1477 & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
1478 Out << (first ? "" : ", ") << "unsafe_unretained";
1479 first = false;
1480 }
1481
1482 if (PDecl->getPropertyAttributes() &
1483 ObjCPropertyDecl::OBJC_PR_readwrite) {
1484 Out << (first ? "" : ", ") << "readwrite";
1485 first = false;
1486 }
1487 if (PDecl->getPropertyAttributes() &
1488 ObjCPropertyDecl::OBJC_PR_readonly) {
1489 Out << (first ? "" : ", ") << "readonly";
1490 first = false;
1491 }
1492
1493 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1494 Out << (first ? "" : ", ") << "getter = ";
1495 PDecl->getGetterName().print(Out);
1496 first = false;
1497 }
1498 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1499 Out << (first ? "" : ", ") << "setter = ";
1500 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001501 first = false;
1502 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001503
Douglas Gregor813a0662015-06-19 18:14:38 +00001504 if (PDecl->getPropertyAttributes() &
1505 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001506 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001507 if (*nullability == NullabilityKind::Unspecified &&
1508 (PDecl->getPropertyAttributes() &
1509 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
David Goldmanfa8185c2019-04-08 19:52:45 +00001510 Out << (first ? "" : ", ") << "null_resettable";
Douglas Gregor849ebc22015-06-19 18:14:46 +00001511 } else {
David Goldmanfa8185c2019-04-08 19:52:45 +00001512 Out << (first ? "" : ", ")
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001513 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001514 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001515 first = false;
1516 }
1517 }
1518
Ted Kremenek897af912011-08-17 21:09:35 +00001519 (void) first; // Silence dead store warning due to idiomatic code.
David Goldmanfa8185c2019-04-08 19:52:45 +00001520 Out << ")";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001521 }
David Goldmanfa8185c2019-04-08 19:52:45 +00001522 std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1523 getAsString(Policy);
1524 Out << ' ' << TypeStr;
1525 if (!StringRef(TypeStr).endswith("*"))
1526 Out << ' ';
1527 Out << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001528 if (Policy.PolishForDeclaration)
1529 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001530}
1531
1532void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1533 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001534 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001535 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001536 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001537 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001538 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001539 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001540}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001541
1542void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001543 if (!D->isAccessDeclaration())
1544 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001545 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001546 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001547 D->getQualifier()->print(Out, Policy);
Alex Lorenzea9b59a2016-10-03 12:22:17 +00001548
1549 // Use the correct record name when the using declaration is used for
1550 // inheriting constructors.
1551 for (const auto *Shadow : D->shadows()) {
1552 if (const auto *ConstructorShadow =
1553 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1554 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1555 Out << *ConstructorShadow->getNominatedBaseClass();
1556 return;
1557 }
1558 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001559 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001560}
1561
John McCalle61f2ba2009-11-18 02:36:19 +00001562void
1563DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1564 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001565 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001566 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001567}
1568
1569void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001570 if (!D->isAccessDeclaration())
1571 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001572 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001573 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001574}
John McCall3f746822009-11-17 05:59:44 +00001575
1576void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1577 // ignore
1578}
Alexey Bataeva769e072013-03-22 06:34:35 +00001579
1580void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1581 Out << "#pragma omp threadprivate";
1582 if (!D->varlist_empty()) {
1583 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1584 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001585 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001586 Out << (I == D->varlist_begin() ? '(' : ',');
George Burgess IV00f70bd2018-03-01 05:43:23 +00001587 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001588 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001589 }
1590 Out << ")";
1591 }
1592}
1593
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001594void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1595 Out << "#pragma omp allocate";
1596 if (!D->varlist_empty()) {
1597 for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(),
1598 E = D->varlist_end();
1599 I != E; ++I) {
1600 Out << (I == D->varlist_begin() ? '(' : ',');
1601 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1602 ND->printQualifiedName(Out);
1603 }
1604 Out << ")";
1605 }
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00001606 if (!D->clauselist_empty()) {
1607 Out << " ";
1608 OMPClausePrinter Printer(Out, Policy);
1609 for (OMPClause *C : D->clauselists())
1610 Printer.Visit(C);
1611 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001612}
1613
Kelvin Li1408f912018-09-26 04:28:39 +00001614void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1615 Out << "#pragma omp requires ";
1616 if (!D->clauselist_empty()) {
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001617 OMPClausePrinter Printer(Out, Policy);
1618 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I)
1619 Printer.Visit(*I);
Kelvin Li1408f912018-09-26 04:28:39 +00001620 }
1621}
1622
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001623void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1624 if (!D->isInvalidDecl()) {
1625 Out << "#pragma omp declare reduction (";
1626 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1627 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1628 nullptr,
1629#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1630 Spelling,
1631#include "clang/Basic/OperatorKinds.def"
1632 };
1633 const char *OpName =
1634 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1635 assert(OpName && "not an overloaded operator");
1636 Out << OpName;
1637 } else {
1638 assert(D->getDeclName().isIdentifier());
1639 D->printName(Out);
1640 }
1641 Out << " : ";
1642 D->getType().print(Out, Policy);
1643 Out << " : ";
1644 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1645 Out << ")";
1646 if (auto *Init = D->getInitializer()) {
1647 Out << " initializer(";
Alexey Bataev070f43a2017-09-06 14:49:58 +00001648 switch (D->getInitializerKind()) {
1649 case OMPDeclareReductionDecl::DirectInit:
1650 Out << "omp_priv(";
1651 break;
1652 case OMPDeclareReductionDecl::CopyInit:
1653 Out << "omp_priv = ";
1654 break;
1655 case OMPDeclareReductionDecl::CallInit:
1656 break;
1657 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001658 Init->printPretty(Out, nullptr, Policy, 0);
Alexey Bataev070f43a2017-09-06 14:49:58 +00001659 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1660 Out << ")";
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001661 Out << ")";
1662 }
1663 }
1664}
1665
Michael Kruse251e1482019-02-01 20:25:04 +00001666void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1667 if (!D->isInvalidDecl()) {
1668 Out << "#pragma omp declare mapper (";
1669 D->printName(Out);
1670 Out << " : ";
1671 D->getType().print(Out, Policy);
1672 Out << " ";
1673 Out << D->getVarName();
1674 Out << ")";
1675 if (!D->clauselist_empty()) {
1676 OMPClausePrinter Printer(Out, Policy);
1677 for (auto *C : D->clauselists()) {
1678 Out << " ";
1679 Printer.Visit(C);
1680 }
1681 }
1682 }
1683}
1684
Alexey Bataev4244be22016-02-11 05:35:55 +00001685void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001686 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1687}
1688