blob: 558654d88759648e15fbf84fab0ab5b273438101 [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);
Douglas Gregor278f52e2009-05-30 00:08:05 +000090 };
91}
92
Richard Trieu82398f92011-07-28 00:19:05 +000093void Decl::print(raw_ostream &Out, unsigned Indentation,
94 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +000095 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +000096}
97
Chris Lattner0e62c1c2011-07-23 10:55:15 +000098void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +000099 unsigned Indentation, bool PrintInstantiation) const {
Richard Smith235341b2012-08-16 03:56:14 +0000100 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000101 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000102}
103
Eli Friedman79635842009-05-30 04:20:30 +0000104static QualType GetBaseType(QualType T) {
105 // FIXME: This should be on the Type class!
106 QualType BaseType = T;
107 while (!BaseType->isSpecifierType()) {
108 if (isa<TypedefType>(BaseType))
109 break;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000110 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000111 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000112 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
113 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000114 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
115 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000116 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000117 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000118 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000119 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000120 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
121 BaseType = RTy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000122 else
David Blaikie83d382b2011-09-23 05:06:16 +0000123 llvm_unreachable("Unknown declarator!");
Eli Friedman79635842009-05-30 04:20:30 +0000124 }
125 return BaseType;
126}
127
128static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000129 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000130 return TDD->getUnderlyingType();
131 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
132 return VD->getType();
133 return QualType();
134}
135
136void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000137 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000138 unsigned Indentation) {
139 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000140 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000141 return;
142 }
143
144 Decl** End = Begin + NumDecls;
145 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
146 if (TD)
147 ++Begin;
148
149 PrintingPolicy SubPolicy(Policy);
John McCallf937c022011-10-07 06:10:15 +0000150 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000151 TD->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000152 Out << " ";
153 SubPolicy.SuppressTag = true;
154 }
155
156 bool isFirst = true;
157 for ( ; Begin != End; ++Begin) {
158 if (isFirst) {
159 SubPolicy.SuppressSpecifiers = false;
160 isFirst = false;
161 } else {
162 if (!isFirst) Out << ", ";
163 SubPolicy.SuppressSpecifiers = true;
164 }
165
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000166 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000167 }
168}
169
Alp Tokeref6b0072014-01-04 13:47:14 +0000170LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000171 // Get the translation unit
172 const DeclContext *DC = this;
173 while (!DC->isTranslationUnit())
174 DC = DC->getParent();
175
176 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith235341b2012-08-16 03:56:14 +0000177 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000178 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
179}
180
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000181raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000182 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000183 Out << " ";
184 return Out;
185}
186
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000187void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000188 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000189 return;
190
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000191 if (D->hasAttrs()) {
192 AttrVec &Attrs = D->getAttrs();
193 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
Richard Smith52f04a22012-08-16 02:43:29 +0000194 Attr *A = *i;
195 A->printPretty(Out, Policy);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000196 }
197 }
198}
199
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000200void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000201 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000202 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000203 Out << ";\n";
204 Decls.clear();
205
206}
207
Anders Carlsson601d6e42009-08-28 22:39:52 +0000208void DeclPrinter::Print(AccessSpecifier AS) {
209 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000210 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000211 case AS_public: Out << "public"; break;
212 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000213 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000214 }
215}
216
Douglas Gregor278f52e2009-05-30 00:08:05 +0000217//----------------------------------------------------------------------------
218// Common C declarations
219//----------------------------------------------------------------------------
220
221void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000222 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000223 return;
224
Douglas Gregor278f52e2009-05-30 00:08:05 +0000225 if (Indent)
226 Indentation += Policy.Indentation;
227
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000228 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000229 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000230 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000231
232 // Don't print ObjCIvarDecls, as they are printed when visiting the
233 // containing ObjCInterfaceDecl.
234 if (isa<ObjCIvarDecl>(*D))
235 continue;
236
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000237 // Skip over implicit declarations in pretty-printing mode.
238 if (D->isImplicit())
239 continue;
240
Eli Friedman79635842009-05-30 04:20:30 +0000241 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
242 // forced to merge the declarations because there's no other way to
243 // refer to the struct in question. This limited merging is safe without
244 // a bunch of other checks because it only merges declarations directly
245 // referring to the tag, not typedefs.
246 //
247 // Check whether the current declaration should be grouped with a previous
248 // unnamed struct.
249 QualType CurDeclType = getDeclType(*D);
250 if (!Decls.empty() && !CurDeclType.isNull()) {
251 QualType BaseType = GetBaseType(CurDeclType);
Eli Friedman24b3fed2013-08-12 21:54:04 +0000252 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
253 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
Eli Friedman79635842009-05-30 04:20:30 +0000254 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
255 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
256 Decls.push_back(*D);
257 continue;
258 }
259 }
260
261 // If we have a merged group waiting to be handled, handle it now.
262 if (!Decls.empty())
263 ProcessDeclGroup(Decls);
264
265 // If the current declaration is an unnamed tag type, save it
266 // so we can merge it with the subsequent declaration(s) using it.
267 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
268 Decls.push_back(*D);
269 continue;
270 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000271
272 if (isa<AccessSpecDecl>(*D)) {
273 Indentation -= Policy.Indentation;
274 this->Indent();
275 Print(D->getAccess());
276 Out << ":\n";
277 Indentation += Policy.Indentation;
278 continue;
279 }
280
Douglas Gregor278f52e2009-05-30 00:08:05 +0000281 this->Indent();
282 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000283
284 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000285 const char *Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000286 if (isa<OMPThreadPrivateDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000287 Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000288 else if (isa<FunctionDecl>(*D) &&
289 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
Yaron Keren51db8772014-05-07 09:53:02 +0000290 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000291 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000292 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000293 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000294 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000295 isa<ObjCInterfaceDecl>(*D) ||
296 isa<ObjCProtocolDecl>(*D) ||
297 isa<ObjCCategoryImplDecl>(*D) ||
298 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000299 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000300 else if (isa<EnumConstantDecl>(*D)) {
301 DeclContext::decl_iterator Next = D;
302 ++Next;
303 if (Next != DEnd)
304 Terminator = ",";
305 } else
306 Terminator = ";";
307
308 if (Terminator)
309 Out << Terminator;
310 Out << "\n";
311 }
312
Eli Friedman79635842009-05-30 04:20:30 +0000313 if (!Decls.empty())
314 ProcessDeclGroup(Decls);
315
Douglas Gregor278f52e2009-05-30 00:08:05 +0000316 if (Indent)
317 Indentation -= Policy.Indentation;
318}
319
320void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
321 VisitDeclContext(D, false);
322}
323
324void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000325 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000326 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000327
328 if (D->isModulePrivate())
329 Out << "__module_private__ ";
330 }
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000331 D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000332 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000333}
334
Richard Smithdda56e42011-04-15 14:24:37 +0000335void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000336 Out << "using " << *D;
337 prettyPrintAttributes(D);
338 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000339}
340
Douglas Gregor278f52e2009-05-30 00:08:05 +0000341void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000342 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
343 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000344 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000345 if (D->isScoped()) {
346 if (D->isScopedUsingClassTag())
347 Out << "class ";
348 else
349 Out << "struct ";
350 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000351 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000352
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000353 if (D->isFixed())
354 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000355
John McCallf937c022011-10-07 06:10:15 +0000356 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000357 Out << " {\n";
358 VisitDeclContext(D);
359 Indent() << "}";
360 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000361 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000362}
363
364void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000365 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
366 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000367 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000368 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000369 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000370
John McCallf937c022011-10-07 06:10:15 +0000371 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000372 Out << " {\n";
373 VisitDeclContext(D);
374 Indent() << "}";
375 }
376}
377
378void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000379 Out << *D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000380 if (Expr *Init = D->getInitExpr()) {
381 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000382 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000383 }
384}
385
Mike Stump11289f42009-09-09 15:08:12 +0000386void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000387 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000388 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000389 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000390 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000391 case SC_None: break;
392 case SC_Extern: Out << "extern "; break;
393 case SC_Static: Out << "static "; break;
394 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000395 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
396 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000397 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000398
Douglas Gregor26701a42011-09-09 02:06:17 +0000399 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000400 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000401 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000402 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000403 if ((CDecl && CDecl->isExplicitSpecified()) ||
404 (ConversionDecl && ConversionDecl->isExplicit()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000405 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000406 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000407
Douglas Gregor2d042f12009-05-30 05:39:39 +0000408 PrintingPolicy SubPolicy(Policy);
409 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000410 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000411
Abramo Bagnara6d810632010-12-14 22:11:44 +0000412 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000413 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000414 Proto = '(' + Proto + ')';
415 Ty = PT->getInnerType();
416 }
417
Reid Kleckner0503a872013-12-05 01:23:43 +0000418 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000419 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000420 if (D->hasWrittenPrototype())
421 FT = dyn_cast<FunctionProtoType>(AFT);
422
423 Proto += "(";
424 if (FT) {
425 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000426 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000427 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
428 if (i) POut << ", ";
429 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
430 }
Mike Stump11289f42009-09-09 15:08:12 +0000431
Douglas Gregor278f52e2009-05-30 00:08:05 +0000432 if (FT->isVariadic()) {
433 if (D->getNumParams()) POut << ", ";
434 POut << "...";
435 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000436 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000437 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
438 if (i)
439 Proto += ", ";
440 Proto += D->getParamDecl(i)->getNameAsString();
441 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000442 }
443
444 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000445
David Blaikief5697e52012-08-10 00:55:35 +0000446 if (FT) {
447 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000448 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000449 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000450 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000451 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000452 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000453
454 switch (FT->getRefQualifier()) {
455 case RQ_None:
456 break;
457 case RQ_LValue:
458 Proto += " &";
459 break;
460 case RQ_RValue:
461 Proto += " &&";
462 break;
463 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000464 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000465
466 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000467 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000468 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000469 Proto += "...";
470 else
471 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
472 if (I)
473 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000474
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000475 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000476 }
477 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000478 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
479 Proto += " noexcept";
480 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
481 Proto += "(";
482 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000483 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000484 Indentation);
485 EOut.flush();
486 Proto += EOut.str();
487 Proto += ")";
488 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000489 }
490
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000491 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000492 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000493 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000494 if (BMInitializer->isInClassMemberInitializer())
495 continue;
496
497 if (!HasInitializerList) {
498 Proto += " : ";
499 Out << Proto;
500 Proto.clear();
501 HasInitializerList = true;
502 } else
503 Out << ", ";
504
505 if (BMInitializer->isAnyMemberInitializer()) {
506 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000507 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000508 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000509 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000510 }
511
512 Out << "(";
513 if (!BMInitializer->getInit()) {
514 // Nothing to print
515 } else {
516 Expr *Init = BMInitializer->getInit();
517 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
518 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000519
Richard Smith938f40b2011-06-11 17:19:42 +0000520 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000521
522 Expr *SimpleInit = nullptr;
523 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000524 unsigned NumArgs = 0;
525 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
526 Args = ParenList->getExprs();
527 NumArgs = ParenList->getNumExprs();
528 } else if (CXXConstructExpr *Construct
529 = dyn_cast<CXXConstructExpr>(Init)) {
530 Args = Construct->getArgs();
531 NumArgs = Construct->getNumArgs();
532 } else
533 SimpleInit = Init;
534
535 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000536 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000537 else {
538 for (unsigned I = 0; I != NumArgs; ++I) {
539 if (isa<CXXDefaultArgExpr>(Args[I]))
540 break;
541
542 if (I)
543 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000544 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000545 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000546 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000547 }
Richard Smith938f40b2011-06-11 17:19:42 +0000548 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000549 if (BMInitializer->isPackExpansion())
550 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000551 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000552 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000553 if (FT && FT->hasTrailingReturn()) {
554 Out << "auto " << Proto << " -> ";
555 Proto.clear();
556 }
Alp Toker314cc812014-01-25 16:55:45 +0000557 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000558 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000559 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000560 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000561 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000562 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000563 }
564
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000565 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000566
567 if (D->isPure())
568 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000569 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000570 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000571 else if (D->isExplicitlyDefaulted())
572 Out << " = default";
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000573 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000574 if (!D->hasPrototype() && D->getNumParams()) {
575 // This is a K&R function definition, so we need to print the
576 // parameters.
577 Out << '\n';
Richard Smith235341b2012-08-16 03:56:14 +0000578 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000579 Indentation += Policy.Indentation;
580 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
581 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000582 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000583 Out << ";\n";
584 }
585 Indentation -= Policy.Indentation;
586 } else
587 Out << ' ';
588
Craig Topper36250ad2014-05-12 05:36:57 +0000589 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000590 Out << '\n';
591 }
592}
593
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000594void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
595 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000596 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
597 for (unsigned i = 0; i < NumTPLists; ++i)
598 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000599 Out << "friend ";
600 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000601 }
602 else if (FunctionDecl *FD =
603 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
604 Out << "friend ";
605 VisitFunctionDecl(FD);
606 }
607 else if (FunctionTemplateDecl *FTD =
608 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
609 Out << "friend ";
610 VisitFunctionTemplateDecl(FTD);
611 }
612 else if (ClassTemplateDecl *CTD =
613 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
614 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000615 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000616 }
617}
618
Douglas Gregor278f52e2009-05-30 00:08:05 +0000619void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000620 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000621 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000622 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
623 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000624
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000625 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Fariborz Jahanian9e075842013-05-01 17:28:37 +0000626 stream(Policy, D->getName());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000627
628 if (D->isBitField()) {
629 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000630 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000631 }
Richard Smith938f40b2011-06-11 17:19:42 +0000632
633 Expr *Init = D->getInClassInitializer();
634 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000635 if (D->getInClassInitStyle() == ICIS_ListInit)
636 Out << " ";
637 else
638 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000639 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000640 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000641 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000642}
643
Chris Lattnercab02a62011-02-17 20:34:02 +0000644void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000645 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000646}
647
648
Douglas Gregor278f52e2009-05-30 00:08:05 +0000649void DeclPrinter::VisitVarDecl(VarDecl *D) {
Richard Smithfd3834f2013-04-13 02:43:54 +0000650 if (!Policy.SuppressSpecifiers) {
651 StorageClass SC = D->getStorageClass();
652 if (SC != SC_None)
653 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000654
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000655 switch (D->getTSCSpec()) {
656 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000657 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000658 case TSCS___thread:
659 Out << "__thread ";
660 break;
661 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000662 Out << "_Thread_local ";
663 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000664 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000665 Out << "thread_local ";
666 break;
667 }
668
669 if (D->isModulePrivate())
670 Out << "__module_private__ ";
671 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000672
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000673 QualType T = D->getTypeSourceInfo()
674 ? D->getTypeSourceInfo()->getType()
675 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000676 T.print(Out, Policy, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000677 Expr *Init = D->getInit();
678 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000679 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000680 if (CXXConstructExpr *Construct =
681 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000682 if (D->getInitStyle() == VarDecl::CallInit &&
683 !Construct->isListInitialization()) {
684 ImplicitInit = Construct->getNumArgs() == 0 ||
685 Construct->getArg(0)->isDefaultArgument();
686 }
687 }
Sebastian Redla9351792012-02-11 23:51:47 +0000688 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000689 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000690 Out << "(";
691 else if (D->getInitStyle() == VarDecl::CInit) {
692 Out << " = ";
693 }
Craig Topper36250ad2014-05-12 05:36:57 +0000694 Init->printPretty(Out, nullptr, Policy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000695 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000696 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000697 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000698 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000699 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000700}
701
702void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
703 VisitVarDecl(D);
704}
705
706void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
707 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000708 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000709 Out << ")";
710}
711
Douglas Gregorba345522011-12-02 23:23:56 +0000712void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000713 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000714 << ";\n";
715}
716
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000717void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
718 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000719 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000720 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000721 D->getMessage()->printPretty(Out, nullptr, Policy, Indentation);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000722 Out << ")";
723}
724
Douglas Gregor278f52e2009-05-30 00:08:05 +0000725//----------------------------------------------------------------------------
726// C++ declarations
727//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000728void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000729 if (D->isInline())
730 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000731 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000732 VisitDeclContext(D);
733 Indent() << "}";
734}
735
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000736void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
737 Out << "using namespace ";
738 if (D->getQualifier())
739 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000740 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000741}
742
Douglas Gregor18231932009-05-30 06:48:27 +0000743void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000744 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000745 if (D->getQualifier())
746 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000747 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000748}
749
Michael Han84324352013-02-22 17:15:32 +0000750void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
751 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000752}
753
Douglas Gregor5f478b72009-05-30 06:58:37 +0000754void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000755 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
756 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000757 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000758 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000759 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000760
John McCallf937c022011-10-07 06:10:15 +0000761 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000762 // Print the base classes
763 if (D->getNumBases()) {
764 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000765 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
766 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000767 if (Base != D->bases_begin())
768 Out << ", ";
769
770 if (Base->isVirtual())
771 Out << "virtual ";
772
Anders Carlsson6df9e072009-08-29 20:36:12 +0000773 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
774 if (AS != AS_none)
775 Print(AS);
Anders Carlsson601d6e42009-08-28 22:39:52 +0000776 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000777
778 if (Base->isPackExpansion())
779 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000780 }
781 }
782
783 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000784 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000785 Out << " {\n";
786 VisitDeclContext(D);
787 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000788 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000789}
790
791void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
792 const char *l;
793 if (D->getLanguage() == LinkageSpecDecl::lang_c)
794 l = "C";
795 else {
796 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
797 "unknown language in linkage specification");
798 l = "C++";
799 }
800
801 Out << "extern \"" << l << "\" ";
802 if (D->hasBraces()) {
803 Out << "{\n";
804 VisitDeclContext(D);
805 Indent() << "}";
806 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000807 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000808}
809
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000810void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
811 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000812 assert(Params);
813 assert(!Args || Params->size() == Args->size());
814
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000815 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000816
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000817 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
818 if (i != 0)
819 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000820
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000821 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000822 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000823 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000824
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000825 if (TTP->wasDeclaredWithTypename())
826 Out << "typename ";
827 else
828 Out << "class ";
829
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000830 if (TTP->isParameterPack())
831 Out << "... ";
Mike Stump11289f42009-09-09 15:08:12 +0000832
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000833 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000834
Richard Trieu82398f92011-07-28 00:19:05 +0000835 if (Args) {
836 Out << " = ";
837 Args->get(i).print(Policy, Out);
838 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000839 Out << " = ";
840 Out << TTP->getDefaultArgument().getAsString(Policy);
841 };
Mike Stump11289f42009-09-09 15:08:12 +0000842 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000843 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
844 Out << NTTP->getType().getAsString(Policy);
845
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000846 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
847 Out << "...";
848
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000849 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
850 Out << ' ';
851 Out << Name->getName();
852 }
Mike Stump11289f42009-09-09 15:08:12 +0000853
Richard Trieu82398f92011-07-28 00:19:05 +0000854 if (Args) {
855 Out << " = ";
856 Args->get(i).print(Policy, Out);
857 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000858 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000859 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
860 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000861 }
Richard Smith030f4992011-04-15 13:38:57 +0000862 } else if (const TemplateTemplateParmDecl *TTPD =
863 dyn_cast<TemplateTemplateParmDecl>(Param)) {
864 VisitTemplateDecl(TTPD);
865 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000866 }
867 }
Mike Stump11289f42009-09-09 15:08:12 +0000868
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000869 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000870}
871
872void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
873 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000874
Richard Smith030f4992011-04-15 13:38:57 +0000875 if (const TemplateTemplateParmDecl *TTP =
876 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000877 Out << "class ";
878 if (TTP->isParameterPack())
879 Out << "...";
880 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000881 } else {
882 Visit(D->getTemplatedDecl());
883 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000884}
885
Richard Trieu82398f92011-07-28 00:19:05 +0000886void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
887 if (PrintInstantiation) {
888 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000889 for (auto *I : D->specializations()) {
890 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
891 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000892 }
893 }
894
895 return VisitRedeclarableTemplateDecl(D);
896}
897
898void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
899 if (PrintInstantiation) {
900 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000901 for (auto *I : D->specializations()) {
902 PrintTemplateParameters(Params, &I->getTemplateArgs());
903 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000904 Out << '\n';
905 }
906 }
907
908 return VisitRedeclarableTemplateDecl(D);
909}
910
Douglas Gregor278f52e2009-05-30 00:08:05 +0000911//----------------------------------------------------------------------------
912// Objective-C declarations
913//----------------------------------------------------------------------------
914
Douglas Gregor278f52e2009-05-30 00:08:05 +0000915void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
916 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000917 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +0000918 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000919 Out << "+ ";
Alp Toker314cc812014-01-25 16:55:45 +0000920 if (!OMD->getReturnType().isNull())
921 Out << '(' << OMD->getASTContext()
922 .getUnqualifiedObjCPointerType(OMD->getReturnType())
923 .getAsString(Policy) << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000924
Douglas Gregor278f52e2009-05-30 00:08:05 +0000925 std::string name = OMD->getSelector().getAsString();
926 std::string::size_type pos, lastPos = 0;
Aaron Ballman43b68be2014-03-07 17:50:17 +0000927 for (const auto *PI : OMD->params()) {
Mike Stump11289f42009-09-09 15:08:12 +0000928 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000929 pos = name.find_first_of(':', lastPos);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000930 Out << " " << name.substr(lastPos, pos - lastPos);
Aaron Ballman43b68be2014-03-07 17:50:17 +0000931 Out << ":(" << PI->getASTContext().getUnqualifiedObjCPointerType(PI->getType()).
932 getAsString(Policy) << ')' << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000933 lastPos = pos + 1;
934 }
Mike Stump11289f42009-09-09 15:08:12 +0000935
Douglas Gregor278f52e2009-05-30 00:08:05 +0000936 if (OMD->param_begin() == OMD->param_end())
937 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +0000938
Douglas Gregor278f52e2009-05-30 00:08:05 +0000939 if (OMD->isVariadic())
940 Out << ", ...";
Mike Stump11289f42009-09-09 15:08:12 +0000941
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000942 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000943 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +0000944 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000945 Out << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000946 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000947 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +0000948 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000949}
950
951void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
952 std::string I = OID->getNameAsString();
953 ObjCInterfaceDecl *SID = OID->getSuperClass();
954
955 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000956 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000957 else
958 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +0000959
960 if (OID->ivar_size() > 0) {
961 Out << "{\n";
962 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +0000963 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000964 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +0000965 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +0000966 }
967 Indentation -= Policy.Indentation;
968 Out << "}\n";
969 }
Douglas Gregor36098ff2009-05-30 00:56:08 +0000970 VisitDeclContext(OID, false);
971 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000972}
973
974void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
975 std::string I = OID->getNameAsString();
976 ObjCInterfaceDecl *SID = OID->getSuperClass();
977
Douglas Gregordc9166c2011-12-15 20:29:51 +0000978 if (!OID->isThisDeclarationADefinition()) {
979 Out << "@class " << I << ";";
980 return;
981 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000982 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000983 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000984 Out << "@interface " << I << " : " << *SID;
Douglas Gregor1c283312010-08-11 12:19:30 +0000985 else
986 Out << "@interface " << I;
Mike Stump11289f42009-09-09 15:08:12 +0000987
Douglas Gregor278f52e2009-05-30 00:08:05 +0000988 // Protocols?
989 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
990 if (!Protocols.empty()) {
991 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
992 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000993 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000994 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000995 }
Mike Stump11289f42009-09-09 15:08:12 +0000996
Douglas Gregor278f52e2009-05-30 00:08:05 +0000997 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000998 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000999 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001000 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001001 for (const auto *I : OID->ivars()) {
1002 Indent() << I->getASTContext()
1003 .getUnqualifiedObjCPointerType(I->getType())
1004 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001005 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001006 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001007 Out << "}\n";
1008 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001009 else if (SID) {
1010 Out << "\n";
1011 eolnOut = true;
1012 }
Mike Stump11289f42009-09-09 15:08:12 +00001013
Douglas Gregor278f52e2009-05-30 00:08:05 +00001014 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001015 if (!eolnOut)
1016 Out << ' ';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001017 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001018 // FIXME: implement the rest...
1019}
1020
Douglas Gregor278f52e2009-05-30 00:08:05 +00001021void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001022 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001023 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001024 return;
1025 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001026 // Protocols?
1027 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1028 if (!Protocols.empty()) {
1029 Out << "@protocol " << *PID;
1030 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1031 E = Protocols.end(); I != E; ++I)
1032 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1033 Out << ">\n";
1034 } else
1035 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001036 VisitDeclContext(PID, false);
1037 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001038}
1039
1040void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001041 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001042
1043 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001044 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001045 // FIXME: implement the rest...
1046}
1047
1048void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001049 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001050 if (PID->ivar_size() > 0) {
1051 Out << "{\n";
1052 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001053 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001054 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001055 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001056 Indentation -= Policy.Indentation;
1057 Out << "}\n";
1058 }
1059
Douglas Gregor278f52e2009-05-30 00:08:05 +00001060 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001061 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001062
Douglas Gregor278f52e2009-05-30 00:08:05 +00001063 // FIXME: implement the rest...
1064}
1065
1066void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001067 Out << "@compatibility_alias " << *AID
1068 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001069}
1070
1071/// PrintObjCPropertyDecl - print a property declaration.
1072///
1073void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1074 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1075 Out << "@required\n";
1076 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1077 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001078
Douglas Gregor278f52e2009-05-30 00:08:05 +00001079 Out << "@property";
1080 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1081 bool first = true;
1082 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001083 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001084 ObjCPropertyDecl::OBJC_PR_readonly) {
1085 Out << (first ? ' ' : ',') << "readonly";
1086 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001087 }
Mike Stump11289f42009-09-09 15:08:12 +00001088
Ted Kremenek897af912011-08-17 21:09:35 +00001089 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001090 Out << (first ? ' ' : ',') << "getter = ";
1091 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001092 first = false;
1093 }
1094 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001095 Out << (first ? ' ' : ',') << "setter = ";
1096 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001097 first = false;
1098 }
Mike Stump11289f42009-09-09 15:08:12 +00001099
Ted Kremenek897af912011-08-17 21:09:35 +00001100 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1101 Out << (first ? ' ' : ',') << "assign";
1102 first = false;
1103 }
Mike Stump11289f42009-09-09 15:08:12 +00001104
Ted Kremenek897af912011-08-17 21:09:35 +00001105 if (PDecl->getPropertyAttributes() &
1106 ObjCPropertyDecl::OBJC_PR_readwrite) {
1107 Out << (first ? ' ' : ',') << "readwrite";
1108 first = false;
1109 }
Mike Stump11289f42009-09-09 15:08:12 +00001110
Ted Kremenek897af912011-08-17 21:09:35 +00001111 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1112 Out << (first ? ' ' : ',') << "retain";
1113 first = false;
1114 }
Mike Stump11289f42009-09-09 15:08:12 +00001115
Ted Kremenek897af912011-08-17 21:09:35 +00001116 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1117 Out << (first ? ' ' : ',') << "strong";
1118 first = false;
1119 }
John McCall31168b02011-06-15 23:02:42 +00001120
Ted Kremenek897af912011-08-17 21:09:35 +00001121 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1122 Out << (first ? ' ' : ',') << "copy";
1123 first = false;
1124 }
Mike Stump11289f42009-09-09 15:08:12 +00001125
Ted Kremenek897af912011-08-17 21:09:35 +00001126 if (PDecl->getPropertyAttributes() &
1127 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1128 Out << (first ? ' ' : ',') << "nonatomic";
1129 first = false;
1130 }
1131 if (PDecl->getPropertyAttributes() &
1132 ObjCPropertyDecl::OBJC_PR_atomic) {
1133 Out << (first ? ' ' : ',') << "atomic";
1134 first = false;
1135 }
1136
1137 (void) first; // Silence dead store warning due to idiomatic code.
1138 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001139 }
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001140 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(PDecl->getType()).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001141 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001142 if (Policy.PolishForDeclaration)
1143 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001144}
1145
1146void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1147 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001148 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001149 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001150 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001151 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001152 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001153 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001154}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001155
1156void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001157 if (!D->isAccessDeclaration())
1158 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001159 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001160 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001161 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001162 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001163}
1164
John McCalle61f2ba2009-11-18 02:36:19 +00001165void
1166DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1167 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001168 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001169 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001170}
1171
1172void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001173 if (!D->isAccessDeclaration())
1174 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001175 D->getQualifier()->print(Out, Policy);
Fariborz Jahanian3ec39212012-12-06 00:09:40 +00001176 Out << D->getName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001177}
John McCall3f746822009-11-17 05:59:44 +00001178
1179void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1180 // ignore
1181}
Alexey Bataeva769e072013-03-22 06:34:35 +00001182
1183void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1184 Out << "#pragma omp threadprivate";
1185 if (!D->varlist_empty()) {
1186 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1187 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001188 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001189 Out << (I == D->varlist_begin() ? '(' : ',');
1190 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1191 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001192 }
1193 Out << ")";
1194 }
1195}
1196