blob: 00db026ebb49150206fea1d4dbffe677fa10e5e6 [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);
Mike Stump11289f42009-09-09 15:08:12 +000040
Douglas Gregor813a0662015-06-19 18:14:38 +000041 /// Print an Objective-C method type in parentheses.
42 ///
43 /// \param Quals The Objective-C declaration qualifiers.
44 /// \param T The type to print.
45 void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
46 QualType T);
47
Douglas Gregor85f3f952015-07-07 03:57:15 +000048 void PrintObjCTypeParams(ObjCTypeParamList *Params);
49
Douglas Gregor278f52e2009-05-30 00:08:05 +000050 public:
Richard Smith235341b2012-08-16 03:56:14 +000051 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
Alex Lorenz36070ed2017-08-17 13:41:55 +000052 const ASTContext &Context, unsigned Indentation = 0,
53 bool PrintInstantiation = false)
54 : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation),
55 PrintInstantiation(PrintInstantiation) {}
Douglas Gregor278f52e2009-05-30 00:08:05 +000056
57 void VisitDeclContext(DeclContext *DC, bool Indent = true);
58
59 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
60 void VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +000061 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000062 void VisitEnumDecl(EnumDecl *D);
63 void VisitRecordDecl(RecordDecl *D);
64 void VisitEnumConstantDecl(EnumConstantDecl *D);
Michael Han84324352013-02-22 17:15:32 +000065 void VisitEmptyDecl(EmptyDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000066 void VisitFunctionDecl(FunctionDecl *D);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +000067 void VisitFriendDecl(FriendDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000068 void VisitFieldDecl(FieldDecl *D);
69 void VisitVarDecl(VarDecl *D);
Chris Lattnercab02a62011-02-17 20:34:02 +000070 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000071 void VisitParmVarDecl(ParmVarDecl *D);
72 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregorba345522011-12-02 23:23:56 +000073 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +000074 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000075 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000076 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000077 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000078 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000079 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith030f4992011-04-15 13:38:57 +000080 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000081 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
82 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Serge Pavlova67a4d22016-11-10 08:49:37 +000083 void VisitClassTemplateSpecializationDecl(
84 ClassTemplateSpecializationDecl *D);
85 void VisitClassTemplatePartialSpecializationDecl(
86 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000087 void VisitObjCMethodDecl(ObjCMethodDecl *D);
88 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
89 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000090 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
91 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
92 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
93 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
94 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
95 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000096 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
97 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +000098 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +000099 void VisitUsingShadowDecl(UsingShadowDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000100 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000101 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000102 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +0000103
Serge Pavlova67a4d22016-11-10 08:49:37 +0000104 void printTemplateParameters(const TemplateParameterList *Params);
105 void printTemplateArguments(const TemplateArgumentList &Args,
106 const TemplateParameterList *Params = nullptr);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000107 void prettyPrintAttributes(Decl *D);
Alexey Bataev6d455322015-10-12 06:59:48 +0000108 void prettyPrintPragmas(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +0000109 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000110 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000111}
Douglas Gregor278f52e2009-05-30 00:08:05 +0000112
Richard Trieu82398f92011-07-28 00:19:05 +0000113void Decl::print(raw_ostream &Out, unsigned Indentation,
114 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +0000115 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000116}
117
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000118void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000119 unsigned Indentation, bool PrintInstantiation) const {
Alex Lorenz36070ed2017-08-17 13:41:55 +0000120 DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
121 PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000122 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000123}
124
Eli Friedman79635842009-05-30 04:20:30 +0000125static QualType GetBaseType(QualType T) {
126 // FIXME: This should be on the Type class!
127 QualType BaseType = T;
128 while (!BaseType->isSpecifierType()) {
129 if (isa<TypedefType>(BaseType))
130 break;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000131 else 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();
Eli Friedman79635842009-05-30 04:20:30 +0000145 else
David Blaikie83d382b2011-09-23 05:06:16 +0000146 llvm_unreachable("Unknown declarator!");
Eli Friedman79635842009-05-30 04:20:30 +0000147 }
148 return BaseType;
149}
150
151static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000152 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000153 return TDD->getUnderlyingType();
154 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
155 return VD->getType();
156 return QualType();
157}
158
159void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000160 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000161 unsigned Indentation) {
162 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000163 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000164 return;
165 }
166
167 Decl** End = Begin + NumDecls;
168 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
169 if (TD)
170 ++Begin;
171
172 PrintingPolicy SubPolicy(Policy);
Eli Friedman79635842009-05-30 04:20:30 +0000173
174 bool isFirst = true;
175 for ( ; Begin != End; ++Begin) {
176 if (isFirst) {
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000177 if(TD)
178 SubPolicy.IncludeTagDefinition = true;
Eli Friedman79635842009-05-30 04:20:30 +0000179 SubPolicy.SuppressSpecifiers = false;
180 isFirst = false;
181 } else {
182 if (!isFirst) Out << ", ";
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000183 SubPolicy.IncludeTagDefinition = false;
Eli Friedman79635842009-05-30 04:20:30 +0000184 SubPolicy.SuppressSpecifiers = true;
185 }
186
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000187 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000188 }
189}
190
Alp Tokeref6b0072014-01-04 13:47:14 +0000191LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000192 // Get the translation unit
193 const DeclContext *DC = this;
194 while (!DC->isTranslationUnit())
195 DC = DC->getParent();
196
197 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Alex Lorenz36070ed2017-08-17 13:41:55 +0000198 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000199 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
200}
201
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000202raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000203 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000204 Out << " ";
205 return Out;
206}
207
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000208void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000209 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000210 return;
Alexey Bataev6d455322015-10-12 06:59:48 +0000211
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000212 if (D->hasAttrs()) {
213 AttrVec &Attrs = D->getAttrs();
Alexey Bataev6d455322015-10-12 06:59:48 +0000214 for (auto *A : Attrs) {
215 switch (A->getKind()) {
216#define ATTR(X)
217#define PRAGMA_SPELLING_ATTR(X) case attr::X:
218#include "clang/Basic/AttrList.inc"
219 break;
220 default:
221 A->printPretty(Out, Policy);
222 break;
223 }
224 }
225 }
226}
227
228void DeclPrinter::prettyPrintPragmas(Decl *D) {
229 if (Policy.PolishForDeclaration)
230 return;
231
232 if (D->hasAttrs()) {
233 AttrVec &Attrs = D->getAttrs();
234 for (auto *A : Attrs) {
235 switch (A->getKind()) {
236#define ATTR(X)
237#define PRAGMA_SPELLING_ATTR(X) case attr::X:
238#include "clang/Basic/AttrList.inc"
239 A->printPretty(Out, Policy);
240 Indent();
241 break;
242 default:
243 break;
244 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000245 }
246 }
247}
248
Richard Smitha4bb2922014-07-23 03:17:06 +0000249void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
250 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
251 // template argument), but if it is the type of a declaration, the ellipsis
252 // is placed before the name being declared.
253 if (auto *PET = T->getAs<PackExpansionType>()) {
254 Pack = true;
255 T = PET->getPattern();
256 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000257 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
Richard Smitha4bb2922014-07-23 03:17:06 +0000258}
259
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000260void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000261 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000262 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000263 Out << ";\n";
264 Decls.clear();
265
266}
267
Anders Carlsson601d6e42009-08-28 22:39:52 +0000268void DeclPrinter::Print(AccessSpecifier AS) {
269 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000270 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000271 case AS_public: Out << "public"; break;
272 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000273 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000274 }
275}
276
Douglas Gregor278f52e2009-05-30 00:08:05 +0000277//----------------------------------------------------------------------------
278// Common C declarations
279//----------------------------------------------------------------------------
280
281void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000282 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000283 return;
284
Douglas Gregor278f52e2009-05-30 00:08:05 +0000285 if (Indent)
286 Indentation += Policy.Indentation;
287
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000288 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000289 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000290 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000291
292 // Don't print ObjCIvarDecls, as they are printed when visiting the
293 // containing ObjCInterfaceDecl.
294 if (isa<ObjCIvarDecl>(*D))
295 continue;
296
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000297 // Skip over implicit declarations in pretty-printing mode.
298 if (D->isImplicit())
299 continue;
300
Serge Pavlova67a4d22016-11-10 08:49:37 +0000301 // Don't print implicit specializations, as they are printed when visiting
302 // corresponding templates.
303 if (auto FD = dyn_cast<FunctionDecl>(*D))
304 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
305 !isa<ClassTemplateSpecializationDecl>(DC))
306 continue;
307
Eli Friedman79635842009-05-30 04:20:30 +0000308 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
309 // forced to merge the declarations because there's no other way to
310 // refer to the struct in question. This limited merging is safe without
311 // a bunch of other checks because it only merges declarations directly
312 // referring to the tag, not typedefs.
313 //
314 // Check whether the current declaration should be grouped with a previous
315 // unnamed struct.
316 QualType CurDeclType = getDeclType(*D);
317 if (!Decls.empty() && !CurDeclType.isNull()) {
318 QualType BaseType = GetBaseType(CurDeclType);
Eli Friedman24b3fed2013-08-12 21:54:04 +0000319 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
320 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
Eli Friedman79635842009-05-30 04:20:30 +0000321 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
322 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
323 Decls.push_back(*D);
324 continue;
325 }
326 }
327
328 // If we have a merged group waiting to be handled, handle it now.
329 if (!Decls.empty())
330 ProcessDeclGroup(Decls);
331
332 // If the current declaration is an unnamed tag type, save it
333 // so we can merge it with the subsequent declaration(s) using it.
334 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
335 Decls.push_back(*D);
336 continue;
337 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000338
339 if (isa<AccessSpecDecl>(*D)) {
340 Indentation -= Policy.Indentation;
341 this->Indent();
342 Print(D->getAccess());
343 Out << ":\n";
344 Indentation += Policy.Indentation;
345 continue;
346 }
347
Douglas Gregor278f52e2009-05-30 00:08:05 +0000348 this->Indent();
349 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000350
351 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000352 const char *Terminator = nullptr;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000353 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000354 Terminator = nullptr;
Serge Pavlovdc586c42016-10-31 05:11:12 +0000355 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000356 Terminator = nullptr;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000357 else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
358 if (FD->isThisDeclarationADefinition())
359 Terminator = nullptr;
360 else
361 Terminator = ";";
362 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
363 if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
364 Terminator = nullptr;
365 else
366 Terminator = ";";
367 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000368 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000369 isa<ObjCInterfaceDecl>(*D) ||
370 isa<ObjCProtocolDecl>(*D) ||
371 isa<ObjCCategoryImplDecl>(*D) ||
372 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000373 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000374 else if (isa<EnumConstantDecl>(*D)) {
375 DeclContext::decl_iterator Next = D;
376 ++Next;
377 if (Next != DEnd)
378 Terminator = ",";
379 } else
380 Terminator = ";";
381
382 if (Terminator)
383 Out << Terminator;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000384 if (!Policy.TerseOutput &&
385 ((isa<FunctionDecl>(*D) &&
386 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
387 (isa<FunctionTemplateDecl>(*D) &&
388 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
389 ; // StmtPrinter already added '\n' after CompoundStmt.
390 else
391 Out << "\n";
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000392
393 // Declare target attribute is special one, natural spelling for the pragma
394 // assumes "ending" construct so print it here.
395 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
396 Out << "#pragma omp end declare target\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000397 }
398
Eli Friedman79635842009-05-30 04:20:30 +0000399 if (!Decls.empty())
400 ProcessDeclGroup(Decls);
401
Douglas Gregor278f52e2009-05-30 00:08:05 +0000402 if (Indent)
403 Indentation -= Policy.Indentation;
404}
405
406void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
407 VisitDeclContext(D, false);
408}
409
410void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000411 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000412 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000413
414 if (D->isModulePrivate())
415 Out << "__module_private__ ";
416 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000417 QualType Ty = D->getTypeSourceInfo()->getType();
418 Ty.print(Out, Policy, D->getName(), Indentation);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000419 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000420}
421
Richard Smithdda56e42011-04-15 14:24:37 +0000422void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000423 Out << "using " << *D;
424 prettyPrintAttributes(D);
425 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000426}
427
Douglas Gregor278f52e2009-05-30 00:08:05 +0000428void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000429 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
430 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000431 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000432 if (D->isScoped()) {
433 if (D->isScopedUsingClassTag())
434 Out << "class ";
435 else
436 Out << "struct ";
437 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000438 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000439
Serge Pavlov08c8de22016-11-04 06:03:34 +0000440 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000441 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000442
John McCallf937c022011-10-07 06:10:15 +0000443 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000444 Out << " {\n";
445 VisitDeclContext(D);
446 Indent() << "}";
447 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000448 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000449}
450
451void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000452 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
453 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000454 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000455
456 prettyPrintAttributes(D);
457
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000458 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000459 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000460
John McCallf937c022011-10-07 06:10:15 +0000461 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000462 Out << " {\n";
463 VisitDeclContext(D);
464 Indent() << "}";
465 }
466}
467
468void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000469 Out << *D;
Jordan Roseccca6692017-01-20 03:33:42 +0000470 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000471 if (Expr *Init = D->getInitExpr()) {
472 Out << " = ";
Alex Lorenz36070ed2017-08-17 13:41:55 +0000473 Init->printPretty(Out, nullptr, Policy, Indentation, &Context);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000474 }
475}
476
Mike Stump11289f42009-09-09 15:08:12 +0000477void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000478 if (!D->getDescribedFunctionTemplate() &&
479 !D->isFunctionTemplateSpecialization())
480 prettyPrintPragmas(D);
481
Serge Pavlova67a4d22016-11-10 08:49:37 +0000482 if (D->isFunctionTemplateSpecialization())
483 Out << "template<> ";
Alex Lorenz47fd10c2017-04-18 15:12:34 +0000484 else if (!D->getDescribedFunctionTemplate()) {
485 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
486 I < NumTemplateParams; ++I)
487 printTemplateParameters(D->getTemplateParameterList(I));
488 }
Serge Pavlova67a4d22016-11-10 08:49:37 +0000489
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000490 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000491 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Richard Smith057ec502017-02-18 01:01:48 +0000492 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000493 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000494 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000495 case SC_None: break;
496 case SC_Extern: Out << "extern "; break;
497 case SC_Static: Out << "static "; break;
498 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000499 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000500 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000501 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000502
Douglas Gregor26701a42011-09-09 02:06:17 +0000503 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000504 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000505 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000506 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000507 if ((CDecl && CDecl->isExplicitSpecified()) ||
Richard Smith057ec502017-02-18 01:01:48 +0000508 (ConversionDecl && ConversionDecl->isExplicitSpecified()) ||
509 (GuideDecl && GuideDecl->isExplicitSpecified()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000510 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000511 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000512
Douglas Gregor2d042f12009-05-30 05:39:39 +0000513 PrintingPolicy SubPolicy(Policy);
514 SubPolicy.SuppressSpecifiers = false;
Alex Lorenza981c7d2017-04-11 16:46:03 +0000515 std::string Proto;
516 if (!Policy.SuppressScope) {
517 if (const NestedNameSpecifier *NS = D->getQualifier()) {
518 llvm::raw_string_ostream OS(Proto);
519 NS->print(OS, Policy);
520 }
521 }
522 Proto += D->getNameInfo().getAsString();
Richard Smith057ec502017-02-18 01:01:48 +0000523 if (GuideDecl)
524 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
Serge Pavlova67a4d22016-11-10 08:49:37 +0000525 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
526 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000527 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000528 TArgPrinter.printTemplateArguments(*TArgs);
529 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000530
Abramo Bagnara6d810632010-12-14 22:11:44 +0000531 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000532 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000533 Proto = '(' + Proto + ')';
534 Ty = PT->getInnerType();
535 }
536
Reid Kleckner0503a872013-12-05 01:23:43 +0000537 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000538 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000539 if (D->hasWrittenPrototype())
540 FT = dyn_cast<FunctionProtoType>(AFT);
541
542 Proto += "(";
543 if (FT) {
544 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000545 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000546 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
547 if (i) POut << ", ";
548 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
549 }
Mike Stump11289f42009-09-09 15:08:12 +0000550
Douglas Gregor278f52e2009-05-30 00:08:05 +0000551 if (FT->isVariadic()) {
552 if (D->getNumParams()) POut << ", ";
553 POut << "...";
554 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000555 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000556 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
557 if (i)
558 Proto += ", ";
559 Proto += D->getParamDecl(i)->getNameAsString();
560 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000561 }
562
563 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000564
David Blaikief5697e52012-08-10 00:55:35 +0000565 if (FT) {
566 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000567 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000568 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000569 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000570 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000571 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000572
573 switch (FT->getRefQualifier()) {
574 case RQ_None:
575 break;
576 case RQ_LValue:
577 Proto += " &";
578 break;
579 case RQ_RValue:
580 Proto += " &&";
581 break;
582 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000583 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000584
585 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000586 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000587 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000588 Proto += "...";
589 else
590 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
591 if (I)
592 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000593
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000594 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000595 }
596 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000597 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
598 Proto += " noexcept";
599 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
600 Proto += "(";
601 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000602 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000603 Indentation);
604 EOut.flush();
605 Proto += EOut.str();
606 Proto += ")";
607 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000608 }
609
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000610 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000611 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000612 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000613 if (BMInitializer->isInClassMemberInitializer())
614 continue;
615
616 if (!HasInitializerList) {
617 Proto += " : ";
618 Out << Proto;
619 Proto.clear();
620 HasInitializerList = true;
621 } else
622 Out << ", ";
623
624 if (BMInitializer->isAnyMemberInitializer()) {
625 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000626 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000627 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000628 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000629 }
630
631 Out << "(";
632 if (!BMInitializer->getInit()) {
633 // Nothing to print
634 } else {
635 Expr *Init = BMInitializer->getInit();
636 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
637 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000638
Richard Smith938f40b2011-06-11 17:19:42 +0000639 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000640
641 Expr *SimpleInit = nullptr;
642 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000643 unsigned NumArgs = 0;
644 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
645 Args = ParenList->getExprs();
646 NumArgs = ParenList->getNumExprs();
647 } else if (CXXConstructExpr *Construct
648 = dyn_cast<CXXConstructExpr>(Init)) {
649 Args = Construct->getArgs();
650 NumArgs = Construct->getNumArgs();
651 } else
652 SimpleInit = Init;
653
654 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000655 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000656 else {
657 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000658 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000659 if (isa<CXXDefaultArgExpr>(Args[I]))
660 break;
661
662 if (I)
663 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000664 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000665 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000666 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000667 }
Richard Smith938f40b2011-06-11 17:19:42 +0000668 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000669 if (BMInitializer->isPackExpansion())
670 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000671 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000672 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000673 if (FT && FT->hasTrailingReturn()) {
Richard Smith057ec502017-02-18 01:01:48 +0000674 if (!GuideDecl)
675 Out << "auto ";
676 Out << Proto << " -> ";
Richard Smithe6560762013-02-22 05:54:51 +0000677 Proto.clear();
678 }
Alp Toker314cc812014-01-25 16:55:45 +0000679 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000680 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000681 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000682 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000683 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000684 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000685 }
686
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000687 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000688
689 if (D->isPure())
690 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000691 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000692 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000693 else if (D->isExplicitlyDefaulted())
694 Out << " = default";
Serge Pavlova67a4d22016-11-10 08:49:37 +0000695 else if (D->doesThisDeclarationHaveABody()) {
696 if (!Policy.TerseOutput) {
697 if (!D->hasPrototype() && D->getNumParams()) {
698 // This is a K&R function definition, so we need to print the
699 // parameters.
700 Out << '\n';
Alex Lorenz36070ed2017-08-17 13:41:55 +0000701 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000702 Indentation += Policy.Indentation;
703 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
704 Indent();
705 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
706 Out << ";\n";
707 }
708 Indentation -= Policy.Indentation;
709 } else
710 Out << ' ';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000711
Serge Pavlova67a4d22016-11-10 08:49:37 +0000712 if (D->getBody())
713 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
714 } else {
715 if (isa<CXXConstructorDecl>(*D))
716 Out << " {}";
717 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000718 }
719}
720
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000721void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
722 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000723 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
724 for (unsigned i = 0; i < NumTPLists; ++i)
Serge Pavlova67a4d22016-11-10 08:49:37 +0000725 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000726 Out << "friend ";
727 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000728 }
729 else if (FunctionDecl *FD =
730 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
731 Out << "friend ";
732 VisitFunctionDecl(FD);
733 }
734 else if (FunctionTemplateDecl *FTD =
735 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
736 Out << "friend ";
737 VisitFunctionTemplateDecl(FTD);
738 }
739 else if (ClassTemplateDecl *CTD =
740 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
741 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000742 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000743 }
744}
745
Douglas Gregor278f52e2009-05-30 00:08:05 +0000746void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000747 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000748 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000749 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000750 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
751 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000752
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000753 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000754 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000755
756 if (D->isBitField()) {
757 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000758 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000759 }
Richard Smith938f40b2011-06-11 17:19:42 +0000760
761 Expr *Init = D->getInClassInitializer();
762 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000763 if (D->getInClassInitStyle() == ICIS_ListInit)
764 Out << " ";
765 else
766 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000767 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000768 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000769 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000770}
771
Chris Lattnercab02a62011-02-17 20:34:02 +0000772void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000773 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000774}
775
Douglas Gregor278f52e2009-05-30 00:08:05 +0000776void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000777 prettyPrintPragmas(D);
Vassil Vassilev10023732016-07-08 21:09:08 +0000778
779 QualType T = D->getTypeSourceInfo()
780 ? D->getTypeSourceInfo()->getType()
781 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
782
Richard Smithfd3834f2013-04-13 02:43:54 +0000783 if (!Policy.SuppressSpecifiers) {
784 StorageClass SC = D->getStorageClass();
785 if (SC != SC_None)
786 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000787
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000788 switch (D->getTSCSpec()) {
789 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000790 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000791 case TSCS___thread:
792 Out << "__thread ";
793 break;
794 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000795 Out << "_Thread_local ";
796 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000797 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000798 Out << "thread_local ";
799 break;
800 }
801
802 if (D->isModulePrivate())
803 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000804
805 if (D->isConstexpr()) {
806 Out << "constexpr ";
807 T.removeLocalConst();
808 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000809 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000810
Richard Smitha4bb2922014-07-23 03:17:06 +0000811 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000812 Expr *Init = D->getInit();
813 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000814 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000815 if (CXXConstructExpr *Construct =
816 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000817 if (D->getInitStyle() == VarDecl::CallInit &&
818 !Construct->isListInitialization()) {
819 ImplicitInit = Construct->getNumArgs() == 0 ||
820 Construct->getArg(0)->isDefaultArgument();
821 }
822 }
Sebastian Redla9351792012-02-11 23:51:47 +0000823 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000824 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000825 Out << "(";
826 else if (D->getInitStyle() == VarDecl::CInit) {
827 Out << " = ";
828 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000829 PrintingPolicy SubPolicy(Policy);
830 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000831 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000832 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000833 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000834 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000835 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000836 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000837 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000838}
839
840void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
841 VisitVarDecl(D);
842}
843
844void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
845 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000846 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000847 Out << ")";
848}
849
Douglas Gregorba345522011-12-02 23:23:56 +0000850void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000851 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000852 << ";\n";
853}
854
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000855void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
856 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000857 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000858 if (StringLiteral *SL = D->getMessage()) {
859 Out << ", ";
860 SL->printPretty(Out, nullptr, Policy, Indentation);
861 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000862 Out << ")";
863}
864
Douglas Gregor278f52e2009-05-30 00:08:05 +0000865//----------------------------------------------------------------------------
866// C++ declarations
867//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000868void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000869 if (D->isInline())
870 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000871 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000872 VisitDeclContext(D);
873 Indent() << "}";
874}
875
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000876void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
877 Out << "using namespace ";
878 if (D->getQualifier())
879 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000880 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000881}
882
Douglas Gregor18231932009-05-30 06:48:27 +0000883void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000884 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000885 if (D->getQualifier())
886 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000887 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000888}
889
Michael Han84324352013-02-22 17:15:32 +0000890void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
891 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000892}
893
Douglas Gregor5f478b72009-05-30 06:58:37 +0000894void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000895 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000896 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
897 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000898 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000899
900 prettyPrintAttributes(D);
901
Serge Pavlova67a4d22016-11-10 08:49:37 +0000902 if (D->getIdentifier()) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000903 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000904
Serge Pavlova67a4d22016-11-10 08:49:37 +0000905 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
906 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
907 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
908 printTemplateArguments(S->getTemplateArgs());
909 }
910
John McCallf937c022011-10-07 06:10:15 +0000911 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000912 // Print the base classes
913 if (D->getNumBases()) {
914 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000915 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
916 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000917 if (Base != D->bases_begin())
918 Out << ", ";
919
920 if (Base->isVirtual())
921 Out << "virtual ";
922
Anders Carlsson6df9e072009-08-29 20:36:12 +0000923 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000924 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000925 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000926 Out << " ";
927 }
928 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000929
930 if (Base->isPackExpansion())
931 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000932 }
933 }
934
935 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000936 // FIXME: Doesn't print access specifiers, e.g., "public:"
Serge Pavlova67a4d22016-11-10 08:49:37 +0000937 if (Policy.TerseOutput) {
938 Out << " {}";
939 } else {
940 Out << " {\n";
941 VisitDeclContext(D);
942 Indent() << "}";
943 }
Mike Stump11289f42009-09-09 15:08:12 +0000944 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000945}
946
947void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
948 const char *l;
949 if (D->getLanguage() == LinkageSpecDecl::lang_c)
950 l = "C";
951 else {
952 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
953 "unknown language in linkage specification");
954 l = "C++";
955 }
956
957 Out << "extern \"" << l << "\" ";
958 if (D->hasBraces()) {
959 Out << "{\n";
960 VisitDeclContext(D);
961 Indent() << "}";
962 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000963 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000964}
965
Serge Pavlova67a4d22016-11-10 08:49:37 +0000966void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params) {
Richard Trieu82398f92011-07-28 00:19:05 +0000967 assert(Params);
Richard Trieu82398f92011-07-28 00:19:05 +0000968
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000969 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000970
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000971 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
972 if (i != 0)
973 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000974
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000975 const Decl *Param = Params->getParam(i);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000976 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000977
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000978 if (TTP->wasDeclaredWithTypename())
979 Out << "typename ";
980 else
981 Out << "class ";
982
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000983 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000984 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000985
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000986 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000987
Serge Pavlova67a4d22016-11-10 08:49:37 +0000988 if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000989 Out << " = ";
990 Out << TTP->getDefaultArgument().getAsString(Policy);
991 };
Serge Pavlova67a4d22016-11-10 08:49:37 +0000992 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000993 StringRef Name;
994 if (IdentifierInfo *II = NTTP->getIdentifier())
995 Name = II->getName();
996 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000997
Serge Pavlova67a4d22016-11-10 08:49:37 +0000998 if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000999 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +00001000 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1001 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001002 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001003 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
Richard Smith030f4992011-04-15 13:38:57 +00001004 VisitTemplateDecl(TTPD);
1005 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001006 }
1007 }
Mike Stump11289f42009-09-09 15:08:12 +00001008
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001009 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +00001010}
1011
Serge Pavlova67a4d22016-11-10 08:49:37 +00001012void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1013 const TemplateParameterList *Params) {
1014 Out << "<";
1015 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1016 const TemplateArgument &A = Args[I];
1017 if (I)
1018 Out << ", ";
1019 if (Params) {
1020 if (A.getKind() == TemplateArgument::Type)
1021 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1022 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1023 Out << *P;
1024 continue;
1025 }
1026 if (A.getKind() == TemplateArgument::Template) {
1027 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1028 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1029 auto P = cast<TemplateTemplateParmDecl>(
1030 Params->getParam(TD->getIndex()));
1031 Out << *P;
1032 continue;
1033 }
1034 }
1035 if (A.getKind() == TemplateArgument::Expression) {
1036 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1037 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1038 auto P = cast<NonTypeTemplateParmDecl>(
1039 Params->getParam(N->getIndex()));
1040 Out << *P;
1041 continue;
1042 }
1043 }
1044 }
1045 A.print(Policy, Out);
1046 }
1047 Out << ">";
1048}
1049
Richard Trieu82398f92011-07-28 00:19:05 +00001050void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001051 printTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001052
Richard Smith030f4992011-04-15 13:38:57 +00001053 if (const TemplateTemplateParmDecl *TTP =
1054 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +00001055 Out << "class ";
1056 if (TTP->isParameterPack())
1057 Out << "...";
1058 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +00001059 } else {
1060 Visit(D->getTemplatedDecl());
1061 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001062}
1063
Richard Trieu82398f92011-07-28 00:19:05 +00001064void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +00001065 prettyPrintPragmas(D->getTemplatedDecl());
Alex Lorenz47fd10c2017-04-18 15:12:34 +00001066 // Print any leading template parameter lists.
1067 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1068 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1069 I < NumTemplateParams; ++I)
1070 printTemplateParameters(FD->getTemplateParameterList(I));
1071 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001072 VisitRedeclarableTemplateDecl(D);
1073
Richard Smith057ec502017-02-18 01:01:48 +00001074 // Never print "instantiations" for deduction guides (they don't really
1075 // have them).
1076 if (PrintInstantiation &&
1077 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001078 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1079 const FunctionDecl *Def;
1080 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1081 return;
1082 for (auto *I : D->specializations())
1083 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1084 if (!PrevDecl->isThisDeclarationADefinition())
1085 Out << ";\n";
1086 Indent();
1087 prettyPrintPragmas(I);
1088 Visit(I);
1089 }
1090 }
Richard Trieu82398f92011-07-28 00:19:05 +00001091}
1092
1093void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001094 VisitRedeclarableTemplateDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001095
Serge Pavlova67a4d22016-11-10 08:49:37 +00001096 if (PrintInstantiation) {
1097 for (auto *I : D->specializations())
1098 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1099 if (D->isThisDeclarationADefinition())
1100 Out << ";";
1101 Out << "\n";
1102 Visit(I);
1103 }
1104 }
1105}
1106
1107void DeclPrinter::VisitClassTemplateSpecializationDecl(
1108 ClassTemplateSpecializationDecl *D) {
1109 Out << "template<> ";
1110 VisitCXXRecordDecl(D);
1111}
1112
1113void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1114 ClassTemplatePartialSpecializationDecl *D) {
1115 printTemplateParameters(D->getTemplateParameters());
1116 VisitCXXRecordDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001117}
1118
Douglas Gregor278f52e2009-05-30 00:08:05 +00001119//----------------------------------------------------------------------------
1120// Objective-C declarations
1121//----------------------------------------------------------------------------
1122
Douglas Gregor813a0662015-06-19 18:14:38 +00001123void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1124 Decl::ObjCDeclQualifier Quals,
1125 QualType T) {
1126 Out << '(';
1127 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1128 Out << "in ";
1129 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1130 Out << "inout ";
1131 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1132 Out << "out ";
1133 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1134 Out << "bycopy ";
1135 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1136 Out << "byref ";
1137 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1138 Out << "oneway ";
1139 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001140 if (auto nullability = AttributedType::stripOuterNullability(T))
1141 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001142 }
1143
1144 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1145 Out << ')';
1146}
1147
Douglas Gregor85f3f952015-07-07 03:57:15 +00001148void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1149 Out << "<";
1150 unsigned First = true;
1151 for (auto *Param : *Params) {
1152 if (First) {
1153 First = false;
1154 } else {
1155 Out << ", ";
1156 }
1157
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001158 switch (Param->getVariance()) {
1159 case ObjCTypeParamVariance::Invariant:
1160 break;
1161
1162 case ObjCTypeParamVariance::Covariant:
1163 Out << "__covariant ";
1164 break;
1165
1166 case ObjCTypeParamVariance::Contravariant:
1167 Out << "__contravariant ";
1168 break;
1169 }
1170
Douglas Gregor85f3f952015-07-07 03:57:15 +00001171 Out << Param->getDeclName().getAsString();
1172
1173 if (Param->hasExplicitBound()) {
1174 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1175 }
1176 }
1177 Out << ">";
1178}
1179
Douglas Gregor278f52e2009-05-30 00:08:05 +00001180void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1181 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001182 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001183 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001184 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001185 if (!OMD->getReturnType().isNull()) {
1186 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1187 OMD->getReturnType());
1188 }
Mike Stump11289f42009-09-09 15:08:12 +00001189
Douglas Gregor278f52e2009-05-30 00:08:05 +00001190 std::string name = OMD->getSelector().getAsString();
1191 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001192 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001193 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001194 pos = name.find_first_of(':', lastPos);
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001195 if (lastPos != 0)
1196 Out << " ";
1197 Out << name.substr(lastPos, pos - lastPos) << ':';
Douglas Gregor813a0662015-06-19 18:14:38 +00001198 PrintObjCMethodType(OMD->getASTContext(),
1199 PI->getObjCDeclQualifier(),
1200 PI->getType());
1201 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001202 lastPos = pos + 1;
1203 }
Mike Stump11289f42009-09-09 15:08:12 +00001204
Douglas Gregor278f52e2009-05-30 00:08:05 +00001205 if (OMD->param_begin() == OMD->param_end())
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001206 Out << name;
Mike Stump11289f42009-09-09 15:08:12 +00001207
Douglas Gregor278f52e2009-05-30 00:08:05 +00001208 if (OMD->isVariadic())
1209 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001210
1211 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001212
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001213 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001214 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001215 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001216 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001217 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001218 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001219}
1220
1221void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1222 std::string I = OID->getNameAsString();
1223 ObjCInterfaceDecl *SID = OID->getSuperClass();
1224
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001225 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001226 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001227 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001228 else
1229 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001230
1231 if (OID->ivar_size() > 0) {
1232 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001233 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001234 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001235 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001236 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001237 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001238 }
1239 Indentation -= Policy.Indentation;
1240 Out << "}\n";
1241 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001242 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1243 Out << "\n";
1244 eolnOut = true;
1245 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001246 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001247 if (!eolnOut)
1248 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001249 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001250}
1251
1252void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1253 std::string I = OID->getNameAsString();
1254 ObjCInterfaceDecl *SID = OID->getSuperClass();
1255
Douglas Gregordc9166c2011-12-15 20:29:51 +00001256 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001257 Out << "@class " << I;
1258
1259 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1260 PrintObjCTypeParams(TypeParams);
1261 }
1262
1263 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001264 return;
1265 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001266 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001267 Out << "@interface " << I;
1268
1269 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1270 PrintObjCTypeParams(TypeParams);
1271 }
1272
Douglas Gregor278f52e2009-05-30 00:08:05 +00001273 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001274 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001275
Douglas Gregor278f52e2009-05-30 00:08:05 +00001276 // Protocols?
1277 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1278 if (!Protocols.empty()) {
1279 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1280 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001281 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001282 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001283 }
Mike Stump11289f42009-09-09 15:08:12 +00001284
Douglas Gregor278f52e2009-05-30 00:08:05 +00001285 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001286 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001287 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001288 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001289 for (const auto *I : OID->ivars()) {
1290 Indent() << I->getASTContext()
1291 .getUnqualifiedObjCPointerType(I->getType())
1292 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001293 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001294 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001295 Out << "}\n";
1296 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001297 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001298 Out << "\n";
1299 eolnOut = true;
1300 }
Mike Stump11289f42009-09-09 15:08:12 +00001301
Douglas Gregor278f52e2009-05-30 00:08:05 +00001302 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001303 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001304 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001305 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001306 // FIXME: implement the rest...
1307}
1308
Douglas Gregor278f52e2009-05-30 00:08:05 +00001309void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001310 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001311 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001312 return;
1313 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001314 // Protocols?
1315 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1316 if (!Protocols.empty()) {
1317 Out << "@protocol " << *PID;
1318 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1319 E = Protocols.end(); I != E; ++I)
1320 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1321 Out << ">\n";
1322 } else
1323 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001324 VisitDeclContext(PID, false);
1325 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001326}
1327
1328void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001329 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001330
1331 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001332 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001333 // FIXME: implement the rest...
1334}
1335
1336void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001337 Out << "@interface " << *PID->getClassInterface();
1338 if (auto TypeParams = PID->getTypeParamList()) {
1339 PrintObjCTypeParams(TypeParams);
1340 }
1341 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001342 if (PID->ivar_size() > 0) {
1343 Out << "{\n";
1344 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001345 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001346 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001347 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001348 Indentation -= Policy.Indentation;
1349 Out << "}\n";
1350 }
1351
Douglas Gregor278f52e2009-05-30 00:08:05 +00001352 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001353 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001354
Douglas Gregor278f52e2009-05-30 00:08:05 +00001355 // FIXME: implement the rest...
1356}
1357
1358void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001359 Out << "@compatibility_alias " << *AID
1360 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001361}
1362
1363/// PrintObjCPropertyDecl - print a property declaration.
1364///
1365void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1366 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1367 Out << "@required\n";
1368 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1369 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001370
Douglas Gregor813a0662015-06-19 18:14:38 +00001371 QualType T = PDecl->getType();
1372
Douglas Gregor278f52e2009-05-30 00:08:05 +00001373 Out << "@property";
1374 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1375 bool first = true;
1376 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001377 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001378 ObjCPropertyDecl::OBJC_PR_readonly) {
1379 Out << (first ? ' ' : ',') << "readonly";
1380 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001381 }
Mike Stump11289f42009-09-09 15:08:12 +00001382
Ted Kremenek897af912011-08-17 21:09:35 +00001383 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001384 Out << (first ? ' ' : ',') << "getter = ";
1385 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001386 first = false;
1387 }
1388 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001389 Out << (first ? ' ' : ',') << "setter = ";
1390 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001391 first = false;
1392 }
Mike Stump11289f42009-09-09 15:08:12 +00001393
Ted Kremenek897af912011-08-17 21:09:35 +00001394 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1395 Out << (first ? ' ' : ',') << "assign";
1396 first = false;
1397 }
Mike Stump11289f42009-09-09 15:08:12 +00001398
Ted Kremenek897af912011-08-17 21:09:35 +00001399 if (PDecl->getPropertyAttributes() &
1400 ObjCPropertyDecl::OBJC_PR_readwrite) {
1401 Out << (first ? ' ' : ',') << "readwrite";
1402 first = false;
1403 }
Mike Stump11289f42009-09-09 15:08:12 +00001404
Ted Kremenek897af912011-08-17 21:09:35 +00001405 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1406 Out << (first ? ' ' : ',') << "retain";
1407 first = false;
1408 }
Mike Stump11289f42009-09-09 15:08:12 +00001409
Ted Kremenek897af912011-08-17 21:09:35 +00001410 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1411 Out << (first ? ' ' : ',') << "strong";
1412 first = false;
1413 }
John McCall31168b02011-06-15 23:02:42 +00001414
Ted Kremenek897af912011-08-17 21:09:35 +00001415 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1416 Out << (first ? ' ' : ',') << "copy";
1417 first = false;
1418 }
Mike Stump11289f42009-09-09 15:08:12 +00001419
Ted Kremenek897af912011-08-17 21:09:35 +00001420 if (PDecl->getPropertyAttributes() &
1421 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1422 Out << (first ? ' ' : ',') << "nonatomic";
1423 first = false;
1424 }
1425 if (PDecl->getPropertyAttributes() &
1426 ObjCPropertyDecl::OBJC_PR_atomic) {
1427 Out << (first ? ' ' : ',') << "atomic";
1428 first = false;
1429 }
1430
Douglas Gregor813a0662015-06-19 18:14:38 +00001431 if (PDecl->getPropertyAttributes() &
1432 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001433 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001434 if (*nullability == NullabilityKind::Unspecified &&
1435 (PDecl->getPropertyAttributes() &
1436 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1437 Out << (first ? ' ' : ',') << "null_resettable";
1438 } else {
1439 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001440 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001441 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001442 first = false;
1443 }
1444 }
1445
Manman Ren387ff7f2016-01-26 18:52:43 +00001446 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1447 Out << (first ? ' ' : ',') << "class";
1448 first = false;
1449 }
1450
Ted Kremenek897af912011-08-17 21:09:35 +00001451 (void) first; // Silence dead store warning due to idiomatic code.
1452 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001453 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001454 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001455 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001456 if (Policy.PolishForDeclaration)
1457 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001458}
1459
1460void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1461 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001462 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001463 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001464 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001465 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001466 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001467 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001468}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001469
1470void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001471 if (!D->isAccessDeclaration())
1472 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001473 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001474 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001475 D->getQualifier()->print(Out, Policy);
Alex Lorenzea9b59a2016-10-03 12:22:17 +00001476
1477 // Use the correct record name when the using declaration is used for
1478 // inheriting constructors.
1479 for (const auto *Shadow : D->shadows()) {
1480 if (const auto *ConstructorShadow =
1481 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1482 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1483 Out << *ConstructorShadow->getNominatedBaseClass();
1484 return;
1485 }
1486 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001487 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001488}
1489
John McCalle61f2ba2009-11-18 02:36:19 +00001490void
1491DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1492 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001493 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001494 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001495}
1496
1497void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001498 if (!D->isAccessDeclaration())
1499 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001500 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001501 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001502}
John McCall3f746822009-11-17 05:59:44 +00001503
1504void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1505 // ignore
1506}
Alexey Bataeva769e072013-03-22 06:34:35 +00001507
1508void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1509 Out << "#pragma omp threadprivate";
1510 if (!D->varlist_empty()) {
1511 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1512 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001513 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001514 Out << (I == D->varlist_begin() ? '(' : ',');
1515 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1516 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001517 }
1518 Out << ")";
1519 }
1520}
1521
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001522void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1523 if (!D->isInvalidDecl()) {
1524 Out << "#pragma omp declare reduction (";
1525 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1526 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1527 nullptr,
1528#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1529 Spelling,
1530#include "clang/Basic/OperatorKinds.def"
1531 };
1532 const char *OpName =
1533 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1534 assert(OpName && "not an overloaded operator");
1535 Out << OpName;
1536 } else {
1537 assert(D->getDeclName().isIdentifier());
1538 D->printName(Out);
1539 }
1540 Out << " : ";
1541 D->getType().print(Out, Policy);
1542 Out << " : ";
1543 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1544 Out << ")";
1545 if (auto *Init = D->getInitializer()) {
1546 Out << " initializer(";
1547 Init->printPretty(Out, nullptr, Policy, 0);
1548 Out << ")";
1549 }
1550 }
1551}
1552
Alexey Bataev4244be22016-02-11 05:35:55 +00001553void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001554 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1555}
1556