blob: bfd090ae20d5e21cf11b34ac403047a1849c0d8b [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();
Aaron Ballman53885382014-09-15 16:45:30 +0000380
381 prettyPrintAttributes(D);
382
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000383 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000384 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000385
John McCallf937c022011-10-07 06:10:15 +0000386 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000387 Out << " {\n";
388 VisitDeclContext(D);
389 Indent() << "}";
390 }
391}
392
393void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000394 Out << *D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000395 if (Expr *Init = D->getInitExpr()) {
396 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000397 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000398 }
399}
400
Mike Stump11289f42009-09-09 15:08:12 +0000401void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000402 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000403 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000404 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000405 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000406 case SC_None: break;
407 case SC_Extern: Out << "extern "; break;
408 case SC_Static: Out << "static "; break;
409 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000410 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
411 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000412 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000413
Douglas Gregor26701a42011-09-09 02:06:17 +0000414 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000415 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000416 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000417 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000418 if ((CDecl && CDecl->isExplicitSpecified()) ||
419 (ConversionDecl && ConversionDecl->isExplicit()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000420 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000421 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000422
Douglas Gregor2d042f12009-05-30 05:39:39 +0000423 PrintingPolicy SubPolicy(Policy);
424 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000425 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000426
Abramo Bagnara6d810632010-12-14 22:11:44 +0000427 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000428 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000429 Proto = '(' + Proto + ')';
430 Ty = PT->getInnerType();
431 }
432
Reid Kleckner0503a872013-12-05 01:23:43 +0000433 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000434 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000435 if (D->hasWrittenPrototype())
436 FT = dyn_cast<FunctionProtoType>(AFT);
437
438 Proto += "(";
439 if (FT) {
440 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000441 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000442 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
443 if (i) POut << ", ";
444 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
445 }
Mike Stump11289f42009-09-09 15:08:12 +0000446
Douglas Gregor278f52e2009-05-30 00:08:05 +0000447 if (FT->isVariadic()) {
448 if (D->getNumParams()) POut << ", ";
449 POut << "...";
450 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000451 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000452 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
453 if (i)
454 Proto += ", ";
455 Proto += D->getParamDecl(i)->getNameAsString();
456 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000457 }
458
459 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000460
David Blaikief5697e52012-08-10 00:55:35 +0000461 if (FT) {
462 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000463 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000464 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000465 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000466 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000467 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000468
469 switch (FT->getRefQualifier()) {
470 case RQ_None:
471 break;
472 case RQ_LValue:
473 Proto += " &";
474 break;
475 case RQ_RValue:
476 Proto += " &&";
477 break;
478 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000479 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000480
481 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000482 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000483 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000484 Proto += "...";
485 else
486 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
487 if (I)
488 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000489
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000490 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000491 }
492 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000493 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
494 Proto += " noexcept";
495 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
496 Proto += "(";
497 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000498 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000499 Indentation);
500 EOut.flush();
501 Proto += EOut.str();
502 Proto += ")";
503 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000504 }
505
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000506 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000507 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000508 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000509 if (BMInitializer->isInClassMemberInitializer())
510 continue;
511
512 if (!HasInitializerList) {
513 Proto += " : ";
514 Out << Proto;
515 Proto.clear();
516 HasInitializerList = true;
517 } else
518 Out << ", ";
519
520 if (BMInitializer->isAnyMemberInitializer()) {
521 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000522 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000523 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000524 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000525 }
526
527 Out << "(";
528 if (!BMInitializer->getInit()) {
529 // Nothing to print
530 } else {
531 Expr *Init = BMInitializer->getInit();
532 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
533 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000534
Richard Smith938f40b2011-06-11 17:19:42 +0000535 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000536
537 Expr *SimpleInit = nullptr;
538 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000539 unsigned NumArgs = 0;
540 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
541 Args = ParenList->getExprs();
542 NumArgs = ParenList->getNumExprs();
543 } else if (CXXConstructExpr *Construct
544 = dyn_cast<CXXConstructExpr>(Init)) {
545 Args = Construct->getArgs();
546 NumArgs = Construct->getNumArgs();
547 } else
548 SimpleInit = Init;
549
550 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000551 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000552 else {
553 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000554 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000555 if (isa<CXXDefaultArgExpr>(Args[I]))
556 break;
557
558 if (I)
559 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000560 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000561 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000562 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000563 }
Richard Smith938f40b2011-06-11 17:19:42 +0000564 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000565 if (BMInitializer->isPackExpansion())
566 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000567 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000568 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000569 if (FT && FT->hasTrailingReturn()) {
570 Out << "auto " << Proto << " -> ";
571 Proto.clear();
572 }
Alp Toker314cc812014-01-25 16:55:45 +0000573 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000574 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000575 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000576 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000577 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000578 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000579 }
580
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000581 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000582
583 if (D->isPure())
584 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000585 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000586 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000587 else if (D->isExplicitlyDefaulted())
588 Out << " = default";
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000589 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000590 if (!D->hasPrototype() && D->getNumParams()) {
591 // This is a K&R function definition, so we need to print the
592 // parameters.
593 Out << '\n';
Richard Smith235341b2012-08-16 03:56:14 +0000594 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000595 Indentation += Policy.Indentation;
596 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
597 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000598 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000599 Out << ";\n";
600 }
601 Indentation -= Policy.Indentation;
602 } else
603 Out << ' ';
604
Richard Trieuddd01ce2014-06-09 22:53:25 +0000605 if (D->getBody())
606 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000607 Out << '\n';
608 }
609}
610
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000611void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
612 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000613 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
614 for (unsigned i = 0; i < NumTPLists; ++i)
615 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000616 Out << "friend ";
617 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000618 }
619 else if (FunctionDecl *FD =
620 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
621 Out << "friend ";
622 VisitFunctionDecl(FD);
623 }
624 else if (FunctionTemplateDecl *FTD =
625 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
626 Out << "friend ";
627 VisitFunctionTemplateDecl(FTD);
628 }
629 else if (ClassTemplateDecl *CTD =
630 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
631 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000632 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000633 }
634}
635
Douglas Gregor278f52e2009-05-30 00:08:05 +0000636void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000637 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000638 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000639 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
640 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000641
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000642 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Fariborz Jahanian9e075842013-05-01 17:28:37 +0000643 stream(Policy, D->getName());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000644
645 if (D->isBitField()) {
646 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000647 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000648 }
Richard Smith938f40b2011-06-11 17:19:42 +0000649
650 Expr *Init = D->getInClassInitializer();
651 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000652 if (D->getInClassInitStyle() == ICIS_ListInit)
653 Out << " ";
654 else
655 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000656 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000657 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000658 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000659}
660
Chris Lattnercab02a62011-02-17 20:34:02 +0000661void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000662 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000663}
664
Douglas Gregor278f52e2009-05-30 00:08:05 +0000665void DeclPrinter::VisitVarDecl(VarDecl *D) {
Richard Smithfd3834f2013-04-13 02:43:54 +0000666 if (!Policy.SuppressSpecifiers) {
667 StorageClass SC = D->getStorageClass();
668 if (SC != SC_None)
669 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000670
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000671 switch (D->getTSCSpec()) {
672 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000673 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000674 case TSCS___thread:
675 Out << "__thread ";
676 break;
677 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000678 Out << "_Thread_local ";
679 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000680 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000681 Out << "thread_local ";
682 break;
683 }
684
685 if (D->isModulePrivate())
686 Out << "__module_private__ ";
687 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000688
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000689 QualType T = D->getTypeSourceInfo()
690 ? D->getTypeSourceInfo()->getType()
691 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
Richard Smitha4bb2922014-07-23 03:17:06 +0000692 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000693 Expr *Init = D->getInit();
694 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000695 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000696 if (CXXConstructExpr *Construct =
697 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000698 if (D->getInitStyle() == VarDecl::CallInit &&
699 !Construct->isListInitialization()) {
700 ImplicitInit = Construct->getNumArgs() == 0 ||
701 Construct->getArg(0)->isDefaultArgument();
702 }
703 }
Sebastian Redla9351792012-02-11 23:51:47 +0000704 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000705 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000706 Out << "(";
707 else if (D->getInitStyle() == VarDecl::CInit) {
708 Out << " = ";
709 }
Craig Topper36250ad2014-05-12 05:36:57 +0000710 Init->printPretty(Out, nullptr, Policy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000711 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000712 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000713 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000714 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000715 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000716}
717
718void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
719 VisitVarDecl(D);
720}
721
722void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
723 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000724 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000725 Out << ")";
726}
727
Douglas Gregorba345522011-12-02 23:23:56 +0000728void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000729 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000730 << ";\n";
731}
732
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000733void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
734 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000735 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000736 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000737 D->getMessage()->printPretty(Out, nullptr, Policy, Indentation);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000738 Out << ")";
739}
740
Douglas Gregor278f52e2009-05-30 00:08:05 +0000741//----------------------------------------------------------------------------
742// C++ declarations
743//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000744void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000745 if (D->isInline())
746 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000747 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000748 VisitDeclContext(D);
749 Indent() << "}";
750}
751
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000752void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
753 Out << "using namespace ";
754 if (D->getQualifier())
755 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000756 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000757}
758
Douglas Gregor18231932009-05-30 06:48:27 +0000759void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000760 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000761 if (D->getQualifier())
762 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000763 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000764}
765
Michael Han84324352013-02-22 17:15:32 +0000766void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
767 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000768}
769
Douglas Gregor5f478b72009-05-30 06:58:37 +0000770void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000771 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
772 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000773 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000774
775 prettyPrintAttributes(D);
776
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000777 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000778 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000779
John McCallf937c022011-10-07 06:10:15 +0000780 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000781 // Print the base classes
782 if (D->getNumBases()) {
783 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000784 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
785 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000786 if (Base != D->bases_begin())
787 Out << ", ";
788
789 if (Base->isVirtual())
790 Out << "virtual ";
791
Anders Carlsson6df9e072009-08-29 20:36:12 +0000792 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000793 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000794 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000795 Out << " ";
796 }
797 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000798
799 if (Base->isPackExpansion())
800 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000801 }
802 }
803
804 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000805 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000806 Out << " {\n";
807 VisitDeclContext(D);
808 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000809 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000810}
811
812void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
813 const char *l;
814 if (D->getLanguage() == LinkageSpecDecl::lang_c)
815 l = "C";
816 else {
817 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
818 "unknown language in linkage specification");
819 l = "C++";
820 }
821
822 Out << "extern \"" << l << "\" ";
823 if (D->hasBraces()) {
824 Out << "{\n";
825 VisitDeclContext(D);
826 Indent() << "}";
827 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000828 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000829}
830
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000831void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
832 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000833 assert(Params);
834 assert(!Args || Params->size() == Args->size());
835
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000836 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000837
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000838 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
839 if (i != 0)
840 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000841
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000842 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000843 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000844 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000845
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000846 if (TTP->wasDeclaredWithTypename())
847 Out << "typename ";
848 else
849 Out << "class ";
850
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000851 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000852 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000853
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000854 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000855
Richard Trieu82398f92011-07-28 00:19:05 +0000856 if (Args) {
857 Out << " = ";
858 Args->get(i).print(Policy, Out);
859 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000860 Out << " = ";
861 Out << TTP->getDefaultArgument().getAsString(Policy);
862 };
Mike Stump11289f42009-09-09 15:08:12 +0000863 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000864 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000865 StringRef Name;
866 if (IdentifierInfo *II = NTTP->getIdentifier())
867 Name = II->getName();
868 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000869
Richard Trieu82398f92011-07-28 00:19:05 +0000870 if (Args) {
871 Out << " = ";
872 Args->get(i).print(Policy, Out);
873 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000874 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000875 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
876 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000877 }
Richard Smith030f4992011-04-15 13:38:57 +0000878 } else if (const TemplateTemplateParmDecl *TTPD =
879 dyn_cast<TemplateTemplateParmDecl>(Param)) {
880 VisitTemplateDecl(TTPD);
881 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000882 }
883 }
Mike Stump11289f42009-09-09 15:08:12 +0000884
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000885 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000886}
887
888void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
889 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000890
Richard Smith030f4992011-04-15 13:38:57 +0000891 if (const TemplateTemplateParmDecl *TTP =
892 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000893 Out << "class ";
894 if (TTP->isParameterPack())
895 Out << "...";
896 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000897 } else {
898 Visit(D->getTemplatedDecl());
899 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000900}
901
Richard Trieu82398f92011-07-28 00:19:05 +0000902void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
903 if (PrintInstantiation) {
904 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000905 for (auto *I : D->specializations()) {
906 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
907 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000908 }
909 }
910
911 return VisitRedeclarableTemplateDecl(D);
912}
913
914void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
915 if (PrintInstantiation) {
916 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000917 for (auto *I : D->specializations()) {
918 PrintTemplateParameters(Params, &I->getTemplateArgs());
919 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000920 Out << '\n';
921 }
922 }
923
924 return VisitRedeclarableTemplateDecl(D);
925}
926
Douglas Gregor278f52e2009-05-30 00:08:05 +0000927//----------------------------------------------------------------------------
928// Objective-C declarations
929//----------------------------------------------------------------------------
930
Douglas Gregor278f52e2009-05-30 00:08:05 +0000931void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
932 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000933 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +0000934 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000935 Out << "+ ";
Alp Toker314cc812014-01-25 16:55:45 +0000936 if (!OMD->getReturnType().isNull())
937 Out << '(' << OMD->getASTContext()
938 .getUnqualifiedObjCPointerType(OMD->getReturnType())
939 .getAsString(Policy) << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000940
Douglas Gregor278f52e2009-05-30 00:08:05 +0000941 std::string name = OMD->getSelector().getAsString();
942 std::string::size_type pos, lastPos = 0;
Aaron Ballman43b68be2014-03-07 17:50:17 +0000943 for (const auto *PI : OMD->params()) {
Mike Stump11289f42009-09-09 15:08:12 +0000944 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000945 pos = name.find_first_of(':', lastPos);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000946 Out << " " << name.substr(lastPos, pos - lastPos);
Aaron Ballman43b68be2014-03-07 17:50:17 +0000947 Out << ":(" << PI->getASTContext().getUnqualifiedObjCPointerType(PI->getType()).
948 getAsString(Policy) << ')' << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000949 lastPos = pos + 1;
950 }
Mike Stump11289f42009-09-09 15:08:12 +0000951
Douglas Gregor278f52e2009-05-30 00:08:05 +0000952 if (OMD->param_begin() == OMD->param_end())
953 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +0000954
Douglas Gregor278f52e2009-05-30 00:08:05 +0000955 if (OMD->isVariadic())
956 Out << ", ...";
Mike Stump11289f42009-09-09 15:08:12 +0000957
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000958 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000959 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +0000960 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000961 Out << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000962 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000963 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +0000964 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +0000965}
966
967void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
968 std::string I = OID->getNameAsString();
969 ObjCInterfaceDecl *SID = OID->getSuperClass();
970
971 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000972 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000973 else
974 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +0000975
976 if (OID->ivar_size() > 0) {
977 Out << "{\n";
978 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +0000979 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000980 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +0000981 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +0000982 }
983 Indentation -= Policy.Indentation;
984 Out << "}\n";
985 }
Douglas Gregor36098ff2009-05-30 00:56:08 +0000986 VisitDeclContext(OID, false);
987 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000988}
989
990void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
991 std::string I = OID->getNameAsString();
992 ObjCInterfaceDecl *SID = OID->getSuperClass();
993
Douglas Gregordc9166c2011-12-15 20:29:51 +0000994 if (!OID->isThisDeclarationADefinition()) {
995 Out << "@class " << I << ";";
996 return;
997 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000998 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000999 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001000 Out << "@interface " << I << " : " << *SID;
Douglas Gregor1c283312010-08-11 12:19:30 +00001001 else
1002 Out << "@interface " << I;
Mike Stump11289f42009-09-09 15:08:12 +00001003
Douglas Gregor278f52e2009-05-30 00:08:05 +00001004 // Protocols?
1005 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1006 if (!Protocols.empty()) {
1007 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1008 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001009 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001010 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001011 }
Mike Stump11289f42009-09-09 15:08:12 +00001012
Douglas Gregor278f52e2009-05-30 00:08:05 +00001013 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001014 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001015 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001016 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001017 for (const auto *I : OID->ivars()) {
1018 Indent() << I->getASTContext()
1019 .getUnqualifiedObjCPointerType(I->getType())
1020 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001021 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001022 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001023 Out << "}\n";
1024 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001025 else if (SID) {
1026 Out << "\n";
1027 eolnOut = true;
1028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Douglas Gregor278f52e2009-05-30 00:08:05 +00001030 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001031 if (!eolnOut)
1032 Out << ' ';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001033 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001034 // FIXME: implement the rest...
1035}
1036
Douglas Gregor278f52e2009-05-30 00:08:05 +00001037void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001038 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001039 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001040 return;
1041 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001042 // Protocols?
1043 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1044 if (!Protocols.empty()) {
1045 Out << "@protocol " << *PID;
1046 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1047 E = Protocols.end(); I != E; ++I)
1048 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1049 Out << ">\n";
1050 } else
1051 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001052 VisitDeclContext(PID, false);
1053 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001054}
1055
1056void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001057 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001058
1059 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001060 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001061 // FIXME: implement the rest...
1062}
1063
1064void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001065 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001066 if (PID->ivar_size() > 0) {
1067 Out << "{\n";
1068 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001069 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001070 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001071 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001072 Indentation -= Policy.Indentation;
1073 Out << "}\n";
1074 }
1075
Douglas Gregor278f52e2009-05-30 00:08:05 +00001076 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001077 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001078
Douglas Gregor278f52e2009-05-30 00:08:05 +00001079 // FIXME: implement the rest...
1080}
1081
1082void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001083 Out << "@compatibility_alias " << *AID
1084 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001085}
1086
1087/// PrintObjCPropertyDecl - print a property declaration.
1088///
1089void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1090 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1091 Out << "@required\n";
1092 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1093 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001094
Douglas Gregor278f52e2009-05-30 00:08:05 +00001095 Out << "@property";
1096 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1097 bool first = true;
1098 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001099 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001100 ObjCPropertyDecl::OBJC_PR_readonly) {
1101 Out << (first ? ' ' : ',') << "readonly";
1102 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001103 }
Mike Stump11289f42009-09-09 15:08:12 +00001104
Ted Kremenek897af912011-08-17 21:09:35 +00001105 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001106 Out << (first ? ' ' : ',') << "getter = ";
1107 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001108 first = false;
1109 }
1110 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001111 Out << (first ? ' ' : ',') << "setter = ";
1112 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001113 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_assign) {
1117 Out << (first ? ' ' : ',') << "assign";
1118 first = false;
1119 }
Mike Stump11289f42009-09-09 15:08:12 +00001120
Ted Kremenek897af912011-08-17 21:09:35 +00001121 if (PDecl->getPropertyAttributes() &
1122 ObjCPropertyDecl::OBJC_PR_readwrite) {
1123 Out << (first ? ' ' : ',') << "readwrite";
1124 first = false;
1125 }
Mike Stump11289f42009-09-09 15:08:12 +00001126
Ted Kremenek897af912011-08-17 21:09:35 +00001127 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1128 Out << (first ? ' ' : ',') << "retain";
1129 first = false;
1130 }
Mike Stump11289f42009-09-09 15:08:12 +00001131
Ted Kremenek897af912011-08-17 21:09:35 +00001132 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1133 Out << (first ? ' ' : ',') << "strong";
1134 first = false;
1135 }
John McCall31168b02011-06-15 23:02:42 +00001136
Ted Kremenek897af912011-08-17 21:09:35 +00001137 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1138 Out << (first ? ' ' : ',') << "copy";
1139 first = false;
1140 }
Mike Stump11289f42009-09-09 15:08:12 +00001141
Ted Kremenek897af912011-08-17 21:09:35 +00001142 if (PDecl->getPropertyAttributes() &
1143 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1144 Out << (first ? ' ' : ',') << "nonatomic";
1145 first = false;
1146 }
1147 if (PDecl->getPropertyAttributes() &
1148 ObjCPropertyDecl::OBJC_PR_atomic) {
1149 Out << (first ? ' ' : ',') << "atomic";
1150 first = false;
1151 }
1152
1153 (void) first; // Silence dead store warning due to idiomatic code.
1154 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001155 }
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001156 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(PDecl->getType()).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001157 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001158 if (Policy.PolishForDeclaration)
1159 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001160}
1161
1162void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1163 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001164 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001165 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001166 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001167 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001168 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001169 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001170}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001171
1172void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001173 if (!D->isAccessDeclaration())
1174 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001175 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001176 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001177 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001178 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001179}
1180
John McCalle61f2ba2009-11-18 02:36:19 +00001181void
1182DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1183 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001184 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001185 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001186}
1187
1188void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001189 if (!D->isAccessDeclaration())
1190 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001191 D->getQualifier()->print(Out, Policy);
Fariborz Jahanian3ec39212012-12-06 00:09:40 +00001192 Out << D->getName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001193}
John McCall3f746822009-11-17 05:59:44 +00001194
1195void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1196 // ignore
1197}
Alexey Bataeva769e072013-03-22 06:34:35 +00001198
1199void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1200 Out << "#pragma omp threadprivate";
1201 if (!D->varlist_empty()) {
1202 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1203 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001204 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001205 Out << (I == D->varlist_begin() ? '(' : ',');
1206 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1207 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001208 }
1209 Out << ")";
1210 }
1211}
1212