blob: dd3f3b4a0d94aee79e1d9ebd85fefe83dc991296 [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);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000095 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +000096 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Richard Trieu82398f92011-07-28 00:19:05 +000097
98 void PrintTemplateParameters(const TemplateParameterList *Params,
Craig Topper36250ad2014-05-12 05:36:57 +000099 const TemplateArgumentList *Args = nullptr);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000100 void prettyPrintAttributes(Decl *D);
Alexey Bataev6d455322015-10-12 06:59:48 +0000101 void prettyPrintPragmas(Decl *D);
Richard Smitha4bb2922014-07-23 03:17:06 +0000102 void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000103 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000104}
Douglas Gregor278f52e2009-05-30 00:08:05 +0000105
Richard Trieu82398f92011-07-28 00:19:05 +0000106void Decl::print(raw_ostream &Out, unsigned Indentation,
107 bool PrintInstantiation) const {
Douglas Gregorc0b07282011-09-27 22:38:19 +0000108 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000109}
110
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000111void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu82398f92011-07-28 00:19:05 +0000112 unsigned Indentation, bool PrintInstantiation) const {
Richard Smith235341b2012-08-16 03:56:14 +0000113 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000114 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000115}
116
Eli Friedman79635842009-05-30 04:20:30 +0000117static QualType GetBaseType(QualType T) {
118 // FIXME: This should be on the Type class!
119 QualType BaseType = T;
120 while (!BaseType->isSpecifierType()) {
121 if (isa<TypedefType>(BaseType))
122 break;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000123 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000124 BaseType = PTy->getPointeeType();
Ted Kremenekfa0a0b62012-10-11 20:58:14 +0000125 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
126 BaseType = BPy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000127 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
128 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000129 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Alp Toker314cc812014-01-25 16:55:45 +0000130 BaseType = FTy->getReturnType();
John McCall9dd450b2009-09-21 23:43:11 +0000131 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000132 BaseType = VTy->getElementType();
Eli Friedman70bc6e62012-08-08 03:47:15 +0000133 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
134 BaseType = RTy->getPointeeType();
Eli Friedman79635842009-05-30 04:20:30 +0000135 else
David Blaikie83d382b2011-09-23 05:06:16 +0000136 llvm_unreachable("Unknown declarator!");
Eli Friedman79635842009-05-30 04:20:30 +0000137 }
138 return BaseType;
139}
140
141static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000142 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000143 return TDD->getUnderlyingType();
144 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
145 return VD->getType();
146 return QualType();
147}
148
149void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000150 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000151 unsigned Indentation) {
152 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000153 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000154 return;
155 }
156
157 Decl** End = Begin + NumDecls;
158 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
159 if (TD)
160 ++Begin;
161
162 PrintingPolicy SubPolicy(Policy);
John McCallf937c022011-10-07 06:10:15 +0000163 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000164 TD->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000165 Out << " ";
166 SubPolicy.SuppressTag = true;
167 }
168
169 bool isFirst = true;
170 for ( ; Begin != End; ++Begin) {
171 if (isFirst) {
172 SubPolicy.SuppressSpecifiers = false;
173 isFirst = false;
174 } else {
175 if (!isFirst) Out << ", ";
176 SubPolicy.SuppressSpecifiers = true;
177 }
178
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000179 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000180 }
181}
182
Alp Tokeref6b0072014-01-04 13:47:14 +0000183LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000184 // Get the translation unit
185 const DeclContext *DC = this;
186 while (!DC->isTranslationUnit())
187 DC = DC->getParent();
188
189 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith235341b2012-08-16 03:56:14 +0000190 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson538af092009-12-09 17:27:46 +0000191 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
192}
193
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000194raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000195 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000196 Out << " ";
197 return Out;
198}
199
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000200void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +0000201 if (Policy.PolishForDeclaration)
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +0000202 return;
Alexey Bataev6d455322015-10-12 06:59:48 +0000203
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000204 if (D->hasAttrs()) {
205 AttrVec &Attrs = D->getAttrs();
Alexey Bataev6d455322015-10-12 06:59:48 +0000206 for (auto *A : Attrs) {
207 switch (A->getKind()) {
208#define ATTR(X)
209#define PRAGMA_SPELLING_ATTR(X) case attr::X:
210#include "clang/Basic/AttrList.inc"
211 break;
212 default:
213 A->printPretty(Out, Policy);
214 break;
215 }
216 }
217 }
218}
219
220void DeclPrinter::prettyPrintPragmas(Decl *D) {
221 if (Policy.PolishForDeclaration)
222 return;
223
224 if (D->hasAttrs()) {
225 AttrVec &Attrs = D->getAttrs();
226 for (auto *A : Attrs) {
227 switch (A->getKind()) {
228#define ATTR(X)
229#define PRAGMA_SPELLING_ATTR(X) case attr::X:
230#include "clang/Basic/AttrList.inc"
231 A->printPretty(Out, Policy);
232 Indent();
233 break;
234 default:
235 break;
236 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000237 }
238 }
239}
240
Richard Smitha4bb2922014-07-23 03:17:06 +0000241void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
242 // Normally, a PackExpansionType is written as T[3]... (for instance, as a
243 // template argument), but if it is the type of a declaration, the ellipsis
244 // is placed before the name being declared.
245 if (auto *PET = T->getAs<PackExpansionType>()) {
246 Pack = true;
247 T = PET->getPattern();
248 }
249 T.print(Out, Policy, (Pack ? "..." : "") + DeclName);
250}
251
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000252void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman79635842009-05-30 04:20:30 +0000253 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000254 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000255 Out << ";\n";
256 Decls.clear();
257
258}
259
Anders Carlsson601d6e42009-08-28 22:39:52 +0000260void DeclPrinter::Print(AccessSpecifier AS) {
261 switch(AS) {
David Blaikieaa347f92011-09-23 20:26:49 +0000262 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson601d6e42009-08-28 22:39:52 +0000263 case AS_public: Out << "public"; break;
264 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000265 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000266 }
267}
268
Douglas Gregor278f52e2009-05-30 00:08:05 +0000269//----------------------------------------------------------------------------
270// Common C declarations
271//----------------------------------------------------------------------------
272
273void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkoa93a7e82012-08-21 17:36:32 +0000274 if (Policy.TerseOutput)
Dmitri Gribenko309856a2012-08-20 23:39:06 +0000275 return;
276
Douglas Gregor278f52e2009-05-30 00:08:05 +0000277 if (Indent)
278 Indentation += Policy.Indentation;
279
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000280 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000281 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000282 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000283
284 // Don't print ObjCIvarDecls, as they are printed when visiting the
285 // containing ObjCInterfaceDecl.
286 if (isa<ObjCIvarDecl>(*D))
287 continue;
288
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000289 // Skip over implicit declarations in pretty-printing mode.
290 if (D->isImplicit())
291 continue;
292
Eli Friedman79635842009-05-30 04:20:30 +0000293 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
294 // forced to merge the declarations because there's no other way to
295 // refer to the struct in question. This limited merging is safe without
296 // a bunch of other checks because it only merges declarations directly
297 // referring to the tag, not typedefs.
298 //
299 // Check whether the current declaration should be grouped with a previous
300 // unnamed struct.
301 QualType CurDeclType = getDeclType(*D);
302 if (!Decls.empty() && !CurDeclType.isNull()) {
303 QualType BaseType = GetBaseType(CurDeclType);
Eli Friedman24b3fed2013-08-12 21:54:04 +0000304 if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
305 BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
Eli Friedman79635842009-05-30 04:20:30 +0000306 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
307 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
308 Decls.push_back(*D);
309 continue;
310 }
311 }
312
313 // If we have a merged group waiting to be handled, handle it now.
314 if (!Decls.empty())
315 ProcessDeclGroup(Decls);
316
317 // If the current declaration is an unnamed tag type, save it
318 // so we can merge it with the subsequent declaration(s) using it.
319 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
320 Decls.push_back(*D);
321 continue;
322 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000323
324 if (isa<AccessSpecDecl>(*D)) {
325 Indentation -= Policy.Indentation;
326 this->Indent();
327 Print(D->getAccess());
328 Out << ":\n";
329 Indentation += Policy.Indentation;
330 continue;
331 }
332
Douglas Gregor278f52e2009-05-30 00:08:05 +0000333 this->Indent();
334 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000335
336 // FIXME: Need to be able to tell the DeclPrinter when
Yaron Keren51db8772014-05-07 09:53:02 +0000337 const char *Terminator = nullptr;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000338 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000339 Terminator = nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +0000340 else if (isa<FunctionDecl>(*D) &&
341 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
Yaron Keren51db8772014-05-07 09:53:02 +0000342 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000343 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000344 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000345 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000346 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000347 isa<ObjCInterfaceDecl>(*D) ||
348 isa<ObjCProtocolDecl>(*D) ||
349 isa<ObjCCategoryImplDecl>(*D) ||
350 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000351 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000352 else if (isa<EnumConstantDecl>(*D)) {
353 DeclContext::decl_iterator Next = D;
354 ++Next;
355 if (Next != DEnd)
356 Terminator = ",";
357 } else
358 Terminator = ";";
359
360 if (Terminator)
361 Out << Terminator;
362 Out << "\n";
363 }
364
Eli Friedman79635842009-05-30 04:20:30 +0000365 if (!Decls.empty())
366 ProcessDeclGroup(Decls);
367
Douglas Gregor278f52e2009-05-30 00:08:05 +0000368 if (Indent)
369 Indentation -= Policy.Indentation;
370}
371
372void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
373 VisitDeclContext(D, false);
374}
375
376void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000377 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000378 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000379
380 if (D->isModulePrivate())
381 Out << "__module_private__ ";
382 }
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000383 D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000384 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000385}
386
Richard Smithdda56e42011-04-15 14:24:37 +0000387void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000388 Out << "using " << *D;
389 prettyPrintAttributes(D);
390 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000391}
392
Douglas Gregor278f52e2009-05-30 00:08:05 +0000393void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000394 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
395 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000396 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000397 if (D->isScoped()) {
398 if (D->isScopedUsingClassTag())
399 Out << "class ";
400 else
401 Out << "struct ";
402 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000403 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000404
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000405 if (D->isFixed())
406 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000407
John McCallf937c022011-10-07 06:10:15 +0000408 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000409 Out << " {\n";
410 VisitDeclContext(D);
411 Indent() << "}";
412 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000413 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000414}
415
416void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000417 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
418 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000419 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000420
421 prettyPrintAttributes(D);
422
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000423 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000424 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000425
John McCallf937c022011-10-07 06:10:15 +0000426 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000427 Out << " {\n";
428 VisitDeclContext(D);
429 Indent() << "}";
430 }
431}
432
433void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000434 Out << *D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000435 if (Expr *Init = D->getInitExpr()) {
436 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000437 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000438 }
439}
440
Mike Stump11289f42009-09-09 15:08:12 +0000441void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000442 if (!D->getDescribedFunctionTemplate() &&
443 !D->isFunctionTemplateSpecialization())
444 prettyPrintPragmas(D);
445
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000446 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000447 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000448 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000449 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000450 case SC_None: break;
451 case SC_Extern: Out << "extern "; break;
452 case SC_Static: Out << "static "; break;
453 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000454 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000455 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000456 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000457
Douglas Gregor26701a42011-09-09 02:06:17 +0000458 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000459 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000460 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000461 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000462 if ((CDecl && CDecl->isExplicitSpecified()) ||
463 (ConversionDecl && ConversionDecl->isExplicit()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000464 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000465 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000466
Douglas Gregor2d042f12009-05-30 05:39:39 +0000467 PrintingPolicy SubPolicy(Policy);
468 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000469 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000470
Abramo Bagnara6d810632010-12-14 22:11:44 +0000471 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000472 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000473 Proto = '(' + Proto + ')';
474 Ty = PT->getInnerType();
475 }
476
Reid Kleckner0503a872013-12-05 01:23:43 +0000477 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000478 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000479 if (D->hasWrittenPrototype())
480 FT = dyn_cast<FunctionProtoType>(AFT);
481
482 Proto += "(";
483 if (FT) {
484 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000485 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000486 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
487 if (i) POut << ", ";
488 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Douglas Gregor278f52e2009-05-30 00:08:05 +0000491 if (FT->isVariadic()) {
492 if (D->getNumParams()) POut << ", ";
493 POut << "...";
494 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000495 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000496 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
497 if (i)
498 Proto += ", ";
499 Proto += D->getParamDecl(i)->getNameAsString();
500 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000501 }
502
503 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000504
David Blaikief5697e52012-08-10 00:55:35 +0000505 if (FT) {
506 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000507 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000508 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000509 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000510 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000511 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000512
513 switch (FT->getRefQualifier()) {
514 case RQ_None:
515 break;
516 case RQ_LValue:
517 Proto += " &";
518 break;
519 case RQ_RValue:
520 Proto += " &&";
521 break;
522 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000523 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000524
525 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000526 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000527 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000528 Proto += "...";
529 else
530 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
531 if (I)
532 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000533
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000534 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000535 }
536 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000537 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
538 Proto += " noexcept";
539 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
540 Proto += "(";
541 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000542 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000543 Indentation);
544 EOut.flush();
545 Proto += EOut.str();
546 Proto += ")";
547 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000548 }
549
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000550 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000551 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000552 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000553 if (BMInitializer->isInClassMemberInitializer())
554 continue;
555
556 if (!HasInitializerList) {
557 Proto += " : ";
558 Out << Proto;
559 Proto.clear();
560 HasInitializerList = true;
561 } else
562 Out << ", ";
563
564 if (BMInitializer->isAnyMemberInitializer()) {
565 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000566 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000567 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000568 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000569 }
570
571 Out << "(";
572 if (!BMInitializer->getInit()) {
573 // Nothing to print
574 } else {
575 Expr *Init = BMInitializer->getInit();
576 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
577 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000578
Richard Smith938f40b2011-06-11 17:19:42 +0000579 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000580
581 Expr *SimpleInit = nullptr;
582 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000583 unsigned NumArgs = 0;
584 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
585 Args = ParenList->getExprs();
586 NumArgs = ParenList->getNumExprs();
587 } else if (CXXConstructExpr *Construct
588 = dyn_cast<CXXConstructExpr>(Init)) {
589 Args = Construct->getArgs();
590 NumArgs = Construct->getNumArgs();
591 } else
592 SimpleInit = Init;
593
594 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000595 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000596 else {
597 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000598 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000599 if (isa<CXXDefaultArgExpr>(Args[I]))
600 break;
601
602 if (I)
603 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000604 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000605 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000606 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000607 }
Richard Smith938f40b2011-06-11 17:19:42 +0000608 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000609 if (BMInitializer->isPackExpansion())
610 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000611 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000612 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000613 if (FT && FT->hasTrailingReturn()) {
614 Out << "auto " << Proto << " -> ";
615 Proto.clear();
616 }
Alp Toker314cc812014-01-25 16:55:45 +0000617 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000618 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000619 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000620 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000621 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000622 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000623 }
624
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000625 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000626
627 if (D->isPure())
628 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000629 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000630 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000631 else if (D->isExplicitlyDefaulted())
632 Out << " = default";
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000633 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000634 if (!D->hasPrototype() && D->getNumParams()) {
635 // This is a K&R function definition, so we need to print the
636 // parameters.
637 Out << '\n';
Richard Smith235341b2012-08-16 03:56:14 +0000638 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000639 Indentation += Policy.Indentation;
640 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
641 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000642 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000643 Out << ";\n";
644 }
645 Indentation -= Policy.Indentation;
646 } else
647 Out << ' ';
648
Richard Trieuddd01ce2014-06-09 22:53:25 +0000649 if (D->getBody())
650 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000651 Out << '\n';
652 }
653}
654
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000655void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
656 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000657 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
658 for (unsigned i = 0; i < NumTPLists; ++i)
659 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000660 Out << "friend ";
661 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000662 }
663 else if (FunctionDecl *FD =
664 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
665 Out << "friend ";
666 VisitFunctionDecl(FD);
667 }
668 else if (FunctionTemplateDecl *FTD =
669 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
670 Out << "friend ";
671 VisitFunctionTemplateDecl(FTD);
672 }
673 else if (ClassTemplateDecl *CTD =
674 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
675 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000676 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000677 }
678}
679
Douglas Gregor278f52e2009-05-30 00:08:05 +0000680void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000681 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000682 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000683 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000684 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
685 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000686
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000687 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Fariborz Jahanian9e075842013-05-01 17:28:37 +0000688 stream(Policy, D->getName());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000689
690 if (D->isBitField()) {
691 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000692 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000693 }
Richard Smith938f40b2011-06-11 17:19:42 +0000694
695 Expr *Init = D->getInClassInitializer();
696 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000697 if (D->getInClassInitStyle() == ICIS_ListInit)
698 Out << " ";
699 else
700 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000701 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000702 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000703 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000704}
705
Chris Lattnercab02a62011-02-17 20:34:02 +0000706void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000707 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000708}
709
Douglas Gregor278f52e2009-05-30 00:08:05 +0000710void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000711 prettyPrintPragmas(D);
Richard Smithfd3834f2013-04-13 02:43:54 +0000712 if (!Policy.SuppressSpecifiers) {
713 StorageClass SC = D->getStorageClass();
714 if (SC != SC_None)
715 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000716
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000717 switch (D->getTSCSpec()) {
718 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000719 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000720 case TSCS___thread:
721 Out << "__thread ";
722 break;
723 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000724 Out << "_Thread_local ";
725 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000726 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000727 Out << "thread_local ";
728 break;
729 }
730
731 if (D->isModulePrivate())
732 Out << "__module_private__ ";
733 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000734
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000735 QualType T = D->getTypeSourceInfo()
736 ? D->getTypeSourceInfo()->getType()
737 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
Richard Smitha4bb2922014-07-23 03:17:06 +0000738 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000739 Expr *Init = D->getInit();
740 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000741 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000742 if (CXXConstructExpr *Construct =
743 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000744 if (D->getInitStyle() == VarDecl::CallInit &&
745 !Construct->isListInitialization()) {
746 ImplicitInit = Construct->getNumArgs() == 0 ||
747 Construct->getArg(0)->isDefaultArgument();
748 }
749 }
Sebastian Redla9351792012-02-11 23:51:47 +0000750 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000751 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000752 Out << "(";
753 else if (D->getInitStyle() == VarDecl::CInit) {
754 Out << " = ";
755 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000756 PrintingPolicy SubPolicy(Policy);
757 SubPolicy.SuppressSpecifiers = false;
758 SubPolicy.SuppressTag = false;
759 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000760 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000761 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000762 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000763 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000764 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000765}
766
767void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
768 VisitVarDecl(D);
769}
770
771void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
772 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000773 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000774 Out << ")";
775}
776
Douglas Gregorba345522011-12-02 23:23:56 +0000777void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000778 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000779 << ";\n";
780}
781
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000782void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
783 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000784 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000785 if (StringLiteral *SL = D->getMessage()) {
786 Out << ", ";
787 SL->printPretty(Out, nullptr, Policy, Indentation);
788 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000789 Out << ")";
790}
791
Douglas Gregor278f52e2009-05-30 00:08:05 +0000792//----------------------------------------------------------------------------
793// C++ declarations
794//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000795void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000796 if (D->isInline())
797 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000798 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000799 VisitDeclContext(D);
800 Indent() << "}";
801}
802
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000803void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
804 Out << "using namespace ";
805 if (D->getQualifier())
806 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000807 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000808}
809
Douglas Gregor18231932009-05-30 06:48:27 +0000810void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000811 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000812 if (D->getQualifier())
813 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000814 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000815}
816
Michael Han84324352013-02-22 17:15:32 +0000817void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
818 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000819}
820
Douglas Gregor5f478b72009-05-30 06:58:37 +0000821void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000822 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000823 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
824 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000825 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000826
827 prettyPrintAttributes(D);
828
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000829 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000830 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000831
John McCallf937c022011-10-07 06:10:15 +0000832 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000833 // Print the base classes
834 if (D->getNumBases()) {
835 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000836 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
837 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000838 if (Base != D->bases_begin())
839 Out << ", ";
840
841 if (Base->isVirtual())
842 Out << "virtual ";
843
Anders Carlsson6df9e072009-08-29 20:36:12 +0000844 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000845 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000846 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000847 Out << " ";
848 }
849 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000850
851 if (Base->isPackExpansion())
852 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000853 }
854 }
855
856 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000857 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000858 Out << " {\n";
859 VisitDeclContext(D);
860 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000861 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000862}
863
864void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
865 const char *l;
866 if (D->getLanguage() == LinkageSpecDecl::lang_c)
867 l = "C";
868 else {
869 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
870 "unknown language in linkage specification");
871 l = "C++";
872 }
873
874 Out << "extern \"" << l << "\" ";
875 if (D->hasBraces()) {
876 Out << "{\n";
877 VisitDeclContext(D);
878 Indent() << "}";
879 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000880 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000881}
882
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000883void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
884 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000885 assert(Params);
886 assert(!Args || Params->size() == Args->size());
887
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000888 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000889
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000890 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
891 if (i != 0)
892 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000893
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000894 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000895 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000896 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000897
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000898 if (TTP->wasDeclaredWithTypename())
899 Out << "typename ";
900 else
901 Out << "class ";
902
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000903 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000904 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000905
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000906 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000907
Richard Trieu82398f92011-07-28 00:19:05 +0000908 if (Args) {
909 Out << " = ";
910 Args->get(i).print(Policy, Out);
911 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000912 Out << " = ";
913 Out << TTP->getDefaultArgument().getAsString(Policy);
914 };
Mike Stump11289f42009-09-09 15:08:12 +0000915 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000916 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000917 StringRef Name;
918 if (IdentifierInfo *II = NTTP->getIdentifier())
919 Name = II->getName();
920 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000921
Richard Trieu82398f92011-07-28 00:19:05 +0000922 if (Args) {
923 Out << " = ";
924 Args->get(i).print(Policy, Out);
925 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000926 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000927 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
928 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000929 }
Richard Smith030f4992011-04-15 13:38:57 +0000930 } else if (const TemplateTemplateParmDecl *TTPD =
931 dyn_cast<TemplateTemplateParmDecl>(Param)) {
932 VisitTemplateDecl(TTPD);
933 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000934 }
935 }
Mike Stump11289f42009-09-09 15:08:12 +0000936
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000937 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000938}
939
940void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
941 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000942
Richard Smith030f4992011-04-15 13:38:57 +0000943 if (const TemplateTemplateParmDecl *TTP =
944 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000945 Out << "class ";
946 if (TTP->isParameterPack())
947 Out << "...";
948 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000949 } else {
950 Visit(D->getTemplatedDecl());
951 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000952}
953
Richard Trieu82398f92011-07-28 00:19:05 +0000954void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
955 if (PrintInstantiation) {
956 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000957 for (auto *I : D->specializations()) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000958 prettyPrintPragmas(I);
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000959 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
960 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000961 }
962 }
963
Alexey Bataev6d455322015-10-12 06:59:48 +0000964 prettyPrintPragmas(D->getTemplatedDecl());
Richard Trieu82398f92011-07-28 00:19:05 +0000965 return VisitRedeclarableTemplateDecl(D);
966}
967
968void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
969 if (PrintInstantiation) {
970 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000971 for (auto *I : D->specializations()) {
972 PrintTemplateParameters(Params, &I->getTemplateArgs());
973 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000974 Out << '\n';
975 }
976 }
977
978 return VisitRedeclarableTemplateDecl(D);
979}
980
Douglas Gregor278f52e2009-05-30 00:08:05 +0000981//----------------------------------------------------------------------------
982// Objective-C declarations
983//----------------------------------------------------------------------------
984
Douglas Gregor813a0662015-06-19 18:14:38 +0000985void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
986 Decl::ObjCDeclQualifier Quals,
987 QualType T) {
988 Out << '(';
989 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
990 Out << "in ";
991 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
992 Out << "inout ";
993 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
994 Out << "out ";
995 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
996 Out << "bycopy ";
997 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
998 Out << "byref ";
999 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1000 Out << "oneway ";
1001 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001002 if (auto nullability = AttributedType::stripOuterNullability(T))
1003 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001004 }
1005
1006 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1007 Out << ')';
1008}
1009
Douglas Gregor85f3f952015-07-07 03:57:15 +00001010void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1011 Out << "<";
1012 unsigned First = true;
1013 for (auto *Param : *Params) {
1014 if (First) {
1015 First = false;
1016 } else {
1017 Out << ", ";
1018 }
1019
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001020 switch (Param->getVariance()) {
1021 case ObjCTypeParamVariance::Invariant:
1022 break;
1023
1024 case ObjCTypeParamVariance::Covariant:
1025 Out << "__covariant ";
1026 break;
1027
1028 case ObjCTypeParamVariance::Contravariant:
1029 Out << "__contravariant ";
1030 break;
1031 }
1032
Douglas Gregor85f3f952015-07-07 03:57:15 +00001033 Out << Param->getDeclName().getAsString();
1034
1035 if (Param->hasExplicitBound()) {
1036 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1037 }
1038 }
1039 Out << ">";
1040}
1041
Douglas Gregor278f52e2009-05-30 00:08:05 +00001042void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1043 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001044 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001045 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001046 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001047 if (!OMD->getReturnType().isNull()) {
1048 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1049 OMD->getReturnType());
1050 }
Mike Stump11289f42009-09-09 15:08:12 +00001051
Douglas Gregor278f52e2009-05-30 00:08:05 +00001052 std::string name = OMD->getSelector().getAsString();
1053 std::string::size_type pos, lastPos = 0;
Aaron Ballman43b68be2014-03-07 17:50:17 +00001054 for (const auto *PI : OMD->params()) {
Mike Stump11289f42009-09-09 15:08:12 +00001055 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001056 pos = name.find_first_of(':', lastPos);
Douglas Gregor813a0662015-06-19 18:14:38 +00001057 Out << " " << name.substr(lastPos, pos - lastPos) << ':';
1058 PrintObjCMethodType(OMD->getASTContext(),
1059 PI->getObjCDeclQualifier(),
1060 PI->getType());
1061 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001062 lastPos = pos + 1;
1063 }
Mike Stump11289f42009-09-09 15:08:12 +00001064
Douglas Gregor278f52e2009-05-30 00:08:05 +00001065 if (OMD->param_begin() == OMD->param_end())
1066 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +00001067
Douglas Gregor278f52e2009-05-30 00:08:05 +00001068 if (OMD->isVariadic())
1069 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001070
1071 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001072
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001073 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001074 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001075 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001076 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001077 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001078 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001079}
1080
1081void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1082 std::string I = OID->getNameAsString();
1083 ObjCInterfaceDecl *SID = OID->getSuperClass();
1084
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001085 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001086 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001087 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001088 else
1089 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001090
1091 if (OID->ivar_size() > 0) {
1092 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001093 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001094 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001095 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001096 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001097 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001098 }
1099 Indentation -= Policy.Indentation;
1100 Out << "}\n";
1101 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001102 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1103 Out << "\n";
1104 eolnOut = true;
1105 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001106 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001107 if (!eolnOut)
1108 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001109 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001110}
1111
1112void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1113 std::string I = OID->getNameAsString();
1114 ObjCInterfaceDecl *SID = OID->getSuperClass();
1115
Douglas Gregordc9166c2011-12-15 20:29:51 +00001116 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001117 Out << "@class " << I;
1118
1119 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1120 PrintObjCTypeParams(TypeParams);
1121 }
1122
1123 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001124 return;
1125 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001126 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001127 Out << "@interface " << I;
1128
1129 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1130 PrintObjCTypeParams(TypeParams);
1131 }
1132
Douglas Gregor278f52e2009-05-30 00:08:05 +00001133 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001134 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001135
Douglas Gregor278f52e2009-05-30 00:08:05 +00001136 // Protocols?
1137 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1138 if (!Protocols.empty()) {
1139 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1140 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001141 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001142 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001143 }
Mike Stump11289f42009-09-09 15:08:12 +00001144
Douglas Gregor278f52e2009-05-30 00:08:05 +00001145 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001146 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001147 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001148 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001149 for (const auto *I : OID->ivars()) {
1150 Indent() << I->getASTContext()
1151 .getUnqualifiedObjCPointerType(I->getType())
1152 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001153 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001154 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001155 Out << "}\n";
1156 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001157 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001158 Out << "\n";
1159 eolnOut = true;
1160 }
Mike Stump11289f42009-09-09 15:08:12 +00001161
Douglas Gregor278f52e2009-05-30 00:08:05 +00001162 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001163 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001164 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001165 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001166 // FIXME: implement the rest...
1167}
1168
Douglas Gregor278f52e2009-05-30 00:08:05 +00001169void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001170 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001171 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001172 return;
1173 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001174 // Protocols?
1175 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1176 if (!Protocols.empty()) {
1177 Out << "@protocol " << *PID;
1178 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1179 E = Protocols.end(); I != E; ++I)
1180 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1181 Out << ">\n";
1182 } else
1183 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001184 VisitDeclContext(PID, false);
1185 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001186}
1187
1188void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001189 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001190
1191 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001192 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001193 // FIXME: implement the rest...
1194}
1195
1196void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001197 Out << "@interface " << *PID->getClassInterface();
1198 if (auto TypeParams = PID->getTypeParamList()) {
1199 PrintObjCTypeParams(TypeParams);
1200 }
1201 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001202 if (PID->ivar_size() > 0) {
1203 Out << "{\n";
1204 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001205 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001206 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001207 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001208 Indentation -= Policy.Indentation;
1209 Out << "}\n";
1210 }
1211
Douglas Gregor278f52e2009-05-30 00:08:05 +00001212 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001213 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001214
Douglas Gregor278f52e2009-05-30 00:08:05 +00001215 // FIXME: implement the rest...
1216}
1217
1218void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001219 Out << "@compatibility_alias " << *AID
1220 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001221}
1222
1223/// PrintObjCPropertyDecl - print a property declaration.
1224///
1225void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1226 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1227 Out << "@required\n";
1228 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1229 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001230
Douglas Gregor813a0662015-06-19 18:14:38 +00001231 QualType T = PDecl->getType();
1232
Douglas Gregor278f52e2009-05-30 00:08:05 +00001233 Out << "@property";
1234 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1235 bool first = true;
1236 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001237 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001238 ObjCPropertyDecl::OBJC_PR_readonly) {
1239 Out << (first ? ' ' : ',') << "readonly";
1240 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001241 }
Mike Stump11289f42009-09-09 15:08:12 +00001242
Ted Kremenek897af912011-08-17 21:09:35 +00001243 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001244 Out << (first ? ' ' : ',') << "getter = ";
1245 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001246 first = false;
1247 }
1248 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001249 Out << (first ? ' ' : ',') << "setter = ";
1250 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001251 first = false;
1252 }
Mike Stump11289f42009-09-09 15:08:12 +00001253
Ted Kremenek897af912011-08-17 21:09:35 +00001254 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1255 Out << (first ? ' ' : ',') << "assign";
1256 first = false;
1257 }
Mike Stump11289f42009-09-09 15:08:12 +00001258
Ted Kremenek897af912011-08-17 21:09:35 +00001259 if (PDecl->getPropertyAttributes() &
1260 ObjCPropertyDecl::OBJC_PR_readwrite) {
1261 Out << (first ? ' ' : ',') << "readwrite";
1262 first = false;
1263 }
Mike Stump11289f42009-09-09 15:08:12 +00001264
Ted Kremenek897af912011-08-17 21:09:35 +00001265 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1266 Out << (first ? ' ' : ',') << "retain";
1267 first = false;
1268 }
Mike Stump11289f42009-09-09 15:08:12 +00001269
Ted Kremenek897af912011-08-17 21:09:35 +00001270 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1271 Out << (first ? ' ' : ',') << "strong";
1272 first = false;
1273 }
John McCall31168b02011-06-15 23:02:42 +00001274
Ted Kremenek897af912011-08-17 21:09:35 +00001275 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1276 Out << (first ? ' ' : ',') << "copy";
1277 first = false;
1278 }
Mike Stump11289f42009-09-09 15:08:12 +00001279
Ted Kremenek897af912011-08-17 21:09:35 +00001280 if (PDecl->getPropertyAttributes() &
1281 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1282 Out << (first ? ' ' : ',') << "nonatomic";
1283 first = false;
1284 }
1285 if (PDecl->getPropertyAttributes() &
1286 ObjCPropertyDecl::OBJC_PR_atomic) {
1287 Out << (first ? ' ' : ',') << "atomic";
1288 first = false;
1289 }
1290
Douglas Gregor813a0662015-06-19 18:14:38 +00001291 if (PDecl->getPropertyAttributes() &
1292 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001293 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001294 if (*nullability == NullabilityKind::Unspecified &&
1295 (PDecl->getPropertyAttributes() &
1296 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1297 Out << (first ? ' ' : ',') << "null_resettable";
1298 } else {
1299 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001300 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001301 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001302 first = false;
1303 }
1304 }
1305
Manman Ren387ff7f2016-01-26 18:52:43 +00001306 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1307 Out << (first ? ' ' : ',') << "class";
1308 first = false;
1309 }
1310
Ted Kremenek897af912011-08-17 21:09:35 +00001311 (void) first; // Silence dead store warning due to idiomatic code.
1312 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001313 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001314 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001315 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001316 if (Policy.PolishForDeclaration)
1317 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001318}
1319
1320void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1321 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001322 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001323 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001324 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001325 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001326 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001327 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001328}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001329
1330void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001331 if (!D->isAccessDeclaration())
1332 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001333 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001334 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001335 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001336 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001337}
1338
John McCalle61f2ba2009-11-18 02:36:19 +00001339void
1340DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1341 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001342 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001343 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001344}
1345
1346void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001347 if (!D->isAccessDeclaration())
1348 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001349 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001350 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001351}
John McCall3f746822009-11-17 05:59:44 +00001352
1353void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1354 // ignore
1355}
Alexey Bataeva769e072013-03-22 06:34:35 +00001356
1357void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1358 Out << "#pragma omp threadprivate";
1359 if (!D->varlist_empty()) {
1360 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1361 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001362 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001363 Out << (I == D->varlist_begin() ? '(' : ',');
1364 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1365 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001366 }
1367 Out << ")";
1368 }
1369}
1370
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001371void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1372 if (!D->isInvalidDecl()) {
1373 Out << "#pragma omp declare reduction (";
1374 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1375 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1376 nullptr,
1377#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1378 Spelling,
1379#include "clang/Basic/OperatorKinds.def"
1380 };
1381 const char *OpName =
1382 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1383 assert(OpName && "not an overloaded operator");
1384 Out << OpName;
1385 } else {
1386 assert(D->getDeclName().isIdentifier());
1387 D->printName(Out);
1388 }
1389 Out << " : ";
1390 D->getType().print(Out, Policy);
1391 Out << " : ";
1392 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1393 Out << ")";
1394 if (auto *Init = D->getInitializer()) {
1395 Out << " initializer(";
1396 Init->printPretty(Out, nullptr, Policy, 0);
1397 Out << ")";
1398 }
1399 }
1400}
1401
Alexey Bataev4244be22016-02-11 05:35:55 +00001402void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001403 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1404}
1405