blob: e82144b0ae987de82828e0897593b2f85754927a [file] [log] [blame]
Douglas Gregor278f52e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Alexander Kornienko90ff6072012-12-20 02:09:13 +000010// This file implements the Decl::print method, which pretty prints the
Douglas Gregor278f52e2009-05-30 00:08:05 +000011// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000020#include "clang/AST/Expr.h"
Douglas Gregor7ae2d772010-01-31 09:12:51 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000022#include "clang/AST/PrettyPrinter.h"
Douglas Gregorba345522011-12-02 23:23:56 +000023#include "clang/Basic/Module.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000024#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26
27namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000028 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000029 raw_ostream &Out;
Douglas Gregor278f52e2009-05-30 00:08:05 +000030 PrintingPolicy Policy;
Alex Lorenz36070ed2017-08-17 13:41:55 +000031 const ASTContext &Context;
Douglas Gregor278f52e2009-05-30 00:08:05 +000032 unsigned Indentation;
Richard Trieu82398f92011-07-28 00:19:05 +000033 bool PrintInstantiation;
Douglas Gregor278f52e2009-05-30 00:08:05 +000034
Chris Lattner0e62c1c2011-07-23 10:55:15 +000035 raw_ostream& Indent() { return Indent(Indentation); }
36 raw_ostream& Indent(unsigned Indentation);
37 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
Douglas Gregor278f52e2009-05-30 00:08:05 +000038
Anders Carlsson601d6e42009-08-28 22:39:52 +000039 void Print(AccessSpecifier AS);
Alex Lorenzdc616fa2017-11-16 01:31:27 +000040 void PrintConstructorInitializers(CXXConstructorDecl *CDecl,
41 std::string &Proto);
Mike Stump11289f42009-09-09 15:08:12 +000042
Douglas Gregor813a0662015-06-19 18:14:38 +000043 /// Print an Objective-C method type in parentheses.
44 ///
45 /// \param Quals The Objective-C declaration qualifiers.
46 /// \param T The type to print.
47 void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
48 QualType T);
49
Douglas Gregor85f3f952015-07-07 03:57:15 +000050 void PrintObjCTypeParams(ObjCTypeParamList *Params);
51
Douglas Gregor278f52e2009-05-30 00:08:05 +000052 public:
Richard Smith235341b2012-08-16 03:56:14 +000053 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
Alex Lorenz36070ed2017-08-17 13:41:55 +000054 const ASTContext &Context, unsigned Indentation = 0,
55 bool PrintInstantiation = false)
56 : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation),
57 PrintInstantiation(PrintInstantiation) {}
Douglas Gregor278f52e2009-05-30 00:08:05 +000058
59 void VisitDeclContext(DeclContext *DC, bool Indent = true);
60
61 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
62 void VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +000063 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000064 void VisitEnumDecl(EnumDecl *D);
65 void VisitRecordDecl(RecordDecl *D);
66 void VisitEnumConstantDecl(EnumConstantDecl *D);
Michael Han84324352013-02-22 17:15:32 +000067 void VisitEmptyDecl(EmptyDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000068 void VisitFunctionDecl(FunctionDecl *D);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +000069 void VisitFriendDecl(FriendDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000070 void VisitFieldDecl(FieldDecl *D);
71 void VisitVarDecl(VarDecl *D);
Chris Lattnercab02a62011-02-17 20:34:02 +000072 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000073 void VisitParmVarDecl(ParmVarDecl *D);
74 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregorba345522011-12-02 23:23:56 +000075 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +000076 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000077 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000078 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000079 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000080 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000081 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith030f4992011-04-15 13:38:57 +000082 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000083 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
84 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Serge Pavlova67a4d22016-11-10 08:49:37 +000085 void VisitClassTemplateSpecializationDecl(
86 ClassTemplateSpecializationDecl *D);
87 void VisitClassTemplatePartialSpecializationDecl(
88 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000089 void VisitObjCMethodDecl(ObjCMethodDecl *D);
90 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
91 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000092 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
93 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
94 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
95 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
96 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
97 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000098 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
99 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000100 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +0000101 void VisitUsingShadowDecl(UsingShadowDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000102 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000103 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000104 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +0000105
Serge Pavlova67a4d22016-11-10 08:49:37 +0000106 void printTemplateParameters(const TemplateParameterList *Params);
107 void printTemplateArguments(const TemplateArgumentList &Args,
108 const TemplateParameterList *Params = nullptr);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000109 void prettyPrintAttributes(Decl *D);
Alexey Bataev6d455322015-10-12 06:59:48 +0000110 void prettyPrintPragmas(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +0000111 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000112 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000113}
Douglas Gregor278f52e2009-05-30 00:08:05 +0000114
Richard Trieu82398f92011-07-28 00:19:05 +0000115void Decl::print(raw_ostream &Out, unsigned Indentation,
116 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +0000117 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000118}
119
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000120void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000121 unsigned Indentation, bool PrintInstantiation) const {
Alex Lorenz36070ed2017-08-17 13:41:55 +0000122 DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
123 PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000124 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000125}
126
Eli Friedman79635842009-05-30 04:20:30 +0000127static QualType GetBaseType(QualType T) {
128 // FIXME: This should be on the Type class!
129 QualType BaseType = T;
130 while (!BaseType->isSpecifierType()) {
Artem Belevich224879e2018-01-17 19:29:39 +0000131 if (const PointerType *PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000132 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000133 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
134 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000135 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
136 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000137 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000138 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000139 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000140 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000141 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
142 BaseType = RTy->getPointeeType();
Vassil Vassilevcdaa31f2016-07-08 16:04:22 +0000143 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
144 BaseType = ATy->getDeducedType();
Artem Belevich224879e2018-01-17 19:29:39 +0000145 else if (const ParenType *PTy = BaseType->getAs<ParenType>())
146 BaseType = PTy->desugar();
Eli Friedman79635842009-05-30 04:20:30 +0000147 else
Artem Belevich224879e2018-01-17 19:29:39 +0000148 // This must be a syntax error.
149 break;
Eli Friedman79635842009-05-30 04:20:30 +0000150 }
151 return BaseType;
152}
153
154static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000155 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000156 return TDD->getUnderlyingType();
157 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
158 return VD->getType();
159 return QualType();
160}
161
162void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000163 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000164 unsigned Indentation) {
165 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000166 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000167 return;
168 }
169
170 Decl** End = Begin + NumDecls;
171 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
172 if (TD)
173 ++Begin;
174
175 PrintingPolicy SubPolicy(Policy);
Eli Friedman79635842009-05-30 04:20:30 +0000176
177 bool isFirst = true;
178 for ( ; Begin != End; ++Begin) {
179 if (isFirst) {
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000180 if(TD)
181 SubPolicy.IncludeTagDefinition = true;
Eli Friedman79635842009-05-30 04:20:30 +0000182 SubPolicy.SuppressSpecifiers = false;
183 isFirst = false;
184 } else {
185 if (!isFirst) Out << ", ";
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000186 SubPolicy.IncludeTagDefinition = false;
Eli Friedman79635842009-05-30 04:20:30 +0000187 SubPolicy.SuppressSpecifiers = true;
188 }
189
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000190 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000191 }
192}
193
Alp Tokeref6b0072014-01-04 13:47:14 +0000194LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000195 // Get the translation unit
196 const DeclContext *DC = this;
197 while (!DC->isTranslationUnit())
198 DC = DC->getParent();
199
200 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Alex Lorenz36070ed2017-08-17 13:41:55 +0000201 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000202 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
203}
204
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000205raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000206 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000207 Out << " ";
208 return Out;
209}
210
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000211void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000212 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000213 return;
Alexey Bataev6d455322015-10-12 06:59:48 +0000214
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000215 if (D->hasAttrs()) {
216 AttrVec &Attrs = D->getAttrs();
Alexey Bataev6d455322015-10-12 06:59:48 +0000217 for (auto *A : Attrs) {
218 switch (A->getKind()) {
219#define ATTR(X)
220#define PRAGMA_SPELLING_ATTR(X) case attr::X:
221#include "clang/Basic/AttrList.inc"
222 break;
223 default:
224 A->printPretty(Out, Policy);
225 break;
226 }
227 }
228 }
229}
230
231void DeclPrinter::prettyPrintPragmas(Decl *D) {
232 if (Policy.PolishForDeclaration)
233 return;
234
235 if (D->hasAttrs()) {
236 AttrVec &Attrs = D->getAttrs();
237 for (auto *A : Attrs) {
238 switch (A->getKind()) {
239#define ATTR(X)
240#define PRAGMA_SPELLING_ATTR(X) case attr::X:
241#include "clang/Basic/AttrList.inc"
242 A->printPretty(Out, Policy);
243 Indent();
244 break;
245 default:
246 break;
247 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000248 }
249 }
250}
251
Richard Smitha4bb2922014-07-23 03:17:06 +0000252void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
253 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
254 // template argument), but if it is the type of a declaration, the ellipsis
255 // is placed before the name being declared.
256 if (auto *PET = T->getAs<PackExpansionType>()) {
257 Pack = true;
258 T = PET->getPattern();
259 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000260 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
Richard Smitha4bb2922014-07-23 03:17:06 +0000261}
262
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000263void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000264 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000265 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000266 Out << ";\n";
267 Decls.clear();
268
269}
270
Anders Carlsson601d6e42009-08-28 22:39:52 +0000271void DeclPrinter::Print(AccessSpecifier AS) {
272 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000273 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000274 case AS_public: Out << "public"; break;
275 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000276 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000277 }
278}
279
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000280void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
281 std::string &Proto) {
282 bool HasInitializerList = false;
283 for (const auto *BMInitializer : CDecl->inits()) {
284 if (BMInitializer->isInClassMemberInitializer())
285 continue;
286
287 if (!HasInitializerList) {
288 Proto += " : ";
289 Out << Proto;
290 Proto.clear();
291 HasInitializerList = true;
292 } else
293 Out << ", ";
294
295 if (BMInitializer->isAnyMemberInitializer()) {
296 FieldDecl *FD = BMInitializer->getAnyMember();
297 Out << *FD;
298 } else {
299 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
300 }
301
302 Out << "(";
303 if (!BMInitializer->getInit()) {
304 // Nothing to print
305 } else {
306 Expr *Init = BMInitializer->getInit();
307 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
308 Init = Tmp->getSubExpr();
309
310 Init = Init->IgnoreParens();
311
312 Expr *SimpleInit = nullptr;
313 Expr **Args = nullptr;
314 unsigned NumArgs = 0;
315 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
316 Args = ParenList->getExprs();
317 NumArgs = ParenList->getNumExprs();
318 } else if (CXXConstructExpr *Construct =
319 dyn_cast<CXXConstructExpr>(Init)) {
320 Args = Construct->getArgs();
321 NumArgs = Construct->getNumArgs();
322 } else
323 SimpleInit = Init;
324
325 if (SimpleInit)
326 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
327 else {
328 for (unsigned I = 0; I != NumArgs; ++I) {
329 assert(Args[I] != nullptr && "Expected non-null Expr");
330 if (isa<CXXDefaultArgExpr>(Args[I]))
331 break;
332
333 if (I)
334 Out << ", ";
335 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
336 }
337 }
338 }
339 Out << ")";
340 if (BMInitializer->isPackExpansion())
341 Out << "...";
342 }
343}
344
Douglas Gregor278f52e2009-05-30 00:08:05 +0000345//----------------------------------------------------------------------------
346// Common C declarations
347//----------------------------------------------------------------------------
348
349void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000350 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000351 return;
352
Douglas Gregor278f52e2009-05-30 00:08:05 +0000353 if (Indent)
354 Indentation += Policy.Indentation;
355
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000356 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000357 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000358 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000359
360 // Don't print ObjCIvarDecls, as they are printed when visiting the
361 // containing ObjCInterfaceDecl.
362 if (isa<ObjCIvarDecl>(*D))
363 continue;
364
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000365 // Skip over implicit declarations in pretty-printing mode.
366 if (D->isImplicit())
367 continue;
368
Serge Pavlova67a4d22016-11-10 08:49:37 +0000369 // Don't print implicit specializations, as they are printed when visiting
370 // corresponding templates.
371 if (auto FD = dyn_cast<FunctionDecl>(*D))
372 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
373 !isa<ClassTemplateSpecializationDecl>(DC))
374 continue;
375
Eli Friedman79635842009-05-30 04:20:30 +0000376 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
377 // forced to merge the declarations because there's no other way to
378 // refer to the struct in question. This limited merging is safe without
379 // a bunch of other checks because it only merges declarations directly
380 // referring to the tag, not typedefs.
381 //
382 // Check whether the current declaration should be grouped with a previous
383 // unnamed struct.
384 QualType CurDeclType = getDeclType(*D);
385 if (!Decls.empty() && !CurDeclType.isNull()) {
386 QualType BaseType = GetBaseType(CurDeclType);
Eli Friedman24b3fed2013-08-12 21:54:04 +0000387 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
388 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
Eli Friedman79635842009-05-30 04:20:30 +0000389 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
390 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
391 Decls.push_back(*D);
392 continue;
393 }
394 }
395
396 // If we have a merged group waiting to be handled, handle it now.
397 if (!Decls.empty())
398 ProcessDeclGroup(Decls);
399
400 // If the current declaration is an unnamed tag type, save it
401 // so we can merge it with the subsequent declaration(s) using it.
402 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
403 Decls.push_back(*D);
404 continue;
405 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000406
407 if (isa<AccessSpecDecl>(*D)) {
408 Indentation -= Policy.Indentation;
409 this->Indent();
410 Print(D->getAccess());
411 Out << ":\n";
412 Indentation += Policy.Indentation;
413 continue;
414 }
415
Douglas Gregor278f52e2009-05-30 00:08:05 +0000416 this->Indent();
417 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000418
419 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000420 const char *Terminator = nullptr;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000421 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000422 Terminator = nullptr;
Serge Pavlovdc586c42016-10-31 05:11:12 +0000423 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000424 Terminator = nullptr;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000425 else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
426 if (FD->isThisDeclarationADefinition())
427 Terminator = nullptr;
428 else
429 Terminator = ";";
430 } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
431 if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
432 Terminator = nullptr;
433 else
434 Terminator = ";";
435 } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000436 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000437 isa<ObjCInterfaceDecl>(*D) ||
438 isa<ObjCProtocolDecl>(*D) ||
439 isa<ObjCCategoryImplDecl>(*D) ||
440 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000441 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000442 else if (isa<EnumConstantDecl>(*D)) {
443 DeclContext::decl_iterator Next = D;
444 ++Next;
445 if (Next != DEnd)
446 Terminator = ",";
447 } else
448 Terminator = ";";
449
450 if (Terminator)
451 Out << Terminator;
Serge Pavlova67a4d22016-11-10 08:49:37 +0000452 if (!Policy.TerseOutput &&
453 ((isa<FunctionDecl>(*D) &&
454 cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
455 (isa<FunctionTemplateDecl>(*D) &&
456 cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
457 ; // StmtPrinter already added '\n' after CompoundStmt.
458 else
459 Out << "\n";
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000460
461 // Declare target attribute is special one, natural spelling for the pragma
462 // assumes "ending" construct so print it here.
463 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
464 Out << "#pragma omp end declare target\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000465 }
466
Eli Friedman79635842009-05-30 04:20:30 +0000467 if (!Decls.empty())
468 ProcessDeclGroup(Decls);
469
Douglas Gregor278f52e2009-05-30 00:08:05 +0000470 if (Indent)
471 Indentation -= Policy.Indentation;
472}
473
474void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
475 VisitDeclContext(D, false);
476}
477
478void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000479 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000480 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000481
482 if (D->isModulePrivate())
483 Out << "__module_private__ ";
484 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000485 QualType Ty = D->getTypeSourceInfo()->getType();
486 Ty.print(Out, Policy, D->getName(), Indentation);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000487 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000488}
489
Richard Smithdda56e42011-04-15 14:24:37 +0000490void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000491 Out << "using " << *D;
492 prettyPrintAttributes(D);
493 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000494}
495
Douglas Gregor278f52e2009-05-30 00:08:05 +0000496void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000497 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
498 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000499 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000500 if (D->isScoped()) {
501 if (D->isScopedUsingClassTag())
502 Out << "class ";
503 else
504 Out << "struct ";
505 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000506 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000507
Serge Pavlov08c8de22016-11-04 06:03:34 +0000508 if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000509 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000510
John McCallf937c022011-10-07 06:10:15 +0000511 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000512 Out << " {\n";
513 VisitDeclContext(D);
514 Indent() << "}";
515 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000516 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000517}
518
519void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000520 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
521 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000522 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000523
524 prettyPrintAttributes(D);
525
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000526 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000527 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000528
John McCallf937c022011-10-07 06:10:15 +0000529 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000530 Out << " {\n";
531 VisitDeclContext(D);
532 Indent() << "}";
533 }
534}
535
536void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000537 Out << *D;
Jordan Roseccca6692017-01-20 03:33:42 +0000538 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000539 if (Expr *Init = D->getInitExpr()) {
540 Out << " = ";
Alex Lorenz36070ed2017-08-17 13:41:55 +0000541 Init->printPretty(Out, nullptr, Policy, Indentation, &Context);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000542 }
543}
544
Mike Stump11289f42009-09-09 15:08:12 +0000545void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000546 if (!D->getDescribedFunctionTemplate() &&
547 !D->isFunctionTemplateSpecialization())
548 prettyPrintPragmas(D);
549
Serge Pavlova67a4d22016-11-10 08:49:37 +0000550 if (D->isFunctionTemplateSpecialization())
551 Out << "template<> ";
Alex Lorenz47fd10c2017-04-18 15:12:34 +0000552 else if (!D->getDescribedFunctionTemplate()) {
553 for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
554 I < NumTemplateParams; ++I)
555 printTemplateParameters(D->getTemplateParameterList(I));
556 }
Serge Pavlova67a4d22016-11-10 08:49:37 +0000557
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000558 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000559 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Richard Smith057ec502017-02-18 01:01:48 +0000560 CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000561 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000562 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000563 case SC_None: break;
564 case SC_Extern: Out << "extern "; break;
565 case SC_Static: Out << "static "; break;
566 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000567 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000568 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000569 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000570
Douglas Gregor26701a42011-09-09 02:06:17 +0000571 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000572 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000573 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000574 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000575 if ((CDecl && CDecl->isExplicitSpecified()) ||
Richard Smith057ec502017-02-18 01:01:48 +0000576 (ConversionDecl && ConversionDecl->isExplicitSpecified()) ||
577 (GuideDecl && GuideDecl->isExplicitSpecified()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000578 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000579 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000580
Douglas Gregor2d042f12009-05-30 05:39:39 +0000581 PrintingPolicy SubPolicy(Policy);
582 SubPolicy.SuppressSpecifiers = false;
Alex Lorenza981c7d2017-04-11 16:46:03 +0000583 std::string Proto;
Serge Pavlov842022a2017-11-23 05:38:20 +0000584
585 if (Policy.FullyQualifiedName) {
586 Proto += D->getQualifiedNameAsString();
587 } else {
588 if (!Policy.SuppressScope) {
589 if (const NestedNameSpecifier *NS = D->getQualifier()) {
590 llvm::raw_string_ostream OS(Proto);
591 NS->print(OS, Policy);
592 }
Alex Lorenza981c7d2017-04-11 16:46:03 +0000593 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000594 Proto += D->getNameInfo().getAsString();
Alex Lorenza981c7d2017-04-11 16:46:03 +0000595 }
Serge Pavlov842022a2017-11-23 05:38:20 +0000596
Richard Smith057ec502017-02-18 01:01:48 +0000597 if (GuideDecl)
598 Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
Serge Pavlova67a4d22016-11-10 08:49:37 +0000599 if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
600 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000601 DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000602 TArgPrinter.printTemplateArguments(*TArgs);
603 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000604
Abramo Bagnara6d810632010-12-14 22:11:44 +0000605 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000606 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000607 Proto = '(' + Proto + ')';
608 Ty = PT->getInnerType();
609 }
610
Reid Kleckner0503a872013-12-05 01:23:43 +0000611 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000612 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000613 if (D->hasWrittenPrototype())
614 FT = dyn_cast<FunctionProtoType>(AFT);
615
616 Proto += "(";
617 if (FT) {
618 llvm::raw_string_ostream POut(Proto);
Alex Lorenz36070ed2017-08-17 13:41:55 +0000619 DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000620 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
621 if (i) POut << ", ";
622 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
623 }
Mike Stump11289f42009-09-09 15:08:12 +0000624
Douglas Gregor278f52e2009-05-30 00:08:05 +0000625 if (FT->isVariadic()) {
626 if (D->getNumParams()) POut << ", ";
627 POut << "...";
628 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000629 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000630 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
631 if (i)
632 Proto += ", ";
633 Proto += D->getParamDecl(i)->getNameAsString();
634 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000635 }
636
637 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000638
David Blaikief5697e52012-08-10 00:55:35 +0000639 if (FT) {
640 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000641 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000642 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000643 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000644 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000645 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000646
647 switch (FT->getRefQualifier()) {
648 case RQ_None:
649 break;
650 case RQ_LValue:
651 Proto += " &";
652 break;
653 case RQ_RValue:
654 Proto += " &&";
655 break;
656 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000657 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000658
659 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000660 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000661 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000662 Proto += "...";
663 else
664 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
665 if (I)
666 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000667
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000668 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000669 }
670 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000671 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
672 Proto += " noexcept";
673 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
674 Proto += "(";
675 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000676 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000677 Indentation);
678 EOut.flush();
679 Proto += EOut.str();
680 Proto += ")";
681 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000682 }
683
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000684 if (CDecl) {
Alex Lorenzdc616fa2017-11-16 01:31:27 +0000685 if (!Policy.TerseOutput)
686 PrintConstructorInitializers(CDecl, Proto);
Benjamin Kramer2907b082014-02-25 18:49:49 +0000687 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000688 if (FT && FT->hasTrailingReturn()) {
Richard Smith057ec502017-02-18 01:01:48 +0000689 if (!GuideDecl)
690 Out << "auto ";
691 Out << Proto << " -> ";
Richard Smithe6560762013-02-22 05:54:51 +0000692 Proto.clear();
693 }
Alp Toker314cc812014-01-25 16:55:45 +0000694 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000695 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000696 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000697 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000698 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000699 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000700 }
701
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000702 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000703
704 if (D->isPure())
705 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000706 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000707 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000708 else if (D->isExplicitlyDefaulted())
709 Out << " = default";
Serge Pavlova67a4d22016-11-10 08:49:37 +0000710 else if (D->doesThisDeclarationHaveABody()) {
711 if (!Policy.TerseOutput) {
712 if (!D->hasPrototype() && D->getNumParams()) {
713 // This is a K&R function definition, so we need to print the
714 // parameters.
715 Out << '\n';
Alex Lorenz36070ed2017-08-17 13:41:55 +0000716 DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000717 Indentation += Policy.Indentation;
718 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
719 Indent();
720 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
721 Out << ";\n";
722 }
723 Indentation -= Policy.Indentation;
724 } else
725 Out << ' ';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000726
Serge Pavlova67a4d22016-11-10 08:49:37 +0000727 if (D->getBody())
728 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
729 } else {
Alex Lorenz35019db2017-11-16 01:28:25 +0000730 if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
Serge Pavlova67a4d22016-11-10 08:49:37 +0000731 Out << " {}";
732 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000733 }
734}
735
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000736void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
737 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000738 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
739 for (unsigned i = 0; i < NumTPLists; ++i)
Serge Pavlova67a4d22016-11-10 08:49:37 +0000740 printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000741 Out << "friend ";
742 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000743 }
744 else if (FunctionDecl *FD =
745 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
746 Out << "friend ";
747 VisitFunctionDecl(FD);
748 }
749 else if (FunctionTemplateDecl *FTD =
750 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
751 Out << "friend ";
752 VisitFunctionTemplateDecl(FTD);
753 }
754 else if (ClassTemplateDecl *CTD =
755 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
756 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000757 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000758 }
759}
760
Douglas Gregor278f52e2009-05-30 00:08:05 +0000761void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000762 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000763 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000764 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000765 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
766 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000767
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000768 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000769 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000770
771 if (D->isBitField()) {
772 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000773 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000774 }
Richard Smith938f40b2011-06-11 17:19:42 +0000775
776 Expr *Init = D->getInClassInitializer();
777 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000778 if (D->getInClassInitStyle() == ICIS_ListInit)
779 Out << " ";
780 else
781 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000782 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000783 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000784 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000785}
786
Chris Lattnercab02a62011-02-17 20:34:02 +0000787void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000788 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000789}
790
Douglas Gregor278f52e2009-05-30 00:08:05 +0000791void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000792 prettyPrintPragmas(D);
Vassil Vassilev10023732016-07-08 21:09:08 +0000793
794 QualType T = D->getTypeSourceInfo()
795 ? D->getTypeSourceInfo()->getType()
796 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
797
Richard Smithfd3834f2013-04-13 02:43:54 +0000798 if (!Policy.SuppressSpecifiers) {
799 StorageClass SC = D->getStorageClass();
800 if (SC != SC_None)
801 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000802
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000803 switch (D->getTSCSpec()) {
804 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000805 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000806 case TSCS___thread:
807 Out << "__thread ";
808 break;
809 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000810 Out << "_Thread_local ";
811 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000812 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000813 Out << "thread_local ";
814 break;
815 }
816
817 if (D->isModulePrivate())
818 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000819
820 if (D->isConstexpr()) {
821 Out << "constexpr ";
822 T.removeLocalConst();
823 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000824 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000825
Richard Smitha4bb2922014-07-23 03:17:06 +0000826 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000827 Expr *Init = D->getInit();
828 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000829 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000830 if (CXXConstructExpr *Construct =
831 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000832 if (D->getInitStyle() == VarDecl::CallInit &&
833 !Construct->isListInitialization()) {
834 ImplicitInit = Construct->getNumArgs() == 0 ||
835 Construct->getArg(0)->isDefaultArgument();
836 }
837 }
Sebastian Redla9351792012-02-11 23:51:47 +0000838 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000839 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000840 Out << "(";
841 else if (D->getInitStyle() == VarDecl::CInit) {
842 Out << " = ";
843 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000844 PrintingPolicy SubPolicy(Policy);
845 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000846 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000847 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000848 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000849 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000850 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000851 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000852 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000853}
854
855void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
856 VisitVarDecl(D);
857}
858
859void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
860 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000861 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000862 Out << ")";
863}
864
Douglas Gregorba345522011-12-02 23:23:56 +0000865void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000866 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000867 << ";\n";
868}
869
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000870void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
871 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000872 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000873 if (StringLiteral *SL = D->getMessage()) {
874 Out << ", ";
875 SL->printPretty(Out, nullptr, Policy, Indentation);
876 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000877 Out << ")";
878}
879
Douglas Gregor278f52e2009-05-30 00:08:05 +0000880//----------------------------------------------------------------------------
881// C++ declarations
882//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000883void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000884 if (D->isInline())
885 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000886 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000887 VisitDeclContext(D);
888 Indent() << "}";
889}
890
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000891void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
892 Out << "using namespace ";
893 if (D->getQualifier())
894 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000895 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000896}
897
Douglas Gregor18231932009-05-30 06:48:27 +0000898void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000899 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000900 if (D->getQualifier())
901 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000902 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000903}
904
Michael Han84324352013-02-22 17:15:32 +0000905void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
906 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000907}
908
Douglas Gregor5f478b72009-05-30 06:58:37 +0000909void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000910 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000911 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
912 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000913 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000914
915 prettyPrintAttributes(D);
916
Serge Pavlova67a4d22016-11-10 08:49:37 +0000917 if (D->getIdentifier()) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000918 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000919
Serge Pavlova67a4d22016-11-10 08:49:37 +0000920 if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
921 printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
922 else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
923 printTemplateArguments(S->getTemplateArgs());
924 }
925
John McCallf937c022011-10-07 06:10:15 +0000926 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000927 // Print the base classes
928 if (D->getNumBases()) {
929 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000930 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
931 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000932 if (Base != D->bases_begin())
933 Out << ", ";
934
935 if (Base->isVirtual())
936 Out << "virtual ";
937
Anders Carlsson6df9e072009-08-29 20:36:12 +0000938 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000939 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000940 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000941 Out << " ";
942 }
943 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000944
945 if (Base->isPackExpansion())
946 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000947 }
948 }
949
950 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000951 // FIXME: Doesn't print access specifiers, e.g., "public:"
Serge Pavlova67a4d22016-11-10 08:49:37 +0000952 if (Policy.TerseOutput) {
953 Out << " {}";
954 } else {
955 Out << " {\n";
956 VisitDeclContext(D);
957 Indent() << "}";
958 }
Mike Stump11289f42009-09-09 15:08:12 +0000959 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000960}
961
962void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
963 const char *l;
964 if (D->getLanguage() == LinkageSpecDecl::lang_c)
965 l = "C";
966 else {
967 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
968 "unknown language in linkage specification");
969 l = "C++";
970 }
971
972 Out << "extern \"" << l << "\" ";
973 if (D->hasBraces()) {
974 Out << "{\n";
975 VisitDeclContext(D);
976 Indent() << "}";
977 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000978 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000979}
980
Serge Pavlova67a4d22016-11-10 08:49:37 +0000981void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params) {
Richard Trieu82398f92011-07-28 00:19:05 +0000982 assert(Params);
Richard Trieu82398f92011-07-28 00:19:05 +0000983
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000984 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000985
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000986 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
987 if (i != 0)
988 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000989
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000990 const Decl *Param = Params->getParam(i);
Serge Pavlova67a4d22016-11-10 08:49:37 +0000991 if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000992
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000993 if (TTP->wasDeclaredWithTypename())
994 Out << "typename ";
995 else
996 Out << "class ";
997
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000998 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000999 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +00001000
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001001 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001002
Serge Pavlova67a4d22016-11-10 08:49:37 +00001003 if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001004 Out << " = ";
1005 Out << TTP->getDefaultArgument().getAsString(Policy);
1006 };
Serge Pavlova67a4d22016-11-10 08:49:37 +00001007 } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +00001008 StringRef Name;
1009 if (IdentifierInfo *II = NTTP->getIdentifier())
1010 Name = II->getName();
1011 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +00001012
Serge Pavlova67a4d22016-11-10 08:49:37 +00001013 if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001014 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +00001015 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1016 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001017 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001018 } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
Richard Smith030f4992011-04-15 13:38:57 +00001019 VisitTemplateDecl(TTPD);
1020 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001021 }
1022 }
Mike Stump11289f42009-09-09 15:08:12 +00001023
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001024 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +00001025}
1026
Serge Pavlova67a4d22016-11-10 08:49:37 +00001027void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1028 const TemplateParameterList *Params) {
1029 Out << "<";
1030 for (size_t I = 0, E = Args.size(); I < E; ++I) {
1031 const TemplateArgument &A = Args[I];
1032 if (I)
1033 Out << ", ";
1034 if (Params) {
1035 if (A.getKind() == TemplateArgument::Type)
1036 if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1037 auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1038 Out << *P;
1039 continue;
1040 }
1041 if (A.getKind() == TemplateArgument::Template) {
1042 if (auto T = A.getAsTemplate().getAsTemplateDecl())
1043 if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1044 auto P = cast<TemplateTemplateParmDecl>(
1045 Params->getParam(TD->getIndex()));
1046 Out << *P;
1047 continue;
1048 }
1049 }
1050 if (A.getKind() == TemplateArgument::Expression) {
1051 if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1052 if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1053 auto P = cast<NonTypeTemplateParmDecl>(
1054 Params->getParam(N->getIndex()));
1055 Out << *P;
1056 continue;
1057 }
1058 }
1059 }
1060 A.print(Policy, Out);
1061 }
1062 Out << ">";
1063}
1064
Richard Trieu82398f92011-07-28 00:19:05 +00001065void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001066 printTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +00001067
Richard Smith030f4992011-04-15 13:38:57 +00001068 if (const TemplateTemplateParmDecl *TTP =
1069 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +00001070 Out << "class ";
1071 if (TTP->isParameterPack())
1072 Out << "...";
1073 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +00001074 } else {
1075 Visit(D->getTemplatedDecl());
1076 }
Douglas Gregor278f52e2009-05-30 00:08:05 +00001077}
1078
Richard Trieu82398f92011-07-28 00:19:05 +00001079void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +00001080 prettyPrintPragmas(D->getTemplatedDecl());
Alex Lorenz47fd10c2017-04-18 15:12:34 +00001081 // Print any leading template parameter lists.
1082 if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1083 for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1084 I < NumTemplateParams; ++I)
1085 printTemplateParameters(FD->getTemplateParameterList(I));
1086 }
Serge Pavlova67a4d22016-11-10 08:49:37 +00001087 VisitRedeclarableTemplateDecl(D);
1088
Richard Smith057ec502017-02-18 01:01:48 +00001089 // Never print "instantiations" for deduction guides (they don't really
1090 // have them).
1091 if (PrintInstantiation &&
1092 !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001093 FunctionDecl *PrevDecl = D->getTemplatedDecl();
1094 const FunctionDecl *Def;
1095 if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1096 return;
1097 for (auto *I : D->specializations())
1098 if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1099 if (!PrevDecl->isThisDeclarationADefinition())
1100 Out << ";\n";
1101 Indent();
1102 prettyPrintPragmas(I);
1103 Visit(I);
1104 }
1105 }
Richard Trieu82398f92011-07-28 00:19:05 +00001106}
1107
1108void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Serge Pavlova67a4d22016-11-10 08:49:37 +00001109 VisitRedeclarableTemplateDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001110
Serge Pavlova67a4d22016-11-10 08:49:37 +00001111 if (PrintInstantiation) {
1112 for (auto *I : D->specializations())
1113 if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1114 if (D->isThisDeclarationADefinition())
1115 Out << ";";
1116 Out << "\n";
1117 Visit(I);
1118 }
1119 }
1120}
1121
1122void DeclPrinter::VisitClassTemplateSpecializationDecl(
1123 ClassTemplateSpecializationDecl *D) {
1124 Out << "template<> ";
1125 VisitCXXRecordDecl(D);
1126}
1127
1128void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1129 ClassTemplatePartialSpecializationDecl *D) {
1130 printTemplateParameters(D->getTemplateParameters());
1131 VisitCXXRecordDecl(D);
Richard Trieu82398f92011-07-28 00:19:05 +00001132}
1133
Douglas Gregor278f52e2009-05-30 00:08:05 +00001134//----------------------------------------------------------------------------
1135// Objective-C declarations
1136//----------------------------------------------------------------------------
1137
Douglas Gregor813a0662015-06-19 18:14:38 +00001138void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1139 Decl::ObjCDeclQualifier Quals,
1140 QualType T) {
1141 Out << '(';
1142 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1143 Out << "in ";
1144 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1145 Out << "inout ";
1146 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1147 Out << "out ";
1148 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1149 Out << "bycopy ";
1150 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1151 Out << "byref ";
1152 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1153 Out << "oneway ";
1154 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001155 if (auto nullability = AttributedType::stripOuterNullability(T))
1156 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001157 }
1158
1159 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1160 Out << ')';
1161}
1162
Douglas Gregor85f3f952015-07-07 03:57:15 +00001163void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1164 Out << "<";
1165 unsigned First = true;
1166 for (auto *Param : *Params) {
1167 if (First) {
1168 First = false;
1169 } else {
1170 Out << ", ";
1171 }
1172
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001173 switch (Param->getVariance()) {
1174 case ObjCTypeParamVariance::Invariant:
1175 break;
1176
1177 case ObjCTypeParamVariance::Covariant:
1178 Out << "__covariant ";
1179 break;
1180
1181 case ObjCTypeParamVariance::Contravariant:
1182 Out << "__contravariant ";
1183 break;
1184 }
1185
Douglas Gregor85f3f952015-07-07 03:57:15 +00001186 Out << Param->getDeclName().getAsString();
1187
1188 if (Param->hasExplicitBound()) {
1189 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1190 }
1191 }
1192 Out << ">";
1193}
1194
Douglas Gregor278f52e2009-05-30 00:08:05 +00001195void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1196 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001197 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001198 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001199 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001200 if (!OMD->getReturnType().isNull()) {
1201 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1202 OMD->getReturnType());
1203 }
Mike Stump11289f42009-09-09 15:08:12 +00001204
Douglas Gregor278f52e2009-05-30 00:08:05 +00001205 std::string name = OMD->getSelector().getAsString();
1206 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001207 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001208 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001209 pos = name.find_first_of(':', lastPos);
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001210 if (lastPos != 0)
1211 Out << " ";
1212 Out << name.substr(lastPos, pos - lastPos) << ':';
Douglas Gregor813a0662015-06-19 18:14:38 +00001213 PrintObjCMethodType(OMD->getASTContext(),
1214 PI->getObjCDeclQualifier(),
1215 PI->getType());
1216 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001217 lastPos = pos + 1;
1218 }
Mike Stump11289f42009-09-09 15:08:12 +00001219
Douglas Gregor278f52e2009-05-30 00:08:05 +00001220 if (OMD->param_begin() == OMD->param_end())
Alex Lorenzbbf4f702017-06-02 15:02:59 +00001221 Out << name;
Mike Stump11289f42009-09-09 15:08:12 +00001222
Douglas Gregor278f52e2009-05-30 00:08:05 +00001223 if (OMD->isVariadic())
1224 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001225
1226 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001227
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001228 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001229 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001230 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001231 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001232 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001233 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001234}
1235
1236void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1237 std::string I = OID->getNameAsString();
1238 ObjCInterfaceDecl *SID = OID->getSuperClass();
1239
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001240 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001241 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001242 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001243 else
1244 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001245
1246 if (OID->ivar_size() > 0) {
1247 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001248 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001249 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001250 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001251 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001252 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001253 }
1254 Indentation -= Policy.Indentation;
1255 Out << "}\n";
1256 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001257 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1258 Out << "\n";
1259 eolnOut = true;
1260 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001261 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001262 if (!eolnOut)
1263 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001264 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001265}
1266
1267void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1268 std::string I = OID->getNameAsString();
1269 ObjCInterfaceDecl *SID = OID->getSuperClass();
1270
Douglas Gregordc9166c2011-12-15 20:29:51 +00001271 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001272 Out << "@class " << I;
1273
1274 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1275 PrintObjCTypeParams(TypeParams);
1276 }
1277
1278 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001279 return;
1280 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001281 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001282 Out << "@interface " << I;
1283
1284 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1285 PrintObjCTypeParams(TypeParams);
1286 }
1287
Douglas Gregor278f52e2009-05-30 00:08:05 +00001288 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001289 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001290
Douglas Gregor278f52e2009-05-30 00:08:05 +00001291 // Protocols?
1292 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1293 if (!Protocols.empty()) {
1294 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1295 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001296 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001297 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001298 }
Mike Stump11289f42009-09-09 15:08:12 +00001299
Douglas Gregor278f52e2009-05-30 00:08:05 +00001300 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001301 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001302 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001303 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001304 for (const auto *I : OID->ivars()) {
1305 Indent() << I->getASTContext()
1306 .getUnqualifiedObjCPointerType(I->getType())
1307 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001308 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001309 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001310 Out << "}\n";
1311 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001312 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001313 Out << "\n";
1314 eolnOut = true;
1315 }
Mike Stump11289f42009-09-09 15:08:12 +00001316
Douglas Gregor278f52e2009-05-30 00:08:05 +00001317 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001318 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001319 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001320 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001321 // FIXME: implement the rest...
1322}
1323
Douglas Gregor278f52e2009-05-30 00:08:05 +00001324void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001325 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001326 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001327 return;
1328 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001329 // Protocols?
1330 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1331 if (!Protocols.empty()) {
1332 Out << "@protocol " << *PID;
1333 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1334 E = Protocols.end(); I != E; ++I)
1335 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1336 Out << ">\n";
1337 } else
1338 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001339 VisitDeclContext(PID, false);
1340 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001341}
1342
1343void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001344 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001345
1346 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001347 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001348 // FIXME: implement the rest...
1349}
1350
1351void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001352 Out << "@interface " << *PID->getClassInterface();
1353 if (auto TypeParams = PID->getTypeParamList()) {
1354 PrintObjCTypeParams(TypeParams);
1355 }
1356 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001357 if (PID->ivar_size() > 0) {
1358 Out << "{\n";
1359 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001360 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001361 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001362 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001363 Indentation -= Policy.Indentation;
1364 Out << "}\n";
1365 }
1366
Douglas Gregor278f52e2009-05-30 00:08:05 +00001367 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001368 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001369
Douglas Gregor278f52e2009-05-30 00:08:05 +00001370 // FIXME: implement the rest...
1371}
1372
1373void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001374 Out << "@compatibility_alias " << *AID
1375 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001376}
1377
1378/// PrintObjCPropertyDecl - print a property declaration.
1379///
1380void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1381 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1382 Out << "@required\n";
1383 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1384 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001385
Douglas Gregor813a0662015-06-19 18:14:38 +00001386 QualType T = PDecl->getType();
1387
Douglas Gregor278f52e2009-05-30 00:08:05 +00001388 Out << "@property";
1389 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1390 bool first = true;
1391 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001392 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001393 ObjCPropertyDecl::OBJC_PR_readonly) {
1394 Out << (first ? ' ' : ',') << "readonly";
1395 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001396 }
Mike Stump11289f42009-09-09 15:08:12 +00001397
Ted Kremenek897af912011-08-17 21:09:35 +00001398 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001399 Out << (first ? ' ' : ',') << "getter = ";
1400 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001401 first = false;
1402 }
1403 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001404 Out << (first ? ' ' : ',') << "setter = ";
1405 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001406 first = false;
1407 }
Mike Stump11289f42009-09-09 15:08:12 +00001408
Ted Kremenek897af912011-08-17 21:09:35 +00001409 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1410 Out << (first ? ' ' : ',') << "assign";
1411 first = false;
1412 }
Mike Stump11289f42009-09-09 15:08:12 +00001413
Ted Kremenek897af912011-08-17 21:09:35 +00001414 if (PDecl->getPropertyAttributes() &
1415 ObjCPropertyDecl::OBJC_PR_readwrite) {
1416 Out << (first ? ' ' : ',') << "readwrite";
1417 first = false;
1418 }
Mike Stump11289f42009-09-09 15:08:12 +00001419
Ted Kremenek897af912011-08-17 21:09:35 +00001420 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1421 Out << (first ? ' ' : ',') << "retain";
1422 first = false;
1423 }
Mike Stump11289f42009-09-09 15:08:12 +00001424
Ted Kremenek897af912011-08-17 21:09:35 +00001425 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1426 Out << (first ? ' ' : ',') << "strong";
1427 first = false;
1428 }
John McCall31168b02011-06-15 23:02:42 +00001429
Ted Kremenek897af912011-08-17 21:09:35 +00001430 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1431 Out << (first ? ' ' : ',') << "copy";
1432 first = false;
1433 }
Mike Stump11289f42009-09-09 15:08:12 +00001434
Ted Kremenek897af912011-08-17 21:09:35 +00001435 if (PDecl->getPropertyAttributes() &
1436 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1437 Out << (first ? ' ' : ',') << "nonatomic";
1438 first = false;
1439 }
1440 if (PDecl->getPropertyAttributes() &
1441 ObjCPropertyDecl::OBJC_PR_atomic) {
1442 Out << (first ? ' ' : ',') << "atomic";
1443 first = false;
1444 }
1445
Douglas Gregor813a0662015-06-19 18:14:38 +00001446 if (PDecl->getPropertyAttributes() &
1447 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001448 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001449 if (*nullability == NullabilityKind::Unspecified &&
1450 (PDecl->getPropertyAttributes() &
1451 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1452 Out << (first ? ' ' : ',') << "null_resettable";
1453 } else {
1454 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001455 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001456 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001457 first = false;
1458 }
1459 }
1460
Manman Ren387ff7f2016-01-26 18:52:43 +00001461 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1462 Out << (first ? ' ' : ',') << "class";
1463 first = false;
1464 }
1465
Ted Kremenek897af912011-08-17 21:09:35 +00001466 (void) first; // Silence dead store warning due to idiomatic code.
1467 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001468 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001469 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001470 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001471 if (Policy.PolishForDeclaration)
1472 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001473}
1474
1475void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1476 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001477 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001478 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001479 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001480 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001481 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001482 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001483}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001484
1485void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001486 if (!D->isAccessDeclaration())
1487 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001488 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001489 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001490 D->getQualifier()->print(Out, Policy);
Alex Lorenzea9b59a2016-10-03 12:22:17 +00001491
1492 // Use the correct record name when the using declaration is used for
1493 // inheriting constructors.
1494 for (const auto *Shadow : D->shadows()) {
1495 if (const auto *ConstructorShadow =
1496 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1497 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1498 Out << *ConstructorShadow->getNominatedBaseClass();
1499 return;
1500 }
1501 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001502 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001503}
1504
John McCalle61f2ba2009-11-18 02:36:19 +00001505void
1506DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1507 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001508 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001509 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001510}
1511
1512void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001513 if (!D->isAccessDeclaration())
1514 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001515 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001516 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001517}
John McCall3f746822009-11-17 05:59:44 +00001518
1519void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1520 // ignore
1521}
Alexey Bataeva769e072013-03-22 06:34:35 +00001522
1523void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1524 Out << "#pragma omp threadprivate";
1525 if (!D->varlist_empty()) {
1526 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1527 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001528 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001529 Out << (I == D->varlist_begin() ? '(' : ',');
1530 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1531 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001532 }
1533 Out << ")";
1534 }
1535}
1536
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001537void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1538 if (!D->isInvalidDecl()) {
1539 Out << "#pragma omp declare reduction (";
1540 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1541 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1542 nullptr,
1543#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1544 Spelling,
1545#include "clang/Basic/OperatorKinds.def"
1546 };
1547 const char *OpName =
1548 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1549 assert(OpName && "not an overloaded operator");
1550 Out << OpName;
1551 } else {
1552 assert(D->getDeclName().isIdentifier());
1553 D->printName(Out);
1554 }
1555 Out << " : ";
1556 D->getType().print(Out, Policy);
1557 Out << " : ";
1558 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1559 Out << ")";
1560 if (auto *Init = D->getInitializer()) {
1561 Out << " initializer(";
Alexey Bataev070f43a2017-09-06 14:49:58 +00001562 switch (D->getInitializerKind()) {
1563 case OMPDeclareReductionDecl::DirectInit:
1564 Out << "omp_priv(";
1565 break;
1566 case OMPDeclareReductionDecl::CopyInit:
1567 Out << "omp_priv = ";
1568 break;
1569 case OMPDeclareReductionDecl::CallInit:
1570 break;
1571 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001572 Init->printPretty(Out, nullptr, Policy, 0);
Alexey Bataev070f43a2017-09-06 14:49:58 +00001573 if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1574 Out << ")";
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001575 Out << ")";
1576 }
1577 }
1578}
1579
Alexey Bataev4244be22016-02-11 05:35:55 +00001580void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001581 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1582}
1583