blob: 55485611054c3b770141d529e2fceed7072480b2 [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);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000103 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000104 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +0000105
Serge Pavlova67a4d22016-11-10 08:49:37 +0000106 void printTemplateParameters(const TemplateParameterList *Params);
107 void printTemplateArguments(const TemplateArgumentList &Args,
108 const TemplateParameterList *Params = nullptr);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000109 void prettyPrintAttributes(Decl *D);
Alexey Bataev6d455322015-10-12 06:59:48 +0000110 void prettyPrintPragmas(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +0000111 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000112 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000113}
Douglas Gregor278f52e2009-05-30 00:08:05 +0000114
Richard Trieu82398f92011-07-28 00:19:05 +0000115void Decl::print(raw_ostream &Out, unsigned Indentation,
116 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +0000117 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000118}
119
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000120void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000121 unsigned Indentation, bool PrintInstantiation) const {
Alex Lorenz36070ed2017-08-17 13:41:55 +0000122 DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
123 PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000124 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000125}
126
Eli Friedman79635842009-05-30 04:20:30 +0000127static QualType GetBaseType(QualType T) {
128 // FIXME: This should be on the Type class!
129 QualType BaseType = T;
130 while (!BaseType->isSpecifierType()) {
Artem Belevich224879e2018-01-17 19:29:39 +0000131 if (const PointerType *PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000132 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000133 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
134 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000135 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
136 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000137 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000138 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000139 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000140 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000141 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
142 BaseType = RTy->getPointeeType();
Vassil Vassilevcdaa31f2016-07-08 16:04:22 +0000143 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
144 BaseType = ATy->getDeducedType();
Artem Belevich224879e2018-01-17 19:29:39 +0000145 else if (const ParenType *PTy = BaseType->getAs<ParenType>())
146 BaseType = PTy->desugar();
Eli Friedman79635842009-05-30 04:20:30 +0000147 else
Artem Belevich224879e2018-01-17 19:29:39 +0000148 // This must be a syntax error.
149 break;
Eli Friedman79635842009-05-30 04:20:30 +0000150 }
151 return BaseType;
152}
153
154static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000155 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000156 return TDD->getUnderlyingType();
157 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
158 return VD->getType();
159 return QualType();
160}
161
162void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000163 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000164 unsigned Indentation) {
165 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000166 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000167 return;
168 }
169
170 Decl** End = Begin + NumDecls;
171 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
172 if (TD)
173 ++Begin;
174
175 PrintingPolicy SubPolicy(Policy);
Eli Friedman79635842009-05-30 04:20:30 +0000176
177 bool isFirst = true;
178 for ( ; Begin != End; ++Begin) {
179 if (isFirst) {
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000180 if(TD)
181 SubPolicy.IncludeTagDefinition = true;
Eli Friedman79635842009-05-30 04:20:30 +0000182 SubPolicy.SuppressSpecifiers = false;
183 isFirst = false;
184 } else {
185 if (!isFirst) Out << ", ";
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000186 SubPolicy.IncludeTagDefinition = false;
Eli Friedman79635842009-05-30 04:20:30 +0000187 SubPolicy.SuppressSpecifiers = true;
188 }
189
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000190 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000191 }
192}
193
Alp Tokeref6b0072014-01-04 13:47:14 +0000194LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000195 // Get the translation unit
196 const DeclContext *DC = this;
197 while (!DC->isTranslationUnit())
198 DC = DC->getParent();
Fangrui Song6907ce22018-07-30 19:24:48 +0000199
Anders Carlsson538af092009-12-09 17:27:46 +0000200 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Alex Lorenz36070ed2017-08-17 13:41:55 +0000201 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000202 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
203}
204
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000205raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000206 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000207 Out << " ";
208 return Out;
209}
210
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000211void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000212 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000213 return;
Alexey Bataev6d455322015-10-12 06:59:48 +0000214
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000215 if (D->hasAttrs()) {
216 AttrVec &Attrs = D->getAttrs();
Alexey Bataev6d455322015-10-12 06:59:48 +0000217 for (auto *A : Attrs) {
Joel E. Denny94548642018-05-15 22:16:47 +0000218 if (A->isInherited() || A->isImplicit())
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000219 continue;
Alexey Bataev6d455322015-10-12 06:59:48 +0000220 switch (A->getKind()) {
221#define ATTR(X)
222#define PRAGMA_SPELLING_ATTR(X) case attr::X:
223#include "clang/Basic/AttrList.inc"
224 break;
225 default:
226 A->printPretty(Out, Policy);
227 break;
228 }
229 }
230 }
231}
232
233void DeclPrinter::prettyPrintPragmas(Decl *D) {
234 if (Policy.PolishForDeclaration)
235 return;
236
237 if (D->hasAttrs()) {
238 AttrVec &Attrs = D->getAttrs();
239 for (auto *A : Attrs) {
240 switch (A->getKind()) {
241#define ATTR(X)
242#define PRAGMA_SPELLING_ATTR(X) case attr::X:
243#include "clang/Basic/AttrList.inc"
244 A->printPretty(Out, Policy);
245 Indent();
246 break;
247 default:
248 break;
249 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000250 }
251 }
252}
253
Richard Smitha4bb2922014-07-23 03:17:06 +0000254void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
255 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
256 // template argument), but if it is the type of a declaration, the ellipsis
257 // is placed before the name being declared.
258 if (auto *PET = T->getAs<PackExpansionType>()) {
259 Pack = true;
260 T = PET->getPattern();
261 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000262 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
Richard Smitha4bb2922014-07-23 03:17:06 +0000263}
264
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000265void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000266 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000267 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000268 Out << ";\n";
269 Decls.clear();
270
271}
272
Anders Carlsson601d6e42009-08-28 22:39:52 +0000273void DeclPrinter::Print(AccessSpecifier AS) {
274 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000275 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000276 case AS_public: Out << "public"; break;
277 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000278 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000279 }
280}
281
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000282void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
283 std::string &Proto) {
284 bool HasInitializerList = false;
285 for (const auto *BMInitializer : CDecl->inits()) {
286 if (BMInitializer->isInClassMemberInitializer())
287 continue;
288
289 if (!HasInitializerList) {
290 Proto += " : ";
291 Out << Proto;
292 Proto.clear();
293 HasInitializerList = true;
294 } else
295 Out << ", ";
296
297 if (BMInitializer->isAnyMemberInitializer()) {
298 FieldDecl *FD = BMInitializer->getAnyMember();
299 Out << *FD;
300 } else {
301 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
302 }
303
304 Out << "(";
305 if (!BMInitializer->getInit()) {
306 // Nothing to print
307 } else {
308 Expr *Init = BMInitializer->getInit();
309 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
310 Init = Tmp->getSubExpr();
311
312 Init = Init->IgnoreParens();
313
314 Expr *SimpleInit = nullptr;
315 Expr **Args = nullptr;
316 unsigned NumArgs = 0;
317 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
318 Args = ParenList->getExprs();
319 NumArgs = ParenList->getNumExprs();
320 } else if (CXXConstructExpr *Construct =
321 dyn_cast<CXXConstructExpr>(Init)) {
322 Args = Construct->getArgs();
323 NumArgs = Construct->getNumArgs();
324 } else
325 SimpleInit = Init;
326
327 if (SimpleInit)
328 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
329 else {
330 for (unsigned I = 0; I != NumArgs; ++I) {
331 assert(Args[I] != nullptr && "Expected non-null Expr");
332 if (isa<CXXDefaultArgExpr>(Args[I]))
333 break;
334
335 if (I)
336 Out << ", ";
337 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
338 }
339 }
340 }
341 Out << ")";
342 if (BMInitializer->isPackExpansion())
343 Out << "...";
344 }
345}
346
Douglas Gregor278f52e2009-05-30 00:08:05 +0000347//----------------------------------------------------------------------------
348// Common C declarations
349//----------------------------------------------------------------------------
350
351void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000352 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000353 return;
354
Douglas Gregor278f52e2009-05-30 00:08:05 +0000355 if (Indent)
356 Indentation += Policy.Indentation;
357
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000358 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000359 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000360 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000361
362 // Don't print ObjCIvarDecls, as they are printed when visiting the
363 // containing ObjCInterfaceDecl.
364 if (isa<ObjCIvarDecl>(*D))
365 continue;
366
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000367 // Skip over implicit declarations in pretty-printing mode.
368 if (D->isImplicit())
369 continue;
370
Serge Pavlova67a4d22016-11-10 08:49:37 +0000371 // Don't print implicit specializations, as they are printed when visiting
372 // corresponding templates.
373 if (auto FD = dyn_cast<FunctionDecl>(*D))
374 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
375 !isa<ClassTemplateSpecializationDecl>(DC))
376 continue;
377
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000378 // The next bits of code handle stuff like "struct {int x;} a,b"; we're
Eli Friedman79635842009-05-30 04:20:30 +0000379 // forced to merge the declarations because there's no other way to
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000380 // refer to the struct in question. When that struct is named instead, we
381 // also need to merge to avoid splitting off a stand-alone struct
382 // declaration that produces the warning ext_no_declarators in some
383 // contexts.
384 //
385 // This limited merging is safe without a bunch of other checks because it
386 // only merges declarations directly referring to the tag, not typedefs.
Eli Friedman79635842009-05-30 04:20:30 +0000387 //
388 // Check whether the current declaration should be grouped with a previous
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000389 // non-free-standing tag declaration.
Eli Friedman79635842009-05-30 04:20:30 +0000390 QualType CurDeclType = getDeclType(*D);
391 if (!Decls.empty() && !CurDeclType.isNull()) {
392 QualType BaseType = GetBaseType(CurDeclType);
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000393 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) &&
394 cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]) {
Eli Friedman79635842009-05-30 04:20:30 +0000395 Decls.push_back(*D);
396 continue;
397 }
398 }
399
400 // If we have a merged group waiting to be handled, handle it now.
401 if (!Decls.empty())
402 ProcessDeclGroup(Decls);
403
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000404 // If the current declaration is not a free standing declaration, save it
Eli Friedman79635842009-05-30 04:20:30 +0000405 // so we can merge it with the subsequent declaration(s) using it.
Joel E. Dennyae7c9442018-05-15 00:44:14 +0000406 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->isFreeStanding()) {
Eli Friedman79635842009-05-30 04:20:30 +0000407 Decls.push_back(*D);
408 continue;
409 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000410
411 if (isa<AccessSpecDecl>(*D)) {
412 Indentation -= Policy.Indentation;
413 this->Indent();
414 Print(D->getAccess());
415 Out << ":\n";
416 Indentation += Policy.Indentation;
417 continue;
418 }
419
Douglas Gregor278f52e2009-05-30 00:08:05 +0000420 this->Indent();
421 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000422
423 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000424 const char *Terminator = nullptr;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000425 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000426 Terminator = nullptr;
Serge Pavlovdc586c42016-10-31 05:11:12 +0000427 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000428 Terminator = nullptr;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000429 else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
430 if (FD->isThisDeclarationADefinition())
431 Terminator = nullptr;
432 else
433 Terminator = ";";
434 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
435 if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
436 Terminator = nullptr;
437 else
438 Terminator = ";";
439 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000440 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000441 isa<ObjCInterfaceDecl>(*D) ||
442 isa<ObjCProtocolDecl>(*D) ||
443 isa<ObjCCategoryImplDecl>(*D) ||
444 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000445 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000446 else if (isa<EnumConstantDecl>(*D)) {
447 DeclContext::decl_iterator Next = D;
448 ++Next;
449 if (Next != DEnd)
450 Terminator = ",";
451 } else
452 Terminator = ";";
453
454 if (Terminator)
455 Out << Terminator;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000456 if (!Policy.TerseOutput &&
457 ((isa<FunctionDecl>(*D) &&
458 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
459 (isa<FunctionTemplateDecl>(*D) &&
460 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
461 ; // StmtPrinter already added '\n' after CompoundStmt.
462 else
463 Out << "\n";
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000464
465 // Declare target attribute is special one, natural spelling for the pragma
466 // assumes "ending" construct so print it here.
467 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
468 Out << "#pragma omp end declare target\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000469 }
470
Eli Friedman79635842009-05-30 04:20:30 +0000471 if (!Decls.empty())
472 ProcessDeclGroup(Decls);
473
Douglas Gregor278f52e2009-05-30 00:08:05 +0000474 if (Indent)
475 Indentation -= Policy.Indentation;
476}
477
478void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
479 VisitDeclContext(D, false);
480}
481
482void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000483 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000484 Out << "typedef ";
Fangrui Song6907ce22018-07-30 19:24:48 +0000485
Douglas Gregor26701a42011-09-09 02:06:17 +0000486 if (D->isModulePrivate())
487 Out << "__module_private__ ";
488 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000489 QualType Ty = D->getTypeSourceInfo()->getType();
490 Ty.print(Out, Policy, D->getName(), Indentation);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000491 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000492}
493
Richard Smithdda56e42011-04-15 14:24:37 +0000494void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000495 Out << "using " << *D;
496 prettyPrintAttributes(D);
497 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000498}
499
Douglas Gregor278f52e2009-05-30 00:08:05 +0000500void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000501 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
502 Out << "__module_private__ ";
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000503 Out << "enum";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000504 if (D->isScoped()) {
505 if (D->isScopedUsingClassTag())
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000506 Out << " class";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000507 else
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000508 Out << " struct";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000509 }
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000510
511 prettyPrintAttributes(D);
512
513 Out << ' ' << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000514
Serge Pavlov08c8de22016-11-04 06:03:34 +0000515 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000516 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000517
John McCallf937c022011-10-07 06:10:15 +0000518 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000519 Out << " {\n";
520 VisitDeclContext(D);
521 Indent() << "}";
522 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000523}
524
525void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000526 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
527 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000528 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000529
530 prettyPrintAttributes(D);
531
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000532 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000533 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000534
John McCallf937c022011-10-07 06:10:15 +0000535 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000536 Out << " {\n";
537 VisitDeclContext(D);
538 Indent() << "}";
539 }
540}
541
542void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000543 Out << *D;
Jordan Roseccca6692017-01-20 03:33:42 +0000544 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000545 if (Expr *Init = D->getInitExpr()) {
546 Out << " = ";
Alex Lorenz36070ed2017-08-17 13:41:55 +0000547 Init->printPretty(Out, nullptr, Policy, Indentation, &Context);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000548 }
549}
550
Mike Stump11289f42009-09-09 15:08:12 +0000551void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000552 if (!D->getDescribedFunctionTemplate() &&
553 !D->isFunctionTemplateSpecialization())
554 prettyPrintPragmas(D);
555
Serge Pavlova67a4d22016-11-10 08:49:37 +0000556 if (D->isFunctionTemplateSpecialization())
557 Out << "template<> ";
Alex Lorenz47fd10c2017-04-18 15:12:34 +0000558 else if (!D->getDescribedFunctionTemplate()) {
559 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
560 I < NumTemplateParams; ++I)
561 printTemplateParameters(D->getTemplateParameterList(I));
562 }
Serge Pavlova67a4d22016-11-10 08:49:37 +0000563
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000564 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000565 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Richard Smith057ec502017-02-18 01:01:48 +0000566 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000567 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000568 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000569 case SC_None: break;
570 case SC_Extern: Out << "extern "; break;
571 case SC_Static: Out << "static "; break;
572 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000573 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000574 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000575 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000576
Douglas Gregor26701a42011-09-09 02:06:17 +0000577 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000578 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000579 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000580 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000581 if ((CDecl && CDecl->isExplicitSpecified()) ||
Richard Smith057ec502017-02-18 01:01:48 +0000582 (ConversionDecl && ConversionDecl->isExplicitSpecified()) ||
583 (GuideDecl && GuideDecl->isExplicitSpecified()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000584 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000585 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000586
Douglas Gregor2d042f12009-05-30 05:39:39 +0000587 PrintingPolicy SubPolicy(Policy);
588 SubPolicy.SuppressSpecifiers = false;
Alex Lorenza981c7d2017-04-11 16:46:03 +0000589 std::string Proto;
Serge Pavlov842022a2017-11-23 05:38:20 +0000590
591 if (Policy.FullyQualifiedName) {
592 Proto += D->getQualifiedNameAsString();
593 } else {
594 if (!Policy.SuppressScope) {
595 if (const NestedNameSpecifier *NS = D->getQualifier()) {
596 llvm::raw_string_ostream OS(Proto);
597 NS->print(OS, Policy);
598 }
Alex Lorenza981c7d2017-04-11 16:46:03 +0000599 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000600 Proto += D->getNameInfo().getAsString();
Alex Lorenza981c7d2017-04-11 16:46:03 +0000601 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000602
Richard Smith057ec502017-02-18 01:01:48 +0000603 if (GuideDecl)
604 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
Serge Pavlova67a4d22016-11-10 08:49:37 +0000605 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
606 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000607 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000608 TArgPrinter.printTemplateArguments(*TArgs);
609 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000610
Abramo Bagnara6d810632010-12-14 22:11:44 +0000611 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000612 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000613 Proto = '(' + Proto + ')';
614 Ty = PT->getInnerType();
615 }
616
Reid Kleckner0503a872013-12-05 01:23:43 +0000617 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000618 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000619 if (D->hasWrittenPrototype())
620 FT = dyn_cast<FunctionProtoType>(AFT);
621
622 Proto += "(";
623 if (FT) {
624 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000625 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000626 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
627 if (i) POut << ", ";
628 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
629 }
Mike Stump11289f42009-09-09 15:08:12 +0000630
Douglas Gregor278f52e2009-05-30 00:08:05 +0000631 if (FT->isVariadic()) {
632 if (D->getNumParams()) POut << ", ";
633 POut << "...";
634 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000635 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000636 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
637 if (i)
638 Proto += ", ";
639 Proto += D->getParamDecl(i)->getNameAsString();
640 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000641 }
642
643 Proto += ")";
Fangrui Song6907ce22018-07-30 19:24:48 +0000644
David Blaikief5697e52012-08-10 00:55:35 +0000645 if (FT) {
646 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000647 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000648 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000649 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000650 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000651 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000652
653 switch (FT->getRefQualifier()) {
654 case RQ_None:
655 break;
656 case RQ_LValue:
657 Proto += " &";
658 break;
659 case RQ_RValue:
660 Proto += " &&";
661 break;
662 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000663 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000664
665 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000666 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000667 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000668 Proto += "...";
Fangrui Song6907ce22018-07-30 19:24:48 +0000669 else
Douglas Gregor049bdca2009-12-08 17:45:32 +0000670 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
671 if (I)
672 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000673
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000674 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000675 }
676 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000677 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
678 Proto += " noexcept";
Richard Smitheaf11ad2018-05-03 03:58:32 +0000679 if (isComputedNoexcept(FT->getExceptionSpecType())) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000680 Proto += "(";
681 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000682 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000683 Indentation);
684 EOut.flush();
685 Proto += EOut.str();
686 Proto += ")";
687 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000688 }
689
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000690 if (CDecl) {
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000691 if (!Policy.TerseOutput)
692 PrintConstructorInitializers(CDecl, Proto);
Benjamin Kramer2907b082014-02-25 18:49:49 +0000693 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000694 if (FT && FT->hasTrailingReturn()) {
Richard Smith057ec502017-02-18 01:01:48 +0000695 if (!GuideDecl)
696 Out << "auto ";
697 Out << Proto << " -> ";
Richard Smithe6560762013-02-22 05:54:51 +0000698 Proto.clear();
699 }
Alp Toker314cc812014-01-25 16:55:45 +0000700 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000701 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000702 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000703 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000704 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000705 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000706 }
707
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000708 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000709
710 if (D->isPure())
711 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000712 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000713 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000714 else if (D->isExplicitlyDefaulted())
715 Out << " = default";
Serge Pavlova67a4d22016-11-10 08:49:37 +0000716 else if (D->doesThisDeclarationHaveABody()) {
717 if (!Policy.TerseOutput) {
718 if (!D->hasPrototype() && D->getNumParams()) {
719 // This is a K&R function definition, so we need to print the
720 // parameters.
721 Out << '\n';
Alex Lorenz36070ed2017-08-17 13:41:55 +0000722 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000723 Indentation += Policy.Indentation;
724 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
725 Indent();
726 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
727 Out << ";\n";
728 }
729 Indentation -= Policy.Indentation;
730 } else
731 Out << ' ';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000732
Serge Pavlova67a4d22016-11-10 08:49:37 +0000733 if (D->getBody())
734 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
735 } else {
Alex Lorenz35019db2017-11-16 01:28:25 +0000736 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
Serge Pavlova67a4d22016-11-10 08:49:37 +0000737 Out << " {}";
738 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000739 }
740}
741
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000742void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
743 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000744 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
745 for (unsigned i = 0; i < NumTPLists; ++i)
Serge Pavlova67a4d22016-11-10 08:49:37 +0000746 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000747 Out << "friend ";
748 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000749 }
750 else if (FunctionDecl *FD =
751 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
752 Out << "friend ";
753 VisitFunctionDecl(FD);
754 }
755 else if (FunctionTemplateDecl *FTD =
756 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
757 Out << "friend ";
758 VisitFunctionTemplateDecl(FTD);
759 }
760 else if (ClassTemplateDecl *CTD =
761 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
762 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000763 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000764 }
765}
766
Douglas Gregor278f52e2009-05-30 00:08:05 +0000767void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000768 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000769 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000770 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000771 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
772 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000773
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000774 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000775 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000776
777 if (D->isBitField()) {
778 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000779 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000780 }
Richard Smith938f40b2011-06-11 17:19:42 +0000781
782 Expr *Init = D->getInClassInitializer();
783 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000784 if (D->getInClassInitStyle() == ICIS_ListInit)
785 Out << " ";
786 else
787 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000788 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000789 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000790 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000791}
792
Chris Lattnercab02a62011-02-17 20:34:02 +0000793void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000794 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000795}
796
Douglas Gregor278f52e2009-05-30 00:08:05 +0000797void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000798 prettyPrintPragmas(D);
Vassil Vassilev10023732016-07-08 21:09:08 +0000799
800 QualType T = D->getTypeSourceInfo()
801 ? D->getTypeSourceInfo()->getType()
802 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
803
Richard Smithfd3834f2013-04-13 02:43:54 +0000804 if (!Policy.SuppressSpecifiers) {
805 StorageClass SC = D->getStorageClass();
806 if (SC != SC_None)
807 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000808
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000809 switch (D->getTSCSpec()) {
810 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000811 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000812 case TSCS___thread:
813 Out << "__thread ";
814 break;
815 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000816 Out << "_Thread_local ";
817 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000818 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000819 Out << "thread_local ";
820 break;
821 }
822
823 if (D->isModulePrivate())
824 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000825
826 if (D->isConstexpr()) {
827 Out << "constexpr ";
828 T.removeLocalConst();
829 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000830 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000831
Richard Smitha4bb2922014-07-23 03:17:06 +0000832 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000833 Expr *Init = D->getInit();
834 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000835 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000836 if (CXXConstructExpr *Construct =
837 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000838 if (D->getInitStyle() == VarDecl::CallInit &&
839 !Construct->isListInitialization()) {
840 ImplicitInit = Construct->getNumArgs() == 0 ||
841 Construct->getArg(0)->isDefaultArgument();
842 }
843 }
Sebastian Redla9351792012-02-11 23:51:47 +0000844 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000845 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000846 Out << "(";
847 else if (D->getInitStyle() == VarDecl::CInit) {
848 Out << " = ";
849 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000850 PrintingPolicy SubPolicy(Policy);
851 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000852 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000853 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000854 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000855 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000856 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000857 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000858 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000859}
860
861void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
862 VisitVarDecl(D);
863}
864
865void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
866 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000867 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000868 Out << ")";
869}
870
Douglas Gregorba345522011-12-02 23:23:56 +0000871void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000872 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000873 << ";\n";
874}
875
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000876void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
877 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000878 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000879 if (StringLiteral *SL = D->getMessage()) {
880 Out << ", ";
881 SL->printPretty(Out, nullptr, Policy, Indentation);
882 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000883 Out << ")";
884}
885
Douglas Gregor278f52e2009-05-30 00:08:05 +0000886//----------------------------------------------------------------------------
887// C++ declarations
888//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000889void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000890 if (D->isInline())
891 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000892 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000893 VisitDeclContext(D);
894 Indent() << "}";
895}
896
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000897void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
898 Out << "using namespace ";
899 if (D->getQualifier())
900 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000901 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000902}
903
Douglas Gregor18231932009-05-30 06:48:27 +0000904void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000905 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000906 if (D->getQualifier())
907 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000908 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000909}
910
Michael Han84324352013-02-22 17:15:32 +0000911void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
912 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000913}
914
Douglas Gregor5f478b72009-05-30 06:58:37 +0000915void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000916 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000917 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
918 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000919 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000920
921 prettyPrintAttributes(D);
922
Serge Pavlova67a4d22016-11-10 08:49:37 +0000923 if (D->getIdentifier()) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000924 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000925
Serge Pavlova67a4d22016-11-10 08:49:37 +0000926 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
927 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
928 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
929 printTemplateArguments(S->getTemplateArgs());
930 }
931
John McCallf937c022011-10-07 06:10:15 +0000932 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000933 // Print the base classes
934 if (D->getNumBases()) {
935 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000936 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
937 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000938 if (Base != D->bases_begin())
939 Out << ", ";
940
941 if (Base->isVirtual())
942 Out << "virtual ";
943
Anders Carlsson6df9e072009-08-29 20:36:12 +0000944 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000945 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000946 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000947 Out << " ";
948 }
949 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000950
951 if (Base->isPackExpansion())
952 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000953 }
954 }
955
956 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000957 // FIXME: Doesn't print access specifiers, e.g., "public:"
Serge Pavlova67a4d22016-11-10 08:49:37 +0000958 if (Policy.TerseOutput) {
959 Out << " {}";
960 } else {
961 Out << " {\n";
962 VisitDeclContext(D);
963 Indent() << "}";
964 }
Mike Stump11289f42009-09-09 15:08:12 +0000965 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000966}
967
968void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
969 const char *l;
970 if (D->getLanguage() == LinkageSpecDecl::lang_c)
971 l = "C";
972 else {
973 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
974 "unknown language in linkage specification");
975 l = "C++";
976 }
977
978 Out << "extern \"" << l << "\" ";
979 if (D->hasBraces()) {
980 Out << "{\n";
981 VisitDeclContext(D);
982 Indent() << "}";
983 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000984 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000985}
986
Serge Pavlova67a4d22016-11-10 08:49:37 +0000987void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params) {
Richard Trieu82398f92011-07-28 00:19:05 +0000988 assert(Params);
Richard Trieu82398f92011-07-28 00:19:05 +0000989
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000990 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000991
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000992 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
993 if (i != 0)
994 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000995
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000996 const Decl *Param = Params->getParam(i);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000997 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000998
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000999 if (TTP->wasDeclaredWithTypename())
1000 Out << "typename ";
1001 else
1002 Out << "class ";
1003
Anders Carlssonfb1d7762009-06-12 22:23:22 +00001004 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +00001005 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +00001006
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001007 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001008
Serge Pavlova67a4d22016-11-10 08:49:37 +00001009 if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001010 Out << " = ";
1011 Out << TTP->getDefaultArgument().getAsString(Policy);
1012 };
Serge Pavlova67a4d22016-11-10 08:49:37 +00001013 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +00001014 StringRef Name;
1015 if (IdentifierInfo *II = NTTP->getIdentifier())
1016 Name = II->getName();
1017 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +00001018
Serge Pavlova67a4d22016-11-10 08:49:37 +00001019 if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001020 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +00001021 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1022 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001023 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001024 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
Richard Smith030f4992011-04-15 13:38:57 +00001025 VisitTemplateDecl(TTPD);
1026 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001027 }
1028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001030 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +00001031}
1032
Serge Pavlova67a4d22016-11-10 08:49:37 +00001033void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1034 const TemplateParameterList *Params) {
1035 Out << "<";
1036 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1037 const TemplateArgument &A = Args[I];
1038 if (I)
1039 Out << ", ";
1040 if (Params) {
1041 if (A.getKind() == TemplateArgument::Type)
1042 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1043 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1044 Out << *P;
1045 continue;
1046 }
1047 if (A.getKind() == TemplateArgument::Template) {
1048 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1049 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1050 auto P = cast<TemplateTemplateParmDecl>(
1051 Params->getParam(TD->getIndex()));
1052 Out << *P;
1053 continue;
1054 }
1055 }
1056 if (A.getKind() == TemplateArgument::Expression) {
1057 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1058 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1059 auto P = cast<NonTypeTemplateParmDecl>(
1060 Params->getParam(N->getIndex()));
1061 Out << *P;
1062 continue;
1063 }
1064 }
1065 }
1066 A.print(Policy, Out);
1067 }
1068 Out << ">";
1069}
1070
Richard Trieu82398f92011-07-28 00:19:05 +00001071void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001072 printTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001073
Richard Smith030f4992011-04-15 13:38:57 +00001074 if (const TemplateTemplateParmDecl *TTP =
1075 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +00001076 Out << "class ";
1077 if (TTP->isParameterPack())
1078 Out << "...";
1079 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +00001080 } else {
1081 Visit(D->getTemplatedDecl());
1082 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001083}
1084
Richard Trieu82398f92011-07-28 00:19:05 +00001085void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +00001086 prettyPrintPragmas(D->getTemplatedDecl());
Alex Lorenz47fd10c2017-04-18 15:12:34 +00001087 // Print any leading template parameter lists.
1088 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1089 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1090 I < NumTemplateParams; ++I)
1091 printTemplateParameters(FD->getTemplateParameterList(I));
1092 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001093 VisitRedeclarableTemplateDecl(D);
Alexey Bataev97b72212018-08-14 18:31:20 +00001094 // Declare target attribute is special one, natural spelling for the pragma
1095 // assumes "ending" construct so print it here.
1096 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1097 Out << "#pragma omp end declare target\n";
Serge Pavlova67a4d22016-11-10 08:49:37 +00001098
Richard Smith057ec502017-02-18 01:01:48 +00001099 // Never print "instantiations" for deduction guides (they don't really
1100 // have them).
1101 if (PrintInstantiation &&
1102 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001103 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1104 const FunctionDecl *Def;
1105 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1106 return;
1107 for (auto *I : D->specializations())
1108 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1109 if (!PrevDecl->isThisDeclarationADefinition())
1110 Out << ";\n";
1111 Indent();
1112 prettyPrintPragmas(I);
1113 Visit(I);
1114 }
1115 }
Richard Trieu82398f92011-07-28 00:19:05 +00001116}
1117
1118void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001119 VisitRedeclarableTemplateDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001120
Serge Pavlova67a4d22016-11-10 08:49:37 +00001121 if (PrintInstantiation) {
1122 for (auto *I : D->specializations())
1123 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1124 if (D->isThisDeclarationADefinition())
1125 Out << ";";
1126 Out << "\n";
1127 Visit(I);
1128 }
1129 }
1130}
1131
1132void DeclPrinter::VisitClassTemplateSpecializationDecl(
1133 ClassTemplateSpecializationDecl *D) {
1134 Out << "template<> ";
1135 VisitCXXRecordDecl(D);
1136}
1137
1138void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1139 ClassTemplatePartialSpecializationDecl *D) {
1140 printTemplateParameters(D->getTemplateParameters());
1141 VisitCXXRecordDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001142}
1143
Douglas Gregor278f52e2009-05-30 00:08:05 +00001144//----------------------------------------------------------------------------
1145// Objective-C declarations
1146//----------------------------------------------------------------------------
1147
Fangrui Song6907ce22018-07-30 19:24:48 +00001148void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1149 Decl::ObjCDeclQualifier Quals,
Douglas Gregor813a0662015-06-19 18:14:38 +00001150 QualType T) {
1151 Out << '(';
1152 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1153 Out << "in ";
1154 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1155 Out << "inout ";
1156 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1157 Out << "out ";
1158 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1159 Out << "bycopy ";
1160 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1161 Out << "byref ";
1162 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1163 Out << "oneway ";
1164 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001165 if (auto nullability = AttributedType::stripOuterNullability(T))
1166 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001167 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001168
Douglas Gregor813a0662015-06-19 18:14:38 +00001169 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1170 Out << ')';
1171}
1172
Douglas Gregor85f3f952015-07-07 03:57:15 +00001173void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1174 Out << "<";
1175 unsigned First = true;
1176 for (auto *Param : *Params) {
1177 if (First) {
1178 First = false;
1179 } else {
1180 Out << ", ";
1181 }
1182
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001183 switch (Param->getVariance()) {
1184 case ObjCTypeParamVariance::Invariant:
1185 break;
1186
1187 case ObjCTypeParamVariance::Covariant:
1188 Out << "__covariant ";
1189 break;
1190
1191 case ObjCTypeParamVariance::Contravariant:
1192 Out << "__contravariant ";
1193 break;
1194 }
1195
Douglas Gregor85f3f952015-07-07 03:57:15 +00001196 Out << Param->getDeclName().getAsString();
1197
1198 if (Param->hasExplicitBound()) {
1199 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1200 }
1201 }
1202 Out << ">";
1203}
1204
Douglas Gregor278f52e2009-05-30 00:08:05 +00001205void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1206 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001207 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001208 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001209 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001210 if (!OMD->getReturnType().isNull()) {
1211 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1212 OMD->getReturnType());
1213 }
Mike Stump11289f42009-09-09 15:08:12 +00001214
Douglas Gregor278f52e2009-05-30 00:08:05 +00001215 std::string name = OMD->getSelector().getAsString();
1216 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001217 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001218 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001219 pos = name.find_first_of(':', lastPos);
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001220 if (lastPos != 0)
1221 Out << " ";
1222 Out << name.substr(lastPos, pos - lastPos) << ':';
Fangrui Song6907ce22018-07-30 19:24:48 +00001223 PrintObjCMethodType(OMD->getASTContext(),
Douglas Gregor813a0662015-06-19 18:14:38 +00001224 PI->getObjCDeclQualifier(),
1225 PI->getType());
1226 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001227 lastPos = pos + 1;
1228 }
Mike Stump11289f42009-09-09 15:08:12 +00001229
Douglas Gregor278f52e2009-05-30 00:08:05 +00001230 if (OMD->param_begin() == OMD->param_end())
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001231 Out << name;
Mike Stump11289f42009-09-09 15:08:12 +00001232
Douglas Gregor278f52e2009-05-30 00:08:05 +00001233 if (OMD->isVariadic())
1234 Out << ", ...";
Fangrui Song6907ce22018-07-30 19:24:48 +00001235
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001236 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001237
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001238 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001239 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001240 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001241 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001242 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001243 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001244}
1245
1246void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1247 std::string I = OID->getNameAsString();
1248 ObjCInterfaceDecl *SID = OID->getSuperClass();
1249
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001250 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001251 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001252 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001253 else
1254 Out << "@implementation " << I;
Fangrui Song6907ce22018-07-30 19:24:48 +00001255
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001256 if (OID->ivar_size() > 0) {
1257 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001258 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001259 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001260 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001261 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001262 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001263 }
1264 Indentation -= Policy.Indentation;
1265 Out << "}\n";
1266 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001267 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1268 Out << "\n";
1269 eolnOut = true;
1270 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001271 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001272 if (!eolnOut)
1273 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001274 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001275}
1276
1277void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1278 std::string I = OID->getNameAsString();
1279 ObjCInterfaceDecl *SID = OID->getSuperClass();
1280
Douglas Gregordc9166c2011-12-15 20:29:51 +00001281 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001282 Out << "@class " << I;
1283
1284 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1285 PrintObjCTypeParams(TypeParams);
1286 }
1287
1288 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001289 return;
1290 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001291 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001292 Out << "@interface " << I;
1293
1294 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1295 PrintObjCTypeParams(TypeParams);
1296 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001297
Douglas Gregor278f52e2009-05-30 00:08:05 +00001298 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001299 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001300
Douglas Gregor278f52e2009-05-30 00:08:05 +00001301 // Protocols?
1302 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1303 if (!Protocols.empty()) {
1304 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1305 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001306 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001307 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001308 }
Mike Stump11289f42009-09-09 15:08:12 +00001309
Douglas Gregor278f52e2009-05-30 00:08:05 +00001310 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001311 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001312 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001313 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001314 for (const auto *I : OID->ivars()) {
1315 Indent() << I->getASTContext()
1316 .getUnqualifiedObjCPointerType(I->getType())
1317 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001318 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001319 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001320 Out << "}\n";
1321 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001322 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001323 Out << "\n";
1324 eolnOut = true;
1325 }
Mike Stump11289f42009-09-09 15:08:12 +00001326
Douglas Gregor278f52e2009-05-30 00:08:05 +00001327 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001328 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001329 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001330 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001331 // FIXME: implement the rest...
1332}
1333
Douglas Gregor278f52e2009-05-30 00:08:05 +00001334void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001335 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001336 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001337 return;
1338 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001339 // Protocols?
1340 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1341 if (!Protocols.empty()) {
1342 Out << "@protocol " << *PID;
1343 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1344 E = Protocols.end(); I != E; ++I)
1345 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1346 Out << ">\n";
1347 } else
1348 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001349 VisitDeclContext(PID, false);
1350 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001351}
1352
1353void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001354 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001355
1356 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001357 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001358 // FIXME: implement the rest...
1359}
1360
1361void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001362 Out << "@interface " << *PID->getClassInterface();
1363 if (auto TypeParams = PID->getTypeParamList()) {
1364 PrintObjCTypeParams(TypeParams);
1365 }
1366 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001367 if (PID->ivar_size() > 0) {
1368 Out << "{\n";
1369 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001370 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001371 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001372 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001373 Indentation -= Policy.Indentation;
1374 Out << "}\n";
1375 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001376
Douglas Gregor278f52e2009-05-30 00:08:05 +00001377 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001378 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001379
Douglas Gregor278f52e2009-05-30 00:08:05 +00001380 // FIXME: implement the rest...
1381}
1382
1383void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001384 Out << "@compatibility_alias " << *AID
1385 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001386}
1387
1388/// PrintObjCPropertyDecl - print a property declaration.
1389///
1390void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1391 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1392 Out << "@required\n";
1393 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1394 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001395
Douglas Gregor813a0662015-06-19 18:14:38 +00001396 QualType T = PDecl->getType();
1397
Douglas Gregor278f52e2009-05-30 00:08:05 +00001398 Out << "@property";
1399 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1400 bool first = true;
1401 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001402 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001403 ObjCPropertyDecl::OBJC_PR_readonly) {
1404 Out << (first ? ' ' : ',') << "readonly";
1405 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001406 }
Mike Stump11289f42009-09-09 15:08:12 +00001407
Ted Kremenek897af912011-08-17 21:09:35 +00001408 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001409 Out << (first ? ' ' : ',') << "getter = ";
1410 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001411 first = false;
1412 }
1413 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001414 Out << (first ? ' ' : ',') << "setter = ";
1415 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001416 first = false;
1417 }
Mike Stump11289f42009-09-09 15:08:12 +00001418
Ted Kremenek897af912011-08-17 21:09:35 +00001419 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1420 Out << (first ? ' ' : ',') << "assign";
1421 first = false;
1422 }
Mike Stump11289f42009-09-09 15:08:12 +00001423
Ted Kremenek897af912011-08-17 21:09:35 +00001424 if (PDecl->getPropertyAttributes() &
1425 ObjCPropertyDecl::OBJC_PR_readwrite) {
1426 Out << (first ? ' ' : ',') << "readwrite";
1427 first = false;
1428 }
Mike Stump11289f42009-09-09 15:08:12 +00001429
Ted Kremenek897af912011-08-17 21:09:35 +00001430 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1431 Out << (first ? ' ' : ',') << "retain";
1432 first = false;
1433 }
Mike Stump11289f42009-09-09 15:08:12 +00001434
Ted Kremenek897af912011-08-17 21:09:35 +00001435 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1436 Out << (first ? ' ' : ',') << "strong";
1437 first = false;
1438 }
John McCall31168b02011-06-15 23:02:42 +00001439
Ted Kremenek897af912011-08-17 21:09:35 +00001440 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1441 Out << (first ? ' ' : ',') << "copy";
1442 first = false;
1443 }
Mike Stump11289f42009-09-09 15:08:12 +00001444
Ted Kremenek897af912011-08-17 21:09:35 +00001445 if (PDecl->getPropertyAttributes() &
1446 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1447 Out << (first ? ' ' : ',') << "nonatomic";
1448 first = false;
1449 }
1450 if (PDecl->getPropertyAttributes() &
1451 ObjCPropertyDecl::OBJC_PR_atomic) {
1452 Out << (first ? ' ' : ',') << "atomic";
1453 first = false;
1454 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001455
Douglas Gregor813a0662015-06-19 18:14:38 +00001456 if (PDecl->getPropertyAttributes() &
1457 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001458 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001459 if (*nullability == NullabilityKind::Unspecified &&
1460 (PDecl->getPropertyAttributes() &
1461 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1462 Out << (first ? ' ' : ',') << "null_resettable";
1463 } else {
1464 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001465 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001466 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001467 first = false;
1468 }
1469 }
1470
Manman Ren387ff7f2016-01-26 18:52:43 +00001471 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1472 Out << (first ? ' ' : ',') << "class";
1473 first = false;
1474 }
1475
Ted Kremenek897af912011-08-17 21:09:35 +00001476 (void) first; // Silence dead store warning due to idiomatic code.
1477 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001478 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001479 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001480 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001481 if (Policy.PolishForDeclaration)
1482 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001483}
1484
1485void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1486 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001487 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001488 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001489 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001490 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001491 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001492 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001493}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001494
1495void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001496 if (!D->isAccessDeclaration())
1497 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001498 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001499 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001500 D->getQualifier()->print(Out, Policy);
Alex Lorenzea9b59a2016-10-03 12:22:17 +00001501
1502 // Use the correct record name when the using declaration is used for
1503 // inheriting constructors.
1504 for (const auto *Shadow : D->shadows()) {
1505 if (const auto *ConstructorShadow =
1506 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1507 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1508 Out << *ConstructorShadow->getNominatedBaseClass();
1509 return;
1510 }
1511 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001512 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001513}
1514
John McCalle61f2ba2009-11-18 02:36:19 +00001515void
1516DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1517 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001518 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001519 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001520}
1521
1522void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001523 if (!D->isAccessDeclaration())
1524 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001525 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001526 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001527}
John McCall3f746822009-11-17 05:59:44 +00001528
1529void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1530 // ignore
1531}
Alexey Bataeva769e072013-03-22 06:34:35 +00001532
1533void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1534 Out << "#pragma omp threadprivate";
1535 if (!D->varlist_empty()) {
1536 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1537 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001538 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001539 Out << (I == D->varlist_begin() ? '(' : ',');
George Burgess IV00f70bd2018-03-01 05:43:23 +00001540 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001541 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001542 }
1543 Out << ")";
1544 }
1545}
1546
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001547void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1548 if (!D->isInvalidDecl()) {
1549 Out << "#pragma omp declare reduction (";
1550 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1551 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1552 nullptr,
1553#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1554 Spelling,
1555#include "clang/Basic/OperatorKinds.def"
1556 };
1557 const char *OpName =
1558 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1559 assert(OpName && "not an overloaded operator");
1560 Out << OpName;
1561 } else {
1562 assert(D->getDeclName().isIdentifier());
1563 D->printName(Out);
1564 }
1565 Out << " : ";
1566 D->getType().print(Out, Policy);
1567 Out << " : ";
1568 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1569 Out << ")";
1570 if (auto *Init = D->getInitializer()) {
1571 Out << " initializer(";
Alexey Bataev070f43a2017-09-06 14:49:58 +00001572 switch (D->getInitializerKind()) {
1573 case OMPDeclareReductionDecl::DirectInit:
1574 Out << "omp_priv(";
1575 break;
1576 case OMPDeclareReductionDecl::CopyInit:
1577 Out << "omp_priv = ";
1578 break;
1579 case OMPDeclareReductionDecl::CallInit:
1580 break;
1581 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001582 Init->printPretty(Out, nullptr, Policy, 0);
Alexey Bataev070f43a2017-09-06 14:49:58 +00001583 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1584 Out << ")";
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001585 Out << ")";
1586 }
1587 }
1588}
1589
Alexey Bataev4244be22016-02-11 05:35:55 +00001590void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001591 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1592}
1593