blob: 609cc08545ac802cbc47dde70066502620434eb1 [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 Gregor278f52e2009-05-30 00:08:05 +000047 public:
Richard Smith235341b2012-08-16 03:56:14 +000048 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
49 unsigned Indentation = 0, bool PrintInstantiation = false)
50 : Out(Out), Policy(Policy), Indentation(Indentation),
Richard Trieu82398f92011-07-28 00:19:05 +000051 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor278f52e2009-05-30 00:08:05 +000052
53 void VisitDeclContext(DeclContext *DC, bool Indent = true);
54
55 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
56 void VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +000057 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000058 void VisitEnumDecl(EnumDecl *D);
59 void VisitRecordDecl(RecordDecl *D);
60 void VisitEnumConstantDecl(EnumConstantDecl *D);
Michael Han84324352013-02-22 17:15:32 +000061 void VisitEmptyDecl(EmptyDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000062 void VisitFunctionDecl(FunctionDecl *D);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +000063 void VisitFriendDecl(FriendDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000064 void VisitFieldDecl(FieldDecl *D);
65 void VisitVarDecl(VarDecl *D);
Chris Lattnercab02a62011-02-17 20:34:02 +000066 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000067 void VisitParmVarDecl(ParmVarDecl *D);
68 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregorba345522011-12-02 23:23:56 +000069 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +000070 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000071 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000072 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000073 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000074 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000075 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith030f4992011-04-15 13:38:57 +000076 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000077 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
78 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000079 void VisitObjCMethodDecl(ObjCMethodDecl *D);
80 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
81 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000082 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
83 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
84 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
85 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
86 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
87 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000088 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
89 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +000090 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +000091 void VisitUsingShadowDecl(UsingShadowDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +000092 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000093
94 void PrintTemplateParameters(const TemplateParameterList *Params,
Craig Topper36250ad2014-05-12 05:36:57 +000095 const TemplateArgumentList *Args = nullptr);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +000096 void prettyPrintAttributes(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +000097 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +000098 };
99}
100
Richard Trieu82398f92011-07-28 00:19:05 +0000101void Decl::print(raw_ostream &Out, unsigned Indentation,
102 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +0000103 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000104}
105
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000106void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000107 unsigned Indentation, bool PrintInstantiation) const {
Richard Smith235341b2012-08-16 03:56:14 +0000108 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000109 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000110}
111
Eli Friedman79635842009-05-30 04:20:30 +0000112static QualType GetBaseType(QualType T) {
113 // FIXME: This should be on the Type class!
114 QualType BaseType = T;
115 while (!BaseType->isSpecifierType()) {
116 if (isa<TypedefType>(BaseType))
117 break;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000118 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000119 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000120 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
121 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000122 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
123 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000124 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000125 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000126 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000127 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000128 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
129 BaseType = RTy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000130 else
David Blaikie83d382b2011-09-23 05:06:16 +0000131 llvm_unreachable("Unknown declarator!");
Eli Friedman79635842009-05-30 04:20:30 +0000132 }
133 return BaseType;
134}
135
136static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000137 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000138 return TDD->getUnderlyingType();
139 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
140 return VD->getType();
141 return QualType();
142}
143
144void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000145 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000146 unsigned Indentation) {
147 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000148 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000149 return;
150 }
151
152 Decl** End = Begin + NumDecls;
153 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
154 if (TD)
155 ++Begin;
156
157 PrintingPolicy SubPolicy(Policy);
John McCallf937c022011-10-07 06:10:15 +0000158 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000159 TD->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000160 Out << " ";
161 SubPolicy.SuppressTag = true;
162 }
163
164 bool isFirst = true;
165 for ( ; Begin != End; ++Begin) {
166 if (isFirst) {
167 SubPolicy.SuppressSpecifiers = false;
168 isFirst = false;
169 } else {
170 if (!isFirst) Out << ", ";
171 SubPolicy.SuppressSpecifiers = true;
172 }
173
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000174 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000175 }
176}
177
Alp Tokeref6b0072014-01-04 13:47:14 +0000178LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000179 // Get the translation unit
180 const DeclContext *DC = this;
181 while (!DC->isTranslationUnit())
182 DC = DC->getParent();
183
184 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith235341b2012-08-16 03:56:14 +0000185 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000186 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
187}
188
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000189raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000190 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000191 Out << " ";
192 return Out;
193}
194
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000195void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000196 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000197 return;
198
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000199 if (D->hasAttrs()) {
200 AttrVec &Attrs = D->getAttrs();
201 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
Richard Smith52f04a22012-08-16 02:43:29 +0000202 Attr *A = *i;
203 A->printPretty(Out, Policy);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000204 }
205 }
206}
207
Richard Smitha4bb2922014-07-23 03:17:06 +0000208void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
209 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
210 // template argument), but if it is the type of a declaration, the ellipsis
211 // is placed before the name being declared.
212 if (auto *PET = T->getAs<PackExpansionType>()) {
213 Pack = true;
214 T = PET->getPattern();
215 }
216 T.print(Out, Policy, (Pack ? "..." : "") + DeclName);
217}
218
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000219void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000220 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000221 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000222 Out << ";\n";
223 Decls.clear();
224
225}
226
Anders Carlsson601d6e42009-08-28 22:39:52 +0000227void DeclPrinter::Print(AccessSpecifier AS) {
228 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000229 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000230 case AS_public: Out << "public"; break;
231 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000232 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000233 }
234}
235
Douglas Gregor278f52e2009-05-30 00:08:05 +0000236//----------------------------------------------------------------------------
237// Common C declarations
238//----------------------------------------------------------------------------
239
240void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000241 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000242 return;
243
Douglas Gregor278f52e2009-05-30 00:08:05 +0000244 if (Indent)
245 Indentation += Policy.Indentation;
246
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000247 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000248 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000249 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000250
251 // Don't print ObjCIvarDecls, as they are printed when visiting the
252 // containing ObjCInterfaceDecl.
253 if (isa<ObjCIvarDecl>(*D))
254 continue;
255
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000256 // Skip over implicit declarations in pretty-printing mode.
257 if (D->isImplicit())
258 continue;
259
Eli Friedman79635842009-05-30 04:20:30 +0000260 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
261 // forced to merge the declarations because there's no other way to
262 // refer to the struct in question. This limited merging is safe without
263 // a bunch of other checks because it only merges declarations directly
264 // referring to the tag, not typedefs.
265 //
266 // Check whether the current declaration should be grouped with a previous
267 // unnamed struct.
268 QualType CurDeclType = getDeclType(*D);
269 if (!Decls.empty() && !CurDeclType.isNull()) {
270 QualType BaseType = GetBaseType(CurDeclType);
Eli Friedman24b3fed2013-08-12 21:54:04 +0000271 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
272 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
Eli Friedman79635842009-05-30 04:20:30 +0000273 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
274 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
275 Decls.push_back(*D);
276 continue;
277 }
278 }
279
280 // If we have a merged group waiting to be handled, handle it now.
281 if (!Decls.empty())
282 ProcessDeclGroup(Decls);
283
284 // If the current declaration is an unnamed tag type, save it
285 // so we can merge it with the subsequent declaration(s) using it.
286 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
287 Decls.push_back(*D);
288 continue;
289 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000290
291 if (isa<AccessSpecDecl>(*D)) {
292 Indentation -= Policy.Indentation;
293 this->Indent();
294 Print(D->getAccess());
295 Out << ":\n";
296 Indentation += Policy.Indentation;
297 continue;
298 }
299
Douglas Gregor278f52e2009-05-30 00:08:05 +0000300 this->Indent();
301 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000302
303 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000304 const char *Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000305 if (isa<OMPThreadPrivateDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000306 Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000307 else if (isa<FunctionDecl>(*D) &&
308 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
Yaron Keren51db8772014-05-07 09:53:02 +0000309 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000310 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000311 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000312 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000313 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000314 isa<ObjCInterfaceDecl>(*D) ||
315 isa<ObjCProtocolDecl>(*D) ||
316 isa<ObjCCategoryImplDecl>(*D) ||
317 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000318 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000319 else if (isa<EnumConstantDecl>(*D)) {
320 DeclContext::decl_iterator Next = D;
321 ++Next;
322 if (Next != DEnd)
323 Terminator = ",";
324 } else
325 Terminator = ";";
326
327 if (Terminator)
328 Out << Terminator;
329 Out << "\n";
330 }
331
Eli Friedman79635842009-05-30 04:20:30 +0000332 if (!Decls.empty())
333 ProcessDeclGroup(Decls);
334
Douglas Gregor278f52e2009-05-30 00:08:05 +0000335 if (Indent)
336 Indentation -= Policy.Indentation;
337}
338
339void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
340 VisitDeclContext(D, false);
341}
342
343void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000344 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000345 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000346
347 if (D->isModulePrivate())
348 Out << "__module_private__ ";
349 }
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000350 D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000351 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000352}
353
Richard Smithdda56e42011-04-15 14:24:37 +0000354void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000355 Out << "using " << *D;
356 prettyPrintAttributes(D);
357 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000358}
359
Douglas Gregor278f52e2009-05-30 00:08:05 +0000360void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000361 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
362 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000363 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000364 if (D->isScoped()) {
365 if (D->isScopedUsingClassTag())
366 Out << "class ";
367 else
368 Out << "struct ";
369 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000370 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000371
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000372 if (D->isFixed())
373 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000374
John McCallf937c022011-10-07 06:10:15 +0000375 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000376 Out << " {\n";
377 VisitDeclContext(D);
378 Indent() << "}";
379 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000380 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000381}
382
383void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000384 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
385 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000386 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000387
388 prettyPrintAttributes(D);
389
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000390 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000391 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000392
John McCallf937c022011-10-07 06:10:15 +0000393 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000394 Out << " {\n";
395 VisitDeclContext(D);
396 Indent() << "}";
397 }
398}
399
400void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000401 Out << *D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000402 if (Expr *Init = D->getInitExpr()) {
403 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000404 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000405 }
406}
407
Mike Stump11289f42009-09-09 15:08:12 +0000408void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000409 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000410 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000411 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000412 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000413 case SC_None: break;
414 case SC_Extern: Out << "extern "; break;
415 case SC_Static: Out << "static "; break;
416 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000417 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
418 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000419 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000420
Douglas Gregor26701a42011-09-09 02:06:17 +0000421 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000422 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000423 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000424 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000425 if ((CDecl && CDecl->isExplicitSpecified()) ||
426 (ConversionDecl && ConversionDecl->isExplicit()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000427 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000428 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000429
Douglas Gregor2d042f12009-05-30 05:39:39 +0000430 PrintingPolicy SubPolicy(Policy);
431 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000432 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000433
Abramo Bagnara6d810632010-12-14 22:11:44 +0000434 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000435 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000436 Proto = '(' + Proto + ')';
437 Ty = PT->getInnerType();
438 }
439
Reid Kleckner0503a872013-12-05 01:23:43 +0000440 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000441 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000442 if (D->hasWrittenPrototype())
443 FT = dyn_cast<FunctionProtoType>(AFT);
444
445 Proto += "(";
446 if (FT) {
447 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000448 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000449 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
450 if (i) POut << ", ";
451 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
452 }
Mike Stump11289f42009-09-09 15:08:12 +0000453
Douglas Gregor278f52e2009-05-30 00:08:05 +0000454 if (FT->isVariadic()) {
455 if (D->getNumParams()) POut << ", ";
456 POut << "...";
457 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000458 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000459 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
460 if (i)
461 Proto += ", ";
462 Proto += D->getParamDecl(i)->getNameAsString();
463 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000464 }
465
466 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000467
David Blaikief5697e52012-08-10 00:55:35 +0000468 if (FT) {
469 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000470 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000471 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000472 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000473 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000474 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000475
476 switch (FT->getRefQualifier()) {
477 case RQ_None:
478 break;
479 case RQ_LValue:
480 Proto += " &";
481 break;
482 case RQ_RValue:
483 Proto += " &&";
484 break;
485 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000486 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000487
488 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000489 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000490 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000491 Proto += "...";
492 else
493 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
494 if (I)
495 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000496
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000497 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000498 }
499 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000500 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
501 Proto += " noexcept";
502 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
503 Proto += "(";
504 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000505 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000506 Indentation);
507 EOut.flush();
508 Proto += EOut.str();
509 Proto += ")";
510 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000511 }
512
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000513 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000514 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000515 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000516 if (BMInitializer->isInClassMemberInitializer())
517 continue;
518
519 if (!HasInitializerList) {
520 Proto += " : ";
521 Out << Proto;
522 Proto.clear();
523 HasInitializerList = true;
524 } else
525 Out << ", ";
526
527 if (BMInitializer->isAnyMemberInitializer()) {
528 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000529 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000530 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000531 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000532 }
533
534 Out << "(";
535 if (!BMInitializer->getInit()) {
536 // Nothing to print
537 } else {
538 Expr *Init = BMInitializer->getInit();
539 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
540 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000541
Richard Smith938f40b2011-06-11 17:19:42 +0000542 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000543
544 Expr *SimpleInit = nullptr;
545 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000546 unsigned NumArgs = 0;
547 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
548 Args = ParenList->getExprs();
549 NumArgs = ParenList->getNumExprs();
550 } else if (CXXConstructExpr *Construct
551 = dyn_cast<CXXConstructExpr>(Init)) {
552 Args = Construct->getArgs();
553 NumArgs = Construct->getNumArgs();
554 } else
555 SimpleInit = Init;
556
557 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000558 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000559 else {
560 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000561 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000562 if (isa<CXXDefaultArgExpr>(Args[I]))
563 break;
564
565 if (I)
566 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000567 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000568 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000569 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000570 }
Richard Smith938f40b2011-06-11 17:19:42 +0000571 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000572 if (BMInitializer->isPackExpansion())
573 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000574 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000575 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000576 if (FT && FT->hasTrailingReturn()) {
577 Out << "auto " << Proto << " -> ";
578 Proto.clear();
579 }
Alp Toker314cc812014-01-25 16:55:45 +0000580 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000581 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000582 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000583 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000584 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000585 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000586 }
587
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000588 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000589
590 if (D->isPure())
591 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000592 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000593 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000594 else if (D->isExplicitlyDefaulted())
595 Out << " = default";
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000596 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000597 if (!D->hasPrototype() && D->getNumParams()) {
598 // This is a K&R function definition, so we need to print the
599 // parameters.
600 Out << '\n';
Richard Smith235341b2012-08-16 03:56:14 +0000601 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000602 Indentation += Policy.Indentation;
603 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
604 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000605 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000606 Out << ";\n";
607 }
608 Indentation -= Policy.Indentation;
609 } else
610 Out << ' ';
611
Richard Trieuddd01ce2014-06-09 22:53:25 +0000612 if (D->getBody())
613 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000614 Out << '\n';
615 }
616}
617
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000618void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
619 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000620 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
621 for (unsigned i = 0; i < NumTPLists; ++i)
622 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000623 Out << "friend ";
624 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000625 }
626 else if (FunctionDecl *FD =
627 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
628 Out << "friend ";
629 VisitFunctionDecl(FD);
630 }
631 else if (FunctionTemplateDecl *FTD =
632 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
633 Out << "friend ";
634 VisitFunctionTemplateDecl(FTD);
635 }
636 else if (ClassTemplateDecl *CTD =
637 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
638 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000639 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000640 }
641}
642
Douglas Gregor278f52e2009-05-30 00:08:05 +0000643void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000644 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000645 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000646 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
647 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000648
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000649 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Fariborz Jahanian9e075842013-05-01 17:28:37 +0000650 stream(Policy, D->getName());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000651
652 if (D->isBitField()) {
653 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000654 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000655 }
Richard Smith938f40b2011-06-11 17:19:42 +0000656
657 Expr *Init = D->getInClassInitializer();
658 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000659 if (D->getInClassInitStyle() == ICIS_ListInit)
660 Out << " ";
661 else
662 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000663 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000664 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000665 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000666}
667
Chris Lattnercab02a62011-02-17 20:34:02 +0000668void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000669 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000670}
671
Douglas Gregor278f52e2009-05-30 00:08:05 +0000672void DeclPrinter::VisitVarDecl(VarDecl *D) {
Richard Smithfd3834f2013-04-13 02:43:54 +0000673 if (!Policy.SuppressSpecifiers) {
674 StorageClass SC = D->getStorageClass();
675 if (SC != SC_None)
676 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000677
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000678 switch (D->getTSCSpec()) {
679 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000680 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000681 case TSCS___thread:
682 Out << "__thread ";
683 break;
684 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000685 Out << "_Thread_local ";
686 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000687 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000688 Out << "thread_local ";
689 break;
690 }
691
692 if (D->isModulePrivate())
693 Out << "__module_private__ ";
694 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000695
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000696 QualType T = D->getTypeSourceInfo()
697 ? D->getTypeSourceInfo()->getType()
698 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
Richard Smitha4bb2922014-07-23 03:17:06 +0000699 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000700 Expr *Init = D->getInit();
701 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000702 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000703 if (CXXConstructExpr *Construct =
704 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000705 if (D->getInitStyle() == VarDecl::CallInit &&
706 !Construct->isListInitialization()) {
707 ImplicitInit = Construct->getNumArgs() == 0 ||
708 Construct->getArg(0)->isDefaultArgument();
709 }
710 }
Sebastian Redla9351792012-02-11 23:51:47 +0000711 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000712 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000713 Out << "(";
714 else if (D->getInitStyle() == VarDecl::CInit) {
715 Out << " = ";
716 }
Craig Topper36250ad2014-05-12 05:36:57 +0000717 Init->printPretty(Out, nullptr, Policy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000718 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000719 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000720 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000721 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000722 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000723}
724
725void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
726 VisitVarDecl(D);
727}
728
729void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
730 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000731 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000732 Out << ")";
733}
734
Douglas Gregorba345522011-12-02 23:23:56 +0000735void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000736 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000737 << ";\n";
738}
739
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000740void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
741 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000742 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000743 if (StringLiteral *SL = D->getMessage()) {
744 Out << ", ";
745 SL->printPretty(Out, nullptr, Policy, Indentation);
746 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000747 Out << ")";
748}
749
Douglas Gregor278f52e2009-05-30 00:08:05 +0000750//----------------------------------------------------------------------------
751// C++ declarations
752//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000753void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000754 if (D->isInline())
755 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000756 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000757 VisitDeclContext(D);
758 Indent() << "}";
759}
760
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000761void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
762 Out << "using namespace ";
763 if (D->getQualifier())
764 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000765 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000766}
767
Douglas Gregor18231932009-05-30 06:48:27 +0000768void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000769 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000770 if (D->getQualifier())
771 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000772 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000773}
774
Michael Han84324352013-02-22 17:15:32 +0000775void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
776 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000777}
778
Douglas Gregor5f478b72009-05-30 06:58:37 +0000779void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000780 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
781 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000782 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000783
784 prettyPrintAttributes(D);
785
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000786 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000787 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000788
John McCallf937c022011-10-07 06:10:15 +0000789 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000790 // Print the base classes
791 if (D->getNumBases()) {
792 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000793 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
794 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000795 if (Base != D->bases_begin())
796 Out << ", ";
797
798 if (Base->isVirtual())
799 Out << "virtual ";
800
Anders Carlsson6df9e072009-08-29 20:36:12 +0000801 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000802 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000803 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000804 Out << " ";
805 }
806 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000807
808 if (Base->isPackExpansion())
809 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000810 }
811 }
812
813 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000814 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000815 Out << " {\n";
816 VisitDeclContext(D);
817 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000818 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000819}
820
821void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
822 const char *l;
823 if (D->getLanguage() == LinkageSpecDecl::lang_c)
824 l = "C";
825 else {
826 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
827 "unknown language in linkage specification");
828 l = "C++";
829 }
830
831 Out << "extern \"" << l << "\" ";
832 if (D->hasBraces()) {
833 Out << "{\n";
834 VisitDeclContext(D);
835 Indent() << "}";
836 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000837 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000838}
839
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000840void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
841 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000842 assert(Params);
843 assert(!Args || Params->size() == Args->size());
844
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000845 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000846
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000847 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
848 if (i != 0)
849 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000850
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000851 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000852 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000853 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000854
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000855 if (TTP->wasDeclaredWithTypename())
856 Out << "typename ";
857 else
858 Out << "class ";
859
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000860 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000861 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000862
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000863 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000864
Richard Trieu82398f92011-07-28 00:19:05 +0000865 if (Args) {
866 Out << " = ";
867 Args->get(i).print(Policy, Out);
868 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000869 Out << " = ";
870 Out << TTP->getDefaultArgument().getAsString(Policy);
871 };
Mike Stump11289f42009-09-09 15:08:12 +0000872 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000873 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000874 StringRef Name;
875 if (IdentifierInfo *II = NTTP->getIdentifier())
876 Name = II->getName();
877 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000878
Richard Trieu82398f92011-07-28 00:19:05 +0000879 if (Args) {
880 Out << " = ";
881 Args->get(i).print(Policy, Out);
882 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000883 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000884 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
885 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000886 }
Richard Smith030f4992011-04-15 13:38:57 +0000887 } else if (const TemplateTemplateParmDecl *TTPD =
888 dyn_cast<TemplateTemplateParmDecl>(Param)) {
889 VisitTemplateDecl(TTPD);
890 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000891 }
892 }
Mike Stump11289f42009-09-09 15:08:12 +0000893
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000894 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000895}
896
897void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
898 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000899
Richard Smith030f4992011-04-15 13:38:57 +0000900 if (const TemplateTemplateParmDecl *TTP =
901 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000902 Out << "class ";
903 if (TTP->isParameterPack())
904 Out << "...";
905 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000906 } else {
907 Visit(D->getTemplatedDecl());
908 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000909}
910
Richard Trieu82398f92011-07-28 00:19:05 +0000911void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
912 if (PrintInstantiation) {
913 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000914 for (auto *I : D->specializations()) {
915 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
916 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000917 }
918 }
919
920 return VisitRedeclarableTemplateDecl(D);
921}
922
923void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
924 if (PrintInstantiation) {
925 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000926 for (auto *I : D->specializations()) {
927 PrintTemplateParameters(Params, &I->getTemplateArgs());
928 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000929 Out << '\n';
930 }
931 }
932
933 return VisitRedeclarableTemplateDecl(D);
934}
935
Douglas Gregor278f52e2009-05-30 00:08:05 +0000936//----------------------------------------------------------------------------
937// Objective-C declarations
938//----------------------------------------------------------------------------
939
Douglas Gregor813a0662015-06-19 18:14:38 +0000940/// Strip off the top-level nullability annotation, if it's there.
941static Optional<NullabilityKind> stripOuterNullability(QualType &T) {
942 if (auto attributed = dyn_cast<AttributedType>(T.getTypePtr())) {
943 if (auto nullability = attributed->getImmediateNullability()) {
944 T = attributed->getModifiedType();
945 return nullability;
946 }
947 }
948
949 return None;
950 }
951
952void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
953 Decl::ObjCDeclQualifier Quals,
954 QualType T) {
955 Out << '(';
956 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
957 Out << "in ";
958 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
959 Out << "inout ";
960 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
961 Out << "out ";
962 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
963 Out << "bycopy ";
964 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
965 Out << "byref ";
966 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
967 Out << "oneway ";
968 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
969 if (auto nullability = stripOuterNullability(T)) {
970 Out << getNullabilitySpelling(*nullability).substr(2) << ' ';
971 }
972 }
973
974 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
975 Out << ')';
976}
977
Douglas Gregor278f52e2009-05-30 00:08:05 +0000978void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
979 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000980 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +0000981 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000982 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +0000983 if (!OMD->getReturnType().isNull()) {
984 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
985 OMD->getReturnType());
986 }
Mike Stump11289f42009-09-09 15:08:12 +0000987
Douglas Gregor278f52e2009-05-30 00:08:05 +0000988 std::string name = OMD->getSelector().getAsString();
989 std::string::size_type pos, lastPos = 0;
Aaron Ballman43b68be2014-03-07 17:50:17 +0000990 for (const auto *PI : OMD->params()) {
Mike Stump11289f42009-09-09 15:08:12 +0000991 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000992 pos = name.find_first_of(':', lastPos);
Douglas Gregor813a0662015-06-19 18:14:38 +0000993 Out << " " << name.substr(lastPos, pos - lastPos) << ':';
994 PrintObjCMethodType(OMD->getASTContext(),
995 PI->getObjCDeclQualifier(),
996 PI->getType());
997 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000998 lastPos = pos + 1;
999 }
Mike Stump11289f42009-09-09 15:08:12 +00001000
Douglas Gregor278f52e2009-05-30 00:08:05 +00001001 if (OMD->param_begin() == OMD->param_end())
1002 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +00001003
Douglas Gregor278f52e2009-05-30 00:08:05 +00001004 if (OMD->isVariadic())
1005 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001006
1007 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001008
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001009 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001010 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001011 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001012 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001013 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001014 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001015}
1016
1017void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1018 std::string I = OID->getNameAsString();
1019 ObjCInterfaceDecl *SID = OID->getSuperClass();
1020
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001021 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001022 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001023 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001024 else
1025 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001026
1027 if (OID->ivar_size() > 0) {
1028 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001029 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001030 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001031 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001032 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001033 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001034 }
1035 Indentation -= Policy.Indentation;
1036 Out << "}\n";
1037 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001038 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1039 Out << "\n";
1040 eolnOut = true;
1041 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001042 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001043 if (!eolnOut)
1044 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001045 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001046}
1047
1048void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1049 std::string I = OID->getNameAsString();
1050 ObjCInterfaceDecl *SID = OID->getSuperClass();
1051
Douglas Gregordc9166c2011-12-15 20:29:51 +00001052 if (!OID->isThisDeclarationADefinition()) {
1053 Out << "@class " << I << ";";
1054 return;
1055 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001056 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001057 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001058 Out << "@interface " << I << " : " << *SID;
Douglas Gregor1c283312010-08-11 12:19:30 +00001059 else
1060 Out << "@interface " << I;
Mike Stump11289f42009-09-09 15:08:12 +00001061
Douglas Gregor278f52e2009-05-30 00:08:05 +00001062 // Protocols?
1063 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1064 if (!Protocols.empty()) {
1065 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1066 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001067 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001068 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001069 }
Mike Stump11289f42009-09-09 15:08:12 +00001070
Douglas Gregor278f52e2009-05-30 00:08:05 +00001071 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001072 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001073 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001074 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001075 for (const auto *I : OID->ivars()) {
1076 Indent() << I->getASTContext()
1077 .getUnqualifiedObjCPointerType(I->getType())
1078 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001079 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001080 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001081 Out << "}\n";
1082 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001083 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001084 Out << "\n";
1085 eolnOut = true;
1086 }
Mike Stump11289f42009-09-09 15:08:12 +00001087
Douglas Gregor278f52e2009-05-30 00:08:05 +00001088 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001089 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001090 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001091 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001092 // FIXME: implement the rest...
1093}
1094
Douglas Gregor278f52e2009-05-30 00:08:05 +00001095void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001096 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001097 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001098 return;
1099 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001100 // Protocols?
1101 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1102 if (!Protocols.empty()) {
1103 Out << "@protocol " << *PID;
1104 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1105 E = Protocols.end(); I != E; ++I)
1106 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1107 Out << ">\n";
1108 } else
1109 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001110 VisitDeclContext(PID, false);
1111 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001112}
1113
1114void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001115 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001116
1117 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001118 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001119 // FIXME: implement the rest...
1120}
1121
1122void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001123 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001124 if (PID->ivar_size() > 0) {
1125 Out << "{\n";
1126 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001127 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001128 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001129 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001130 Indentation -= Policy.Indentation;
1131 Out << "}\n";
1132 }
1133
Douglas Gregor278f52e2009-05-30 00:08:05 +00001134 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001135 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001136
Douglas Gregor278f52e2009-05-30 00:08:05 +00001137 // FIXME: implement the rest...
1138}
1139
1140void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001141 Out << "@compatibility_alias " << *AID
1142 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001143}
1144
1145/// PrintObjCPropertyDecl - print a property declaration.
1146///
1147void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1148 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1149 Out << "@required\n";
1150 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1151 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001152
Douglas Gregor813a0662015-06-19 18:14:38 +00001153 QualType T = PDecl->getType();
1154
Douglas Gregor278f52e2009-05-30 00:08:05 +00001155 Out << "@property";
1156 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1157 bool first = true;
1158 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001159 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001160 ObjCPropertyDecl::OBJC_PR_readonly) {
1161 Out << (first ? ' ' : ',') << "readonly";
1162 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001163 }
Mike Stump11289f42009-09-09 15:08:12 +00001164
Ted Kremenek897af912011-08-17 21:09:35 +00001165 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001166 Out << (first ? ' ' : ',') << "getter = ";
1167 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001168 first = false;
1169 }
1170 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001171 Out << (first ? ' ' : ',') << "setter = ";
1172 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001173 first = false;
1174 }
Mike Stump11289f42009-09-09 15:08:12 +00001175
Ted Kremenek897af912011-08-17 21:09:35 +00001176 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1177 Out << (first ? ' ' : ',') << "assign";
1178 first = false;
1179 }
Mike Stump11289f42009-09-09 15:08:12 +00001180
Ted Kremenek897af912011-08-17 21:09:35 +00001181 if (PDecl->getPropertyAttributes() &
1182 ObjCPropertyDecl::OBJC_PR_readwrite) {
1183 Out << (first ? ' ' : ',') << "readwrite";
1184 first = false;
1185 }
Mike Stump11289f42009-09-09 15:08:12 +00001186
Ted Kremenek897af912011-08-17 21:09:35 +00001187 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1188 Out << (first ? ' ' : ',') << "retain";
1189 first = false;
1190 }
Mike Stump11289f42009-09-09 15:08:12 +00001191
Ted Kremenek897af912011-08-17 21:09:35 +00001192 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1193 Out << (first ? ' ' : ',') << "strong";
1194 first = false;
1195 }
John McCall31168b02011-06-15 23:02:42 +00001196
Ted Kremenek897af912011-08-17 21:09:35 +00001197 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1198 Out << (first ? ' ' : ',') << "copy";
1199 first = false;
1200 }
Mike Stump11289f42009-09-09 15:08:12 +00001201
Ted Kremenek897af912011-08-17 21:09:35 +00001202 if (PDecl->getPropertyAttributes() &
1203 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1204 Out << (first ? ' ' : ',') << "nonatomic";
1205 first = false;
1206 }
1207 if (PDecl->getPropertyAttributes() &
1208 ObjCPropertyDecl::OBJC_PR_atomic) {
1209 Out << (first ? ' ' : ',') << "atomic";
1210 first = false;
1211 }
1212
Douglas Gregor813a0662015-06-19 18:14:38 +00001213 if (PDecl->getPropertyAttributes() &
1214 ObjCPropertyDecl::OBJC_PR_nullability) {
1215 if (auto nullability = stripOuterNullability(T)) {
1216 Out << (first ? ' ' : ',')
1217 << getNullabilitySpelling(*nullability).substr(2);
1218 first = false;
1219 }
1220 }
1221
Ted Kremenek897af912011-08-17 21:09:35 +00001222 (void) first; // Silence dead store warning due to idiomatic code.
1223 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001224 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001225 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001226 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001227 if (Policy.PolishForDeclaration)
1228 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001229}
1230
1231void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1232 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001233 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001234 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001235 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001236 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001237 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001238 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001239}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001240
1241void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001242 if (!D->isAccessDeclaration())
1243 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001244 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001245 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001246 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001247 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001248}
1249
John McCalle61f2ba2009-11-18 02:36:19 +00001250void
1251DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1252 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001253 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001254 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001255}
1256
1257void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001258 if (!D->isAccessDeclaration())
1259 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001260 D->getQualifier()->print(Out, Policy);
Fariborz Jahanian3ec39212012-12-06 00:09:40 +00001261 Out << D->getName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001262}
John McCall3f746822009-11-17 05:59:44 +00001263
1264void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1265 // ignore
1266}
Alexey Bataeva769e072013-03-22 06:34:35 +00001267
1268void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1269 Out << "#pragma omp threadprivate";
1270 if (!D->varlist_empty()) {
1271 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1272 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001273 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001274 Out << (I == D->varlist_begin() ? '(' : ',');
1275 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1276 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001277 }
1278 Out << ")";
1279 }
1280}
1281