blob: 3e5e7c0c3551138f0dfd4bac2f8d548c96287fea [file] [log] [blame]
Douglas Gregor278f52e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Alexander Kornienko90ff6072012-12-20 02:09:13 +000010// This file implements the Decl::print method, which pretty prints the
Douglas Gregor278f52e2009-05-30 00:08:05 +000011// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000020#include "clang/AST/Expr.h"
Douglas Gregor7ae2d772010-01-31 09:12:51 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000022#include "clang/AST/PrettyPrinter.h"
Douglas Gregorba345522011-12-02 23:23:56 +000023#include "clang/Basic/Module.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000024#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26
27namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000028 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000029 raw_ostream &Out;
Douglas Gregor278f52e2009-05-30 00:08:05 +000030 PrintingPolicy Policy;
31 unsigned Indentation;
Richard Trieu82398f92011-07-28 00:19:05 +000032 bool PrintInstantiation;
Douglas Gregor278f52e2009-05-30 00:08:05 +000033
Chris Lattner0e62c1c2011-07-23 10:55:15 +000034 raw_ostream& Indent() { return Indent(Indentation); }
35 raw_ostream& Indent(unsigned Indentation);
36 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
Douglas Gregor278f52e2009-05-30 00:08:05 +000037
Anders Carlsson601d6e42009-08-28 22:39:52 +000038 void Print(AccessSpecifier AS);
Mike Stump11289f42009-09-09 15:08:12 +000039
Douglas Gregor813a0662015-06-19 18:14:38 +000040 /// Print an Objective-C method type in parentheses.
41 ///
42 /// \param Quals The Objective-C declaration qualifiers.
43 /// \param T The type to print.
44 void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
45 QualType T);
46
Douglas Gregor85f3f952015-07-07 03:57:15 +000047 void PrintObjCTypeParams(ObjCTypeParamList *Params);
48
Douglas Gregor278f52e2009-05-30 00:08:05 +000049 public:
Richard Smith235341b2012-08-16 03:56:14 +000050 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
51 unsigned Indentation = 0, bool PrintInstantiation = false)
52 : Out(Out), Policy(Policy), Indentation(Indentation),
Richard Trieu82398f92011-07-28 00:19:05 +000053 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor278f52e2009-05-30 00:08:05 +000054
55 void VisitDeclContext(DeclContext *DC, bool Indent = true);
56
57 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
58 void VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +000059 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000060 void VisitEnumDecl(EnumDecl *D);
61 void VisitRecordDecl(RecordDecl *D);
62 void VisitEnumConstantDecl(EnumConstantDecl *D);
Michael Han84324352013-02-22 17:15:32 +000063 void VisitEmptyDecl(EmptyDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000064 void VisitFunctionDecl(FunctionDecl *D);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +000065 void VisitFriendDecl(FriendDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000066 void VisitFieldDecl(FieldDecl *D);
67 void VisitVarDecl(VarDecl *D);
Chris Lattnercab02a62011-02-17 20:34:02 +000068 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000069 void VisitParmVarDecl(ParmVarDecl *D);
70 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregorba345522011-12-02 23:23:56 +000071 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +000072 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000073 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000074 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000075 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000076 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000077 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith030f4992011-04-15 13:38:57 +000078 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000079 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
80 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000081 void VisitObjCMethodDecl(ObjCMethodDecl *D);
82 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
83 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000084 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
85 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
86 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
87 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
88 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
89 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000090 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
91 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +000092 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +000093 void VisitUsingShadowDecl(UsingShadowDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +000094 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000095
96 void PrintTemplateParameters(const TemplateParameterList *Params,
Craig Topper36250ad2014-05-12 05:36:57 +000097 const TemplateArgumentList *Args = nullptr);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +000098 void prettyPrintAttributes(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +000099 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000100 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000101}
Douglas Gregor278f52e2009-05-30 00:08:05 +0000102
Richard Trieu82398f92011-07-28 00:19:05 +0000103void Decl::print(raw_ostream &Out, unsigned Indentation,
104 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +0000105 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000106}
107
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000108void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000109 unsigned Indentation, bool PrintInstantiation) const {
Richard Smith235341b2012-08-16 03:56:14 +0000110 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000111 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000112}
113
Eli Friedman79635842009-05-30 04:20:30 +0000114static QualType GetBaseType(QualType T) {
115 // FIXME: This should be on the Type class!
116 QualType BaseType = T;
117 while (!BaseType->isSpecifierType()) {
118 if (isa<TypedefType>(BaseType))
119 break;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000120 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000121 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000122 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
123 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000124 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
125 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000126 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000127 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000128 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000129 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000130 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
131 BaseType = RTy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000132 else
David Blaikie83d382b2011-09-23 05:06:16 +0000133 llvm_unreachable("Unknown declarator!");
Eli Friedman79635842009-05-30 04:20:30 +0000134 }
135 return BaseType;
136}
137
138static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000139 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000140 return TDD->getUnderlyingType();
141 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
142 return VD->getType();
143 return QualType();
144}
145
146void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000147 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000148 unsigned Indentation) {
149 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000150 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000151 return;
152 }
153
154 Decl** End = Begin + NumDecls;
155 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
156 if (TD)
157 ++Begin;
158
159 PrintingPolicy SubPolicy(Policy);
John McCallf937c022011-10-07 06:10:15 +0000160 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000161 TD->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000162 Out << " ";
163 SubPolicy.SuppressTag = true;
164 }
165
166 bool isFirst = true;
167 for ( ; Begin != End; ++Begin) {
168 if (isFirst) {
169 SubPolicy.SuppressSpecifiers = false;
170 isFirst = false;
171 } else {
172 if (!isFirst) Out << ", ";
173 SubPolicy.SuppressSpecifiers = true;
174 }
175
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000176 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000177 }
178}
179
Alp Tokeref6b0072014-01-04 13:47:14 +0000180LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000181 // Get the translation unit
182 const DeclContext *DC = this;
183 while (!DC->isTranslationUnit())
184 DC = DC->getParent();
185
186 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith235341b2012-08-16 03:56:14 +0000187 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000188 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
189}
190
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000191raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000192 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000193 Out << " ";
194 return Out;
195}
196
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000197void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000198 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000199 return;
200
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000201 if (D->hasAttrs()) {
202 AttrVec &Attrs = D->getAttrs();
203 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
Richard Smith52f04a22012-08-16 02:43:29 +0000204 Attr *A = *i;
205 A->printPretty(Out, Policy);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000206 }
207 }
208}
209
Richard Smitha4bb2922014-07-23 03:17:06 +0000210void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
211 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
212 // template argument), but if it is the type of a declaration, the ellipsis
213 // is placed before the name being declared.
214 if (auto *PET = T->getAs<PackExpansionType>()) {
215 Pack = true;
216 T = PET->getPattern();
217 }
218 T.print(Out, Policy, (Pack ? "..." : "") + DeclName);
219}
220
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000221void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000222 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000223 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000224 Out << ";\n";
225 Decls.clear();
226
227}
228
Anders Carlsson601d6e42009-08-28 22:39:52 +0000229void DeclPrinter::Print(AccessSpecifier AS) {
230 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000231 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000232 case AS_public: Out << "public"; break;
233 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000234 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000235 }
236}
237
Douglas Gregor278f52e2009-05-30 00:08:05 +0000238//----------------------------------------------------------------------------
239// Common C declarations
240//----------------------------------------------------------------------------
241
242void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000243 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000244 return;
245
Douglas Gregor278f52e2009-05-30 00:08:05 +0000246 if (Indent)
247 Indentation += Policy.Indentation;
248
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000249 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000250 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000251 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000252
253 // Don't print ObjCIvarDecls, as they are printed when visiting the
254 // containing ObjCInterfaceDecl.
255 if (isa<ObjCIvarDecl>(*D))
256 continue;
257
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000258 // Skip over implicit declarations in pretty-printing mode.
259 if (D->isImplicit())
260 continue;
261
Eli Friedman79635842009-05-30 04:20:30 +0000262 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
263 // forced to merge the declarations because there's no other way to
264 // refer to the struct in question. This limited merging is safe without
265 // a bunch of other checks because it only merges declarations directly
266 // referring to the tag, not typedefs.
267 //
268 // Check whether the current declaration should be grouped with a previous
269 // unnamed struct.
270 QualType CurDeclType = getDeclType(*D);
271 if (!Decls.empty() && !CurDeclType.isNull()) {
272 QualType BaseType = GetBaseType(CurDeclType);
Eli Friedman24b3fed2013-08-12 21:54:04 +0000273 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
274 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
Eli Friedman79635842009-05-30 04:20:30 +0000275 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
276 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
277 Decls.push_back(*D);
278 continue;
279 }
280 }
281
282 // If we have a merged group waiting to be handled, handle it now.
283 if (!Decls.empty())
284 ProcessDeclGroup(Decls);
285
286 // If the current declaration is an unnamed tag type, save it
287 // so we can merge it with the subsequent declaration(s) using it.
288 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
289 Decls.push_back(*D);
290 continue;
291 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000292
293 if (isa<AccessSpecDecl>(*D)) {
294 Indentation -= Policy.Indentation;
295 this->Indent();
296 Print(D->getAccess());
297 Out << ":\n";
298 Indentation += Policy.Indentation;
299 continue;
300 }
301
Douglas Gregor278f52e2009-05-30 00:08:05 +0000302 this->Indent();
303 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000304
305 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000306 const char *Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000307 if (isa<OMPThreadPrivateDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000308 Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000309 else if (isa<FunctionDecl>(*D) &&
310 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
Yaron Keren51db8772014-05-07 09:53:02 +0000311 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000312 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000313 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000314 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000315 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000316 isa<ObjCInterfaceDecl>(*D) ||
317 isa<ObjCProtocolDecl>(*D) ||
318 isa<ObjCCategoryImplDecl>(*D) ||
319 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000320 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000321 else if (isa<EnumConstantDecl>(*D)) {
322 DeclContext::decl_iterator Next = D;
323 ++Next;
324 if (Next != DEnd)
325 Terminator = ",";
326 } else
327 Terminator = ";";
328
329 if (Terminator)
330 Out << Terminator;
331 Out << "\n";
332 }
333
Eli Friedman79635842009-05-30 04:20:30 +0000334 if (!Decls.empty())
335 ProcessDeclGroup(Decls);
336
Douglas Gregor278f52e2009-05-30 00:08:05 +0000337 if (Indent)
338 Indentation -= Policy.Indentation;
339}
340
341void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
342 VisitDeclContext(D, false);
343}
344
345void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000346 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000347 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000348
349 if (D->isModulePrivate())
350 Out << "__module_private__ ";
351 }
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000352 D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000353 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000354}
355
Richard Smithdda56e42011-04-15 14:24:37 +0000356void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000357 Out << "using " << *D;
358 prettyPrintAttributes(D);
359 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000360}
361
Douglas Gregor278f52e2009-05-30 00:08:05 +0000362void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000363 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
364 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000365 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000366 if (D->isScoped()) {
367 if (D->isScopedUsingClassTag())
368 Out << "class ";
369 else
370 Out << "struct ";
371 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000372 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000373
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000374 if (D->isFixed())
375 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000376
John McCallf937c022011-10-07 06:10:15 +0000377 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000378 Out << " {\n";
379 VisitDeclContext(D);
380 Indent() << "}";
381 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000382 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000383}
384
385void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000386 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
387 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000388 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000389
390 prettyPrintAttributes(D);
391
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000392 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000393 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000394
John McCallf937c022011-10-07 06:10:15 +0000395 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000396 Out << " {\n";
397 VisitDeclContext(D);
398 Indent() << "}";
399 }
400}
401
402void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000403 Out << *D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000404 if (Expr *Init = D->getInitExpr()) {
405 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000406 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000407 }
408}
409
Mike Stump11289f42009-09-09 15:08:12 +0000410void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000411 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000412 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000413 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000414 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000415 case SC_None: break;
416 case SC_Extern: Out << "extern "; break;
417 case SC_Static: Out << "static "; break;
418 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000419 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
420 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000421 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000422
Douglas Gregor26701a42011-09-09 02:06:17 +0000423 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000424 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000425 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000426 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000427 if ((CDecl && CDecl->isExplicitSpecified()) ||
428 (ConversionDecl && ConversionDecl->isExplicit()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000429 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000430 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000431
Douglas Gregor2d042f12009-05-30 05:39:39 +0000432 PrintingPolicy SubPolicy(Policy);
433 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000434 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000435
Abramo Bagnara6d810632010-12-14 22:11:44 +0000436 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000437 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000438 Proto = '(' + Proto + ')';
439 Ty = PT->getInnerType();
440 }
441
Reid Kleckner0503a872013-12-05 01:23:43 +0000442 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000443 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000444 if (D->hasWrittenPrototype())
445 FT = dyn_cast<FunctionProtoType>(AFT);
446
447 Proto += "(";
448 if (FT) {
449 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000450 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000451 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
452 if (i) POut << ", ";
453 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
454 }
Mike Stump11289f42009-09-09 15:08:12 +0000455
Douglas Gregor278f52e2009-05-30 00:08:05 +0000456 if (FT->isVariadic()) {
457 if (D->getNumParams()) POut << ", ";
458 POut << "...";
459 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000460 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000461 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
462 if (i)
463 Proto += ", ";
464 Proto += D->getParamDecl(i)->getNameAsString();
465 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000466 }
467
468 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000469
David Blaikief5697e52012-08-10 00:55:35 +0000470 if (FT) {
471 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000472 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000473 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000474 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000475 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000476 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000477
478 switch (FT->getRefQualifier()) {
479 case RQ_None:
480 break;
481 case RQ_LValue:
482 Proto += " &";
483 break;
484 case RQ_RValue:
485 Proto += " &&";
486 break;
487 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000488 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000489
490 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000491 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000492 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000493 Proto += "...";
494 else
495 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
496 if (I)
497 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000498
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000499 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000500 }
501 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000502 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
503 Proto += " noexcept";
504 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
505 Proto += "(";
506 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000507 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000508 Indentation);
509 EOut.flush();
510 Proto += EOut.str();
511 Proto += ")";
512 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000513 }
514
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000515 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000516 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000517 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000518 if (BMInitializer->isInClassMemberInitializer())
519 continue;
520
521 if (!HasInitializerList) {
522 Proto += " : ";
523 Out << Proto;
524 Proto.clear();
525 HasInitializerList = true;
526 } else
527 Out << ", ";
528
529 if (BMInitializer->isAnyMemberInitializer()) {
530 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000531 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000532 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000533 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000534 }
535
536 Out << "(";
537 if (!BMInitializer->getInit()) {
538 // Nothing to print
539 } else {
540 Expr *Init = BMInitializer->getInit();
541 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
542 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000543
Richard Smith938f40b2011-06-11 17:19:42 +0000544 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000545
546 Expr *SimpleInit = nullptr;
547 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000548 unsigned NumArgs = 0;
549 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
550 Args = ParenList->getExprs();
551 NumArgs = ParenList->getNumExprs();
552 } else if (CXXConstructExpr *Construct
553 = dyn_cast<CXXConstructExpr>(Init)) {
554 Args = Construct->getArgs();
555 NumArgs = Construct->getNumArgs();
556 } else
557 SimpleInit = Init;
558
559 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000560 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000561 else {
562 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000563 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000564 if (isa<CXXDefaultArgExpr>(Args[I]))
565 break;
566
567 if (I)
568 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000569 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000570 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000571 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000572 }
Richard Smith938f40b2011-06-11 17:19:42 +0000573 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000574 if (BMInitializer->isPackExpansion())
575 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000576 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000577 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000578 if (FT && FT->hasTrailingReturn()) {
579 Out << "auto " << Proto << " -> ";
580 Proto.clear();
581 }
Alp Toker314cc812014-01-25 16:55:45 +0000582 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000583 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000584 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000585 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000586 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000587 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000588 }
589
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000590 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000591
592 if (D->isPure())
593 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000594 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000595 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000596 else if (D->isExplicitlyDefaulted())
597 Out << " = default";
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000598 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000599 if (!D->hasPrototype() && D->getNumParams()) {
600 // This is a K&R function definition, so we need to print the
601 // parameters.
602 Out << '\n';
Richard Smith235341b2012-08-16 03:56:14 +0000603 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000604 Indentation += Policy.Indentation;
605 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
606 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000607 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000608 Out << ";\n";
609 }
610 Indentation -= Policy.Indentation;
611 } else
612 Out << ' ';
613
Richard Trieuddd01ce2014-06-09 22:53:25 +0000614 if (D->getBody())
615 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000616 Out << '\n';
617 }
618}
619
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000620void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
621 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000622 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
623 for (unsigned i = 0; i < NumTPLists; ++i)
624 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000625 Out << "friend ";
626 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000627 }
628 else if (FunctionDecl *FD =
629 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
630 Out << "friend ";
631 VisitFunctionDecl(FD);
632 }
633 else if (FunctionTemplateDecl *FTD =
634 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
635 Out << "friend ";
636 VisitFunctionTemplateDecl(FTD);
637 }
638 else if (ClassTemplateDecl *CTD =
639 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
640 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000641 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000642 }
643}
644
Douglas Gregor278f52e2009-05-30 00:08:05 +0000645void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000646 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000647 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000648 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
649 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000650
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000651 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Fariborz Jahanian9e075842013-05-01 17:28:37 +0000652 stream(Policy, D->getName());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000653
654 if (D->isBitField()) {
655 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000656 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000657 }
Richard Smith938f40b2011-06-11 17:19:42 +0000658
659 Expr *Init = D->getInClassInitializer();
660 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000661 if (D->getInClassInitStyle() == ICIS_ListInit)
662 Out << " ";
663 else
664 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000665 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000666 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000667 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000668}
669
Chris Lattnercab02a62011-02-17 20:34:02 +0000670void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000671 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000672}
673
Douglas Gregor278f52e2009-05-30 00:08:05 +0000674void DeclPrinter::VisitVarDecl(VarDecl *D) {
Richard Smithfd3834f2013-04-13 02:43:54 +0000675 if (!Policy.SuppressSpecifiers) {
676 StorageClass SC = D->getStorageClass();
677 if (SC != SC_None)
678 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000679
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000680 switch (D->getTSCSpec()) {
681 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000682 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000683 case TSCS___thread:
684 Out << "__thread ";
685 break;
686 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000687 Out << "_Thread_local ";
688 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000689 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000690 Out << "thread_local ";
691 break;
692 }
693
694 if (D->isModulePrivate())
695 Out << "__module_private__ ";
696 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000697
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000698 QualType T = D->getTypeSourceInfo()
699 ? D->getTypeSourceInfo()->getType()
700 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
Richard Smitha4bb2922014-07-23 03:17:06 +0000701 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000702 Expr *Init = D->getInit();
703 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000704 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000705 if (CXXConstructExpr *Construct =
706 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000707 if (D->getInitStyle() == VarDecl::CallInit &&
708 !Construct->isListInitialization()) {
709 ImplicitInit = Construct->getNumArgs() == 0 ||
710 Construct->getArg(0)->isDefaultArgument();
711 }
712 }
Sebastian Redla9351792012-02-11 23:51:47 +0000713 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000714 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000715 Out << "(";
716 else if (D->getInitStyle() == VarDecl::CInit) {
717 Out << " = ";
718 }
Craig Topper36250ad2014-05-12 05:36:57 +0000719 Init->printPretty(Out, nullptr, Policy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000720 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000721 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000722 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000723 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000724 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000725}
726
727void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
728 VisitVarDecl(D);
729}
730
731void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
732 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000733 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000734 Out << ")";
735}
736
Douglas Gregorba345522011-12-02 23:23:56 +0000737void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000738 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000739 << ";\n";
740}
741
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000742void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
743 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000744 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000745 if (StringLiteral *SL = D->getMessage()) {
746 Out << ", ";
747 SL->printPretty(Out, nullptr, Policy, Indentation);
748 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000749 Out << ")";
750}
751
Douglas Gregor278f52e2009-05-30 00:08:05 +0000752//----------------------------------------------------------------------------
753// C++ declarations
754//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000755void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000756 if (D->isInline())
757 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000758 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000759 VisitDeclContext(D);
760 Indent() << "}";
761}
762
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000763void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
764 Out << "using namespace ";
765 if (D->getQualifier())
766 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000767 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000768}
769
Douglas Gregor18231932009-05-30 06:48:27 +0000770void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000771 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000772 if (D->getQualifier())
773 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000774 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000775}
776
Michael Han84324352013-02-22 17:15:32 +0000777void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
778 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000779}
780
Douglas Gregor5f478b72009-05-30 06:58:37 +0000781void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000782 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
783 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000784 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000785
786 prettyPrintAttributes(D);
787
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000788 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000789 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000790
John McCallf937c022011-10-07 06:10:15 +0000791 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000792 // Print the base classes
793 if (D->getNumBases()) {
794 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000795 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
796 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000797 if (Base != D->bases_begin())
798 Out << ", ";
799
800 if (Base->isVirtual())
801 Out << "virtual ";
802
Anders Carlsson6df9e072009-08-29 20:36:12 +0000803 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000804 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000805 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000806 Out << " ";
807 }
808 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000809
810 if (Base->isPackExpansion())
811 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000812 }
813 }
814
815 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000816 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000817 Out << " {\n";
818 VisitDeclContext(D);
819 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000820 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000821}
822
823void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
824 const char *l;
825 if (D->getLanguage() == LinkageSpecDecl::lang_c)
826 l = "C";
827 else {
828 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
829 "unknown language in linkage specification");
830 l = "C++";
831 }
832
833 Out << "extern \"" << l << "\" ";
834 if (D->hasBraces()) {
835 Out << "{\n";
836 VisitDeclContext(D);
837 Indent() << "}";
838 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000839 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000840}
841
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000842void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
843 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000844 assert(Params);
845 assert(!Args || Params->size() == Args->size());
846
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000847 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000848
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000849 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
850 if (i != 0)
851 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000852
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000853 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000854 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000855 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000856
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000857 if (TTP->wasDeclaredWithTypename())
858 Out << "typename ";
859 else
860 Out << "class ";
861
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000862 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000863 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000864
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000865 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000866
Richard Trieu82398f92011-07-28 00:19:05 +0000867 if (Args) {
868 Out << " = ";
869 Args->get(i).print(Policy, Out);
870 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000871 Out << " = ";
872 Out << TTP->getDefaultArgument().getAsString(Policy);
873 };
Mike Stump11289f42009-09-09 15:08:12 +0000874 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000875 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000876 StringRef Name;
877 if (IdentifierInfo *II = NTTP->getIdentifier())
878 Name = II->getName();
879 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000880
Richard Trieu82398f92011-07-28 00:19:05 +0000881 if (Args) {
882 Out << " = ";
883 Args->get(i).print(Policy, Out);
884 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000885 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000886 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
887 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000888 }
Richard Smith030f4992011-04-15 13:38:57 +0000889 } else if (const TemplateTemplateParmDecl *TTPD =
890 dyn_cast<TemplateTemplateParmDecl>(Param)) {
891 VisitTemplateDecl(TTPD);
892 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000893 }
894 }
Mike Stump11289f42009-09-09 15:08:12 +0000895
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000896 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000897}
898
899void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
900 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000901
Richard Smith030f4992011-04-15 13:38:57 +0000902 if (const TemplateTemplateParmDecl *TTP =
903 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000904 Out << "class ";
905 if (TTP->isParameterPack())
906 Out << "...";
907 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000908 } else {
909 Visit(D->getTemplatedDecl());
910 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000911}
912
Richard Trieu82398f92011-07-28 00:19:05 +0000913void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
914 if (PrintInstantiation) {
915 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000916 for (auto *I : D->specializations()) {
917 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
918 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000919 }
920 }
921
922 return VisitRedeclarableTemplateDecl(D);
923}
924
925void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
926 if (PrintInstantiation) {
927 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000928 for (auto *I : D->specializations()) {
929 PrintTemplateParameters(Params, &I->getTemplateArgs());
930 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000931 Out << '\n';
932 }
933 }
934
935 return VisitRedeclarableTemplateDecl(D);
936}
937
Douglas Gregor278f52e2009-05-30 00:08:05 +0000938//----------------------------------------------------------------------------
939// Objective-C declarations
940//----------------------------------------------------------------------------
941
Douglas Gregor813a0662015-06-19 18:14:38 +0000942void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
943 Decl::ObjCDeclQualifier Quals,
944 QualType T) {
945 Out << '(';
946 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
947 Out << "in ";
948 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
949 Out << "inout ";
950 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
951 Out << "out ";
952 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
953 Out << "bycopy ";
954 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
955 Out << "byref ";
956 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
957 Out << "oneway ";
958 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000959 if (auto nullability = AttributedType::stripOuterNullability(T))
960 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +0000961 }
962
963 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
964 Out << ')';
965}
966
Douglas Gregor85f3f952015-07-07 03:57:15 +0000967void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
968 Out << "<";
969 unsigned First = true;
970 for (auto *Param : *Params) {
971 if (First) {
972 First = false;
973 } else {
974 Out << ", ";
975 }
976
977 Out << Param->getDeclName().getAsString();
978
979 if (Param->hasExplicitBound()) {
980 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
981 }
982 }
983 Out << ">";
984}
985
Douglas Gregor278f52e2009-05-30 00:08:05 +0000986void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
987 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000988 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +0000989 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000990 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +0000991 if (!OMD->getReturnType().isNull()) {
992 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
993 OMD->getReturnType());
994 }
Mike Stump11289f42009-09-09 15:08:12 +0000995
Douglas Gregor278f52e2009-05-30 00:08:05 +0000996 std::string name = OMD->getSelector().getAsString();
997 std::string::size_type pos, lastPos = 0;
Aaron Ballman43b68be2014-03-07 17:50:17 +0000998 for (const auto *PI : OMD->params()) {
Mike Stump11289f42009-09-09 15:08:12 +0000999 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001000 pos = name.find_first_of(':', lastPos);
Douglas Gregor813a0662015-06-19 18:14:38 +00001001 Out << " " << name.substr(lastPos, pos - lastPos) << ':';
1002 PrintObjCMethodType(OMD->getASTContext(),
1003 PI->getObjCDeclQualifier(),
1004 PI->getType());
1005 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001006 lastPos = pos + 1;
1007 }
Mike Stump11289f42009-09-09 15:08:12 +00001008
Douglas Gregor278f52e2009-05-30 00:08:05 +00001009 if (OMD->param_begin() == OMD->param_end())
1010 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +00001011
Douglas Gregor278f52e2009-05-30 00:08:05 +00001012 if (OMD->isVariadic())
1013 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001014
1015 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001016
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001017 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001018 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001019 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001020 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001021 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001022 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001023}
1024
1025void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1026 std::string I = OID->getNameAsString();
1027 ObjCInterfaceDecl *SID = OID->getSuperClass();
1028
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001029 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001030 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001031 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001032 else
1033 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001034
1035 if (OID->ivar_size() > 0) {
1036 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001037 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001038 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001039 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001040 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001041 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001042 }
1043 Indentation -= Policy.Indentation;
1044 Out << "}\n";
1045 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001046 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1047 Out << "\n";
1048 eolnOut = true;
1049 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001050 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001051 if (!eolnOut)
1052 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001053 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001054}
1055
1056void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1057 std::string I = OID->getNameAsString();
1058 ObjCInterfaceDecl *SID = OID->getSuperClass();
1059
Douglas Gregordc9166c2011-12-15 20:29:51 +00001060 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001061 Out << "@class " << I;
1062
1063 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1064 PrintObjCTypeParams(TypeParams);
1065 }
1066
1067 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001068 return;
1069 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001070 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001071 Out << "@interface " << I;
1072
1073 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1074 PrintObjCTypeParams(TypeParams);
1075 }
1076
Douglas Gregor278f52e2009-05-30 00:08:05 +00001077 if (SID)
Douglas Gregor85f3f952015-07-07 03:57:15 +00001078 Out << " : " << OID->getSuperClass()->getName();
Mike Stump11289f42009-09-09 15:08:12 +00001079
Douglas Gregor278f52e2009-05-30 00:08:05 +00001080 // Protocols?
1081 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1082 if (!Protocols.empty()) {
1083 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1084 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001085 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001086 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001087 }
Mike Stump11289f42009-09-09 15:08:12 +00001088
Douglas Gregor278f52e2009-05-30 00:08:05 +00001089 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001090 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001091 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001092 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001093 for (const auto *I : OID->ivars()) {
1094 Indent() << I->getASTContext()
1095 .getUnqualifiedObjCPointerType(I->getType())
1096 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001097 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001098 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001099 Out << "}\n";
1100 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001101 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001102 Out << "\n";
1103 eolnOut = true;
1104 }
Mike Stump11289f42009-09-09 15:08:12 +00001105
Douglas Gregor278f52e2009-05-30 00:08:05 +00001106 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001107 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001108 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001109 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001110 // FIXME: implement the rest...
1111}
1112
Douglas Gregor278f52e2009-05-30 00:08:05 +00001113void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001114 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001115 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001116 return;
1117 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001118 // Protocols?
1119 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1120 if (!Protocols.empty()) {
1121 Out << "@protocol " << *PID;
1122 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1123 E = Protocols.end(); I != E; ++I)
1124 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1125 Out << ">\n";
1126 } else
1127 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001128 VisitDeclContext(PID, false);
1129 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001130}
1131
1132void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001133 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001134
1135 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001136 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001137 // FIXME: implement the rest...
1138}
1139
1140void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001141 Out << "@interface " << *PID->getClassInterface();
1142 if (auto TypeParams = PID->getTypeParamList()) {
1143 PrintObjCTypeParams(TypeParams);
1144 }
1145 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001146 if (PID->ivar_size() > 0) {
1147 Out << "{\n";
1148 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001149 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001150 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001151 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001152 Indentation -= Policy.Indentation;
1153 Out << "}\n";
1154 }
1155
Douglas Gregor278f52e2009-05-30 00:08:05 +00001156 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001157 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001158
Douglas Gregor278f52e2009-05-30 00:08:05 +00001159 // FIXME: implement the rest...
1160}
1161
1162void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001163 Out << "@compatibility_alias " << *AID
1164 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001165}
1166
1167/// PrintObjCPropertyDecl - print a property declaration.
1168///
1169void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1170 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1171 Out << "@required\n";
1172 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1173 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001174
Douglas Gregor813a0662015-06-19 18:14:38 +00001175 QualType T = PDecl->getType();
1176
Douglas Gregor278f52e2009-05-30 00:08:05 +00001177 Out << "@property";
1178 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1179 bool first = true;
1180 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001181 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001182 ObjCPropertyDecl::OBJC_PR_readonly) {
1183 Out << (first ? ' ' : ',') << "readonly";
1184 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001185 }
Mike Stump11289f42009-09-09 15:08:12 +00001186
Ted Kremenek897af912011-08-17 21:09:35 +00001187 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001188 Out << (first ? ' ' : ',') << "getter = ";
1189 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001190 first = false;
1191 }
1192 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001193 Out << (first ? ' ' : ',') << "setter = ";
1194 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001195 first = false;
1196 }
Mike Stump11289f42009-09-09 15:08:12 +00001197
Ted Kremenek897af912011-08-17 21:09:35 +00001198 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1199 Out << (first ? ' ' : ',') << "assign";
1200 first = false;
1201 }
Mike Stump11289f42009-09-09 15:08:12 +00001202
Ted Kremenek897af912011-08-17 21:09:35 +00001203 if (PDecl->getPropertyAttributes() &
1204 ObjCPropertyDecl::OBJC_PR_readwrite) {
1205 Out << (first ? ' ' : ',') << "readwrite";
1206 first = false;
1207 }
Mike Stump11289f42009-09-09 15:08:12 +00001208
Ted Kremenek897af912011-08-17 21:09:35 +00001209 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1210 Out << (first ? ' ' : ',') << "retain";
1211 first = false;
1212 }
Mike Stump11289f42009-09-09 15:08:12 +00001213
Ted Kremenek897af912011-08-17 21:09:35 +00001214 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1215 Out << (first ? ' ' : ',') << "strong";
1216 first = false;
1217 }
John McCall31168b02011-06-15 23:02:42 +00001218
Ted Kremenek897af912011-08-17 21:09:35 +00001219 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1220 Out << (first ? ' ' : ',') << "copy";
1221 first = false;
1222 }
Mike Stump11289f42009-09-09 15:08:12 +00001223
Ted Kremenek897af912011-08-17 21:09:35 +00001224 if (PDecl->getPropertyAttributes() &
1225 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1226 Out << (first ? ' ' : ',') << "nonatomic";
1227 first = false;
1228 }
1229 if (PDecl->getPropertyAttributes() &
1230 ObjCPropertyDecl::OBJC_PR_atomic) {
1231 Out << (first ? ' ' : ',') << "atomic";
1232 first = false;
1233 }
1234
Douglas Gregor813a0662015-06-19 18:14:38 +00001235 if (PDecl->getPropertyAttributes() &
1236 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001237 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001238 if (*nullability == NullabilityKind::Unspecified &&
1239 (PDecl->getPropertyAttributes() &
1240 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1241 Out << (first ? ' ' : ',') << "null_resettable";
1242 } else {
1243 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001244 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001245 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001246 first = false;
1247 }
1248 }
1249
Ted Kremenek897af912011-08-17 21:09:35 +00001250 (void) first; // Silence dead store warning due to idiomatic code.
1251 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001252 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001253 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001254 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001255 if (Policy.PolishForDeclaration)
1256 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001257}
1258
1259void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1260 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001261 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001262 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001263 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001264 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001265 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001266 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001267}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001268
1269void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001270 if (!D->isAccessDeclaration())
1271 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001272 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001273 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001274 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001275 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001276}
1277
John McCalle61f2ba2009-11-18 02:36:19 +00001278void
1279DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1280 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001281 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001282 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001283}
1284
1285void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001286 if (!D->isAccessDeclaration())
1287 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001288 D->getQualifier()->print(Out, Policy);
Fariborz Jahanian3ec39212012-12-06 00:09:40 +00001289 Out << D->getName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001290}
John McCall3f746822009-11-17 05:59:44 +00001291
1292void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1293 // ignore
1294}
Alexey Bataeva769e072013-03-22 06:34:35 +00001295
1296void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1297 Out << "#pragma omp threadprivate";
1298 if (!D->varlist_empty()) {
1299 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1300 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001301 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001302 Out << (I == D->varlist_begin() ? '(' : ',');
1303 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1304 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001305 }
1306 Out << ")";
1307 }
1308}
1309