blob: 160e48b6d95bdfefbd639224d2c73116b15ea12b [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 Gregor278f52e2009-05-30 00:08:05 +000040 public:
Richard Smith235341b2012-08-16 03:56:14 +000041 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
42 unsigned Indentation = 0, bool PrintInstantiation = false)
43 : Out(Out), Policy(Policy), Indentation(Indentation),
Richard Trieu82398f92011-07-28 00:19:05 +000044 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor278f52e2009-05-30 00:08:05 +000045
46 void VisitDeclContext(DeclContext *DC, bool Indent = true);
47
48 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
49 void VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +000050 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000051 void VisitEnumDecl(EnumDecl *D);
52 void VisitRecordDecl(RecordDecl *D);
53 void VisitEnumConstantDecl(EnumConstantDecl *D);
Michael Han84324352013-02-22 17:15:32 +000054 void VisitEmptyDecl(EmptyDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000055 void VisitFunctionDecl(FunctionDecl *D);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +000056 void VisitFriendDecl(FriendDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000057 void VisitFieldDecl(FieldDecl *D);
58 void VisitVarDecl(VarDecl *D);
Chris Lattnercab02a62011-02-17 20:34:02 +000059 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000060 void VisitParmVarDecl(ParmVarDecl *D);
61 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregorba345522011-12-02 23:23:56 +000062 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +000063 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000064 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000065 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000066 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000067 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000068 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith030f4992011-04-15 13:38:57 +000069 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000070 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
71 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000072 void VisitObjCMethodDecl(ObjCMethodDecl *D);
73 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
74 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000075 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
76 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
77 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
78 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
79 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
80 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000081 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
82 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +000083 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +000084 void VisitUsingShadowDecl(UsingShadowDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +000085 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000086
87 void PrintTemplateParameters(const TemplateParameterList *Params,
Craig Topper36250ad2014-05-12 05:36:57 +000088 const TemplateArgumentList *Args = nullptr);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +000089 void prettyPrintAttributes(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +000090 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +000091 };
92}
93
Richard Trieu82398f92011-07-28 00:19:05 +000094void Decl::print(raw_ostream &Out, unsigned Indentation,
95 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +000096 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +000097}
98
Chris Lattner0e62c1c2011-07-23 10:55:15 +000099void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000100 unsigned Indentation, bool PrintInstantiation) const {
Richard Smith235341b2012-08-16 03:56:14 +0000101 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000102 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000103}
104
Eli Friedman79635842009-05-30 04:20:30 +0000105static QualType GetBaseType(QualType T) {
106 // FIXME: This should be on the Type class!
107 QualType BaseType = T;
108 while (!BaseType->isSpecifierType()) {
109 if (isa<TypedefType>(BaseType))
110 break;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000111 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000112 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000113 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
114 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000115 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
116 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000117 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000118 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000119 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000120 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000121 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
122 BaseType = RTy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000123 else
David Blaikie83d382b2011-09-23 05:06:16 +0000124 llvm_unreachable("Unknown declarator!");
Eli Friedman79635842009-05-30 04:20:30 +0000125 }
126 return BaseType;
127}
128
129static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000130 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000131 return TDD->getUnderlyingType();
132 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
133 return VD->getType();
134 return QualType();
135}
136
137void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000138 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000139 unsigned Indentation) {
140 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000141 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000142 return;
143 }
144
145 Decl** End = Begin + NumDecls;
146 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
147 if (TD)
148 ++Begin;
149
150 PrintingPolicy SubPolicy(Policy);
John McCallf937c022011-10-07 06:10:15 +0000151 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000152 TD->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000153 Out << " ";
154 SubPolicy.SuppressTag = true;
155 }
156
157 bool isFirst = true;
158 for ( ; Begin != End; ++Begin) {
159 if (isFirst) {
160 SubPolicy.SuppressSpecifiers = false;
161 isFirst = false;
162 } else {
163 if (!isFirst) Out << ", ";
164 SubPolicy.SuppressSpecifiers = true;
165 }
166
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000167 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000168 }
169}
170
Alp Tokeref6b0072014-01-04 13:47:14 +0000171LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000172 // Get the translation unit
173 const DeclContext *DC = this;
174 while (!DC->isTranslationUnit())
175 DC = DC->getParent();
176
177 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith235341b2012-08-16 03:56:14 +0000178 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000179 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
180}
181
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000182raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000183 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000184 Out << " ";
185 return Out;
186}
187
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000188void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000189 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000190 return;
191
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000192 if (D->hasAttrs()) {
193 AttrVec &Attrs = D->getAttrs();
194 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
Richard Smith52f04a22012-08-16 02:43:29 +0000195 Attr *A = *i;
196 A->printPretty(Out, Policy);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000197 }
198 }
199}
200
Richard Smitha4bb2922014-07-23 03:17:06 +0000201void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
202 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
203 // template argument), but if it is the type of a declaration, the ellipsis
204 // is placed before the name being declared.
205 if (auto *PET = T->getAs<PackExpansionType>()) {
206 Pack = true;
207 T = PET->getPattern();
208 }
209 T.print(Out, Policy, (Pack ? "..." : "") + DeclName);
210}
211
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000212void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000213 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000214 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000215 Out << ";\n";
216 Decls.clear();
217
218}
219
Anders Carlsson601d6e42009-08-28 22:39:52 +0000220void DeclPrinter::Print(AccessSpecifier AS) {
221 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000222 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000223 case AS_public: Out << "public"; break;
224 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000225 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000226 }
227}
228
Douglas Gregor278f52e2009-05-30 00:08:05 +0000229//----------------------------------------------------------------------------
230// Common C declarations
231//----------------------------------------------------------------------------
232
233void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000234 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000235 return;
236
Douglas Gregor278f52e2009-05-30 00:08:05 +0000237 if (Indent)
238 Indentation += Policy.Indentation;
239
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000240 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000241 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000242 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000243
244 // Don't print ObjCIvarDecls, as they are printed when visiting the
245 // containing ObjCInterfaceDecl.
246 if (isa<ObjCIvarDecl>(*D))
247 continue;
248
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000249 // Skip over implicit declarations in pretty-printing mode.
250 if (D->isImplicit())
251 continue;
252
Eli Friedman79635842009-05-30 04:20:30 +0000253 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
254 // forced to merge the declarations because there's no other way to
255 // refer to the struct in question. This limited merging is safe without
256 // a bunch of other checks because it only merges declarations directly
257 // referring to the tag, not typedefs.
258 //
259 // Check whether the current declaration should be grouped with a previous
260 // unnamed struct.
261 QualType CurDeclType = getDeclType(*D);
262 if (!Decls.empty() && !CurDeclType.isNull()) {
263 QualType BaseType = GetBaseType(CurDeclType);
Eli Friedman24b3fed2013-08-12 21:54:04 +0000264 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
265 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
Eli Friedman79635842009-05-30 04:20:30 +0000266 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
267 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
268 Decls.push_back(*D);
269 continue;
270 }
271 }
272
273 // If we have a merged group waiting to be handled, handle it now.
274 if (!Decls.empty())
275 ProcessDeclGroup(Decls);
276
277 // If the current declaration is an unnamed tag type, save it
278 // so we can merge it with the subsequent declaration(s) using it.
279 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
280 Decls.push_back(*D);
281 continue;
282 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000283
284 if (isa<AccessSpecDecl>(*D)) {
285 Indentation -= Policy.Indentation;
286 this->Indent();
287 Print(D->getAccess());
288 Out << ":\n";
289 Indentation += Policy.Indentation;
290 continue;
291 }
292
Douglas Gregor278f52e2009-05-30 00:08:05 +0000293 this->Indent();
294 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000295
296 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000297 const char *Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000298 if (isa<OMPThreadPrivateDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000299 Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000300 else if (isa<FunctionDecl>(*D) &&
301 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
Yaron Keren51db8772014-05-07 09:53:02 +0000302 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000303 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000304 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000305 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000306 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000307 isa<ObjCInterfaceDecl>(*D) ||
308 isa<ObjCProtocolDecl>(*D) ||
309 isa<ObjCCategoryImplDecl>(*D) ||
310 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000311 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000312 else if (isa<EnumConstantDecl>(*D)) {
313 DeclContext::decl_iterator Next = D;
314 ++Next;
315 if (Next != DEnd)
316 Terminator = ",";
317 } else
318 Terminator = ";";
319
320 if (Terminator)
321 Out << Terminator;
322 Out << "\n";
323 }
324
Eli Friedman79635842009-05-30 04:20:30 +0000325 if (!Decls.empty())
326 ProcessDeclGroup(Decls);
327
Douglas Gregor278f52e2009-05-30 00:08:05 +0000328 if (Indent)
329 Indentation -= Policy.Indentation;
330}
331
332void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
333 VisitDeclContext(D, false);
334}
335
336void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000337 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000338 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000339
340 if (D->isModulePrivate())
341 Out << "__module_private__ ";
342 }
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000343 D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000344 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000345}
346
Richard Smithdda56e42011-04-15 14:24:37 +0000347void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000348 Out << "using " << *D;
349 prettyPrintAttributes(D);
350 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000351}
352
Douglas Gregor278f52e2009-05-30 00:08:05 +0000353void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000354 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
355 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000356 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000357 if (D->isScoped()) {
358 if (D->isScopedUsingClassTag())
359 Out << "class ";
360 else
361 Out << "struct ";
362 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000363 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000364
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000365 if (D->isFixed())
366 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000367
John McCallf937c022011-10-07 06:10:15 +0000368 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000369 Out << " {\n";
370 VisitDeclContext(D);
371 Indent() << "}";
372 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000373 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000374}
375
376void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000377 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
378 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000379 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000380 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000381 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000382
John McCallf937c022011-10-07 06:10:15 +0000383 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000384 Out << " {\n";
385 VisitDeclContext(D);
386 Indent() << "}";
387 }
388}
389
390void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000391 Out << *D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000392 if (Expr *Init = D->getInitExpr()) {
393 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000394 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000395 }
396}
397
Mike Stump11289f42009-09-09 15:08:12 +0000398void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000399 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000400 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000401 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000402 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000403 case SC_None: break;
404 case SC_Extern: Out << "extern "; break;
405 case SC_Static: Out << "static "; break;
406 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000407 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
408 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000409 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000410
Douglas Gregor26701a42011-09-09 02:06:17 +0000411 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000412 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000413 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000414 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000415 if ((CDecl && CDecl->isExplicitSpecified()) ||
416 (ConversionDecl && ConversionDecl->isExplicit()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000417 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000418 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000419
Douglas Gregor2d042f12009-05-30 05:39:39 +0000420 PrintingPolicy SubPolicy(Policy);
421 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000422 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000423
Abramo Bagnara6d810632010-12-14 22:11:44 +0000424 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000425 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000426 Proto = '(' + Proto + ')';
427 Ty = PT->getInnerType();
428 }
429
Reid Kleckner0503a872013-12-05 01:23:43 +0000430 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000431 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000432 if (D->hasWrittenPrototype())
433 FT = dyn_cast<FunctionProtoType>(AFT);
434
435 Proto += "(";
436 if (FT) {
437 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000438 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000439 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
440 if (i) POut << ", ";
441 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
Douglas Gregor278f52e2009-05-30 00:08:05 +0000444 if (FT->isVariadic()) {
445 if (D->getNumParams()) POut << ", ";
446 POut << "...";
447 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000448 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000449 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
450 if (i)
451 Proto += ", ";
452 Proto += D->getParamDecl(i)->getNameAsString();
453 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000454 }
455
456 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000457
David Blaikief5697e52012-08-10 00:55:35 +0000458 if (FT) {
459 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000460 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000461 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000462 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000463 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000464 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000465
466 switch (FT->getRefQualifier()) {
467 case RQ_None:
468 break;
469 case RQ_LValue:
470 Proto += " &";
471 break;
472 case RQ_RValue:
473 Proto += " &&";
474 break;
475 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000476 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000477
478 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000479 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000480 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000481 Proto += "...";
482 else
483 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
484 if (I)
485 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000486
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000487 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000488 }
489 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000490 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
491 Proto += " noexcept";
492 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
493 Proto += "(";
494 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000495 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000496 Indentation);
497 EOut.flush();
498 Proto += EOut.str();
499 Proto += ")";
500 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000501 }
502
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000503 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000504 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000505 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000506 if (BMInitializer->isInClassMemberInitializer())
507 continue;
508
509 if (!HasInitializerList) {
510 Proto += " : ";
511 Out << Proto;
512 Proto.clear();
513 HasInitializerList = true;
514 } else
515 Out << ", ";
516
517 if (BMInitializer->isAnyMemberInitializer()) {
518 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000519 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000520 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000521 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000522 }
523
524 Out << "(";
525 if (!BMInitializer->getInit()) {
526 // Nothing to print
527 } else {
528 Expr *Init = BMInitializer->getInit();
529 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
530 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000531
Richard Smith938f40b2011-06-11 17:19:42 +0000532 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000533
534 Expr *SimpleInit = nullptr;
535 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000536 unsigned NumArgs = 0;
537 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
538 Args = ParenList->getExprs();
539 NumArgs = ParenList->getNumExprs();
540 } else if (CXXConstructExpr *Construct
541 = dyn_cast<CXXConstructExpr>(Init)) {
542 Args = Construct->getArgs();
543 NumArgs = Construct->getNumArgs();
544 } else
545 SimpleInit = Init;
546
547 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000548 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000549 else {
550 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000551 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000552 if (isa<CXXDefaultArgExpr>(Args[I]))
553 break;
554
555 if (I)
556 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000557 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000558 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000559 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000560 }
Richard Smith938f40b2011-06-11 17:19:42 +0000561 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000562 if (BMInitializer->isPackExpansion())
563 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000564 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000565 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000566 if (FT && FT->hasTrailingReturn()) {
567 Out << "auto " << Proto << " -> ";
568 Proto.clear();
569 }
Alp Toker314cc812014-01-25 16:55:45 +0000570 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000571 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000572 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000573 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000574 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000575 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000576 }
577
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000578 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000579
580 if (D->isPure())
581 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000582 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000583 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000584 else if (D->isExplicitlyDefaulted())
585 Out << " = default";
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000586 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000587 if (!D->hasPrototype() && D->getNumParams()) {
588 // This is a K&R function definition, so we need to print the
589 // parameters.
590 Out << '\n';
Richard Smith235341b2012-08-16 03:56:14 +0000591 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000592 Indentation += Policy.Indentation;
593 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
594 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000595 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000596 Out << ";\n";
597 }
598 Indentation -= Policy.Indentation;
599 } else
600 Out << ' ';
601
Richard Trieuddd01ce2014-06-09 22:53:25 +0000602 if (D->getBody())
603 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000604 Out << '\n';
605 }
606}
607
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000608void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
609 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000610 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
611 for (unsigned i = 0; i < NumTPLists; ++i)
612 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000613 Out << "friend ";
614 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000615 }
616 else if (FunctionDecl *FD =
617 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
618 Out << "friend ";
619 VisitFunctionDecl(FD);
620 }
621 else if (FunctionTemplateDecl *FTD =
622 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
623 Out << "friend ";
624 VisitFunctionTemplateDecl(FTD);
625 }
626 else if (ClassTemplateDecl *CTD =
627 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
628 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000629 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000630 }
631}
632
Douglas Gregor278f52e2009-05-30 00:08:05 +0000633void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000634 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000635 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000636 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
637 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000638
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000639 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Fariborz Jahanian9e075842013-05-01 17:28:37 +0000640 stream(Policy, D->getName());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000641
642 if (D->isBitField()) {
643 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000644 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000645 }
Richard Smith938f40b2011-06-11 17:19:42 +0000646
647 Expr *Init = D->getInClassInitializer();
648 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000649 if (D->getInClassInitStyle() == ICIS_ListInit)
650 Out << " ";
651 else
652 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000653 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000654 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000655 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000656}
657
Chris Lattnercab02a62011-02-17 20:34:02 +0000658void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000659 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000660}
661
Douglas Gregor278f52e2009-05-30 00:08:05 +0000662void DeclPrinter::VisitVarDecl(VarDecl *D) {
Richard Smithfd3834f2013-04-13 02:43:54 +0000663 if (!Policy.SuppressSpecifiers) {
664 StorageClass SC = D->getStorageClass();
665 if (SC != SC_None)
666 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000667
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000668 switch (D->getTSCSpec()) {
669 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000670 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000671 case TSCS___thread:
672 Out << "__thread ";
673 break;
674 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000675 Out << "_Thread_local ";
676 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000677 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000678 Out << "thread_local ";
679 break;
680 }
681
682 if (D->isModulePrivate())
683 Out << "__module_private__ ";
684 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000685
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000686 QualType T = D->getTypeSourceInfo()
687 ? D->getTypeSourceInfo()->getType()
688 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
Richard Smitha4bb2922014-07-23 03:17:06 +0000689 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000690 Expr *Init = D->getInit();
691 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000692 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000693 if (CXXConstructExpr *Construct =
694 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000695 if (D->getInitStyle() == VarDecl::CallInit &&
696 !Construct->isListInitialization()) {
697 ImplicitInit = Construct->getNumArgs() == 0 ||
698 Construct->getArg(0)->isDefaultArgument();
699 }
700 }
Sebastian Redla9351792012-02-11 23:51:47 +0000701 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000702 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000703 Out << "(";
704 else if (D->getInitStyle() == VarDecl::CInit) {
705 Out << " = ";
706 }
Craig Topper36250ad2014-05-12 05:36:57 +0000707 Init->printPretty(Out, nullptr, Policy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000708 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000709 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000710 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000711 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000712 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000713}
714
715void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
716 VisitVarDecl(D);
717}
718
719void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
720 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000721 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000722 Out << ")";
723}
724
Douglas Gregorba345522011-12-02 23:23:56 +0000725void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000726 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000727 << ";\n";
728}
729
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000730void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
731 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000732 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000733 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000734 D->getMessage()->printPretty(Out, nullptr, Policy, Indentation);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000735 Out << ")";
736}
737
Douglas Gregor278f52e2009-05-30 00:08:05 +0000738//----------------------------------------------------------------------------
739// C++ declarations
740//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000741void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000742 if (D->isInline())
743 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000744 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000745 VisitDeclContext(D);
746 Indent() << "}";
747}
748
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000749void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
750 Out << "using namespace ";
751 if (D->getQualifier())
752 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000753 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000754}
755
Douglas Gregor18231932009-05-30 06:48:27 +0000756void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000757 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000758 if (D->getQualifier())
759 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000760 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000761}
762
Michael Han84324352013-02-22 17:15:32 +0000763void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
764 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000765}
766
Douglas Gregor5f478b72009-05-30 06:58:37 +0000767void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000768 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
769 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000770 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000771 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000772 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000773
John McCallf937c022011-10-07 06:10:15 +0000774 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000775 // Print the base classes
776 if (D->getNumBases()) {
777 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000778 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
779 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000780 if (Base != D->bases_begin())
781 Out << ", ";
782
783 if (Base->isVirtual())
784 Out << "virtual ";
785
Anders Carlsson6df9e072009-08-29 20:36:12 +0000786 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000787 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000788 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000789 Out << " ";
790 }
791 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000792
793 if (Base->isPackExpansion())
794 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000795 }
796 }
797
798 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000799 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000800 Out << " {\n";
801 VisitDeclContext(D);
802 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000803 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000804}
805
806void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
807 const char *l;
808 if (D->getLanguage() == LinkageSpecDecl::lang_c)
809 l = "C";
810 else {
811 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
812 "unknown language in linkage specification");
813 l = "C++";
814 }
815
816 Out << "extern \"" << l << "\" ";
817 if (D->hasBraces()) {
818 Out << "{\n";
819 VisitDeclContext(D);
820 Indent() << "}";
821 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000822 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000823}
824
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000825void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
826 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000827 assert(Params);
828 assert(!Args || Params->size() == Args->size());
829
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000830 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000831
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000832 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
833 if (i != 0)
834 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000835
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000836 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000837 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000838 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000839
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000840 if (TTP->wasDeclaredWithTypename())
841 Out << "typename ";
842 else
843 Out << "class ";
844
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000845 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000846 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000847
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000848 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000849
Richard Trieu82398f92011-07-28 00:19:05 +0000850 if (Args) {
851 Out << " = ";
852 Args->get(i).print(Policy, Out);
853 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000854 Out << " = ";
855 Out << TTP->getDefaultArgument().getAsString(Policy);
856 };
Mike Stump11289f42009-09-09 15:08:12 +0000857 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000858 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000859 StringRef Name;
860 if (IdentifierInfo *II = NTTP->getIdentifier())
861 Name = II->getName();
862 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000863
Richard Trieu82398f92011-07-28 00:19:05 +0000864 if (Args) {
865 Out << " = ";
866 Args->get(i).print(Policy, Out);
867 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000868 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000869 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
870 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000871 }
Richard Smith030f4992011-04-15 13:38:57 +0000872 } else if (const TemplateTemplateParmDecl *TTPD =
873 dyn_cast<TemplateTemplateParmDecl>(Param)) {
874 VisitTemplateDecl(TTPD);
875 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000876 }
877 }
Mike Stump11289f42009-09-09 15:08:12 +0000878
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000879 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000880}
881
882void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
883 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000884
Richard Smith030f4992011-04-15 13:38:57 +0000885 if (const TemplateTemplateParmDecl *TTP =
886 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000887 Out << "class ";
888 if (TTP->isParameterPack())
889 Out << "...";
890 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000891 } else {
892 Visit(D->getTemplatedDecl());
893 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000894}
895
Richard Trieu82398f92011-07-28 00:19:05 +0000896void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
897 if (PrintInstantiation) {
898 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000899 for (auto *I : D->specializations()) {
900 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
901 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000902 }
903 }
904
905 return VisitRedeclarableTemplateDecl(D);
906}
907
908void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
909 if (PrintInstantiation) {
910 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000911 for (auto *I : D->specializations()) {
912 PrintTemplateParameters(Params, &I->getTemplateArgs());
913 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000914 Out << '\n';
915 }
916 }
917
918 return VisitRedeclarableTemplateDecl(D);
919}
920
Douglas Gregor278f52e2009-05-30 00:08:05 +0000921//----------------------------------------------------------------------------
922// Objective-C declarations
923//----------------------------------------------------------------------------
924
Douglas Gregor278f52e2009-05-30 00:08:05 +0000925void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
926 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000927 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +0000928 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000929 Out << "+ ";
Alp Toker314cc812014-01-25 16:55:45 +0000930 if (!OMD->getReturnType().isNull())
931 Out << '(' << OMD->getASTContext()
932 .getUnqualifiedObjCPointerType(OMD->getReturnType())
933 .getAsString(Policy) << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000934
Douglas Gregor278f52e2009-05-30 00:08:05 +0000935 std::string name = OMD->getSelector().getAsString();
936 std::string::size_type pos, lastPos = 0;
Aaron Ballman43b68be2014-03-07 17:50:17 +0000937 for (const auto *PI : OMD->params()) {
Mike Stump11289f42009-09-09 15:08:12 +0000938 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000939 pos = name.find_first_of(':', lastPos);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000940 Out << " " << name.substr(lastPos, pos - lastPos);
Aaron Ballman43b68be2014-03-07 17:50:17 +0000941 Out << ":(" << PI->getASTContext().getUnqualifiedObjCPointerType(PI->getType()).
942 getAsString(Policy) << ')' << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000943 lastPos = pos + 1;
944 }
Mike Stump11289f42009-09-09 15:08:12 +0000945
Douglas Gregor278f52e2009-05-30 00:08:05 +0000946 if (OMD->param_begin() == OMD->param_end())
947 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +0000948
Douglas Gregor278f52e2009-05-30 00:08:05 +0000949 if (OMD->isVariadic())
950 Out << ", ...";
Mike Stump11289f42009-09-09 15:08:12 +0000951
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000952 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000953 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +0000954 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000955 Out << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000956 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000957 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +0000958 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000959}
960
961void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
962 std::string I = OID->getNameAsString();
963 ObjCInterfaceDecl *SID = OID->getSuperClass();
964
965 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000966 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000967 else
968 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +0000969
970 if (OID->ivar_size() > 0) {
971 Out << "{\n";
972 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +0000973 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000974 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +0000975 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +0000976 }
977 Indentation -= Policy.Indentation;
978 Out << "}\n";
979 }
Douglas Gregor36098ff2009-05-30 00:56:08 +0000980 VisitDeclContext(OID, false);
981 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000982}
983
984void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
985 std::string I = OID->getNameAsString();
986 ObjCInterfaceDecl *SID = OID->getSuperClass();
987
Douglas Gregordc9166c2011-12-15 20:29:51 +0000988 if (!OID->isThisDeclarationADefinition()) {
989 Out << "@class " << I << ";";
990 return;
991 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000992 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000993 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000994 Out << "@interface " << I << " : " << *SID;
Douglas Gregor1c283312010-08-11 12:19:30 +0000995 else
996 Out << "@interface " << I;
Mike Stump11289f42009-09-09 15:08:12 +0000997
Douglas Gregor278f52e2009-05-30 00:08:05 +0000998 // Protocols?
999 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1000 if (!Protocols.empty()) {
1001 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1002 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001003 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001004 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001005 }
Mike Stump11289f42009-09-09 15:08:12 +00001006
Douglas Gregor278f52e2009-05-30 00:08:05 +00001007 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001008 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001009 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001010 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001011 for (const auto *I : OID->ivars()) {
1012 Indent() << I->getASTContext()
1013 .getUnqualifiedObjCPointerType(I->getType())
1014 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001015 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001016 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001017 Out << "}\n";
1018 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001019 else if (SID) {
1020 Out << "\n";
1021 eolnOut = true;
1022 }
Mike Stump11289f42009-09-09 15:08:12 +00001023
Douglas Gregor278f52e2009-05-30 00:08:05 +00001024 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001025 if (!eolnOut)
1026 Out << ' ';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001027 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001028 // FIXME: implement the rest...
1029}
1030
Douglas Gregor278f52e2009-05-30 00:08:05 +00001031void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001032 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001033 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001034 return;
1035 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001036 // Protocols?
1037 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1038 if (!Protocols.empty()) {
1039 Out << "@protocol " << *PID;
1040 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1041 E = Protocols.end(); I != E; ++I)
1042 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1043 Out << ">\n";
1044 } else
1045 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001046 VisitDeclContext(PID, false);
1047 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001048}
1049
1050void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001051 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001052
1053 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001054 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001055 // FIXME: implement the rest...
1056}
1057
1058void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001059 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001060 if (PID->ivar_size() > 0) {
1061 Out << "{\n";
1062 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001063 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001064 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001065 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001066 Indentation -= Policy.Indentation;
1067 Out << "}\n";
1068 }
1069
Douglas Gregor278f52e2009-05-30 00:08:05 +00001070 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001071 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001072
Douglas Gregor278f52e2009-05-30 00:08:05 +00001073 // FIXME: implement the rest...
1074}
1075
1076void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001077 Out << "@compatibility_alias " << *AID
1078 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001079}
1080
1081/// PrintObjCPropertyDecl - print a property declaration.
1082///
1083void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1084 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1085 Out << "@required\n";
1086 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1087 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001088
Douglas Gregor278f52e2009-05-30 00:08:05 +00001089 Out << "@property";
1090 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1091 bool first = true;
1092 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001093 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001094 ObjCPropertyDecl::OBJC_PR_readonly) {
1095 Out << (first ? ' ' : ',') << "readonly";
1096 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001097 }
Mike Stump11289f42009-09-09 15:08:12 +00001098
Ted Kremenek897af912011-08-17 21:09:35 +00001099 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001100 Out << (first ? ' ' : ',') << "getter = ";
1101 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001102 first = false;
1103 }
1104 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001105 Out << (first ? ' ' : ',') << "setter = ";
1106 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001107 first = false;
1108 }
Mike Stump11289f42009-09-09 15:08:12 +00001109
Ted Kremenek897af912011-08-17 21:09:35 +00001110 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1111 Out << (first ? ' ' : ',') << "assign";
1112 first = false;
1113 }
Mike Stump11289f42009-09-09 15:08:12 +00001114
Ted Kremenek897af912011-08-17 21:09:35 +00001115 if (PDecl->getPropertyAttributes() &
1116 ObjCPropertyDecl::OBJC_PR_readwrite) {
1117 Out << (first ? ' ' : ',') << "readwrite";
1118 first = false;
1119 }
Mike Stump11289f42009-09-09 15:08:12 +00001120
Ted Kremenek897af912011-08-17 21:09:35 +00001121 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1122 Out << (first ? ' ' : ',') << "retain";
1123 first = false;
1124 }
Mike Stump11289f42009-09-09 15:08:12 +00001125
Ted Kremenek897af912011-08-17 21:09:35 +00001126 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1127 Out << (first ? ' ' : ',') << "strong";
1128 first = false;
1129 }
John McCall31168b02011-06-15 23:02:42 +00001130
Ted Kremenek897af912011-08-17 21:09:35 +00001131 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1132 Out << (first ? ' ' : ',') << "copy";
1133 first = false;
1134 }
Mike Stump11289f42009-09-09 15:08:12 +00001135
Ted Kremenek897af912011-08-17 21:09:35 +00001136 if (PDecl->getPropertyAttributes() &
1137 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1138 Out << (first ? ' ' : ',') << "nonatomic";
1139 first = false;
1140 }
1141 if (PDecl->getPropertyAttributes() &
1142 ObjCPropertyDecl::OBJC_PR_atomic) {
1143 Out << (first ? ' ' : ',') << "atomic";
1144 first = false;
1145 }
1146
1147 (void) first; // Silence dead store warning due to idiomatic code.
1148 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001149 }
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001150 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(PDecl->getType()).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001151 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001152 if (Policy.PolishForDeclaration)
1153 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001154}
1155
1156void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1157 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001158 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001159 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001160 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001161 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001162 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001163 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001164}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001165
1166void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001167 if (!D->isAccessDeclaration())
1168 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001169 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001170 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001171 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001172 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001173}
1174
John McCalle61f2ba2009-11-18 02:36:19 +00001175void
1176DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1177 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001178 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001179 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001180}
1181
1182void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001183 if (!D->isAccessDeclaration())
1184 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001185 D->getQualifier()->print(Out, Policy);
Fariborz Jahanian3ec39212012-12-06 00:09:40 +00001186 Out << D->getName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001187}
John McCall3f746822009-11-17 05:59:44 +00001188
1189void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1190 // ignore
1191}
Alexey Bataeva769e072013-03-22 06:34:35 +00001192
1193void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1194 Out << "#pragma omp threadprivate";
1195 if (!D->varlist_empty()) {
1196 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1197 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001198 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001199 Out << (I == D->varlist_begin() ? '(' : ',');
1200 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1201 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001202 }
1203 Out << ")";
1204 }
1205}
1206