blob: 7fc275f90e9440c5df6f244e70df9978277c5cd7 [file] [log] [blame]
Douglas Gregor278f52e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Alexander Kornienko90ff6072012-12-20 02:09:13 +000010// This file implements the Decl::print method, which pretty prints the
Douglas Gregor278f52e2009-05-30 00:08:05 +000011// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.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);
Kelvin Li1408f912018-09-26 04:28:39 +0000103 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000104 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000105 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +0000106
Serge Pavlova67a4d22016-11-10 08:49:37 +0000107 void printTemplateParameters(const TemplateParameterList *Params);
108 void printTemplateArguments(const TemplateArgumentList &Args,
109 const TemplateParameterList *Params = nullptr);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000110 void prettyPrintAttributes(Decl *D);
Alexey Bataev6d455322015-10-12 06:59:48 +0000111 void prettyPrintPragmas(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +0000112 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000113 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000114}
Douglas Gregor278f52e2009-05-30 00:08:05 +0000115
Richard Trieu82398f92011-07-28 00:19:05 +0000116void Decl::print(raw_ostream &Out, unsigned Indentation,
117 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +0000118 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000119}
120
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000121void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000122 unsigned Indentation, bool PrintInstantiation) const {
Alex Lorenz36070ed2017-08-17 13:41:55 +0000123 DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
124 PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000125 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000126}
127
Eli Friedman79635842009-05-30 04:20:30 +0000128static QualType GetBaseType(QualType T) {
129 // FIXME: This should be on the Type class!
130 QualType BaseType = T;
131 while (!BaseType->isSpecifierType()) {
Artem Belevich224879e2018-01-17 19:29:39 +0000132 if (const PointerType *PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000133 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000134 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
135 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000136 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
137 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000138 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000139 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000140 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000141 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000142 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
143 BaseType = RTy->getPointeeType();
Vassil Vassilevcdaa31f2016-07-08 16:04:22 +0000144 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
145 BaseType = ATy->getDeducedType();
Artem Belevich224879e2018-01-17 19:29:39 +0000146 else if (const ParenType *PTy = BaseType->getAs<ParenType>())
147 BaseType = PTy->desugar();
Eli Friedman79635842009-05-30 04:20:30 +0000148 else
Artem Belevich224879e2018-01-17 19:29:39 +0000149 // This must be a syntax error.
150 break;
Eli Friedman79635842009-05-30 04:20:30 +0000151 }
152 return BaseType;
153}
154
155static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000156 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000157 return TDD->getUnderlyingType();
158 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
159 return VD->getType();
160 return QualType();
161}
162
163void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000164 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000165 unsigned Indentation) {
166 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000167 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000168 return;
169 }
170
171 Decl** End = Begin + NumDecls;
172 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
173 if (TD)
174 ++Begin;
175
176 PrintingPolicy SubPolicy(Policy);
Eli Friedman79635842009-05-30 04:20:30 +0000177
178 bool isFirst = true;
179 for ( ; Begin != End; ++Begin) {
180 if (isFirst) {
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000181 if(TD)
182 SubPolicy.IncludeTagDefinition = true;
Eli Friedman79635842009-05-30 04:20:30 +0000183 SubPolicy.SuppressSpecifiers = false;
184 isFirst = false;
185 } else {
186 if (!isFirst) Out << ", ";
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000187 SubPolicy.IncludeTagDefinition = false;
Eli Friedman79635842009-05-30 04:20:30 +0000188 SubPolicy.SuppressSpecifiers = true;
189 }
190
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000191 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000192 }
193}
194
Alp Tokeref6b0072014-01-04 13:47:14 +0000195LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000196 // Get the translation unit
197 const DeclContext *DC = this;
198 while (!DC->isTranslationUnit())
199 DC = DC->getParent();
Fangrui Song6907ce22018-07-30 19:24:48 +0000200
Anders Carlsson538af092009-12-09 17:27:46 +0000201 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Alex Lorenz36070ed2017-08-17 13:41:55 +0000202 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000203 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
204}
205
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000206raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000207 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000208 Out << " ";
209 return Out;
210}
211
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000212void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000213 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000214 return;
Alexey Bataev6d455322015-10-12 06:59:48 +0000215
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000216 if (D->hasAttrs()) {
217 AttrVec &Attrs = D->getAttrs();
Alexey Bataev6d455322015-10-12 06:59:48 +0000218 for (auto *A : Attrs) {
Joel E. Denny94548642018-05-15 22:16:47 +0000219 if (A->isInherited() || A->isImplicit())
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000220 continue;
Alexey Bataev6d455322015-10-12 06:59:48 +0000221 switch (A->getKind()) {
222#define ATTR(X)
223#define PRAGMA_SPELLING_ATTR(X) case attr::X:
224#include "clang/Basic/AttrList.inc"
225 break;
226 default:
227 A->printPretty(Out, Policy);
228 break;
229 }
230 }
231 }
232}
233
234void DeclPrinter::prettyPrintPragmas(Decl *D) {
235 if (Policy.PolishForDeclaration)
236 return;
237
238 if (D->hasAttrs()) {
239 AttrVec &Attrs = D->getAttrs();
240 for (auto *A : Attrs) {
241 switch (A->getKind()) {
242#define ATTR(X)
243#define PRAGMA_SPELLING_ATTR(X) case attr::X:
244#include "clang/Basic/AttrList.inc"
245 A->printPretty(Out, Policy);
246 Indent();
247 break;
248 default:
249 break;
250 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000251 }
252 }
253}
254
Richard Smitha4bb2922014-07-23 03:17:06 +0000255void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
256 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
257 // template argument), but if it is the type of a declaration, the ellipsis
258 // is placed before the name being declared.
259 if (auto *PET = T->getAs<PackExpansionType>()) {
260 Pack = true;
261 T = PET->getPattern();
262 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000263 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
Richard Smitha4bb2922014-07-23 03:17:06 +0000264}
265
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000266void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000267 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000268 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000269 Out << ";\n";
270 Decls.clear();
271
272}
273
Anders Carlsson601d6e42009-08-28 22:39:52 +0000274void DeclPrinter::Print(AccessSpecifier AS) {
275 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000276 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000277 case AS_public: Out << "public"; break;
278 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000279 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000280 }
281}
282
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000283void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
284 std::string &Proto) {
285 bool HasInitializerList = false;
286 for (const auto *BMInitializer : CDecl->inits()) {
287 if (BMInitializer->isInClassMemberInitializer())
288 continue;
289
290 if (!HasInitializerList) {
291 Proto += " : ";
292 Out << Proto;
293 Proto.clear();
294 HasInitializerList = true;
295 } else
296 Out << ", ";
297
298 if (BMInitializer->isAnyMemberInitializer()) {
299 FieldDecl *FD = BMInitializer->getAnyMember();
300 Out << *FD;
301 } else {
302 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
303 }
304
305 Out << "(";
306 if (!BMInitializer->getInit()) {
307 // Nothing to print
308 } else {
309 Expr *Init = BMInitializer->getInit();
310 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
311 Init = Tmp->getSubExpr();
312
313 Init = Init->IgnoreParens();
314
315 Expr *SimpleInit = nullptr;
316 Expr **Args = nullptr;
317 unsigned NumArgs = 0;
318 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
319 Args = ParenList->getExprs();
320 NumArgs = ParenList->getNumExprs();
321 } else if (CXXConstructExpr *Construct =
322 dyn_cast<CXXConstructExpr>(Init)) {
323 Args = Construct->getArgs();
324 NumArgs = Construct->getNumArgs();
325 } else
326 SimpleInit = Init;
327
328 if (SimpleInit)
329 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
330 else {
331 for (unsigned I = 0; I != NumArgs; ++I) {
332 assert(Args[I] != nullptr && "Expected non-null Expr");
333 if (isa<CXXDefaultArgExpr>(Args[I]))
334 break;
335
336 if (I)
337 Out << ", ";
338 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
339 }
340 }
341 }
342 Out << ")";
343 if (BMInitializer->isPackExpansion())
344 Out << "...";
345 }
346}
347
Douglas Gregor278f52e2009-05-30 00:08:05 +0000348//----------------------------------------------------------------------------
349// Common C declarations
350//----------------------------------------------------------------------------
351
352void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000353 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000354 return;
355
Douglas Gregor278f52e2009-05-30 00:08:05 +0000356 if (Indent)
357 Indentation += Policy.Indentation;
358
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000359 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000360 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000361 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000362
363 // Don't print ObjCIvarDecls, as they are printed when visiting the
364 // containing ObjCInterfaceDecl.
365 if (isa<ObjCIvarDecl>(*D))
366 continue;
367
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000368 // Skip over implicit declarations in pretty-printing mode.
369 if (D->isImplicit())
370 continue;
371
Serge Pavlova67a4d22016-11-10 08:49:37 +0000372 // Don't print implicit specializations, as they are printed when visiting
373 // corresponding templates.
374 if (auto FD = dyn_cast<FunctionDecl>(*D))
375 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
376 !isa<ClassTemplateSpecializationDecl>(DC))
377 continue;
378
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000379 // The next bits of code handle stuff like "struct {int x;} a,b"; we're
Eli Friedman79635842009-05-30 04:20:30 +0000380 // forced to merge the declarations because there's no other way to
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000381 // refer to the struct in question. When that struct is named instead, we
382 // also need to merge to avoid splitting off a stand-alone struct
383 // declaration that produces the warning ext_no_declarators in some
384 // contexts.
385 //
386 // This limited merging is safe without a bunch of other checks because it
387 // only merges declarations directly referring to the tag, not typedefs.
Eli Friedman79635842009-05-30 04:20:30 +0000388 //
389 // Check whether the current declaration should be grouped with a previous
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000390 // non-free-standing tag declaration.
Eli Friedman79635842009-05-30 04:20:30 +0000391 QualType CurDeclType = getDeclType(*D);
392 if (!Decls.empty() && !CurDeclType.isNull()) {
393 QualType BaseType = GetBaseType(CurDeclType);
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000394 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) &&
395 cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]) {
Eli Friedman79635842009-05-30 04:20:30 +0000396 Decls.push_back(*D);
397 continue;
398 }
399 }
400
401 // If we have a merged group waiting to be handled, handle it now.
402 if (!Decls.empty())
403 ProcessDeclGroup(Decls);
404
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000405 // If the current declaration is not a free standing declaration, save it
Eli Friedman79635842009-05-30 04:20:30 +0000406 // so we can merge it with the subsequent declaration(s) using it.
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000407 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->isFreeStanding()) {
Eli Friedman79635842009-05-30 04:20:30 +0000408 Decls.push_back(*D);
409 continue;
410 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000411
412 if (isa<AccessSpecDecl>(*D)) {
413 Indentation -= Policy.Indentation;
414 this->Indent();
415 Print(D->getAccess());
416 Out << ":\n";
417 Indentation += Policy.Indentation;
418 continue;
419 }
420
Douglas Gregor278f52e2009-05-30 00:08:05 +0000421 this->Indent();
422 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000423
424 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000425 const char *Terminator = nullptr;
Kelvin Li1408f912018-09-26 04:28:39 +0000426 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D) ||
427 isa<OMPRequiresDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000428 Terminator = nullptr;
Serge Pavlovdc586c42016-10-31 05:11:12 +0000429 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000430 Terminator = nullptr;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000431 else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
432 if (FD->isThisDeclarationADefinition())
433 Terminator = nullptr;
434 else
435 Terminator = ";";
436 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
437 if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
438 Terminator = nullptr;
439 else
440 Terminator = ";";
441 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000442 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000443 isa<ObjCInterfaceDecl>(*D) ||
444 isa<ObjCProtocolDecl>(*D) ||
445 isa<ObjCCategoryImplDecl>(*D) ||
446 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000447 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000448 else if (isa<EnumConstantDecl>(*D)) {
449 DeclContext::decl_iterator Next = D;
450 ++Next;
451 if (Next != DEnd)
452 Terminator = ",";
453 } else
454 Terminator = ";";
455
456 if (Terminator)
457 Out << Terminator;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000458 if (!Policy.TerseOutput &&
459 ((isa<FunctionDecl>(*D) &&
460 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
461 (isa<FunctionTemplateDecl>(*D) &&
462 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
463 ; // StmtPrinter already added '\n' after CompoundStmt.
464 else
465 Out << "\n";
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000466
467 // Declare target attribute is special one, natural spelling for the pragma
468 // assumes "ending" construct so print it here.
469 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
470 Out << "#pragma omp end declare target\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000471 }
472
Eli Friedman79635842009-05-30 04:20:30 +0000473 if (!Decls.empty())
474 ProcessDeclGroup(Decls);
475
Douglas Gregor278f52e2009-05-30 00:08:05 +0000476 if (Indent)
477 Indentation -= Policy.Indentation;
478}
479
480void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
481 VisitDeclContext(D, false);
482}
483
484void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000485 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000486 Out << "typedef ";
Fangrui Song6907ce22018-07-30 19:24:48 +0000487
Douglas Gregor26701a42011-09-09 02:06:17 +0000488 if (D->isModulePrivate())
489 Out << "__module_private__ ";
490 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000491 QualType Ty = D->getTypeSourceInfo()->getType();
492 Ty.print(Out, Policy, D->getName(), Indentation);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000493 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000494}
495
Richard Smithdda56e42011-04-15 14:24:37 +0000496void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000497 Out << "using " << *D;
498 prettyPrintAttributes(D);
499 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000500}
501
Douglas Gregor278f52e2009-05-30 00:08:05 +0000502void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000503 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
504 Out << "__module_private__ ";
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000505 Out << "enum";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000506 if (D->isScoped()) {
507 if (D->isScopedUsingClassTag())
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000508 Out << " class";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000509 else
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000510 Out << " struct";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000511 }
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000512
513 prettyPrintAttributes(D);
514
515 Out << ' ' << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000516
Serge Pavlov08c8de22016-11-04 06:03:34 +0000517 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000518 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000519
John McCallf937c022011-10-07 06:10:15 +0000520 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000521 Out << " {\n";
522 VisitDeclContext(D);
523 Indent() << "}";
524 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000525}
526
527void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000528 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
529 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000530 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000531
532 prettyPrintAttributes(D);
533
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000534 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000535 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000536
John McCallf937c022011-10-07 06:10:15 +0000537 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000538 Out << " {\n";
539 VisitDeclContext(D);
540 Indent() << "}";
541 }
542}
543
544void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000545 Out << *D;
Jordan Roseccca6692017-01-20 03:33:42 +0000546 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000547 if (Expr *Init = D->getInitExpr()) {
548 Out << " = ";
George Karpenkov64885ae2018-09-15 02:02:31 +0000549 Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000550 }
551}
552
Mike Stump11289f42009-09-09 15:08:12 +0000553void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000554 if (!D->getDescribedFunctionTemplate() &&
555 !D->isFunctionTemplateSpecialization())
556 prettyPrintPragmas(D);
557
Serge Pavlova67a4d22016-11-10 08:49:37 +0000558 if (D->isFunctionTemplateSpecialization())
559 Out << "template<> ";
Alex Lorenz47fd10c2017-04-18 15:12:34 +0000560 else if (!D->getDescribedFunctionTemplate()) {
561 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
562 I < NumTemplateParams; ++I)
563 printTemplateParameters(D->getTemplateParameterList(I));
564 }
Serge Pavlova67a4d22016-11-10 08:49:37 +0000565
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000566 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000567 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Richard Smith057ec502017-02-18 01:01:48 +0000568 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000569 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000570 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000571 case SC_None: break;
572 case SC_Extern: Out << "extern "; break;
573 case SC_Static: Out << "static "; break;
574 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000575 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000576 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000577 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000578
Douglas Gregor26701a42011-09-09 02:06:17 +0000579 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000580 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000581 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000582 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000583 if ((CDecl && CDecl->isExplicitSpecified()) ||
Richard Smith057ec502017-02-18 01:01:48 +0000584 (ConversionDecl && ConversionDecl->isExplicitSpecified()) ||
585 (GuideDecl && GuideDecl->isExplicitSpecified()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000586 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000587 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000588
Douglas Gregor2d042f12009-05-30 05:39:39 +0000589 PrintingPolicy SubPolicy(Policy);
590 SubPolicy.SuppressSpecifiers = false;
Alex Lorenza981c7d2017-04-11 16:46:03 +0000591 std::string Proto;
Serge Pavlov842022a2017-11-23 05:38:20 +0000592
593 if (Policy.FullyQualifiedName) {
594 Proto += D->getQualifiedNameAsString();
595 } else {
596 if (!Policy.SuppressScope) {
597 if (const NestedNameSpecifier *NS = D->getQualifier()) {
598 llvm::raw_string_ostream OS(Proto);
599 NS->print(OS, Policy);
600 }
Alex Lorenza981c7d2017-04-11 16:46:03 +0000601 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000602 Proto += D->getNameInfo().getAsString();
Alex Lorenza981c7d2017-04-11 16:46:03 +0000603 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000604
Richard Smith057ec502017-02-18 01:01:48 +0000605 if (GuideDecl)
606 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
Serge Pavlova67a4d22016-11-10 08:49:37 +0000607 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
608 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000609 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000610 TArgPrinter.printTemplateArguments(*TArgs);
611 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000612
Abramo Bagnara6d810632010-12-14 22:11:44 +0000613 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000614 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000615 Proto = '(' + Proto + ')';
616 Ty = PT->getInnerType();
617 }
618
Reid Kleckner0503a872013-12-05 01:23:43 +0000619 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000620 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000621 if (D->hasWrittenPrototype())
622 FT = dyn_cast<FunctionProtoType>(AFT);
623
624 Proto += "(";
625 if (FT) {
626 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000627 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000628 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
629 if (i) POut << ", ";
630 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
631 }
Mike Stump11289f42009-09-09 15:08:12 +0000632
Douglas Gregor278f52e2009-05-30 00:08:05 +0000633 if (FT->isVariadic()) {
634 if (D->getNumParams()) POut << ", ";
635 POut << "...";
636 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000637 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000638 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
639 if (i)
640 Proto += ", ";
641 Proto += D->getParamDecl(i)->getNameAsString();
642 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000643 }
644
645 Proto += ")";
Fangrui Song6907ce22018-07-30 19:24:48 +0000646
David Blaikief5697e52012-08-10 00:55:35 +0000647 if (FT) {
648 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000649 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000650 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000651 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000652 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000653 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000654
655 switch (FT->getRefQualifier()) {
656 case RQ_None:
657 break;
658 case RQ_LValue:
659 Proto += " &";
660 break;
661 case RQ_RValue:
662 Proto += " &&";
663 break;
664 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000665 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000666
667 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000668 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000669 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000670 Proto += "...";
Fangrui Song6907ce22018-07-30 19:24:48 +0000671 else
Douglas Gregor049bdca2009-12-08 17:45:32 +0000672 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
673 if (I)
674 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000675
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000676 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000677 }
678 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000679 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
680 Proto += " noexcept";
Richard Smitheaf11ad2018-05-03 03:58:32 +0000681 if (isComputedNoexcept(FT->getExceptionSpecType())) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000682 Proto += "(";
683 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000684 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000685 Indentation);
686 EOut.flush();
687 Proto += EOut.str();
688 Proto += ")";
689 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000690 }
691
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000692 if (CDecl) {
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000693 if (!Policy.TerseOutput)
694 PrintConstructorInitializers(CDecl, Proto);
Benjamin Kramer2907b082014-02-25 18:49:49 +0000695 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000696 if (FT && FT->hasTrailingReturn()) {
Richard Smith057ec502017-02-18 01:01:48 +0000697 if (!GuideDecl)
698 Out << "auto ";
699 Out << Proto << " -> ";
Richard Smithe6560762013-02-22 05:54:51 +0000700 Proto.clear();
701 }
Alp Toker314cc812014-01-25 16:55:45 +0000702 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000703 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000704 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000705 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000706 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000707 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000708 }
709
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000710 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000711
712 if (D->isPure())
713 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000714 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000715 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000716 else if (D->isExplicitlyDefaulted())
717 Out << " = default";
Serge Pavlova67a4d22016-11-10 08:49:37 +0000718 else if (D->doesThisDeclarationHaveABody()) {
719 if (!Policy.TerseOutput) {
720 if (!D->hasPrototype() && D->getNumParams()) {
721 // This is a K&R function definition, so we need to print the
722 // parameters.
723 Out << '\n';
Alex Lorenz36070ed2017-08-17 13:41:55 +0000724 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000725 Indentation += Policy.Indentation;
726 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
727 Indent();
728 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
729 Out << ";\n";
730 }
731 Indentation -= Policy.Indentation;
732 } else
733 Out << ' ';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000734
Serge Pavlova67a4d22016-11-10 08:49:37 +0000735 if (D->getBody())
736 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
737 } else {
Alex Lorenz35019db2017-11-16 01:28:25 +0000738 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
Serge Pavlova67a4d22016-11-10 08:49:37 +0000739 Out << " {}";
740 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000741 }
742}
743
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000744void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
745 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000746 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
747 for (unsigned i = 0; i < NumTPLists; ++i)
Serge Pavlova67a4d22016-11-10 08:49:37 +0000748 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000749 Out << "friend ";
750 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000751 }
752 else if (FunctionDecl *FD =
753 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
754 Out << "friend ";
755 VisitFunctionDecl(FD);
756 }
757 else if (FunctionTemplateDecl *FTD =
758 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
759 Out << "friend ";
760 VisitFunctionTemplateDecl(FTD);
761 }
762 else if (ClassTemplateDecl *CTD =
763 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
764 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000765 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000766 }
767}
768
Douglas Gregor278f52e2009-05-30 00:08:05 +0000769void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000770 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000771 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000772 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000773 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
774 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000775
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000776 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000777 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000778
779 if (D->isBitField()) {
780 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000781 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000782 }
Richard Smith938f40b2011-06-11 17:19:42 +0000783
784 Expr *Init = D->getInClassInitializer();
785 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000786 if (D->getInClassInitStyle() == ICIS_ListInit)
787 Out << " ";
788 else
789 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000790 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000791 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000792 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000793}
794
Chris Lattnercab02a62011-02-17 20:34:02 +0000795void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000796 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000797}
798
Douglas Gregor278f52e2009-05-30 00:08:05 +0000799void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000800 prettyPrintPragmas(D);
Vassil Vassilev10023732016-07-08 21:09:08 +0000801
802 QualType T = D->getTypeSourceInfo()
803 ? D->getTypeSourceInfo()->getType()
804 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
805
Richard Smithfd3834f2013-04-13 02:43:54 +0000806 if (!Policy.SuppressSpecifiers) {
807 StorageClass SC = D->getStorageClass();
808 if (SC != SC_None)
809 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000810
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000811 switch (D->getTSCSpec()) {
812 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000813 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000814 case TSCS___thread:
815 Out << "__thread ";
816 break;
817 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000818 Out << "_Thread_local ";
819 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000820 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000821 Out << "thread_local ";
822 break;
823 }
824
825 if (D->isModulePrivate())
826 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000827
828 if (D->isConstexpr()) {
829 Out << "constexpr ";
830 T.removeLocalConst();
831 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000832 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000833
Richard Smitha4bb2922014-07-23 03:17:06 +0000834 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000835 Expr *Init = D->getInit();
836 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000837 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000838 if (CXXConstructExpr *Construct =
839 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000840 if (D->getInitStyle() == VarDecl::CallInit &&
841 !Construct->isListInitialization()) {
842 ImplicitInit = Construct->getNumArgs() == 0 ||
843 Construct->getArg(0)->isDefaultArgument();
844 }
845 }
Sebastian Redla9351792012-02-11 23:51:47 +0000846 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000847 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000848 Out << "(";
849 else if (D->getInitStyle() == VarDecl::CInit) {
850 Out << " = ";
851 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000852 PrintingPolicy SubPolicy(Policy);
853 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000854 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000855 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000856 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000857 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000858 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000859 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000860 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000861}
862
863void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
864 VisitVarDecl(D);
865}
866
867void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
868 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000869 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000870 Out << ")";
871}
872
Douglas Gregorba345522011-12-02 23:23:56 +0000873void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000874 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000875 << ";\n";
876}
877
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000878void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
879 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000880 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000881 if (StringLiteral *SL = D->getMessage()) {
882 Out << ", ";
883 SL->printPretty(Out, nullptr, Policy, Indentation);
884 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000885 Out << ")";
886}
887
Douglas Gregor278f52e2009-05-30 00:08:05 +0000888//----------------------------------------------------------------------------
889// C++ declarations
890//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000891void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000892 if (D->isInline())
893 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000894 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000895 VisitDeclContext(D);
896 Indent() << "}";
897}
898
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000899void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
900 Out << "using namespace ";
901 if (D->getQualifier())
902 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000903 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000904}
905
Douglas Gregor18231932009-05-30 06:48:27 +0000906void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000907 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000908 if (D->getQualifier())
909 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000910 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000911}
912
Michael Han84324352013-02-22 17:15:32 +0000913void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
914 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000915}
916
Douglas Gregor5f478b72009-05-30 06:58:37 +0000917void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000918 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000919 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
920 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000921 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000922
923 prettyPrintAttributes(D);
924
Serge Pavlova67a4d22016-11-10 08:49:37 +0000925 if (D->getIdentifier()) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000926 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000927
Serge Pavlova67a4d22016-11-10 08:49:37 +0000928 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
929 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
930 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
931 printTemplateArguments(S->getTemplateArgs());
932 }
933
John McCallf937c022011-10-07 06:10:15 +0000934 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000935 // Print the base classes
936 if (D->getNumBases()) {
937 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000938 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
939 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000940 if (Base != D->bases_begin())
941 Out << ", ";
942
943 if (Base->isVirtual())
944 Out << "virtual ";
945
Anders Carlsson6df9e072009-08-29 20:36:12 +0000946 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000947 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000948 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000949 Out << " ";
950 }
951 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000952
953 if (Base->isPackExpansion())
954 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000955 }
956 }
957
958 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000959 // FIXME: Doesn't print access specifiers, e.g., "public:"
Serge Pavlova67a4d22016-11-10 08:49:37 +0000960 if (Policy.TerseOutput) {
961 Out << " {}";
962 } else {
963 Out << " {\n";
964 VisitDeclContext(D);
965 Indent() << "}";
966 }
Mike Stump11289f42009-09-09 15:08:12 +0000967 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000968}
969
970void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
971 const char *l;
972 if (D->getLanguage() == LinkageSpecDecl::lang_c)
973 l = "C";
974 else {
975 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
976 "unknown language in linkage specification");
977 l = "C++";
978 }
979
980 Out << "extern \"" << l << "\" ";
981 if (D->hasBraces()) {
982 Out << "{\n";
983 VisitDeclContext(D);
984 Indent() << "}";
985 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000986 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000987}
988
Serge Pavlova67a4d22016-11-10 08:49:37 +0000989void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params) {
Richard Trieu82398f92011-07-28 00:19:05 +0000990 assert(Params);
Richard Trieu82398f92011-07-28 00:19:05 +0000991
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000992 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000993
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000994 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
995 if (i != 0)
996 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000997
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000998 const Decl *Param = Params->getParam(i);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000999 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +00001000
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001001 if (TTP->wasDeclaredWithTypename())
1002 Out << "typename ";
1003 else
1004 Out << "class ";
1005
Anders Carlssonfb1d7762009-06-12 22:23:22 +00001006 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +00001007 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +00001008
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001009 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001010
Serge Pavlova67a4d22016-11-10 08:49:37 +00001011 if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001012 Out << " = ";
1013 Out << TTP->getDefaultArgument().getAsString(Policy);
1014 };
Serge Pavlova67a4d22016-11-10 08:49:37 +00001015 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +00001016 StringRef Name;
1017 if (IdentifierInfo *II = NTTP->getIdentifier())
1018 Name = II->getName();
1019 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +00001020
Serge Pavlova67a4d22016-11-10 08:49:37 +00001021 if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001022 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +00001023 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1024 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001025 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001026 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
Richard Smith030f4992011-04-15 13:38:57 +00001027 VisitTemplateDecl(TTPD);
1028 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001029 }
1030 }
Mike Stump11289f42009-09-09 15:08:12 +00001031
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001032 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +00001033}
1034
Serge Pavlova67a4d22016-11-10 08:49:37 +00001035void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1036 const TemplateParameterList *Params) {
1037 Out << "<";
1038 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1039 const TemplateArgument &A = Args[I];
1040 if (I)
1041 Out << ", ";
1042 if (Params) {
1043 if (A.getKind() == TemplateArgument::Type)
1044 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1045 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1046 Out << *P;
1047 continue;
1048 }
1049 if (A.getKind() == TemplateArgument::Template) {
1050 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1051 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1052 auto P = cast<TemplateTemplateParmDecl>(
1053 Params->getParam(TD->getIndex()));
1054 Out << *P;
1055 continue;
1056 }
1057 }
1058 if (A.getKind() == TemplateArgument::Expression) {
1059 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1060 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1061 auto P = cast<NonTypeTemplateParmDecl>(
1062 Params->getParam(N->getIndex()));
1063 Out << *P;
1064 continue;
1065 }
1066 }
1067 }
1068 A.print(Policy, Out);
1069 }
1070 Out << ">";
1071}
1072
Richard Trieu82398f92011-07-28 00:19:05 +00001073void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001074 printTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001075
Richard Smith030f4992011-04-15 13:38:57 +00001076 if (const TemplateTemplateParmDecl *TTP =
1077 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +00001078 Out << "class ";
1079 if (TTP->isParameterPack())
1080 Out << "...";
1081 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +00001082 } else {
1083 Visit(D->getTemplatedDecl());
1084 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001085}
1086
Richard Trieu82398f92011-07-28 00:19:05 +00001087void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +00001088 prettyPrintPragmas(D->getTemplatedDecl());
Alex Lorenz47fd10c2017-04-18 15:12:34 +00001089 // Print any leading template parameter lists.
1090 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1091 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1092 I < NumTemplateParams; ++I)
1093 printTemplateParameters(FD->getTemplateParameterList(I));
1094 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001095 VisitRedeclarableTemplateDecl(D);
Alexey Bataev97b72212018-08-14 18:31:20 +00001096 // Declare target attribute is special one, natural spelling for the pragma
1097 // assumes "ending" construct so print it here.
1098 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1099 Out << "#pragma omp end declare target\n";
Serge Pavlova67a4d22016-11-10 08:49:37 +00001100
Richard Smith057ec502017-02-18 01:01:48 +00001101 // Never print "instantiations" for deduction guides (they don't really
1102 // have them).
1103 if (PrintInstantiation &&
1104 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001105 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1106 const FunctionDecl *Def;
1107 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1108 return;
1109 for (auto *I : D->specializations())
1110 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1111 if (!PrevDecl->isThisDeclarationADefinition())
1112 Out << ";\n";
1113 Indent();
1114 prettyPrintPragmas(I);
1115 Visit(I);
1116 }
1117 }
Richard Trieu82398f92011-07-28 00:19:05 +00001118}
1119
1120void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001121 VisitRedeclarableTemplateDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001122
Serge Pavlova67a4d22016-11-10 08:49:37 +00001123 if (PrintInstantiation) {
1124 for (auto *I : D->specializations())
1125 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1126 if (D->isThisDeclarationADefinition())
1127 Out << ";";
1128 Out << "\n";
1129 Visit(I);
1130 }
1131 }
1132}
1133
1134void DeclPrinter::VisitClassTemplateSpecializationDecl(
1135 ClassTemplateSpecializationDecl *D) {
1136 Out << "template<> ";
1137 VisitCXXRecordDecl(D);
1138}
1139
1140void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1141 ClassTemplatePartialSpecializationDecl *D) {
1142 printTemplateParameters(D->getTemplateParameters());
1143 VisitCXXRecordDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001144}
1145
Douglas Gregor278f52e2009-05-30 00:08:05 +00001146//----------------------------------------------------------------------------
1147// Objective-C declarations
1148//----------------------------------------------------------------------------
1149
Fangrui Song6907ce22018-07-30 19:24:48 +00001150void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1151 Decl::ObjCDeclQualifier Quals,
Douglas Gregor813a0662015-06-19 18:14:38 +00001152 QualType T) {
1153 Out << '(';
1154 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1155 Out << "in ";
1156 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1157 Out << "inout ";
1158 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1159 Out << "out ";
1160 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1161 Out << "bycopy ";
1162 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1163 Out << "byref ";
1164 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1165 Out << "oneway ";
1166 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001167 if (auto nullability = AttributedType::stripOuterNullability(T))
1168 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001169 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001170
Douglas Gregor813a0662015-06-19 18:14:38 +00001171 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1172 Out << ')';
1173}
1174
Douglas Gregor85f3f952015-07-07 03:57:15 +00001175void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1176 Out << "<";
1177 unsigned First = true;
1178 for (auto *Param : *Params) {
1179 if (First) {
1180 First = false;
1181 } else {
1182 Out << ", ";
1183 }
1184
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001185 switch (Param->getVariance()) {
1186 case ObjCTypeParamVariance::Invariant:
1187 break;
1188
1189 case ObjCTypeParamVariance::Covariant:
1190 Out << "__covariant ";
1191 break;
1192
1193 case ObjCTypeParamVariance::Contravariant:
1194 Out << "__contravariant ";
1195 break;
1196 }
1197
Douglas Gregor85f3f952015-07-07 03:57:15 +00001198 Out << Param->getDeclName().getAsString();
1199
1200 if (Param->hasExplicitBound()) {
1201 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1202 }
1203 }
1204 Out << ">";
1205}
1206
Douglas Gregor278f52e2009-05-30 00:08:05 +00001207void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1208 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001209 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001210 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001211 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001212 if (!OMD->getReturnType().isNull()) {
1213 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1214 OMD->getReturnType());
1215 }
Mike Stump11289f42009-09-09 15:08:12 +00001216
Douglas Gregor278f52e2009-05-30 00:08:05 +00001217 std::string name = OMD->getSelector().getAsString();
1218 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001219 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001220 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001221 pos = name.find_first_of(':', lastPos);
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001222 if (lastPos != 0)
1223 Out << " ";
1224 Out << name.substr(lastPos, pos - lastPos) << ':';
Fangrui Song6907ce22018-07-30 19:24:48 +00001225 PrintObjCMethodType(OMD->getASTContext(),
Douglas Gregor813a0662015-06-19 18:14:38 +00001226 PI->getObjCDeclQualifier(),
1227 PI->getType());
1228 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001229 lastPos = pos + 1;
1230 }
Mike Stump11289f42009-09-09 15:08:12 +00001231
Douglas Gregor278f52e2009-05-30 00:08:05 +00001232 if (OMD->param_begin() == OMD->param_end())
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001233 Out << name;
Mike Stump11289f42009-09-09 15:08:12 +00001234
Douglas Gregor278f52e2009-05-30 00:08:05 +00001235 if (OMD->isVariadic())
1236 Out << ", ...";
Fangrui Song6907ce22018-07-30 19:24:48 +00001237
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001238 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001239
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001240 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001241 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001242 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001243 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001244 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001245 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001246}
1247
1248void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1249 std::string I = OID->getNameAsString();
1250 ObjCInterfaceDecl *SID = OID->getSuperClass();
1251
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001252 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001253 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001254 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001255 else
1256 Out << "@implementation " << I;
Fangrui Song6907ce22018-07-30 19:24:48 +00001257
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001258 if (OID->ivar_size() > 0) {
1259 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001260 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001261 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001262 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001263 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001264 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001265 }
1266 Indentation -= Policy.Indentation;
1267 Out << "}\n";
1268 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001269 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1270 Out << "\n";
1271 eolnOut = true;
1272 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001273 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001274 if (!eolnOut)
1275 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001276 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001277}
1278
1279void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1280 std::string I = OID->getNameAsString();
1281 ObjCInterfaceDecl *SID = OID->getSuperClass();
1282
Douglas Gregordc9166c2011-12-15 20:29:51 +00001283 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001284 Out << "@class " << I;
1285
1286 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1287 PrintObjCTypeParams(TypeParams);
1288 }
1289
1290 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001291 return;
1292 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001293 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001294 Out << "@interface " << I;
1295
1296 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1297 PrintObjCTypeParams(TypeParams);
1298 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001299
Douglas Gregor278f52e2009-05-30 00:08:05 +00001300 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001301 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001302
Douglas Gregor278f52e2009-05-30 00:08:05 +00001303 // Protocols?
1304 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1305 if (!Protocols.empty()) {
1306 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1307 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001308 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001309 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001310 }
Mike Stump11289f42009-09-09 15:08:12 +00001311
Douglas Gregor278f52e2009-05-30 00:08:05 +00001312 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001313 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001314 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001315 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001316 for (const auto *I : OID->ivars()) {
1317 Indent() << I->getASTContext()
1318 .getUnqualifiedObjCPointerType(I->getType())
1319 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001320 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001321 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001322 Out << "}\n";
1323 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001324 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001325 Out << "\n";
1326 eolnOut = true;
1327 }
Mike Stump11289f42009-09-09 15:08:12 +00001328
Douglas Gregor278f52e2009-05-30 00:08:05 +00001329 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001330 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001331 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001332 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001333 // FIXME: implement the rest...
1334}
1335
Douglas Gregor278f52e2009-05-30 00:08:05 +00001336void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001337 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001338 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001339 return;
1340 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001341 // Protocols?
1342 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1343 if (!Protocols.empty()) {
1344 Out << "@protocol " << *PID;
1345 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1346 E = Protocols.end(); I != E; ++I)
1347 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1348 Out << ">\n";
1349 } else
1350 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001351 VisitDeclContext(PID, false);
1352 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001353}
1354
1355void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001356 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001357
1358 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001359 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001360 // FIXME: implement the rest...
1361}
1362
1363void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001364 Out << "@interface " << *PID->getClassInterface();
1365 if (auto TypeParams = PID->getTypeParamList()) {
1366 PrintObjCTypeParams(TypeParams);
1367 }
1368 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001369 if (PID->ivar_size() > 0) {
1370 Out << "{\n";
1371 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001372 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001373 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001374 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001375 Indentation -= Policy.Indentation;
1376 Out << "}\n";
1377 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001378
Douglas Gregor278f52e2009-05-30 00:08:05 +00001379 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001380 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001381
Douglas Gregor278f52e2009-05-30 00:08:05 +00001382 // FIXME: implement the rest...
1383}
1384
1385void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001386 Out << "@compatibility_alias " << *AID
1387 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001388}
1389
1390/// PrintObjCPropertyDecl - print a property declaration.
1391///
1392void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1393 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1394 Out << "@required\n";
1395 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1396 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001397
Douglas Gregor813a0662015-06-19 18:14:38 +00001398 QualType T = PDecl->getType();
1399
Douglas Gregor278f52e2009-05-30 00:08:05 +00001400 Out << "@property";
1401 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1402 bool first = true;
1403 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001404 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001405 ObjCPropertyDecl::OBJC_PR_readonly) {
1406 Out << (first ? ' ' : ',') << "readonly";
1407 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001408 }
Mike Stump11289f42009-09-09 15:08:12 +00001409
Ted Kremenek897af912011-08-17 21:09:35 +00001410 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001411 Out << (first ? ' ' : ',') << "getter = ";
1412 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001413 first = false;
1414 }
1415 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001416 Out << (first ? ' ' : ',') << "setter = ";
1417 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001418 first = false;
1419 }
Mike Stump11289f42009-09-09 15:08:12 +00001420
Ted Kremenek897af912011-08-17 21:09:35 +00001421 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1422 Out << (first ? ' ' : ',') << "assign";
1423 first = false;
1424 }
Mike Stump11289f42009-09-09 15:08:12 +00001425
Ted Kremenek897af912011-08-17 21:09:35 +00001426 if (PDecl->getPropertyAttributes() &
1427 ObjCPropertyDecl::OBJC_PR_readwrite) {
1428 Out << (first ? ' ' : ',') << "readwrite";
1429 first = false;
1430 }
Mike Stump11289f42009-09-09 15:08:12 +00001431
Ted Kremenek897af912011-08-17 21:09:35 +00001432 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1433 Out << (first ? ' ' : ',') << "retain";
1434 first = false;
1435 }
Mike Stump11289f42009-09-09 15:08:12 +00001436
Ted Kremenek897af912011-08-17 21:09:35 +00001437 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1438 Out << (first ? ' ' : ',') << "strong";
1439 first = false;
1440 }
John McCall31168b02011-06-15 23:02:42 +00001441
Ted Kremenek897af912011-08-17 21:09:35 +00001442 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1443 Out << (first ? ' ' : ',') << "copy";
1444 first = false;
1445 }
Mike Stump11289f42009-09-09 15:08:12 +00001446
Ted Kremenek897af912011-08-17 21:09:35 +00001447 if (PDecl->getPropertyAttributes() &
1448 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1449 Out << (first ? ' ' : ',') << "nonatomic";
1450 first = false;
1451 }
1452 if (PDecl->getPropertyAttributes() &
1453 ObjCPropertyDecl::OBJC_PR_atomic) {
1454 Out << (first ? ' ' : ',') << "atomic";
1455 first = false;
1456 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001457
Douglas Gregor813a0662015-06-19 18:14:38 +00001458 if (PDecl->getPropertyAttributes() &
1459 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001460 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001461 if (*nullability == NullabilityKind::Unspecified &&
1462 (PDecl->getPropertyAttributes() &
1463 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1464 Out << (first ? ' ' : ',') << "null_resettable";
1465 } else {
1466 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001467 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001468 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001469 first = false;
1470 }
1471 }
1472
Manman Ren387ff7f2016-01-26 18:52:43 +00001473 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1474 Out << (first ? ' ' : ',') << "class";
1475 first = false;
1476 }
1477
Ted Kremenek897af912011-08-17 21:09:35 +00001478 (void) first; // Silence dead store warning due to idiomatic code.
1479 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001480 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001481 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001482 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001483 if (Policy.PolishForDeclaration)
1484 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001485}
1486
1487void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1488 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001489 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001490 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001491 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001492 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001493 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001494 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001495}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001496
1497void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001498 if (!D->isAccessDeclaration())
1499 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001500 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001501 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001502 D->getQualifier()->print(Out, Policy);
Alex Lorenzea9b59a2016-10-03 12:22:17 +00001503
1504 // Use the correct record name when the using declaration is used for
1505 // inheriting constructors.
1506 for (const auto *Shadow : D->shadows()) {
1507 if (const auto *ConstructorShadow =
1508 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1509 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1510 Out << *ConstructorShadow->getNominatedBaseClass();
1511 return;
1512 }
1513 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001514 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001515}
1516
John McCalle61f2ba2009-11-18 02:36:19 +00001517void
1518DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1519 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001520 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001521 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001522}
1523
1524void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001525 if (!D->isAccessDeclaration())
1526 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001527 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001528 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001529}
John McCall3f746822009-11-17 05:59:44 +00001530
1531void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1532 // ignore
1533}
Alexey Bataeva769e072013-03-22 06:34:35 +00001534
1535void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1536 Out << "#pragma omp threadprivate";
1537 if (!D->varlist_empty()) {
1538 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1539 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001540 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001541 Out << (I == D->varlist_begin() ? '(' : ',');
George Burgess IV00f70bd2018-03-01 05:43:23 +00001542 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001543 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001544 }
1545 Out << ")";
1546 }
1547}
1548
Kelvin Li1408f912018-09-26 04:28:39 +00001549void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1550 Out << "#pragma omp requires ";
1551 if (!D->clauselist_empty()) {
1552 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I) {
1553 if (I != D->clauselist_begin())
1554 Out << ',';
1555 Out << getOpenMPClauseName((*I)->getClauseKind());
1556 }
1557 }
1558}
1559
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001560void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1561 if (!D->isInvalidDecl()) {
1562 Out << "#pragma omp declare reduction (";
1563 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1564 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1565 nullptr,
1566#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1567 Spelling,
1568#include "clang/Basic/OperatorKinds.def"
1569 };
1570 const char *OpName =
1571 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1572 assert(OpName && "not an overloaded operator");
1573 Out << OpName;
1574 } else {
1575 assert(D->getDeclName().isIdentifier());
1576 D->printName(Out);
1577 }
1578 Out << " : ";
1579 D->getType().print(Out, Policy);
1580 Out << " : ";
1581 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1582 Out << ")";
1583 if (auto *Init = D->getInitializer()) {
1584 Out << " initializer(";
Alexey Bataev070f43a2017-09-06 14:49:58 +00001585 switch (D->getInitializerKind()) {
1586 case OMPDeclareReductionDecl::DirectInit:
1587 Out << "omp_priv(";
1588 break;
1589 case OMPDeclareReductionDecl::CopyInit:
1590 Out << "omp_priv = ";
1591 break;
1592 case OMPDeclareReductionDecl::CallInit:
1593 break;
1594 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001595 Init->printPretty(Out, nullptr, Policy, 0);
Alexey Bataev070f43a2017-09-06 14:49:58 +00001596 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1597 Out << ")";
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001598 Out << ")";
1599 }
1600 }
1601}
1602
Alexey Bataev4244be22016-02-11 05:35:55 +00001603void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001604 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1605}
1606