blob: 6eeba88e403366474e82a7f4780486cd963fc1f9 [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;
31 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);
Mike Stump11289f42009-09-09 15:08:12 +000039
Douglas Gregor813a0662015-06-19 18:14:38 +000040 /// Print an Objective-C method type in parentheses.
41 ///
42 /// \param Quals The Objective-C declaration qualifiers.
43 /// \param T The type to print.
44 void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
45 QualType T);
46
Douglas Gregor85f3f952015-07-07 03:57:15 +000047 void PrintObjCTypeParams(ObjCTypeParamList *Params);
48
Douglas Gregor278f52e2009-05-30 00:08:05 +000049 public:
Richard Smith235341b2012-08-16 03:56:14 +000050 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
51 unsigned Indentation = 0, bool PrintInstantiation = false)
52 : Out(Out), Policy(Policy), Indentation(Indentation),
Richard Trieu82398f92011-07-28 00:19:05 +000053 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor278f52e2009-05-30 00:08:05 +000054
55 void VisitDeclContext(DeclContext *DC, bool Indent = true);
56
57 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
58 void VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +000059 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000060 void VisitEnumDecl(EnumDecl *D);
61 void VisitRecordDecl(RecordDecl *D);
62 void VisitEnumConstantDecl(EnumConstantDecl *D);
Michael Han84324352013-02-22 17:15:32 +000063 void VisitEmptyDecl(EmptyDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000064 void VisitFunctionDecl(FunctionDecl *D);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +000065 void VisitFriendDecl(FriendDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000066 void VisitFieldDecl(FieldDecl *D);
67 void VisitVarDecl(VarDecl *D);
Chris Lattnercab02a62011-02-17 20:34:02 +000068 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000069 void VisitParmVarDecl(ParmVarDecl *D);
70 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregorba345522011-12-02 23:23:56 +000071 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +000072 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000073 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000074 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000075 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000076 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000077 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith030f4992011-04-15 13:38:57 +000078 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000079 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
80 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Serge Pavlova67a4d22016-11-10 08:49:37 +000081 void VisitClassTemplateSpecializationDecl(
82 ClassTemplateSpecializationDecl *D);
83 void VisitClassTemplatePartialSpecializationDecl(
84 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000085 void VisitObjCMethodDecl(ObjCMethodDecl *D);
86 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
87 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000088 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
89 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
90 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
91 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
92 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
93 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000094 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
95 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +000096 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +000097 void VisitUsingShadowDecl(UsingShadowDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +000098 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000099 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000100 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +0000101
Serge Pavlova67a4d22016-11-10 08:49:37 +0000102 void printTemplateParameters(const TemplateParameterList *Params);
103 void printTemplateArguments(const TemplateArgumentList &Args,
104 const TemplateParameterList *Params = nullptr);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000105 void prettyPrintAttributes(Decl *D);
Alexey Bataev6d455322015-10-12 06:59:48 +0000106 void prettyPrintPragmas(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +0000107 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000108 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000109}
Douglas Gregor278f52e2009-05-30 00:08:05 +0000110
Richard Trieu82398f92011-07-28 00:19:05 +0000111void Decl::print(raw_ostream &Out, unsigned Indentation,
112 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +0000113 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000114}
115
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000116void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000117 unsigned Indentation, bool PrintInstantiation) const {
Richard Smith235341b2012-08-16 03:56:14 +0000118 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000119 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000120}
121
Eli Friedman79635842009-05-30 04:20:30 +0000122static QualType GetBaseType(QualType T) {
123 // FIXME: This should be on the Type class!
124 QualType BaseType = T;
125 while (!BaseType->isSpecifierType()) {
126 if (isa<TypedefType>(BaseType))
127 break;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000128 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000129 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000130 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
131 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000132 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
133 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000134 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000135 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000136 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000137 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000138 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
139 BaseType = RTy->getPointeeType();
Vassil Vassilevcdaa31f2016-07-08 16:04:22 +0000140 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
141 BaseType = ATy->getDeducedType();
Eli Friedman79635842009-05-30 04:20:30 +0000142 else
David Blaikie83d382b2011-09-23 05:06:16 +0000143 llvm_unreachable("Unknown declarator!");
Eli Friedman79635842009-05-30 04:20:30 +0000144 }
145 return BaseType;
146}
147
148static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000149 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000150 return TDD->getUnderlyingType();
151 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
152 return VD->getType();
153 return QualType();
154}
155
156void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000157 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000158 unsigned Indentation) {
159 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000160 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000161 return;
162 }
163
164 Decl** End = Begin + NumDecls;
165 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
166 if (TD)
167 ++Begin;
168
169 PrintingPolicy SubPolicy(Policy);
Eli Friedman79635842009-05-30 04:20:30 +0000170
171 bool isFirst = true;
172 for ( ; Begin != End; ++Begin) {
173 if (isFirst) {
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000174 if(TD)
175 SubPolicy.IncludeTagDefinition = true;
Eli Friedman79635842009-05-30 04:20:30 +0000176 SubPolicy.SuppressSpecifiers = false;
177 isFirst = false;
178 } else {
179 if (!isFirst) Out << ", ";
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000180 SubPolicy.IncludeTagDefinition = false;
Eli Friedman79635842009-05-30 04:20:30 +0000181 SubPolicy.SuppressSpecifiers = true;
182 }
183
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000184 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000185 }
186}
187
Alp Tokeref6b0072014-01-04 13:47:14 +0000188LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000189 // Get the translation unit
190 const DeclContext *DC = this;
191 while (!DC->isTranslationUnit())
192 DC = DC->getParent();
193
194 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith235341b2012-08-16 03:56:14 +0000195 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000196 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
197}
198
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000199raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000200 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000201 Out << " ";
202 return Out;
203}
204
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000205void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000206 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000207 return;
Alexey Bataev6d455322015-10-12 06:59:48 +0000208
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000209 if (D->hasAttrs()) {
210 AttrVec &Attrs = D->getAttrs();
Alexey Bataev6d455322015-10-12 06:59:48 +0000211 for (auto *A : Attrs) {
212 switch (A->getKind()) {
213#define ATTR(X)
214#define PRAGMA_SPELLING_ATTR(X) case attr::X:
215#include "clang/Basic/AttrList.inc"
216 break;
217 default:
218 A->printPretty(Out, Policy);
219 break;
220 }
221 }
222 }
223}
224
225void DeclPrinter::prettyPrintPragmas(Decl *D) {
226 if (Policy.PolishForDeclaration)
227 return;
228
229 if (D->hasAttrs()) {
230 AttrVec &Attrs = D->getAttrs();
231 for (auto *A : Attrs) {
232 switch (A->getKind()) {
233#define ATTR(X)
234#define PRAGMA_SPELLING_ATTR(X) case attr::X:
235#include "clang/Basic/AttrList.inc"
236 A->printPretty(Out, Policy);
237 Indent();
238 break;
239 default:
240 break;
241 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000242 }
243 }
244}
245
Richard Smitha4bb2922014-07-23 03:17:06 +0000246void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
247 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
248 // template argument), but if it is the type of a declaration, the ellipsis
249 // is placed before the name being declared.
250 if (auto *PET = T->getAs<PackExpansionType>()) {
251 Pack = true;
252 T = PET->getPattern();
253 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000254 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
Richard Smitha4bb2922014-07-23 03:17:06 +0000255}
256
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000257void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000258 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000259 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000260 Out << ";\n";
261 Decls.clear();
262
263}
264
Anders Carlsson601d6e42009-08-28 22:39:52 +0000265void DeclPrinter::Print(AccessSpecifier AS) {
266 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000267 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000268 case AS_public: Out << "public"; break;
269 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000270 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000271 }
272}
273
Douglas Gregor278f52e2009-05-30 00:08:05 +0000274//----------------------------------------------------------------------------
275// Common C declarations
276//----------------------------------------------------------------------------
277
278void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000279 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000280 return;
281
Douglas Gregor278f52e2009-05-30 00:08:05 +0000282 if (Indent)
283 Indentation += Policy.Indentation;
284
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000285 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000286 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000287 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000288
289 // Don't print ObjCIvarDecls, as they are printed when visiting the
290 // containing ObjCInterfaceDecl.
291 if (isa<ObjCIvarDecl>(*D))
292 continue;
293
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000294 // Skip over implicit declarations in pretty-printing mode.
295 if (D->isImplicit())
296 continue;
297
Serge Pavlova67a4d22016-11-10 08:49:37 +0000298 // Don't print implicit specializations, as they are printed when visiting
299 // corresponding templates.
300 if (auto FD = dyn_cast<FunctionDecl>(*D))
301 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
302 !isa<ClassTemplateSpecializationDecl>(DC))
303 continue;
304
Eli Friedman79635842009-05-30 04:20:30 +0000305 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
306 // forced to merge the declarations because there's no other way to
307 // refer to the struct in question. This limited merging is safe without
308 // a bunch of other checks because it only merges declarations directly
309 // referring to the tag, not typedefs.
310 //
311 // Check whether the current declaration should be grouped with a previous
312 // unnamed struct.
313 QualType CurDeclType = getDeclType(*D);
314 if (!Decls.empty() && !CurDeclType.isNull()) {
315 QualType BaseType = GetBaseType(CurDeclType);
Eli Friedman24b3fed2013-08-12 21:54:04 +0000316 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
317 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
Eli Friedman79635842009-05-30 04:20:30 +0000318 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
319 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
320 Decls.push_back(*D);
321 continue;
322 }
323 }
324
325 // If we have a merged group waiting to be handled, handle it now.
326 if (!Decls.empty())
327 ProcessDeclGroup(Decls);
328
329 // If the current declaration is an unnamed tag type, save it
330 // so we can merge it with the subsequent declaration(s) using it.
331 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
332 Decls.push_back(*D);
333 continue;
334 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000335
336 if (isa<AccessSpecDecl>(*D)) {
337 Indentation -= Policy.Indentation;
338 this->Indent();
339 Print(D->getAccess());
340 Out << ":\n";
341 Indentation += Policy.Indentation;
342 continue;
343 }
344
Douglas Gregor278f52e2009-05-30 00:08:05 +0000345 this->Indent();
346 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000347
348 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000349 const char *Terminator = nullptr;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000350 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000351 Terminator = nullptr;
Serge Pavlovdc586c42016-10-31 05:11:12 +0000352 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000353 Terminator = nullptr;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000354 else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
355 if (FD->isThisDeclarationADefinition())
356 Terminator = nullptr;
357 else
358 Terminator = ";";
359 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
360 if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
361 Terminator = nullptr;
362 else
363 Terminator = ";";
364 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000365 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000366 isa<ObjCInterfaceDecl>(*D) ||
367 isa<ObjCProtocolDecl>(*D) ||
368 isa<ObjCCategoryImplDecl>(*D) ||
369 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000370 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000371 else if (isa<EnumConstantDecl>(*D)) {
372 DeclContext::decl_iterator Next = D;
373 ++Next;
374 if (Next != DEnd)
375 Terminator = ",";
376 } else
377 Terminator = ";";
378
379 if (Terminator)
380 Out << Terminator;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000381 if (!Policy.TerseOutput &&
382 ((isa<FunctionDecl>(*D) &&
383 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
384 (isa<FunctionTemplateDecl>(*D) &&
385 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
386 ; // StmtPrinter already added '\n' after CompoundStmt.
387 else
388 Out << "\n";
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000389
390 // Declare target attribute is special one, natural spelling for the pragma
391 // assumes "ending" construct so print it here.
392 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
393 Out << "#pragma omp end declare target\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000394 }
395
Eli Friedman79635842009-05-30 04:20:30 +0000396 if (!Decls.empty())
397 ProcessDeclGroup(Decls);
398
Douglas Gregor278f52e2009-05-30 00:08:05 +0000399 if (Indent)
400 Indentation -= Policy.Indentation;
401}
402
403void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
404 VisitDeclContext(D, false);
405}
406
407void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000408 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000409 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000410
411 if (D->isModulePrivate())
412 Out << "__module_private__ ";
413 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000414 QualType Ty = D->getTypeSourceInfo()->getType();
415 Ty.print(Out, Policy, D->getName(), Indentation);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000416 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000417}
418
Richard Smithdda56e42011-04-15 14:24:37 +0000419void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000420 Out << "using " << *D;
421 prettyPrintAttributes(D);
422 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000423}
424
Douglas Gregor278f52e2009-05-30 00:08:05 +0000425void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000426 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
427 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000428 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000429 if (D->isScoped()) {
430 if (D->isScopedUsingClassTag())
431 Out << "class ";
432 else
433 Out << "struct ";
434 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000435 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000436
Serge Pavlov08c8de22016-11-04 06:03:34 +0000437 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000438 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000439
John McCallf937c022011-10-07 06:10:15 +0000440 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000441 Out << " {\n";
442 VisitDeclContext(D);
443 Indent() << "}";
444 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000445 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000446}
447
448void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000449 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
450 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000451 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000452
453 prettyPrintAttributes(D);
454
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000455 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000456 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000457
John McCallf937c022011-10-07 06:10:15 +0000458 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000459 Out << " {\n";
460 VisitDeclContext(D);
461 Indent() << "}";
462 }
463}
464
465void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000466 Out << *D;
Jordan Roseccca6692017-01-20 03:33:42 +0000467 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000468 if (Expr *Init = D->getInitExpr()) {
469 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000470 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000471 }
472}
473
Mike Stump11289f42009-09-09 15:08:12 +0000474void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000475 if (!D->getDescribedFunctionTemplate() &&
476 !D->isFunctionTemplateSpecialization())
477 prettyPrintPragmas(D);
478
Serge Pavlova67a4d22016-11-10 08:49:37 +0000479 if (D->isFunctionTemplateSpecialization())
480 Out << "template<> ";
Alex Lorenz47fd10c2017-04-18 15:12:34 +0000481 else if (!D->getDescribedFunctionTemplate()) {
482 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
483 I < NumTemplateParams; ++I)
484 printTemplateParameters(D->getTemplateParameterList(I));
485 }
Serge Pavlova67a4d22016-11-10 08:49:37 +0000486
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000487 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000488 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Richard Smith057ec502017-02-18 01:01:48 +0000489 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000490 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000491 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000492 case SC_None: break;
493 case SC_Extern: Out << "extern "; break;
494 case SC_Static: Out << "static "; break;
495 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000496 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000497 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000498 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000499
Douglas Gregor26701a42011-09-09 02:06:17 +0000500 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000501 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000502 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000503 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000504 if ((CDecl && CDecl->isExplicitSpecified()) ||
Richard Smith057ec502017-02-18 01:01:48 +0000505 (ConversionDecl && ConversionDecl->isExplicitSpecified()) ||
506 (GuideDecl && GuideDecl->isExplicitSpecified()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000507 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000508 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000509
Douglas Gregor2d042f12009-05-30 05:39:39 +0000510 PrintingPolicy SubPolicy(Policy);
511 SubPolicy.SuppressSpecifiers = false;
Alex Lorenza981c7d2017-04-11 16:46:03 +0000512 std::string Proto;
513 if (!Policy.SuppressScope) {
514 if (const NestedNameSpecifier *NS = D->getQualifier()) {
515 llvm::raw_string_ostream OS(Proto);
516 NS->print(OS, Policy);
517 }
518 }
519 Proto += D->getNameInfo().getAsString();
Richard Smith057ec502017-02-18 01:01:48 +0000520 if (GuideDecl)
521 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
Serge Pavlova67a4d22016-11-10 08:49:37 +0000522 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
523 llvm::raw_string_ostream POut(Proto);
524 DeclPrinter TArgPrinter(POut, SubPolicy, Indentation);
525 TArgPrinter.printTemplateArguments(*TArgs);
526 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000527
Abramo Bagnara6d810632010-12-14 22:11:44 +0000528 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000529 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000530 Proto = '(' + Proto + ')';
531 Ty = PT->getInnerType();
532 }
533
Reid Kleckner0503a872013-12-05 01:23:43 +0000534 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000535 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000536 if (D->hasWrittenPrototype())
537 FT = dyn_cast<FunctionProtoType>(AFT);
538
539 Proto += "(";
540 if (FT) {
541 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000542 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000543 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
544 if (i) POut << ", ";
545 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
546 }
Mike Stump11289f42009-09-09 15:08:12 +0000547
Douglas Gregor278f52e2009-05-30 00:08:05 +0000548 if (FT->isVariadic()) {
549 if (D->getNumParams()) POut << ", ";
550 POut << "...";
551 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000552 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000553 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
554 if (i)
555 Proto += ", ";
556 Proto += D->getParamDecl(i)->getNameAsString();
557 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000558 }
559
560 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000561
David Blaikief5697e52012-08-10 00:55:35 +0000562 if (FT) {
563 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000564 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000565 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000566 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000567 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000568 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000569
570 switch (FT->getRefQualifier()) {
571 case RQ_None:
572 break;
573 case RQ_LValue:
574 Proto += " &";
575 break;
576 case RQ_RValue:
577 Proto += " &&";
578 break;
579 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000580 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000581
582 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000583 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000584 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000585 Proto += "...";
586 else
587 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
588 if (I)
589 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000590
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000591 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000592 }
593 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000594 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
595 Proto += " noexcept";
596 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
597 Proto += "(";
598 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000599 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000600 Indentation);
601 EOut.flush();
602 Proto += EOut.str();
603 Proto += ")";
604 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000605 }
606
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000607 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000608 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000609 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000610 if (BMInitializer->isInClassMemberInitializer())
611 continue;
612
613 if (!HasInitializerList) {
614 Proto += " : ";
615 Out << Proto;
616 Proto.clear();
617 HasInitializerList = true;
618 } else
619 Out << ", ";
620
621 if (BMInitializer->isAnyMemberInitializer()) {
622 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000623 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000624 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000625 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000626 }
627
628 Out << "(";
629 if (!BMInitializer->getInit()) {
630 // Nothing to print
631 } else {
632 Expr *Init = BMInitializer->getInit();
633 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
634 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000635
Richard Smith938f40b2011-06-11 17:19:42 +0000636 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000637
638 Expr *SimpleInit = nullptr;
639 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000640 unsigned NumArgs = 0;
641 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
642 Args = ParenList->getExprs();
643 NumArgs = ParenList->getNumExprs();
644 } else if (CXXConstructExpr *Construct
645 = dyn_cast<CXXConstructExpr>(Init)) {
646 Args = Construct->getArgs();
647 NumArgs = Construct->getNumArgs();
648 } else
649 SimpleInit = Init;
650
651 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000652 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000653 else {
654 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000655 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000656 if (isa<CXXDefaultArgExpr>(Args[I]))
657 break;
658
659 if (I)
660 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000661 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000662 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000663 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000664 }
Richard Smith938f40b2011-06-11 17:19:42 +0000665 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000666 if (BMInitializer->isPackExpansion())
667 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000668 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000669 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000670 if (FT && FT->hasTrailingReturn()) {
Richard Smith057ec502017-02-18 01:01:48 +0000671 if (!GuideDecl)
672 Out << "auto ";
673 Out << Proto << " -> ";
Richard Smithe6560762013-02-22 05:54:51 +0000674 Proto.clear();
675 }
Alp Toker314cc812014-01-25 16:55:45 +0000676 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000677 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000678 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000679 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000680 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000681 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000682 }
683
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000684 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000685
686 if (D->isPure())
687 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000688 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000689 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000690 else if (D->isExplicitlyDefaulted())
691 Out << " = default";
Serge Pavlova67a4d22016-11-10 08:49:37 +0000692 else if (D->doesThisDeclarationHaveABody()) {
693 if (!Policy.TerseOutput) {
694 if (!D->hasPrototype() && D->getNumParams()) {
695 // This is a K&R function definition, so we need to print the
696 // parameters.
697 Out << '\n';
698 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
699 Indentation += Policy.Indentation;
700 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
701 Indent();
702 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
703 Out << ";\n";
704 }
705 Indentation -= Policy.Indentation;
706 } else
707 Out << ' ';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000708
Serge Pavlova67a4d22016-11-10 08:49:37 +0000709 if (D->getBody())
710 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
711 } else {
712 if (isa<CXXConstructorDecl>(*D))
713 Out << " {}";
714 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000715 }
716}
717
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000718void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
719 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000720 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
721 for (unsigned i = 0; i < NumTPLists; ++i)
Serge Pavlova67a4d22016-11-10 08:49:37 +0000722 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000723 Out << "friend ";
724 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000725 }
726 else if (FunctionDecl *FD =
727 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
728 Out << "friend ";
729 VisitFunctionDecl(FD);
730 }
731 else if (FunctionTemplateDecl *FTD =
732 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
733 Out << "friend ";
734 VisitFunctionTemplateDecl(FTD);
735 }
736 else if (ClassTemplateDecl *CTD =
737 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
738 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000739 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000740 }
741}
742
Douglas Gregor278f52e2009-05-30 00:08:05 +0000743void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000744 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000745 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000746 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000747 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
748 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000749
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000750 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000751 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000752
753 if (D->isBitField()) {
754 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000755 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000756 }
Richard Smith938f40b2011-06-11 17:19:42 +0000757
758 Expr *Init = D->getInClassInitializer();
759 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000760 if (D->getInClassInitStyle() == ICIS_ListInit)
761 Out << " ";
762 else
763 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000764 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000765 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000766 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000767}
768
Chris Lattnercab02a62011-02-17 20:34:02 +0000769void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000770 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000771}
772
Douglas Gregor278f52e2009-05-30 00:08:05 +0000773void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000774 prettyPrintPragmas(D);
Vassil Vassilev10023732016-07-08 21:09:08 +0000775
776 QualType T = D->getTypeSourceInfo()
777 ? D->getTypeSourceInfo()->getType()
778 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
779
Richard Smithfd3834f2013-04-13 02:43:54 +0000780 if (!Policy.SuppressSpecifiers) {
781 StorageClass SC = D->getStorageClass();
782 if (SC != SC_None)
783 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000784
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000785 switch (D->getTSCSpec()) {
786 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000787 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000788 case TSCS___thread:
789 Out << "__thread ";
790 break;
791 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000792 Out << "_Thread_local ";
793 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000794 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000795 Out << "thread_local ";
796 break;
797 }
798
799 if (D->isModulePrivate())
800 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000801
802 if (D->isConstexpr()) {
803 Out << "constexpr ";
804 T.removeLocalConst();
805 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000806 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000807
Richard Smitha4bb2922014-07-23 03:17:06 +0000808 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000809 Expr *Init = D->getInit();
810 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000811 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000812 if (CXXConstructExpr *Construct =
813 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000814 if (D->getInitStyle() == VarDecl::CallInit &&
815 !Construct->isListInitialization()) {
816 ImplicitInit = Construct->getNumArgs() == 0 ||
817 Construct->getArg(0)->isDefaultArgument();
818 }
819 }
Sebastian Redla9351792012-02-11 23:51:47 +0000820 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000821 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000822 Out << "(";
823 else if (D->getInitStyle() == VarDecl::CInit) {
824 Out << " = ";
825 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000826 PrintingPolicy SubPolicy(Policy);
827 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000828 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000829 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000830 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000831 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000832 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000833 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000834 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000835}
836
837void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
838 VisitVarDecl(D);
839}
840
841void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
842 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000843 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000844 Out << ")";
845}
846
Douglas Gregorba345522011-12-02 23:23:56 +0000847void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000848 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000849 << ";\n";
850}
851
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000852void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
853 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000854 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000855 if (StringLiteral *SL = D->getMessage()) {
856 Out << ", ";
857 SL->printPretty(Out, nullptr, Policy, Indentation);
858 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000859 Out << ")";
860}
861
Douglas Gregor278f52e2009-05-30 00:08:05 +0000862//----------------------------------------------------------------------------
863// C++ declarations
864//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000865void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000866 if (D->isInline())
867 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000868 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000869 VisitDeclContext(D);
870 Indent() << "}";
871}
872
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000873void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
874 Out << "using namespace ";
875 if (D->getQualifier())
876 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000877 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000878}
879
Douglas Gregor18231932009-05-30 06:48:27 +0000880void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000881 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000882 if (D->getQualifier())
883 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000884 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000885}
886
Michael Han84324352013-02-22 17:15:32 +0000887void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
888 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000889}
890
Douglas Gregor5f478b72009-05-30 06:58:37 +0000891void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000892 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000893 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
894 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000895 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000896
897 prettyPrintAttributes(D);
898
Serge Pavlova67a4d22016-11-10 08:49:37 +0000899 if (D->getIdentifier()) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000900 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000901
Serge Pavlova67a4d22016-11-10 08:49:37 +0000902 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
903 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
904 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
905 printTemplateArguments(S->getTemplateArgs());
906 }
907
John McCallf937c022011-10-07 06:10:15 +0000908 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000909 // Print the base classes
910 if (D->getNumBases()) {
911 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000912 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
913 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000914 if (Base != D->bases_begin())
915 Out << ", ";
916
917 if (Base->isVirtual())
918 Out << "virtual ";
919
Anders Carlsson6df9e072009-08-29 20:36:12 +0000920 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000921 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000922 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000923 Out << " ";
924 }
925 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000926
927 if (Base->isPackExpansion())
928 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000929 }
930 }
931
932 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000933 // FIXME: Doesn't print access specifiers, e.g., "public:"
Serge Pavlova67a4d22016-11-10 08:49:37 +0000934 if (Policy.TerseOutput) {
935 Out << " {}";
936 } else {
937 Out << " {\n";
938 VisitDeclContext(D);
939 Indent() << "}";
940 }
Mike Stump11289f42009-09-09 15:08:12 +0000941 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000942}
943
944void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
945 const char *l;
946 if (D->getLanguage() == LinkageSpecDecl::lang_c)
947 l = "C";
948 else {
949 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
950 "unknown language in linkage specification");
951 l = "C++";
952 }
953
954 Out << "extern \"" << l << "\" ";
955 if (D->hasBraces()) {
956 Out << "{\n";
957 VisitDeclContext(D);
958 Indent() << "}";
959 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000960 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000961}
962
Serge Pavlova67a4d22016-11-10 08:49:37 +0000963void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params) {
Richard Trieu82398f92011-07-28 00:19:05 +0000964 assert(Params);
Richard Trieu82398f92011-07-28 00:19:05 +0000965
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000966 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000967
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000968 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
969 if (i != 0)
970 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000971
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000972 const Decl *Param = Params->getParam(i);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000973 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000974
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000975 if (TTP->wasDeclaredWithTypename())
976 Out << "typename ";
977 else
978 Out << "class ";
979
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000980 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000981 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000982
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000983 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000984
Serge Pavlova67a4d22016-11-10 08:49:37 +0000985 if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000986 Out << " = ";
987 Out << TTP->getDefaultArgument().getAsString(Policy);
988 };
Serge Pavlova67a4d22016-11-10 08:49:37 +0000989 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000990 StringRef Name;
991 if (IdentifierInfo *II = NTTP->getIdentifier())
992 Name = II->getName();
993 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000994
Serge Pavlova67a4d22016-11-10 08:49:37 +0000995 if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000996 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000997 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
998 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000999 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001000 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
Richard Smith030f4992011-04-15 13:38:57 +00001001 VisitTemplateDecl(TTPD);
1002 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001003 }
1004 }
Mike Stump11289f42009-09-09 15:08:12 +00001005
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001006 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +00001007}
1008
Serge Pavlova67a4d22016-11-10 08:49:37 +00001009void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1010 const TemplateParameterList *Params) {
1011 Out << "<";
1012 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1013 const TemplateArgument &A = Args[I];
1014 if (I)
1015 Out << ", ";
1016 if (Params) {
1017 if (A.getKind() == TemplateArgument::Type)
1018 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1019 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1020 Out << *P;
1021 continue;
1022 }
1023 if (A.getKind() == TemplateArgument::Template) {
1024 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1025 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1026 auto P = cast<TemplateTemplateParmDecl>(
1027 Params->getParam(TD->getIndex()));
1028 Out << *P;
1029 continue;
1030 }
1031 }
1032 if (A.getKind() == TemplateArgument::Expression) {
1033 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1034 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1035 auto P = cast<NonTypeTemplateParmDecl>(
1036 Params->getParam(N->getIndex()));
1037 Out << *P;
1038 continue;
1039 }
1040 }
1041 }
1042 A.print(Policy, Out);
1043 }
1044 Out << ">";
1045}
1046
Richard Trieu82398f92011-07-28 00:19:05 +00001047void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001048 printTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001049
Richard Smith030f4992011-04-15 13:38:57 +00001050 if (const TemplateTemplateParmDecl *TTP =
1051 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +00001052 Out << "class ";
1053 if (TTP->isParameterPack())
1054 Out << "...";
1055 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +00001056 } else {
1057 Visit(D->getTemplatedDecl());
1058 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001059}
1060
Richard Trieu82398f92011-07-28 00:19:05 +00001061void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +00001062 prettyPrintPragmas(D->getTemplatedDecl());
Alex Lorenz47fd10c2017-04-18 15:12:34 +00001063 // Print any leading template parameter lists.
1064 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1065 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1066 I < NumTemplateParams; ++I)
1067 printTemplateParameters(FD->getTemplateParameterList(I));
1068 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001069 VisitRedeclarableTemplateDecl(D);
1070
Richard Smith057ec502017-02-18 01:01:48 +00001071 // Never print "instantiations" for deduction guides (they don't really
1072 // have them).
1073 if (PrintInstantiation &&
1074 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001075 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1076 const FunctionDecl *Def;
1077 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1078 return;
1079 for (auto *I : D->specializations())
1080 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1081 if (!PrevDecl->isThisDeclarationADefinition())
1082 Out << ";\n";
1083 Indent();
1084 prettyPrintPragmas(I);
1085 Visit(I);
1086 }
1087 }
Richard Trieu82398f92011-07-28 00:19:05 +00001088}
1089
1090void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001091 VisitRedeclarableTemplateDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001092
Serge Pavlova67a4d22016-11-10 08:49:37 +00001093 if (PrintInstantiation) {
1094 for (auto *I : D->specializations())
1095 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1096 if (D->isThisDeclarationADefinition())
1097 Out << ";";
1098 Out << "\n";
1099 Visit(I);
1100 }
1101 }
1102}
1103
1104void DeclPrinter::VisitClassTemplateSpecializationDecl(
1105 ClassTemplateSpecializationDecl *D) {
1106 Out << "template<> ";
1107 VisitCXXRecordDecl(D);
1108}
1109
1110void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1111 ClassTemplatePartialSpecializationDecl *D) {
1112 printTemplateParameters(D->getTemplateParameters());
1113 VisitCXXRecordDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001114}
1115
Douglas Gregor278f52e2009-05-30 00:08:05 +00001116//----------------------------------------------------------------------------
1117// Objective-C declarations
1118//----------------------------------------------------------------------------
1119
Douglas Gregor813a0662015-06-19 18:14:38 +00001120void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1121 Decl::ObjCDeclQualifier Quals,
1122 QualType T) {
1123 Out << '(';
1124 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1125 Out << "in ";
1126 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1127 Out << "inout ";
1128 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1129 Out << "out ";
1130 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1131 Out << "bycopy ";
1132 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1133 Out << "byref ";
1134 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1135 Out << "oneway ";
1136 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001137 if (auto nullability = AttributedType::stripOuterNullability(T))
1138 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001139 }
1140
1141 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1142 Out << ')';
1143}
1144
Douglas Gregor85f3f952015-07-07 03:57:15 +00001145void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1146 Out << "<";
1147 unsigned First = true;
1148 for (auto *Param : *Params) {
1149 if (First) {
1150 First = false;
1151 } else {
1152 Out << ", ";
1153 }
1154
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001155 switch (Param->getVariance()) {
1156 case ObjCTypeParamVariance::Invariant:
1157 break;
1158
1159 case ObjCTypeParamVariance::Covariant:
1160 Out << "__covariant ";
1161 break;
1162
1163 case ObjCTypeParamVariance::Contravariant:
1164 Out << "__contravariant ";
1165 break;
1166 }
1167
Douglas Gregor85f3f952015-07-07 03:57:15 +00001168 Out << Param->getDeclName().getAsString();
1169
1170 if (Param->hasExplicitBound()) {
1171 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1172 }
1173 }
1174 Out << ">";
1175}
1176
Douglas Gregor278f52e2009-05-30 00:08:05 +00001177void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1178 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001179 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001180 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001181 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001182 if (!OMD->getReturnType().isNull()) {
1183 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1184 OMD->getReturnType());
1185 }
Mike Stump11289f42009-09-09 15:08:12 +00001186
Douglas Gregor278f52e2009-05-30 00:08:05 +00001187 std::string name = OMD->getSelector().getAsString();
1188 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001189 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001190 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001191 pos = name.find_first_of(':', lastPos);
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001192 if (lastPos != 0)
1193 Out << " ";
1194 Out << name.substr(lastPos, pos - lastPos) << ':';
Douglas Gregor813a0662015-06-19 18:14:38 +00001195 PrintObjCMethodType(OMD->getASTContext(),
1196 PI->getObjCDeclQualifier(),
1197 PI->getType());
1198 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001199 lastPos = pos + 1;
1200 }
Mike Stump11289f42009-09-09 15:08:12 +00001201
Douglas Gregor278f52e2009-05-30 00:08:05 +00001202 if (OMD->param_begin() == OMD->param_end())
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001203 Out << name;
Mike Stump11289f42009-09-09 15:08:12 +00001204
Douglas Gregor278f52e2009-05-30 00:08:05 +00001205 if (OMD->isVariadic())
1206 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001207
1208 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001209
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001210 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001211 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001212 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001213 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001214 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001215 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001216}
1217
1218void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1219 std::string I = OID->getNameAsString();
1220 ObjCInterfaceDecl *SID = OID->getSuperClass();
1221
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001222 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001223 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001224 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001225 else
1226 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001227
1228 if (OID->ivar_size() > 0) {
1229 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001230 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001231 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001232 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001233 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001234 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001235 }
1236 Indentation -= Policy.Indentation;
1237 Out << "}\n";
1238 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001239 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1240 Out << "\n";
1241 eolnOut = true;
1242 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001243 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001244 if (!eolnOut)
1245 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001246 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001247}
1248
1249void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1250 std::string I = OID->getNameAsString();
1251 ObjCInterfaceDecl *SID = OID->getSuperClass();
1252
Douglas Gregordc9166c2011-12-15 20:29:51 +00001253 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001254 Out << "@class " << I;
1255
1256 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1257 PrintObjCTypeParams(TypeParams);
1258 }
1259
1260 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001261 return;
1262 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001263 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001264 Out << "@interface " << I;
1265
1266 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1267 PrintObjCTypeParams(TypeParams);
1268 }
1269
Douglas Gregor278f52e2009-05-30 00:08:05 +00001270 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001271 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001272
Douglas Gregor278f52e2009-05-30 00:08:05 +00001273 // Protocols?
1274 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1275 if (!Protocols.empty()) {
1276 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1277 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001278 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001279 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001280 }
Mike Stump11289f42009-09-09 15:08:12 +00001281
Douglas Gregor278f52e2009-05-30 00:08:05 +00001282 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001283 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001284 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001285 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001286 for (const auto *I : OID->ivars()) {
1287 Indent() << I->getASTContext()
1288 .getUnqualifiedObjCPointerType(I->getType())
1289 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001290 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001291 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001292 Out << "}\n";
1293 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001294 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001295 Out << "\n";
1296 eolnOut = true;
1297 }
Mike Stump11289f42009-09-09 15:08:12 +00001298
Douglas Gregor278f52e2009-05-30 00:08:05 +00001299 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001300 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001301 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001302 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001303 // FIXME: implement the rest...
1304}
1305
Douglas Gregor278f52e2009-05-30 00:08:05 +00001306void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001307 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001308 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001309 return;
1310 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001311 // Protocols?
1312 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1313 if (!Protocols.empty()) {
1314 Out << "@protocol " << *PID;
1315 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1316 E = Protocols.end(); I != E; ++I)
1317 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1318 Out << ">\n";
1319 } else
1320 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001321 VisitDeclContext(PID, false);
1322 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001323}
1324
1325void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001326 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001327
1328 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001329 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001330 // FIXME: implement the rest...
1331}
1332
1333void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001334 Out << "@interface " << *PID->getClassInterface();
1335 if (auto TypeParams = PID->getTypeParamList()) {
1336 PrintObjCTypeParams(TypeParams);
1337 }
1338 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001339 if (PID->ivar_size() > 0) {
1340 Out << "{\n";
1341 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001342 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001343 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001344 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001345 Indentation -= Policy.Indentation;
1346 Out << "}\n";
1347 }
1348
Douglas Gregor278f52e2009-05-30 00:08:05 +00001349 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001350 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001351
Douglas Gregor278f52e2009-05-30 00:08:05 +00001352 // FIXME: implement the rest...
1353}
1354
1355void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001356 Out << "@compatibility_alias " << *AID
1357 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001358}
1359
1360/// PrintObjCPropertyDecl - print a property declaration.
1361///
1362void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1363 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1364 Out << "@required\n";
1365 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1366 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001367
Douglas Gregor813a0662015-06-19 18:14:38 +00001368 QualType T = PDecl->getType();
1369
Douglas Gregor278f52e2009-05-30 00:08:05 +00001370 Out << "@property";
1371 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1372 bool first = true;
1373 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001374 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001375 ObjCPropertyDecl::OBJC_PR_readonly) {
1376 Out << (first ? ' ' : ',') << "readonly";
1377 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001378 }
Mike Stump11289f42009-09-09 15:08:12 +00001379
Ted Kremenek897af912011-08-17 21:09:35 +00001380 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001381 Out << (first ? ' ' : ',') << "getter = ";
1382 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001383 first = false;
1384 }
1385 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001386 Out << (first ? ' ' : ',') << "setter = ";
1387 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001388 first = false;
1389 }
Mike Stump11289f42009-09-09 15:08:12 +00001390
Ted Kremenek897af912011-08-17 21:09:35 +00001391 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1392 Out << (first ? ' ' : ',') << "assign";
1393 first = false;
1394 }
Mike Stump11289f42009-09-09 15:08:12 +00001395
Ted Kremenek897af912011-08-17 21:09:35 +00001396 if (PDecl->getPropertyAttributes() &
1397 ObjCPropertyDecl::OBJC_PR_readwrite) {
1398 Out << (first ? ' ' : ',') << "readwrite";
1399 first = false;
1400 }
Mike Stump11289f42009-09-09 15:08:12 +00001401
Ted Kremenek897af912011-08-17 21:09:35 +00001402 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1403 Out << (first ? ' ' : ',') << "retain";
1404 first = false;
1405 }
Mike Stump11289f42009-09-09 15:08:12 +00001406
Ted Kremenek897af912011-08-17 21:09:35 +00001407 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1408 Out << (first ? ' ' : ',') << "strong";
1409 first = false;
1410 }
John McCall31168b02011-06-15 23:02:42 +00001411
Ted Kremenek897af912011-08-17 21:09:35 +00001412 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1413 Out << (first ? ' ' : ',') << "copy";
1414 first = false;
1415 }
Mike Stump11289f42009-09-09 15:08:12 +00001416
Ted Kremenek897af912011-08-17 21:09:35 +00001417 if (PDecl->getPropertyAttributes() &
1418 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1419 Out << (first ? ' ' : ',') << "nonatomic";
1420 first = false;
1421 }
1422 if (PDecl->getPropertyAttributes() &
1423 ObjCPropertyDecl::OBJC_PR_atomic) {
1424 Out << (first ? ' ' : ',') << "atomic";
1425 first = false;
1426 }
1427
Douglas Gregor813a0662015-06-19 18:14:38 +00001428 if (PDecl->getPropertyAttributes() &
1429 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001430 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001431 if (*nullability == NullabilityKind::Unspecified &&
1432 (PDecl->getPropertyAttributes() &
1433 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1434 Out << (first ? ' ' : ',') << "null_resettable";
1435 } else {
1436 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001437 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001438 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001439 first = false;
1440 }
1441 }
1442
Manman Ren387ff7f2016-01-26 18:52:43 +00001443 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1444 Out << (first ? ' ' : ',') << "class";
1445 first = false;
1446 }
1447
Ted Kremenek897af912011-08-17 21:09:35 +00001448 (void) first; // Silence dead store warning due to idiomatic code.
1449 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001450 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001451 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001452 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001453 if (Policy.PolishForDeclaration)
1454 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001455}
1456
1457void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1458 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001459 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001460 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001461 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001462 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001463 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001464 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001465}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001466
1467void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001468 if (!D->isAccessDeclaration())
1469 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001470 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001471 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001472 D->getQualifier()->print(Out, Policy);
Alex Lorenzea9b59a2016-10-03 12:22:17 +00001473
1474 // Use the correct record name when the using declaration is used for
1475 // inheriting constructors.
1476 for (const auto *Shadow : D->shadows()) {
1477 if (const auto *ConstructorShadow =
1478 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1479 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1480 Out << *ConstructorShadow->getNominatedBaseClass();
1481 return;
1482 }
1483 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001484 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001485}
1486
John McCalle61f2ba2009-11-18 02:36:19 +00001487void
1488DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1489 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001490 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001491 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001492}
1493
1494void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001495 if (!D->isAccessDeclaration())
1496 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001497 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001498 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001499}
John McCall3f746822009-11-17 05:59:44 +00001500
1501void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1502 // ignore
1503}
Alexey Bataeva769e072013-03-22 06:34:35 +00001504
1505void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1506 Out << "#pragma omp threadprivate";
1507 if (!D->varlist_empty()) {
1508 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1509 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001510 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001511 Out << (I == D->varlist_begin() ? '(' : ',');
1512 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1513 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001514 }
1515 Out << ")";
1516 }
1517}
1518
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001519void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1520 if (!D->isInvalidDecl()) {
1521 Out << "#pragma omp declare reduction (";
1522 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1523 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1524 nullptr,
1525#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1526 Spelling,
1527#include "clang/Basic/OperatorKinds.def"
1528 };
1529 const char *OpName =
1530 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1531 assert(OpName && "not an overloaded operator");
1532 Out << OpName;
1533 } else {
1534 assert(D->getDeclName().isIdentifier());
1535 D->printName(Out);
1536 }
1537 Out << " : ";
1538 D->getType().print(Out, Policy);
1539 Out << " : ";
1540 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1541 Out << ")";
1542 if (auto *Init = D->getInitializer()) {
1543 Out << " initializer(";
1544 Init->printPretty(Out, nullptr, Policy, 0);
1545 Out << ")";
1546 }
1547 }
1548}
1549
Alexey Bataev4244be22016-02-11 05:35:55 +00001550void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001551 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1552}
1553