blob: 6a3e8e2ae1a016110162576a89340df872422ac3 [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);
Alexey Bataev6d455322015-10-12 06:59:48 +000099 void prettyPrintPragmas(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +0000100 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000101 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000102}
Douglas Gregor278f52e2009-05-30 00:08:05 +0000103
Richard Trieu82398f92011-07-28 00:19:05 +0000104void Decl::print(raw_ostream &Out, unsigned Indentation,
105 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +0000106 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000107}
108
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000109void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000110 unsigned Indentation, bool PrintInstantiation) const {
Richard Smith235341b2012-08-16 03:56:14 +0000111 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000112 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000113}
114
Eli Friedman79635842009-05-30 04:20:30 +0000115static QualType GetBaseType(QualType T) {
116 // FIXME: This should be on the Type class!
117 QualType BaseType = T;
118 while (!BaseType->isSpecifierType()) {
119 if (isa<TypedefType>(BaseType))
120 break;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000121 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000122 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000123 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
124 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000125 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
126 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000127 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000128 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000129 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000130 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000131 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
132 BaseType = RTy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000133 else
David Blaikie83d382b2011-09-23 05:06:16 +0000134 llvm_unreachable("Unknown declarator!");
Eli Friedman79635842009-05-30 04:20:30 +0000135 }
136 return BaseType;
137}
138
139static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000140 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000141 return TDD->getUnderlyingType();
142 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
143 return VD->getType();
144 return QualType();
145}
146
147void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000148 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000149 unsigned Indentation) {
150 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000151 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000152 return;
153 }
154
155 Decl** End = Begin + NumDecls;
156 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
157 if (TD)
158 ++Begin;
159
160 PrintingPolicy SubPolicy(Policy);
John McCallf937c022011-10-07 06:10:15 +0000161 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000162 TD->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000163 Out << " ";
164 SubPolicy.SuppressTag = true;
165 }
166
167 bool isFirst = true;
168 for ( ; Begin != End; ++Begin) {
169 if (isFirst) {
170 SubPolicy.SuppressSpecifiers = false;
171 isFirst = false;
172 } else {
173 if (!isFirst) Out << ", ";
174 SubPolicy.SuppressSpecifiers = true;
175 }
176
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000177 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000178 }
179}
180
Alp Tokeref6b0072014-01-04 13:47:14 +0000181LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000182 // Get the translation unit
183 const DeclContext *DC = this;
184 while (!DC->isTranslationUnit())
185 DC = DC->getParent();
186
187 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith235341b2012-08-16 03:56:14 +0000188 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000189 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
190}
191
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000192raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000193 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000194 Out << " ";
195 return Out;
196}
197
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000198void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000199 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000200 return;
Alexey Bataev6d455322015-10-12 06:59:48 +0000201
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000202 if (D->hasAttrs()) {
203 AttrVec &Attrs = D->getAttrs();
Alexey Bataev6d455322015-10-12 06:59:48 +0000204 for (auto *A : Attrs) {
205 switch (A->getKind()) {
206#define ATTR(X)
207#define PRAGMA_SPELLING_ATTR(X) case attr::X:
208#include "clang/Basic/AttrList.inc"
209 break;
210 default:
211 A->printPretty(Out, Policy);
212 break;
213 }
214 }
215 }
216}
217
218void DeclPrinter::prettyPrintPragmas(Decl *D) {
219 if (Policy.PolishForDeclaration)
220 return;
221
222 if (D->hasAttrs()) {
223 AttrVec &Attrs = D->getAttrs();
224 for (auto *A : Attrs) {
225 switch (A->getKind()) {
226#define ATTR(X)
227#define PRAGMA_SPELLING_ATTR(X) case attr::X:
228#include "clang/Basic/AttrList.inc"
229 A->printPretty(Out, Policy);
230 Indent();
231 break;
232 default:
233 break;
234 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000235 }
236 }
237}
238
Richard Smitha4bb2922014-07-23 03:17:06 +0000239void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
240 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
241 // template argument), but if it is the type of a declaration, the ellipsis
242 // is placed before the name being declared.
243 if (auto *PET = T->getAs<PackExpansionType>()) {
244 Pack = true;
245 T = PET->getPattern();
246 }
247 T.print(Out, Policy, (Pack ? "..." : "") + DeclName);
248}
249
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000250void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000251 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000252 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000253 Out << ";\n";
254 Decls.clear();
255
256}
257
Anders Carlsson601d6e42009-08-28 22:39:52 +0000258void DeclPrinter::Print(AccessSpecifier AS) {
259 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000260 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000261 case AS_public: Out << "public"; break;
262 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000263 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000264 }
265}
266
Douglas Gregor278f52e2009-05-30 00:08:05 +0000267//----------------------------------------------------------------------------
268// Common C declarations
269//----------------------------------------------------------------------------
270
271void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000272 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000273 return;
274
Douglas Gregor278f52e2009-05-30 00:08:05 +0000275 if (Indent)
276 Indentation += Policy.Indentation;
277
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000278 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000279 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000280 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000281
282 // Don't print ObjCIvarDecls, as they are printed when visiting the
283 // containing ObjCInterfaceDecl.
284 if (isa<ObjCIvarDecl>(*D))
285 continue;
286
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000287 // Skip over implicit declarations in pretty-printing mode.
288 if (D->isImplicit())
289 continue;
290
Eli Friedman79635842009-05-30 04:20:30 +0000291 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
292 // forced to merge the declarations because there's no other way to
293 // refer to the struct in question. This limited merging is safe without
294 // a bunch of other checks because it only merges declarations directly
295 // referring to the tag, not typedefs.
296 //
297 // Check whether the current declaration should be grouped with a previous
298 // unnamed struct.
299 QualType CurDeclType = getDeclType(*D);
300 if (!Decls.empty() && !CurDeclType.isNull()) {
301 QualType BaseType = GetBaseType(CurDeclType);
Eli Friedman24b3fed2013-08-12 21:54:04 +0000302 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
303 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
Eli Friedman79635842009-05-30 04:20:30 +0000304 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
305 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
306 Decls.push_back(*D);
307 continue;
308 }
309 }
310
311 // If we have a merged group waiting to be handled, handle it now.
312 if (!Decls.empty())
313 ProcessDeclGroup(Decls);
314
315 // If the current declaration is an unnamed tag type, save it
316 // so we can merge it with the subsequent declaration(s) using it.
317 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
318 Decls.push_back(*D);
319 continue;
320 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000321
322 if (isa<AccessSpecDecl>(*D)) {
323 Indentation -= Policy.Indentation;
324 this->Indent();
325 Print(D->getAccess());
326 Out << ":\n";
327 Indentation += Policy.Indentation;
328 continue;
329 }
330
Douglas Gregor278f52e2009-05-30 00:08:05 +0000331 this->Indent();
332 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000333
334 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000335 const char *Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000336 if (isa<OMPThreadPrivateDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000337 Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000338 else if (isa<FunctionDecl>(*D) &&
339 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
Yaron Keren51db8772014-05-07 09:53:02 +0000340 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000341 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000342 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000343 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000344 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000345 isa<ObjCInterfaceDecl>(*D) ||
346 isa<ObjCProtocolDecl>(*D) ||
347 isa<ObjCCategoryImplDecl>(*D) ||
348 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000349 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000350 else if (isa<EnumConstantDecl>(*D)) {
351 DeclContext::decl_iterator Next = D;
352 ++Next;
353 if (Next != DEnd)
354 Terminator = ",";
355 } else
356 Terminator = ";";
357
358 if (Terminator)
359 Out << Terminator;
360 Out << "\n";
361 }
362
Eli Friedman79635842009-05-30 04:20:30 +0000363 if (!Decls.empty())
364 ProcessDeclGroup(Decls);
365
Douglas Gregor278f52e2009-05-30 00:08:05 +0000366 if (Indent)
367 Indentation -= Policy.Indentation;
368}
369
370void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
371 VisitDeclContext(D, false);
372}
373
374void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000375 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000376 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000377
378 if (D->isModulePrivate())
379 Out << "__module_private__ ";
380 }
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000381 D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000382 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000383}
384
Richard Smithdda56e42011-04-15 14:24:37 +0000385void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000386 Out << "using " << *D;
387 prettyPrintAttributes(D);
388 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000389}
390
Douglas Gregor278f52e2009-05-30 00:08:05 +0000391void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000392 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
393 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000394 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000395 if (D->isScoped()) {
396 if (D->isScopedUsingClassTag())
397 Out << "class ";
398 else
399 Out << "struct ";
400 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000401 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000402
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000403 if (D->isFixed())
404 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000405
John McCallf937c022011-10-07 06:10:15 +0000406 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000407 Out << " {\n";
408 VisitDeclContext(D);
409 Indent() << "}";
410 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000411 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000412}
413
414void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000415 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
416 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000417 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000418
419 prettyPrintAttributes(D);
420
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000421 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000422 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000423
John McCallf937c022011-10-07 06:10:15 +0000424 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000425 Out << " {\n";
426 VisitDeclContext(D);
427 Indent() << "}";
428 }
429}
430
431void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000432 Out << *D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000433 if (Expr *Init = D->getInitExpr()) {
434 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000435 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000436 }
437}
438
Mike Stump11289f42009-09-09 15:08:12 +0000439void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000440 if (!D->getDescribedFunctionTemplate() &&
441 !D->isFunctionTemplateSpecialization())
442 prettyPrintPragmas(D);
443
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000444 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000445 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000446 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000447 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000448 case SC_None: break;
449 case SC_Extern: Out << "extern "; break;
450 case SC_Static: Out << "static "; break;
451 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000452 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000453 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000454 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000455
Douglas Gregor26701a42011-09-09 02:06:17 +0000456 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000457 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000458 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000459 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000460 if ((CDecl && CDecl->isExplicitSpecified()) ||
461 (ConversionDecl && ConversionDecl->isExplicit()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000462 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000463 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000464
Douglas Gregor2d042f12009-05-30 05:39:39 +0000465 PrintingPolicy SubPolicy(Policy);
466 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000467 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000468
Abramo Bagnara6d810632010-12-14 22:11:44 +0000469 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000470 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000471 Proto = '(' + Proto + ')';
472 Ty = PT->getInnerType();
473 }
474
Reid Kleckner0503a872013-12-05 01:23:43 +0000475 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000476 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000477 if (D->hasWrittenPrototype())
478 FT = dyn_cast<FunctionProtoType>(AFT);
479
480 Proto += "(";
481 if (FT) {
482 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000483 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000484 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
485 if (i) POut << ", ";
486 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
487 }
Mike Stump11289f42009-09-09 15:08:12 +0000488
Douglas Gregor278f52e2009-05-30 00:08:05 +0000489 if (FT->isVariadic()) {
490 if (D->getNumParams()) POut << ", ";
491 POut << "...";
492 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000493 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000494 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
495 if (i)
496 Proto += ", ";
497 Proto += D->getParamDecl(i)->getNameAsString();
498 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000499 }
500
501 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000502
David Blaikief5697e52012-08-10 00:55:35 +0000503 if (FT) {
504 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000505 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000506 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000507 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000508 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000509 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000510
511 switch (FT->getRefQualifier()) {
512 case RQ_None:
513 break;
514 case RQ_LValue:
515 Proto += " &";
516 break;
517 case RQ_RValue:
518 Proto += " &&";
519 break;
520 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000521 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000522
523 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000524 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000525 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000526 Proto += "...";
527 else
528 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
529 if (I)
530 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000531
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000532 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000533 }
534 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000535 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
536 Proto += " noexcept";
537 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
538 Proto += "(";
539 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000540 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000541 Indentation);
542 EOut.flush();
543 Proto += EOut.str();
544 Proto += ")";
545 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000546 }
547
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000548 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000549 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000550 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000551 if (BMInitializer->isInClassMemberInitializer())
552 continue;
553
554 if (!HasInitializerList) {
555 Proto += " : ";
556 Out << Proto;
557 Proto.clear();
558 HasInitializerList = true;
559 } else
560 Out << ", ";
561
562 if (BMInitializer->isAnyMemberInitializer()) {
563 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000564 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000565 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000566 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000567 }
568
569 Out << "(";
570 if (!BMInitializer->getInit()) {
571 // Nothing to print
572 } else {
573 Expr *Init = BMInitializer->getInit();
574 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
575 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000576
Richard Smith938f40b2011-06-11 17:19:42 +0000577 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000578
579 Expr *SimpleInit = nullptr;
580 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000581 unsigned NumArgs = 0;
582 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
583 Args = ParenList->getExprs();
584 NumArgs = ParenList->getNumExprs();
585 } else if (CXXConstructExpr *Construct
586 = dyn_cast<CXXConstructExpr>(Init)) {
587 Args = Construct->getArgs();
588 NumArgs = Construct->getNumArgs();
589 } else
590 SimpleInit = Init;
591
592 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000593 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000594 else {
595 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000596 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000597 if (isa<CXXDefaultArgExpr>(Args[I]))
598 break;
599
600 if (I)
601 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000602 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000603 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000604 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000605 }
Richard Smith938f40b2011-06-11 17:19:42 +0000606 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000607 if (BMInitializer->isPackExpansion())
608 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000609 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000610 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000611 if (FT && FT->hasTrailingReturn()) {
612 Out << "auto " << Proto << " -> ";
613 Proto.clear();
614 }
Alp Toker314cc812014-01-25 16:55:45 +0000615 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000616 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000617 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000618 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000619 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000620 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000621 }
622
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000623 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000624
625 if (D->isPure())
626 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000627 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000628 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000629 else if (D->isExplicitlyDefaulted())
630 Out << " = default";
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000631 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000632 if (!D->hasPrototype() && D->getNumParams()) {
633 // This is a K&R function definition, so we need to print the
634 // parameters.
635 Out << '\n';
Richard Smith235341b2012-08-16 03:56:14 +0000636 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000637 Indentation += Policy.Indentation;
638 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
639 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000640 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000641 Out << ";\n";
642 }
643 Indentation -= Policy.Indentation;
644 } else
645 Out << ' ';
646
Richard Trieuddd01ce2014-06-09 22:53:25 +0000647 if (D->getBody())
648 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000649 Out << '\n';
650 }
651}
652
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000653void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
654 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000655 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
656 for (unsigned i = 0; i < NumTPLists; ++i)
657 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000658 Out << "friend ";
659 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000660 }
661 else if (FunctionDecl *FD =
662 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
663 Out << "friend ";
664 VisitFunctionDecl(FD);
665 }
666 else if (FunctionTemplateDecl *FTD =
667 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
668 Out << "friend ";
669 VisitFunctionTemplateDecl(FTD);
670 }
671 else if (ClassTemplateDecl *CTD =
672 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
673 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000674 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000675 }
676}
677
Douglas Gregor278f52e2009-05-30 00:08:05 +0000678void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000679 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000680 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000681 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000682 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
683 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000684
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000685 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Fariborz Jahanian9e075842013-05-01 17:28:37 +0000686 stream(Policy, D->getName());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000687
688 if (D->isBitField()) {
689 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000690 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000691 }
Richard Smith938f40b2011-06-11 17:19:42 +0000692
693 Expr *Init = D->getInClassInitializer();
694 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000695 if (D->getInClassInitStyle() == ICIS_ListInit)
696 Out << " ";
697 else
698 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000699 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000700 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000701 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000702}
703
Chris Lattnercab02a62011-02-17 20:34:02 +0000704void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000705 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000706}
707
Douglas Gregor278f52e2009-05-30 00:08:05 +0000708void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000709 prettyPrintPragmas(D);
Richard Smithfd3834f2013-04-13 02:43:54 +0000710 if (!Policy.SuppressSpecifiers) {
711 StorageClass SC = D->getStorageClass();
712 if (SC != SC_None)
713 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000714
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000715 switch (D->getTSCSpec()) {
716 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000717 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000718 case TSCS___thread:
719 Out << "__thread ";
720 break;
721 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000722 Out << "_Thread_local ";
723 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000724 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000725 Out << "thread_local ";
726 break;
727 }
728
729 if (D->isModulePrivate())
730 Out << "__module_private__ ";
731 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000732
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000733 QualType T = D->getTypeSourceInfo()
734 ? D->getTypeSourceInfo()->getType()
735 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
Richard Smitha4bb2922014-07-23 03:17:06 +0000736 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000737 Expr *Init = D->getInit();
738 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000739 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000740 if (CXXConstructExpr *Construct =
741 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000742 if (D->getInitStyle() == VarDecl::CallInit &&
743 !Construct->isListInitialization()) {
744 ImplicitInit = Construct->getNumArgs() == 0 ||
745 Construct->getArg(0)->isDefaultArgument();
746 }
747 }
Sebastian Redla9351792012-02-11 23:51:47 +0000748 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000749 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000750 Out << "(";
751 else if (D->getInitStyle() == VarDecl::CInit) {
752 Out << " = ";
753 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000754 PrintingPolicy SubPolicy(Policy);
755 SubPolicy.SuppressSpecifiers = false;
756 SubPolicy.SuppressTag = false;
757 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000758 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000759 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000760 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000761 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000762 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000763}
764
765void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
766 VisitVarDecl(D);
767}
768
769void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
770 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000771 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000772 Out << ")";
773}
774
Douglas Gregorba345522011-12-02 23:23:56 +0000775void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000776 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000777 << ";\n";
778}
779
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000780void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
781 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000782 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000783 if (StringLiteral *SL = D->getMessage()) {
784 Out << ", ";
785 SL->printPretty(Out, nullptr, Policy, Indentation);
786 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000787 Out << ")";
788}
789
Douglas Gregor278f52e2009-05-30 00:08:05 +0000790//----------------------------------------------------------------------------
791// C++ declarations
792//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000793void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000794 if (D->isInline())
795 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000796 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000797 VisitDeclContext(D);
798 Indent() << "}";
799}
800
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000801void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
802 Out << "using namespace ";
803 if (D->getQualifier())
804 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000805 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000806}
807
Douglas Gregor18231932009-05-30 06:48:27 +0000808void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000809 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000810 if (D->getQualifier())
811 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000812 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000813}
814
Michael Han84324352013-02-22 17:15:32 +0000815void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
816 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000817}
818
Douglas Gregor5f478b72009-05-30 06:58:37 +0000819void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000820 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000821 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
822 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000823 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000824
825 prettyPrintAttributes(D);
826
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000827 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000828 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000829
John McCallf937c022011-10-07 06:10:15 +0000830 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000831 // Print the base classes
832 if (D->getNumBases()) {
833 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000834 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
835 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000836 if (Base != D->bases_begin())
837 Out << ", ";
838
839 if (Base->isVirtual())
840 Out << "virtual ";
841
Anders Carlsson6df9e072009-08-29 20:36:12 +0000842 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000843 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000844 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000845 Out << " ";
846 }
847 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000848
849 if (Base->isPackExpansion())
850 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000851 }
852 }
853
854 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000855 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000856 Out << " {\n";
857 VisitDeclContext(D);
858 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000859 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000860}
861
862void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
863 const char *l;
864 if (D->getLanguage() == LinkageSpecDecl::lang_c)
865 l = "C";
866 else {
867 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
868 "unknown language in linkage specification");
869 l = "C++";
870 }
871
872 Out << "extern \"" << l << "\" ";
873 if (D->hasBraces()) {
874 Out << "{\n";
875 VisitDeclContext(D);
876 Indent() << "}";
877 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000878 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000879}
880
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000881void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
882 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000883 assert(Params);
884 assert(!Args || Params->size() == Args->size());
885
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000886 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000887
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000888 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
889 if (i != 0)
890 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000891
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000892 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000893 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000894 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000895
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000896 if (TTP->wasDeclaredWithTypename())
897 Out << "typename ";
898 else
899 Out << "class ";
900
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000901 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000902 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000903
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000904 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000905
Richard Trieu82398f92011-07-28 00:19:05 +0000906 if (Args) {
907 Out << " = ";
908 Args->get(i).print(Policy, Out);
909 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000910 Out << " = ";
911 Out << TTP->getDefaultArgument().getAsString(Policy);
912 };
Mike Stump11289f42009-09-09 15:08:12 +0000913 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000914 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000915 StringRef Name;
916 if (IdentifierInfo *II = NTTP->getIdentifier())
917 Name = II->getName();
918 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000919
Richard Trieu82398f92011-07-28 00:19:05 +0000920 if (Args) {
921 Out << " = ";
922 Args->get(i).print(Policy, Out);
923 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000924 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000925 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
926 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000927 }
Richard Smith030f4992011-04-15 13:38:57 +0000928 } else if (const TemplateTemplateParmDecl *TTPD =
929 dyn_cast<TemplateTemplateParmDecl>(Param)) {
930 VisitTemplateDecl(TTPD);
931 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000932 }
933 }
Mike Stump11289f42009-09-09 15:08:12 +0000934
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000935 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000936}
937
938void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
939 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000940
Richard Smith030f4992011-04-15 13:38:57 +0000941 if (const TemplateTemplateParmDecl *TTP =
942 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000943 Out << "class ";
944 if (TTP->isParameterPack())
945 Out << "...";
946 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000947 } else {
948 Visit(D->getTemplatedDecl());
949 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000950}
951
Richard Trieu82398f92011-07-28 00:19:05 +0000952void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
953 if (PrintInstantiation) {
954 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000955 for (auto *I : D->specializations()) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000956 prettyPrintPragmas(I);
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000957 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
958 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000959 }
960 }
961
Alexey Bataev6d455322015-10-12 06:59:48 +0000962 prettyPrintPragmas(D->getTemplatedDecl());
Richard Trieu82398f92011-07-28 00:19:05 +0000963 return VisitRedeclarableTemplateDecl(D);
964}
965
966void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
967 if (PrintInstantiation) {
968 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000969 for (auto *I : D->specializations()) {
970 PrintTemplateParameters(Params, &I->getTemplateArgs());
971 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000972 Out << '\n';
973 }
974 }
975
976 return VisitRedeclarableTemplateDecl(D);
977}
978
Douglas Gregor278f52e2009-05-30 00:08:05 +0000979//----------------------------------------------------------------------------
980// Objective-C declarations
981//----------------------------------------------------------------------------
982
Douglas Gregor813a0662015-06-19 18:14:38 +0000983void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
984 Decl::ObjCDeclQualifier Quals,
985 QualType T) {
986 Out << '(';
987 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
988 Out << "in ";
989 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
990 Out << "inout ";
991 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
992 Out << "out ";
993 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
994 Out << "bycopy ";
995 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
996 Out << "byref ";
997 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
998 Out << "oneway ";
999 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001000 if (auto nullability = AttributedType::stripOuterNullability(T))
1001 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001002 }
1003
1004 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1005 Out << ')';
1006}
1007
Douglas Gregor85f3f952015-07-07 03:57:15 +00001008void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1009 Out << "<";
1010 unsigned First = true;
1011 for (auto *Param : *Params) {
1012 if (First) {
1013 First = false;
1014 } else {
1015 Out << ", ";
1016 }
1017
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001018 switch (Param->getVariance()) {
1019 case ObjCTypeParamVariance::Invariant:
1020 break;
1021
1022 case ObjCTypeParamVariance::Covariant:
1023 Out << "__covariant ";
1024 break;
1025
1026 case ObjCTypeParamVariance::Contravariant:
1027 Out << "__contravariant ";
1028 break;
1029 }
1030
Douglas Gregor85f3f952015-07-07 03:57:15 +00001031 Out << Param->getDeclName().getAsString();
1032
1033 if (Param->hasExplicitBound()) {
1034 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1035 }
1036 }
1037 Out << ">";
1038}
1039
Douglas Gregor278f52e2009-05-30 00:08:05 +00001040void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1041 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001042 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001043 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001044 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001045 if (!OMD->getReturnType().isNull()) {
1046 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1047 OMD->getReturnType());
1048 }
Mike Stump11289f42009-09-09 15:08:12 +00001049
Douglas Gregor278f52e2009-05-30 00:08:05 +00001050 std::string name = OMD->getSelector().getAsString();
1051 std::string::size_type pos, lastPos = 0;
Aaron Ballman43b68be2014-03-07 17:50:17 +00001052 for (const auto *PI : OMD->params()) {
Mike Stump11289f42009-09-09 15:08:12 +00001053 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001054 pos = name.find_first_of(':', lastPos);
Douglas Gregor813a0662015-06-19 18:14:38 +00001055 Out << " " << name.substr(lastPos, pos - lastPos) << ':';
1056 PrintObjCMethodType(OMD->getASTContext(),
1057 PI->getObjCDeclQualifier(),
1058 PI->getType());
1059 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001060 lastPos = pos + 1;
1061 }
Mike Stump11289f42009-09-09 15:08:12 +00001062
Douglas Gregor278f52e2009-05-30 00:08:05 +00001063 if (OMD->param_begin() == OMD->param_end())
1064 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +00001065
Douglas Gregor278f52e2009-05-30 00:08:05 +00001066 if (OMD->isVariadic())
1067 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001068
1069 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001070
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001071 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001072 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001073 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001074 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001075 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001076 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001077}
1078
1079void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1080 std::string I = OID->getNameAsString();
1081 ObjCInterfaceDecl *SID = OID->getSuperClass();
1082
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001083 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001084 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001085 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001086 else
1087 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001088
1089 if (OID->ivar_size() > 0) {
1090 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001091 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001092 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001093 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001094 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001095 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001096 }
1097 Indentation -= Policy.Indentation;
1098 Out << "}\n";
1099 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001100 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1101 Out << "\n";
1102 eolnOut = true;
1103 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001104 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001105 if (!eolnOut)
1106 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001107 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001108}
1109
1110void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1111 std::string I = OID->getNameAsString();
1112 ObjCInterfaceDecl *SID = OID->getSuperClass();
1113
Douglas Gregordc9166c2011-12-15 20:29:51 +00001114 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001115 Out << "@class " << I;
1116
1117 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1118 PrintObjCTypeParams(TypeParams);
1119 }
1120
1121 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001122 return;
1123 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001124 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001125 Out << "@interface " << I;
1126
1127 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1128 PrintObjCTypeParams(TypeParams);
1129 }
1130
Douglas Gregor278f52e2009-05-30 00:08:05 +00001131 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001132 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001133
Douglas Gregor278f52e2009-05-30 00:08:05 +00001134 // Protocols?
1135 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1136 if (!Protocols.empty()) {
1137 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1138 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001139 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001140 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001141 }
Mike Stump11289f42009-09-09 15:08:12 +00001142
Douglas Gregor278f52e2009-05-30 00:08:05 +00001143 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001144 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001145 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001146 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001147 for (const auto *I : OID->ivars()) {
1148 Indent() << I->getASTContext()
1149 .getUnqualifiedObjCPointerType(I->getType())
1150 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001151 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001152 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001153 Out << "}\n";
1154 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001155 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001156 Out << "\n";
1157 eolnOut = true;
1158 }
Mike Stump11289f42009-09-09 15:08:12 +00001159
Douglas Gregor278f52e2009-05-30 00:08:05 +00001160 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001161 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001162 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001163 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001164 // FIXME: implement the rest...
1165}
1166
Douglas Gregor278f52e2009-05-30 00:08:05 +00001167void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001168 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001169 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001170 return;
1171 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001172 // Protocols?
1173 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1174 if (!Protocols.empty()) {
1175 Out << "@protocol " << *PID;
1176 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1177 E = Protocols.end(); I != E; ++I)
1178 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1179 Out << ">\n";
1180 } else
1181 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001182 VisitDeclContext(PID, false);
1183 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001184}
1185
1186void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001187 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001188
1189 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001190 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001191 // FIXME: implement the rest...
1192}
1193
1194void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001195 Out << "@interface " << *PID->getClassInterface();
1196 if (auto TypeParams = PID->getTypeParamList()) {
1197 PrintObjCTypeParams(TypeParams);
1198 }
1199 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001200 if (PID->ivar_size() > 0) {
1201 Out << "{\n";
1202 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001203 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001204 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001205 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001206 Indentation -= Policy.Indentation;
1207 Out << "}\n";
1208 }
1209
Douglas Gregor278f52e2009-05-30 00:08:05 +00001210 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001211 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001212
Douglas Gregor278f52e2009-05-30 00:08:05 +00001213 // FIXME: implement the rest...
1214}
1215
1216void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001217 Out << "@compatibility_alias " << *AID
1218 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001219}
1220
1221/// PrintObjCPropertyDecl - print a property declaration.
1222///
1223void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1224 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1225 Out << "@required\n";
1226 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1227 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001228
Douglas Gregor813a0662015-06-19 18:14:38 +00001229 QualType T = PDecl->getType();
1230
Douglas Gregor278f52e2009-05-30 00:08:05 +00001231 Out << "@property";
1232 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1233 bool first = true;
1234 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001235 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001236 ObjCPropertyDecl::OBJC_PR_readonly) {
1237 Out << (first ? ' ' : ',') << "readonly";
1238 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001239 }
Mike Stump11289f42009-09-09 15:08:12 +00001240
Ted Kremenek897af912011-08-17 21:09:35 +00001241 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001242 Out << (first ? ' ' : ',') << "getter = ";
1243 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001244 first = false;
1245 }
1246 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001247 Out << (first ? ' ' : ',') << "setter = ";
1248 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001249 first = false;
1250 }
Mike Stump11289f42009-09-09 15:08:12 +00001251
Ted Kremenek897af912011-08-17 21:09:35 +00001252 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1253 Out << (first ? ' ' : ',') << "assign";
1254 first = false;
1255 }
Mike Stump11289f42009-09-09 15:08:12 +00001256
Ted Kremenek897af912011-08-17 21:09:35 +00001257 if (PDecl->getPropertyAttributes() &
1258 ObjCPropertyDecl::OBJC_PR_readwrite) {
1259 Out << (first ? ' ' : ',') << "readwrite";
1260 first = false;
1261 }
Mike Stump11289f42009-09-09 15:08:12 +00001262
Ted Kremenek897af912011-08-17 21:09:35 +00001263 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1264 Out << (first ? ' ' : ',') << "retain";
1265 first = false;
1266 }
Mike Stump11289f42009-09-09 15:08:12 +00001267
Ted Kremenek897af912011-08-17 21:09:35 +00001268 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1269 Out << (first ? ' ' : ',') << "strong";
1270 first = false;
1271 }
John McCall31168b02011-06-15 23:02:42 +00001272
Ted Kremenek897af912011-08-17 21:09:35 +00001273 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1274 Out << (first ? ' ' : ',') << "copy";
1275 first = false;
1276 }
Mike Stump11289f42009-09-09 15:08:12 +00001277
Ted Kremenek897af912011-08-17 21:09:35 +00001278 if (PDecl->getPropertyAttributes() &
1279 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1280 Out << (first ? ' ' : ',') << "nonatomic";
1281 first = false;
1282 }
1283 if (PDecl->getPropertyAttributes() &
1284 ObjCPropertyDecl::OBJC_PR_atomic) {
1285 Out << (first ? ' ' : ',') << "atomic";
1286 first = false;
1287 }
1288
Douglas Gregor813a0662015-06-19 18:14:38 +00001289 if (PDecl->getPropertyAttributes() &
1290 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001291 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001292 if (*nullability == NullabilityKind::Unspecified &&
1293 (PDecl->getPropertyAttributes() &
1294 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1295 Out << (first ? ' ' : ',') << "null_resettable";
1296 } else {
1297 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001298 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001299 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001300 first = false;
1301 }
1302 }
1303
Ted Kremenek897af912011-08-17 21:09:35 +00001304 (void) first; // Silence dead store warning due to idiomatic code.
1305 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001306 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001307 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001308 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001309 if (Policy.PolishForDeclaration)
1310 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001311}
1312
1313void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1314 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001315 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001316 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001317 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001318 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001319 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001320 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001321}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001322
1323void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001324 if (!D->isAccessDeclaration())
1325 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001326 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001327 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001328 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001329 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001330}
1331
John McCalle61f2ba2009-11-18 02:36:19 +00001332void
1333DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1334 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001335 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001336 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001337}
1338
1339void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001340 if (!D->isAccessDeclaration())
1341 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001342 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001343 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001344}
John McCall3f746822009-11-17 05:59:44 +00001345
1346void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1347 // ignore
1348}
Alexey Bataeva769e072013-03-22 06:34:35 +00001349
1350void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1351 Out << "#pragma omp threadprivate";
1352 if (!D->varlist_empty()) {
1353 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1354 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001355 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001356 Out << (I == D->varlist_begin() ? '(' : ',');
1357 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1358 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001359 }
1360 Out << ")";
1361 }
1362}
1363