blob: a3e8475a1e790ed238e5c85624c6f0fc05d3a860 [file] [log] [blame]
Douglas Gregor278f52e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregor278f52e2009-05-30 00:08:05 +00006//
7//===----------------------------------------------------------------------===//
8//
Alexander Kornienko90ff6072012-12-20 02:09:13 +00009// This file implements the Decl::print method, which pretty prints the
Douglas Gregor278f52e2009-05-30 00:08:05 +000010// AST back out to C/Objective-C/C++/Objective-C++ code.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000014#include "clang/AST/Attr.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000015#include "clang/AST/Decl.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclObjC.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000018#include "clang/AST/DeclVisitor.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000019#include "clang/AST/Expr.h"
Douglas Gregor7ae2d772010-01-31 09:12:51 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000021#include "clang/AST/PrettyPrinter.h"
Douglas Gregorba345522011-12-02 23:23:56 +000022#include "clang/Basic/Module.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000023#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25
26namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000027 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000028 raw_ostream &Out;
Douglas Gregor278f52e2009-05-30 00:08:05 +000029 PrintingPolicy Policy;
Alex Lorenz36070ed2017-08-17 13:41:55 +000030 const ASTContext &Context;
Douglas Gregor278f52e2009-05-30 00:08:05 +000031 unsigned Indentation;
Richard Trieu82398f92011-07-28 00:19:05 +000032 bool PrintInstantiation;
Douglas Gregor278f52e2009-05-30 00:08:05 +000033
Chris Lattner0e62c1c2011-07-23 10:55:15 +000034 raw_ostream& Indent() { return Indent(Indentation); }
35 raw_ostream& Indent(unsigned Indentation);
36 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
Douglas Gregor278f52e2009-05-30 00:08:05 +000037
Anders Carlsson601d6e42009-08-28 22:39:52 +000038 void Print(AccessSpecifier AS);
Alex Lorenzdc616fa2017-11-16 01:31:27 +000039 void PrintConstructorInitializers(CXXConstructorDecl *CDecl,
40 std::string &Proto);
Mike Stump11289f42009-09-09 15:08:12 +000041
Douglas Gregor813a0662015-06-19 18:14:38 +000042 /// Print an Objective-C method type in parentheses.
43 ///
44 /// \param Quals The Objective-C declaration qualifiers.
45 /// \param T The type to print.
Fangrui Song6907ce22018-07-30 19:24:48 +000046 void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
Douglas Gregor813a0662015-06-19 18:14:38 +000047 QualType T);
48
Douglas Gregor85f3f952015-07-07 03:57:15 +000049 void PrintObjCTypeParams(ObjCTypeParamList *Params);
50
Douglas Gregor278f52e2009-05-30 00:08:05 +000051 public:
Richard Smith235341b2012-08-16 03:56:14 +000052 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
Alex Lorenz36070ed2017-08-17 13:41:55 +000053 const ASTContext &Context, unsigned Indentation = 0,
54 bool PrintInstantiation = false)
55 : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation),
56 PrintInstantiation(PrintInstantiation) {}
Douglas Gregor278f52e2009-05-30 00:08:05 +000057
58 void VisitDeclContext(DeclContext *DC, bool Indent = true);
59
60 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
61 void VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +000062 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000063 void VisitEnumDecl(EnumDecl *D);
64 void VisitRecordDecl(RecordDecl *D);
65 void VisitEnumConstantDecl(EnumConstantDecl *D);
Michael Han84324352013-02-22 17:15:32 +000066 void VisitEmptyDecl(EmptyDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000067 void VisitFunctionDecl(FunctionDecl *D);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +000068 void VisitFriendDecl(FriendDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000069 void VisitFieldDecl(FieldDecl *D);
70 void VisitVarDecl(VarDecl *D);
Chris Lattnercab02a62011-02-17 20:34:02 +000071 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000072 void VisitParmVarDecl(ParmVarDecl *D);
73 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregorba345522011-12-02 23:23:56 +000074 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +000075 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000076 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000077 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000078 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000079 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000080 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith030f4992011-04-15 13:38:57 +000081 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000082 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
83 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Serge Pavlova67a4d22016-11-10 08:49:37 +000084 void VisitClassTemplateSpecializationDecl(
85 ClassTemplateSpecializationDecl *D);
86 void VisitClassTemplatePartialSpecializationDecl(
87 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000088 void VisitObjCMethodDecl(ObjCMethodDecl *D);
89 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
90 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000091 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
92 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
93 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
94 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
95 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
96 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000097 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
98 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +000099 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +0000100 void VisitUsingShadowDecl(UsingShadowDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000101 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Kelvin Li1408f912018-09-26 04:28:39 +0000102 void VisitOMPRequiresDecl(OMPRequiresDecl *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;
Kelvin Li1408f912018-09-26 04:28:39 +0000425 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D) ||
426 isa<OMPRequiresDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000427 Terminator = nullptr;
Serge Pavlovdc586c42016-10-31 05:11:12 +0000428 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000429 Terminator = nullptr;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000430 else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
431 if (FD->isThisDeclarationADefinition())
432 Terminator = nullptr;
433 else
434 Terminator = ";";
435 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
436 if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
437 Terminator = nullptr;
438 else
439 Terminator = ";";
440 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000441 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000442 isa<ObjCInterfaceDecl>(*D) ||
443 isa<ObjCProtocolDecl>(*D) ||
444 isa<ObjCCategoryImplDecl>(*D) ||
445 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000446 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000447 else if (isa<EnumConstantDecl>(*D)) {
448 DeclContext::decl_iterator Next = D;
449 ++Next;
450 if (Next != DEnd)
451 Terminator = ",";
452 } else
453 Terminator = ";";
454
455 if (Terminator)
456 Out << Terminator;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000457 if (!Policy.TerseOutput &&
458 ((isa<FunctionDecl>(*D) &&
459 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
460 (isa<FunctionTemplateDecl>(*D) &&
461 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
462 ; // StmtPrinter already added '\n' after CompoundStmt.
463 else
464 Out << "\n";
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000465
466 // Declare target attribute is special one, natural spelling for the pragma
467 // assumes "ending" construct so print it here.
468 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
469 Out << "#pragma omp end declare target\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000470 }
471
Eli Friedman79635842009-05-30 04:20:30 +0000472 if (!Decls.empty())
473 ProcessDeclGroup(Decls);
474
Douglas Gregor278f52e2009-05-30 00:08:05 +0000475 if (Indent)
476 Indentation -= Policy.Indentation;
477}
478
479void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
480 VisitDeclContext(D, false);
481}
482
483void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000484 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000485 Out << "typedef ";
Fangrui Song6907ce22018-07-30 19:24:48 +0000486
Douglas Gregor26701a42011-09-09 02:06:17 +0000487 if (D->isModulePrivate())
488 Out << "__module_private__ ";
489 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000490 QualType Ty = D->getTypeSourceInfo()->getType();
491 Ty.print(Out, Policy, D->getName(), Indentation);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000492 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000493}
494
Richard Smithdda56e42011-04-15 14:24:37 +0000495void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000496 Out << "using " << *D;
497 prettyPrintAttributes(D);
498 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000499}
500
Douglas Gregor278f52e2009-05-30 00:08:05 +0000501void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000502 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
503 Out << "__module_private__ ";
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000504 Out << "enum";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000505 if (D->isScoped()) {
506 if (D->isScopedUsingClassTag())
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000507 Out << " class";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000508 else
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000509 Out << " struct";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000510 }
Joel E. Dennyc2575a32018-04-24 14:50:23 +0000511
512 prettyPrintAttributes(D);
513
514 Out << ' ' << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000515
Serge Pavlov08c8de22016-11-04 06:03:34 +0000516 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000517 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000518
John McCallf937c022011-10-07 06:10:15 +0000519 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000520 Out << " {\n";
521 VisitDeclContext(D);
522 Indent() << "}";
523 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000524}
525
526void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000527 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
528 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000529 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000530
531 prettyPrintAttributes(D);
532
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000533 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000534 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000535
John McCallf937c022011-10-07 06:10:15 +0000536 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000537 Out << " {\n";
538 VisitDeclContext(D);
539 Indent() << "}";
540 }
541}
542
543void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000544 Out << *D;
Jordan Roseccca6692017-01-20 03:33:42 +0000545 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000546 if (Expr *Init = D->getInitExpr()) {
547 Out << " = ";
George Karpenkov64885ae2018-09-15 02:02:31 +0000548 Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000549 }
550}
551
Mike Stump11289f42009-09-09 15:08:12 +0000552void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000553 if (!D->getDescribedFunctionTemplate() &&
554 !D->isFunctionTemplateSpecialization())
555 prettyPrintPragmas(D);
556
Serge Pavlova67a4d22016-11-10 08:49:37 +0000557 if (D->isFunctionTemplateSpecialization())
558 Out << "template<> ";
Alex Lorenz47fd10c2017-04-18 15:12:34 +0000559 else if (!D->getDescribedFunctionTemplate()) {
560 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
561 I < NumTemplateParams; ++I)
562 printTemplateParameters(D->getTemplateParameterList(I));
563 }
Serge Pavlova67a4d22016-11-10 08:49:37 +0000564
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000565 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000566 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Richard Smith057ec502017-02-18 01:01:48 +0000567 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000568 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000569 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000570 case SC_None: break;
571 case SC_Extern: Out << "extern "; break;
572 case SC_Static: Out << "static "; break;
573 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000574 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000575 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000576 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000577
Douglas Gregor26701a42011-09-09 02:06:17 +0000578 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000579 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000580 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000581 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000582 if ((CDecl && CDecl->isExplicitSpecified()) ||
Richard Smith057ec502017-02-18 01:01:48 +0000583 (ConversionDecl && ConversionDecl->isExplicitSpecified()) ||
584 (GuideDecl && GuideDecl->isExplicitSpecified()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000585 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000586 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000587
Douglas Gregor2d042f12009-05-30 05:39:39 +0000588 PrintingPolicy SubPolicy(Policy);
589 SubPolicy.SuppressSpecifiers = false;
Alex Lorenza981c7d2017-04-11 16:46:03 +0000590 std::string Proto;
Serge Pavlov842022a2017-11-23 05:38:20 +0000591
592 if (Policy.FullyQualifiedName) {
593 Proto += D->getQualifiedNameAsString();
594 } else {
595 if (!Policy.SuppressScope) {
596 if (const NestedNameSpecifier *NS = D->getQualifier()) {
597 llvm::raw_string_ostream OS(Proto);
598 NS->print(OS, Policy);
599 }
Alex Lorenza981c7d2017-04-11 16:46:03 +0000600 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000601 Proto += D->getNameInfo().getAsString();
Alex Lorenza981c7d2017-04-11 16:46:03 +0000602 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000603
Richard Smith057ec502017-02-18 01:01:48 +0000604 if (GuideDecl)
605 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
Serge Pavlova67a4d22016-11-10 08:49:37 +0000606 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
607 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000608 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000609 TArgPrinter.printTemplateArguments(*TArgs);
610 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000611
Abramo Bagnara6d810632010-12-14 22:11:44 +0000612 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000613 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000614 Proto = '(' + Proto + ')';
615 Ty = PT->getInnerType();
616 }
617
Reid Kleckner0503a872013-12-05 01:23:43 +0000618 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000619 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000620 if (D->hasWrittenPrototype())
621 FT = dyn_cast<FunctionProtoType>(AFT);
622
623 Proto += "(";
624 if (FT) {
625 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000626 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000627 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
628 if (i) POut << ", ";
629 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
630 }
Mike Stump11289f42009-09-09 15:08:12 +0000631
Douglas Gregor278f52e2009-05-30 00:08:05 +0000632 if (FT->isVariadic()) {
633 if (D->getNumParams()) POut << ", ";
634 POut << "...";
635 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000636 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000637 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
638 if (i)
639 Proto += ", ";
640 Proto += D->getParamDecl(i)->getNameAsString();
641 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000642 }
643
644 Proto += ")";
Fangrui Song6907ce22018-07-30 19:24:48 +0000645
David Blaikief5697e52012-08-10 00:55:35 +0000646 if (FT) {
647 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000648 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000649 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000650 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000651 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000652 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000653
654 switch (FT->getRefQualifier()) {
655 case RQ_None:
656 break;
657 case RQ_LValue:
658 Proto += " &";
659 break;
660 case RQ_RValue:
661 Proto += " &&";
662 break;
663 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000664 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000665
666 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000667 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000668 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000669 Proto += "...";
Fangrui Song6907ce22018-07-30 19:24:48 +0000670 else
Douglas Gregor049bdca2009-12-08 17:45:32 +0000671 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
672 if (I)
673 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000674
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000675 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000676 }
677 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000678 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
679 Proto += " noexcept";
Richard Smitheaf11ad2018-05-03 03:58:32 +0000680 if (isComputedNoexcept(FT->getExceptionSpecType())) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000681 Proto += "(";
682 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000683 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000684 Indentation);
685 EOut.flush();
686 Proto += EOut.str();
687 Proto += ")";
688 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000689 }
690
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000691 if (CDecl) {
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000692 if (!Policy.TerseOutput)
693 PrintConstructorInitializers(CDecl, Proto);
Benjamin Kramer2907b082014-02-25 18:49:49 +0000694 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000695 if (FT && FT->hasTrailingReturn()) {
Richard Smith057ec502017-02-18 01:01:48 +0000696 if (!GuideDecl)
697 Out << "auto ";
698 Out << Proto << " -> ";
Richard Smithe6560762013-02-22 05:54:51 +0000699 Proto.clear();
700 }
Alp Toker314cc812014-01-25 16:55:45 +0000701 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000702 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000703 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000704 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000705 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000706 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000707 }
708
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000709 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000710
711 if (D->isPure())
712 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000713 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000714 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000715 else if (D->isExplicitlyDefaulted())
716 Out << " = default";
Serge Pavlova67a4d22016-11-10 08:49:37 +0000717 else if (D->doesThisDeclarationHaveABody()) {
718 if (!Policy.TerseOutput) {
719 if (!D->hasPrototype() && D->getNumParams()) {
720 // This is a K&R function definition, so we need to print the
721 // parameters.
722 Out << '\n';
Alex Lorenz36070ed2017-08-17 13:41:55 +0000723 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000724 Indentation += Policy.Indentation;
725 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
726 Indent();
727 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
728 Out << ";\n";
729 }
730 Indentation -= Policy.Indentation;
731 } else
732 Out << ' ';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000733
Serge Pavlova67a4d22016-11-10 08:49:37 +0000734 if (D->getBody())
735 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
736 } else {
Alex Lorenz35019db2017-11-16 01:28:25 +0000737 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
Serge Pavlova67a4d22016-11-10 08:49:37 +0000738 Out << " {}";
739 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000740 }
741}
742
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000743void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
744 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000745 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
746 for (unsigned i = 0; i < NumTPLists; ++i)
Serge Pavlova67a4d22016-11-10 08:49:37 +0000747 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000748 Out << "friend ";
749 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000750 }
751 else if (FunctionDecl *FD =
752 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
753 Out << "friend ";
754 VisitFunctionDecl(FD);
755 }
756 else if (FunctionTemplateDecl *FTD =
757 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
758 Out << "friend ";
759 VisitFunctionTemplateDecl(FTD);
760 }
761 else if (ClassTemplateDecl *CTD =
762 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
763 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000764 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000765 }
766}
767
Douglas Gregor278f52e2009-05-30 00:08:05 +0000768void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000769 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000770 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000771 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000772 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
773 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000774
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000775 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000776 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000777
778 if (D->isBitField()) {
779 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000780 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000781 }
Richard Smith938f40b2011-06-11 17:19:42 +0000782
783 Expr *Init = D->getInClassInitializer();
784 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000785 if (D->getInClassInitStyle() == ICIS_ListInit)
786 Out << " ";
787 else
788 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000789 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000790 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000791 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000792}
793
Chris Lattnercab02a62011-02-17 20:34:02 +0000794void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000795 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000796}
797
Douglas Gregor278f52e2009-05-30 00:08:05 +0000798void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000799 prettyPrintPragmas(D);
Vassil Vassilev10023732016-07-08 21:09:08 +0000800
801 QualType T = D->getTypeSourceInfo()
802 ? D->getTypeSourceInfo()->getType()
803 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
804
Richard Smithfd3834f2013-04-13 02:43:54 +0000805 if (!Policy.SuppressSpecifiers) {
806 StorageClass SC = D->getStorageClass();
807 if (SC != SC_None)
808 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000809
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000810 switch (D->getTSCSpec()) {
811 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000812 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000813 case TSCS___thread:
814 Out << "__thread ";
815 break;
816 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000817 Out << "_Thread_local ";
818 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000819 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000820 Out << "thread_local ";
821 break;
822 }
823
824 if (D->isModulePrivate())
825 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000826
827 if (D->isConstexpr()) {
828 Out << "constexpr ";
829 T.removeLocalConst();
830 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000831 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000832
Richard Smitha4bb2922014-07-23 03:17:06 +0000833 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000834 Expr *Init = D->getInit();
835 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000836 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000837 if (CXXConstructExpr *Construct =
838 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000839 if (D->getInitStyle() == VarDecl::CallInit &&
840 !Construct->isListInitialization()) {
841 ImplicitInit = Construct->getNumArgs() == 0 ||
842 Construct->getArg(0)->isDefaultArgument();
843 }
844 }
Sebastian Redla9351792012-02-11 23:51:47 +0000845 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000846 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000847 Out << "(";
848 else if (D->getInitStyle() == VarDecl::CInit) {
849 Out << " = ";
850 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000851 PrintingPolicy SubPolicy(Policy);
852 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000853 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000854 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000855 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000856 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000857 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000858 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000859 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000860}
861
862void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
863 VisitVarDecl(D);
864}
865
866void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
867 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000868 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000869 Out << ")";
870}
871
Douglas Gregorba345522011-12-02 23:23:56 +0000872void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000873 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000874 << ";\n";
875}
876
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000877void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
878 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000879 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000880 if (StringLiteral *SL = D->getMessage()) {
881 Out << ", ";
882 SL->printPretty(Out, nullptr, Policy, Indentation);
883 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000884 Out << ")";
885}
886
Douglas Gregor278f52e2009-05-30 00:08:05 +0000887//----------------------------------------------------------------------------
888// C++ declarations
889//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000890void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000891 if (D->isInline())
892 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000893 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000894 VisitDeclContext(D);
895 Indent() << "}";
896}
897
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000898void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
899 Out << "using namespace ";
900 if (D->getQualifier())
901 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000902 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000903}
904
Douglas Gregor18231932009-05-30 06:48:27 +0000905void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000906 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000907 if (D->getQualifier())
908 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000909 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000910}
911
Michael Han84324352013-02-22 17:15:32 +0000912void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
913 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000914}
915
Douglas Gregor5f478b72009-05-30 06:58:37 +0000916void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000917 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000918 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
919 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000920 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000921
922 prettyPrintAttributes(D);
923
Serge Pavlova67a4d22016-11-10 08:49:37 +0000924 if (D->getIdentifier()) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000925 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000926
Serge Pavlova67a4d22016-11-10 08:49:37 +0000927 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
928 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
929 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
930 printTemplateArguments(S->getTemplateArgs());
931 }
932
John McCallf937c022011-10-07 06:10:15 +0000933 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000934 // Print the base classes
935 if (D->getNumBases()) {
936 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000937 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
938 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000939 if (Base != D->bases_begin())
940 Out << ", ";
941
942 if (Base->isVirtual())
943 Out << "virtual ";
944
Anders Carlsson6df9e072009-08-29 20:36:12 +0000945 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000946 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000947 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000948 Out << " ";
949 }
950 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000951
952 if (Base->isPackExpansion())
953 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000954 }
955 }
956
957 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000958 // FIXME: Doesn't print access specifiers, e.g., "public:"
Serge Pavlova67a4d22016-11-10 08:49:37 +0000959 if (Policy.TerseOutput) {
960 Out << " {}";
961 } else {
962 Out << " {\n";
963 VisitDeclContext(D);
964 Indent() << "}";
965 }
Mike Stump11289f42009-09-09 15:08:12 +0000966 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000967}
968
969void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
970 const char *l;
971 if (D->getLanguage() == LinkageSpecDecl::lang_c)
972 l = "C";
973 else {
974 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
975 "unknown language in linkage specification");
976 l = "C++";
977 }
978
979 Out << "extern \"" << l << "\" ";
980 if (D->hasBraces()) {
981 Out << "{\n";
982 VisitDeclContext(D);
983 Indent() << "}";
984 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000985 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000986}
987
Serge Pavlova67a4d22016-11-10 08:49:37 +0000988void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params) {
Richard Trieu82398f92011-07-28 00:19:05 +0000989 assert(Params);
Richard Trieu82398f92011-07-28 00:19:05 +0000990
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000991 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000992
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000993 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
994 if (i != 0)
995 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000996
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000997 const Decl *Param = Params->getParam(i);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000998 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000999
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001000 if (TTP->wasDeclaredWithTypename())
1001 Out << "typename ";
1002 else
1003 Out << "class ";
1004
Anders Carlssonfb1d7762009-06-12 22:23:22 +00001005 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +00001006 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +00001007
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001008 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001009
Serge Pavlova67a4d22016-11-10 08:49:37 +00001010 if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001011 Out << " = ";
1012 Out << TTP->getDefaultArgument().getAsString(Policy);
1013 };
Serge Pavlova67a4d22016-11-10 08:49:37 +00001014 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +00001015 StringRef Name;
1016 if (IdentifierInfo *II = NTTP->getIdentifier())
1017 Name = II->getName();
1018 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +00001019
Serge Pavlova67a4d22016-11-10 08:49:37 +00001020 if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001021 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +00001022 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1023 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001024 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001025 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
Richard Smith030f4992011-04-15 13:38:57 +00001026 VisitTemplateDecl(TTPD);
1027 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001028 }
1029 }
Mike Stump11289f42009-09-09 15:08:12 +00001030
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001031 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +00001032}
1033
Serge Pavlova67a4d22016-11-10 08:49:37 +00001034void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1035 const TemplateParameterList *Params) {
1036 Out << "<";
1037 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1038 const TemplateArgument &A = Args[I];
1039 if (I)
1040 Out << ", ";
1041 if (Params) {
1042 if (A.getKind() == TemplateArgument::Type)
1043 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1044 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1045 Out << *P;
1046 continue;
1047 }
1048 if (A.getKind() == TemplateArgument::Template) {
1049 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1050 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1051 auto P = cast<TemplateTemplateParmDecl>(
1052 Params->getParam(TD->getIndex()));
1053 Out << *P;
1054 continue;
1055 }
1056 }
1057 if (A.getKind() == TemplateArgument::Expression) {
1058 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1059 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1060 auto P = cast<NonTypeTemplateParmDecl>(
1061 Params->getParam(N->getIndex()));
1062 Out << *P;
1063 continue;
1064 }
1065 }
1066 }
1067 A.print(Policy, Out);
1068 }
1069 Out << ">";
1070}
1071
Richard Trieu82398f92011-07-28 00:19:05 +00001072void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001073 printTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001074
Richard Smith030f4992011-04-15 13:38:57 +00001075 if (const TemplateTemplateParmDecl *TTP =
1076 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +00001077 Out << "class ";
1078 if (TTP->isParameterPack())
1079 Out << "...";
1080 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +00001081 } else {
1082 Visit(D->getTemplatedDecl());
1083 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001084}
1085
Richard Trieu82398f92011-07-28 00:19:05 +00001086void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +00001087 prettyPrintPragmas(D->getTemplatedDecl());
Alex Lorenz47fd10c2017-04-18 15:12:34 +00001088 // Print any leading template parameter lists.
1089 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1090 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1091 I < NumTemplateParams; ++I)
1092 printTemplateParameters(FD->getTemplateParameterList(I));
1093 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001094 VisitRedeclarableTemplateDecl(D);
Alexey Bataev97b72212018-08-14 18:31:20 +00001095 // Declare target attribute is special one, natural spelling for the pragma
1096 // assumes "ending" construct so print it here.
1097 if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1098 Out << "#pragma omp end declare target\n";
Serge Pavlova67a4d22016-11-10 08:49:37 +00001099
Richard Smith057ec502017-02-18 01:01:48 +00001100 // Never print "instantiations" for deduction guides (they don't really
1101 // have them).
1102 if (PrintInstantiation &&
1103 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001104 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1105 const FunctionDecl *Def;
1106 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1107 return;
1108 for (auto *I : D->specializations())
1109 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1110 if (!PrevDecl->isThisDeclarationADefinition())
1111 Out << ";\n";
1112 Indent();
1113 prettyPrintPragmas(I);
1114 Visit(I);
1115 }
1116 }
Richard Trieu82398f92011-07-28 00:19:05 +00001117}
1118
1119void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001120 VisitRedeclarableTemplateDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001121
Serge Pavlova67a4d22016-11-10 08:49:37 +00001122 if (PrintInstantiation) {
1123 for (auto *I : D->specializations())
1124 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1125 if (D->isThisDeclarationADefinition())
1126 Out << ";";
1127 Out << "\n";
1128 Visit(I);
1129 }
1130 }
1131}
1132
1133void DeclPrinter::VisitClassTemplateSpecializationDecl(
1134 ClassTemplateSpecializationDecl *D) {
1135 Out << "template<> ";
1136 VisitCXXRecordDecl(D);
1137}
1138
1139void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1140 ClassTemplatePartialSpecializationDecl *D) {
1141 printTemplateParameters(D->getTemplateParameters());
1142 VisitCXXRecordDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001143}
1144
Douglas Gregor278f52e2009-05-30 00:08:05 +00001145//----------------------------------------------------------------------------
1146// Objective-C declarations
1147//----------------------------------------------------------------------------
1148
Fangrui Song6907ce22018-07-30 19:24:48 +00001149void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1150 Decl::ObjCDeclQualifier Quals,
Douglas Gregor813a0662015-06-19 18:14:38 +00001151 QualType T) {
1152 Out << '(';
1153 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1154 Out << "in ";
1155 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1156 Out << "inout ";
1157 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1158 Out << "out ";
1159 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1160 Out << "bycopy ";
1161 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1162 Out << "byref ";
1163 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1164 Out << "oneway ";
1165 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001166 if (auto nullability = AttributedType::stripOuterNullability(T))
1167 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001168 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001169
Douglas Gregor813a0662015-06-19 18:14:38 +00001170 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1171 Out << ')';
1172}
1173
Douglas Gregor85f3f952015-07-07 03:57:15 +00001174void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1175 Out << "<";
1176 unsigned First = true;
1177 for (auto *Param : *Params) {
1178 if (First) {
1179 First = false;
1180 } else {
1181 Out << ", ";
1182 }
1183
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001184 switch (Param->getVariance()) {
1185 case ObjCTypeParamVariance::Invariant:
1186 break;
1187
1188 case ObjCTypeParamVariance::Covariant:
1189 Out << "__covariant ";
1190 break;
1191
1192 case ObjCTypeParamVariance::Contravariant:
1193 Out << "__contravariant ";
1194 break;
1195 }
1196
Douglas Gregor85f3f952015-07-07 03:57:15 +00001197 Out << Param->getDeclName().getAsString();
1198
1199 if (Param->hasExplicitBound()) {
1200 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1201 }
1202 }
1203 Out << ">";
1204}
1205
Douglas Gregor278f52e2009-05-30 00:08:05 +00001206void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1207 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001208 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001209 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001210 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001211 if (!OMD->getReturnType().isNull()) {
1212 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1213 OMD->getReturnType());
1214 }
Mike Stump11289f42009-09-09 15:08:12 +00001215
Douglas Gregor278f52e2009-05-30 00:08:05 +00001216 std::string name = OMD->getSelector().getAsString();
1217 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001218 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001219 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001220 pos = name.find_first_of(':', lastPos);
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001221 if (lastPos != 0)
1222 Out << " ";
1223 Out << name.substr(lastPos, pos - lastPos) << ':';
Fangrui Song6907ce22018-07-30 19:24:48 +00001224 PrintObjCMethodType(OMD->getASTContext(),
Douglas Gregor813a0662015-06-19 18:14:38 +00001225 PI->getObjCDeclQualifier(),
1226 PI->getType());
1227 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001228 lastPos = pos + 1;
1229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
Douglas Gregor278f52e2009-05-30 00:08:05 +00001231 if (OMD->param_begin() == OMD->param_end())
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001232 Out << name;
Mike Stump11289f42009-09-09 15:08:12 +00001233
Douglas Gregor278f52e2009-05-30 00:08:05 +00001234 if (OMD->isVariadic())
1235 Out << ", ...";
Fangrui Song6907ce22018-07-30 19:24:48 +00001236
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001237 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001238
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001239 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001240 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001241 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001242 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001243 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001244 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001245}
1246
1247void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1248 std::string I = OID->getNameAsString();
1249 ObjCInterfaceDecl *SID = OID->getSuperClass();
1250
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001251 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001252 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001253 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001254 else
1255 Out << "@implementation " << I;
Fangrui Song6907ce22018-07-30 19:24:48 +00001256
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001257 if (OID->ivar_size() > 0) {
1258 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001259 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001260 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001261 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001262 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001263 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001264 }
1265 Indentation -= Policy.Indentation;
1266 Out << "}\n";
1267 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001268 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1269 Out << "\n";
1270 eolnOut = true;
1271 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001272 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001273 if (!eolnOut)
1274 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001275 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001276}
1277
1278void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1279 std::string I = OID->getNameAsString();
1280 ObjCInterfaceDecl *SID = OID->getSuperClass();
1281
Douglas Gregordc9166c2011-12-15 20:29:51 +00001282 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001283 Out << "@class " << I;
1284
1285 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1286 PrintObjCTypeParams(TypeParams);
1287 }
1288
1289 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001290 return;
1291 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001292 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001293 Out << "@interface " << I;
1294
1295 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1296 PrintObjCTypeParams(TypeParams);
1297 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001298
Douglas Gregor278f52e2009-05-30 00:08:05 +00001299 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001300 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001301
Douglas Gregor278f52e2009-05-30 00:08:05 +00001302 // Protocols?
1303 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1304 if (!Protocols.empty()) {
1305 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1306 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001307 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001308 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001309 }
Mike Stump11289f42009-09-09 15:08:12 +00001310
Douglas Gregor278f52e2009-05-30 00:08:05 +00001311 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001312 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001313 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001314 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001315 for (const auto *I : OID->ivars()) {
1316 Indent() << I->getASTContext()
1317 .getUnqualifiedObjCPointerType(I->getType())
1318 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001319 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001320 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001321 Out << "}\n";
1322 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001323 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001324 Out << "\n";
1325 eolnOut = true;
1326 }
Mike Stump11289f42009-09-09 15:08:12 +00001327
Douglas Gregor278f52e2009-05-30 00:08:05 +00001328 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001329 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001330 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001331 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001332 // FIXME: implement the rest...
1333}
1334
Douglas Gregor278f52e2009-05-30 00:08:05 +00001335void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001336 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001337 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001338 return;
1339 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001340 // Protocols?
1341 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1342 if (!Protocols.empty()) {
1343 Out << "@protocol " << *PID;
1344 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1345 E = Protocols.end(); I != E; ++I)
1346 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1347 Out << ">\n";
1348 } else
1349 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001350 VisitDeclContext(PID, false);
1351 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001352}
1353
1354void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001355 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001356
1357 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001358 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001359 // FIXME: implement the rest...
1360}
1361
1362void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001363 Out << "@interface " << *PID->getClassInterface();
1364 if (auto TypeParams = PID->getTypeParamList()) {
1365 PrintObjCTypeParams(TypeParams);
1366 }
1367 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001368 if (PID->ivar_size() > 0) {
1369 Out << "{\n";
1370 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001371 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001372 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001373 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001374 Indentation -= Policy.Indentation;
1375 Out << "}\n";
1376 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001377
Douglas Gregor278f52e2009-05-30 00:08:05 +00001378 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001379 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001380
Douglas Gregor278f52e2009-05-30 00:08:05 +00001381 // FIXME: implement the rest...
1382}
1383
1384void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001385 Out << "@compatibility_alias " << *AID
1386 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001387}
1388
1389/// PrintObjCPropertyDecl - print a property declaration.
1390///
1391void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1392 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1393 Out << "@required\n";
1394 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1395 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001396
Douglas Gregor813a0662015-06-19 18:14:38 +00001397 QualType T = PDecl->getType();
1398
Douglas Gregor278f52e2009-05-30 00:08:05 +00001399 Out << "@property";
1400 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1401 bool first = true;
1402 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001403 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001404 ObjCPropertyDecl::OBJC_PR_readonly) {
1405 Out << (first ? ' ' : ',') << "readonly";
1406 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001407 }
Mike Stump11289f42009-09-09 15:08:12 +00001408
Ted Kremenek897af912011-08-17 21:09:35 +00001409 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001410 Out << (first ? ' ' : ',') << "getter = ";
1411 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001412 first = false;
1413 }
1414 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001415 Out << (first ? ' ' : ',') << "setter = ";
1416 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001417 first = false;
1418 }
Mike Stump11289f42009-09-09 15:08:12 +00001419
Ted Kremenek897af912011-08-17 21:09:35 +00001420 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1421 Out << (first ? ' ' : ',') << "assign";
1422 first = false;
1423 }
Mike Stump11289f42009-09-09 15:08:12 +00001424
Ted Kremenek897af912011-08-17 21:09:35 +00001425 if (PDecl->getPropertyAttributes() &
1426 ObjCPropertyDecl::OBJC_PR_readwrite) {
1427 Out << (first ? ' ' : ',') << "readwrite";
1428 first = false;
1429 }
Mike Stump11289f42009-09-09 15:08:12 +00001430
Ted Kremenek897af912011-08-17 21:09:35 +00001431 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1432 Out << (first ? ' ' : ',') << "retain";
1433 first = false;
1434 }
Mike Stump11289f42009-09-09 15:08:12 +00001435
Ted Kremenek897af912011-08-17 21:09:35 +00001436 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1437 Out << (first ? ' ' : ',') << "strong";
1438 first = false;
1439 }
John McCall31168b02011-06-15 23:02:42 +00001440
Ted Kremenek897af912011-08-17 21:09:35 +00001441 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1442 Out << (first ? ' ' : ',') << "copy";
1443 first = false;
1444 }
Mike Stump11289f42009-09-09 15:08:12 +00001445
Ted Kremenek897af912011-08-17 21:09:35 +00001446 if (PDecl->getPropertyAttributes() &
1447 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1448 Out << (first ? ' ' : ',') << "nonatomic";
1449 first = false;
1450 }
1451 if (PDecl->getPropertyAttributes() &
1452 ObjCPropertyDecl::OBJC_PR_atomic) {
1453 Out << (first ? ' ' : ',') << "atomic";
1454 first = false;
1455 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001456
Douglas Gregor813a0662015-06-19 18:14:38 +00001457 if (PDecl->getPropertyAttributes() &
1458 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001459 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001460 if (*nullability == NullabilityKind::Unspecified &&
1461 (PDecl->getPropertyAttributes() &
1462 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1463 Out << (first ? ' ' : ',') << "null_resettable";
1464 } else {
1465 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001466 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001467 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001468 first = false;
1469 }
1470 }
1471
Manman Ren387ff7f2016-01-26 18:52:43 +00001472 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1473 Out << (first ? ' ' : ',') << "class";
1474 first = false;
1475 }
1476
Ted Kremenek897af912011-08-17 21:09:35 +00001477 (void) first; // Silence dead store warning due to idiomatic code.
1478 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001479 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001480 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001481 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001482 if (Policy.PolishForDeclaration)
1483 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001484}
1485
1486void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1487 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001488 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001489 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001490 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001491 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001492 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001493 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001494}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001495
1496void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001497 if (!D->isAccessDeclaration())
1498 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001499 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001500 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001501 D->getQualifier()->print(Out, Policy);
Alex Lorenzea9b59a2016-10-03 12:22:17 +00001502
1503 // Use the correct record name when the using declaration is used for
1504 // inheriting constructors.
1505 for (const auto *Shadow : D->shadows()) {
1506 if (const auto *ConstructorShadow =
1507 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1508 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1509 Out << *ConstructorShadow->getNominatedBaseClass();
1510 return;
1511 }
1512 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001513 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001514}
1515
John McCalle61f2ba2009-11-18 02:36:19 +00001516void
1517DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1518 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001519 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001520 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001521}
1522
1523void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001524 if (!D->isAccessDeclaration())
1525 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001526 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001527 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001528}
John McCall3f746822009-11-17 05:59:44 +00001529
1530void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1531 // ignore
1532}
Alexey Bataeva769e072013-03-22 06:34:35 +00001533
1534void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1535 Out << "#pragma omp threadprivate";
1536 if (!D->varlist_empty()) {
1537 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1538 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001539 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001540 Out << (I == D->varlist_begin() ? '(' : ',');
George Burgess IV00f70bd2018-03-01 05:43:23 +00001541 NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001542 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001543 }
1544 Out << ")";
1545 }
1546}
1547
Kelvin Li1408f912018-09-26 04:28:39 +00001548void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1549 Out << "#pragma omp requires ";
1550 if (!D->clauselist_empty()) {
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00001551 OMPClausePrinter Printer(Out, Policy);
1552 for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I)
1553 Printer.Visit(*I);
Kelvin Li1408f912018-09-26 04:28:39 +00001554 }
1555}
1556
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001557void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1558 if (!D->isInvalidDecl()) {
1559 Out << "#pragma omp declare reduction (";
1560 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1561 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1562 nullptr,
1563#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1564 Spelling,
1565#include "clang/Basic/OperatorKinds.def"
1566 };
1567 const char *OpName =
1568 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1569 assert(OpName && "not an overloaded operator");
1570 Out << OpName;
1571 } else {
1572 assert(D->getDeclName().isIdentifier());
1573 D->printName(Out);
1574 }
1575 Out << " : ";
1576 D->getType().print(Out, Policy);
1577 Out << " : ";
1578 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1579 Out << ")";
1580 if (auto *Init = D->getInitializer()) {
1581 Out << " initializer(";
Alexey Bataev070f43a2017-09-06 14:49:58 +00001582 switch (D->getInitializerKind()) {
1583 case OMPDeclareReductionDecl::DirectInit:
1584 Out << "omp_priv(";
1585 break;
1586 case OMPDeclareReductionDecl::CopyInit:
1587 Out << "omp_priv = ";
1588 break;
1589 case OMPDeclareReductionDecl::CallInit:
1590 break;
1591 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001592 Init->printPretty(Out, nullptr, Policy, 0);
Alexey Bataev070f43a2017-09-06 14:49:58 +00001593 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1594 Out << ")";
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001595 Out << ")";
1596 }
1597 }
1598}
1599
Alexey Bataev4244be22016-02-11 05:35:55 +00001600void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001601 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1602}
1603