blob: 6868ea2a6d7d8b521810f5ac583d4297dca23f3d [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);
Eli Friedman79635842009-05-30 04:20:30 +0000163
164 bool isFirst = true;
165 for ( ; Begin != End; ++Begin) {
166 if (isFirst) {
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000167 if(TD)
168 SubPolicy.IncludeTagDefinition = true;
Eli Friedman79635842009-05-30 04:20:30 +0000169 SubPolicy.SuppressSpecifiers = false;
170 isFirst = false;
171 } else {
172 if (!isFirst) Out << ", ";
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000173 SubPolicy.IncludeTagDefinition = false;
Eli Friedman79635842009-05-30 04:20:30 +0000174 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 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000247 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
Richard Smitha4bb2922014-07-23 03:17:06 +0000248}
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 Bataev94a4f0c2016-03-03 05:21:39 +0000336 if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*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";
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000361
362 // Declare target attribute is special one, natural spelling for the pragma
363 // assumes "ending" construct so print it here.
364 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
365 Out << "#pragma omp end declare target\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000366 }
367
Eli Friedman79635842009-05-30 04:20:30 +0000368 if (!Decls.empty())
369 ProcessDeclGroup(Decls);
370
Douglas Gregor278f52e2009-05-30 00:08:05 +0000371 if (Indent)
372 Indentation -= Policy.Indentation;
373}
374
375void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
376 VisitDeclContext(D, false);
377}
378
379void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000380 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000381 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000382
383 if (D->isModulePrivate())
384 Out << "__module_private__ ";
385 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000386 QualType Ty = D->getTypeSourceInfo()->getType();
387 Ty.print(Out, Policy, D->getName(), Indentation);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000388 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000389}
390
Richard Smithdda56e42011-04-15 14:24:37 +0000391void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000392 Out << "using " << *D;
393 prettyPrintAttributes(D);
394 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000395}
396
Douglas Gregor278f52e2009-05-30 00:08:05 +0000397void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000398 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
399 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000400 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000401 if (D->isScoped()) {
402 if (D->isScopedUsingClassTag())
403 Out << "class ";
404 else
405 Out << "struct ";
406 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000407 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000408
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000409 if (D->isFixed())
410 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000411
John McCallf937c022011-10-07 06:10:15 +0000412 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000413 Out << " {\n";
414 VisitDeclContext(D);
415 Indent() << "}";
416 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000417 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000418}
419
420void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000421 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
422 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000423 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000424
425 prettyPrintAttributes(D);
426
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000427 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000428 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000429
John McCallf937c022011-10-07 06:10:15 +0000430 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000431 Out << " {\n";
432 VisitDeclContext(D);
433 Indent() << "}";
434 }
435}
436
437void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000438 Out << *D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000439 if (Expr *Init = D->getInitExpr()) {
440 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000441 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000442 }
443}
444
Mike Stump11289f42009-09-09 15:08:12 +0000445void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000446 if (!D->getDescribedFunctionTemplate() &&
447 !D->isFunctionTemplateSpecialization())
448 prettyPrintPragmas(D);
449
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000450 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000451 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000452 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000453 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000454 case SC_None: break;
455 case SC_Extern: Out << "extern "; break;
456 case SC_Static: Out << "static "; break;
457 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000458 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000459 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000460 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000461
Douglas Gregor26701a42011-09-09 02:06:17 +0000462 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000463 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000464 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000465 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000466 if ((CDecl && CDecl->isExplicitSpecified()) ||
467 (ConversionDecl && ConversionDecl->isExplicit()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000468 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000469 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000470
Douglas Gregor2d042f12009-05-30 05:39:39 +0000471 PrintingPolicy SubPolicy(Policy);
472 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000473 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000474
Abramo Bagnara6d810632010-12-14 22:11:44 +0000475 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000476 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000477 Proto = '(' + Proto + ')';
478 Ty = PT->getInnerType();
479 }
480
Reid Kleckner0503a872013-12-05 01:23:43 +0000481 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000482 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000483 if (D->hasWrittenPrototype())
484 FT = dyn_cast<FunctionProtoType>(AFT);
485
486 Proto += "(";
487 if (FT) {
488 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000489 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000490 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
491 if (i) POut << ", ";
492 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
493 }
Mike Stump11289f42009-09-09 15:08:12 +0000494
Douglas Gregor278f52e2009-05-30 00:08:05 +0000495 if (FT->isVariadic()) {
496 if (D->getNumParams()) POut << ", ";
497 POut << "...";
498 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000499 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000500 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
501 if (i)
502 Proto += ", ";
503 Proto += D->getParamDecl(i)->getNameAsString();
504 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000505 }
506
507 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000508
David Blaikief5697e52012-08-10 00:55:35 +0000509 if (FT) {
510 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000511 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000512 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000513 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000514 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000515 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000516
517 switch (FT->getRefQualifier()) {
518 case RQ_None:
519 break;
520 case RQ_LValue:
521 Proto += " &";
522 break;
523 case RQ_RValue:
524 Proto += " &&";
525 break;
526 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000527 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000528
529 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000530 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000531 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000532 Proto += "...";
533 else
534 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
535 if (I)
536 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000537
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000538 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000539 }
540 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000541 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
542 Proto += " noexcept";
543 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
544 Proto += "(";
545 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000546 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000547 Indentation);
548 EOut.flush();
549 Proto += EOut.str();
550 Proto += ")";
551 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000552 }
553
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000554 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000555 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000556 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000557 if (BMInitializer->isInClassMemberInitializer())
558 continue;
559
560 if (!HasInitializerList) {
561 Proto += " : ";
562 Out << Proto;
563 Proto.clear();
564 HasInitializerList = true;
565 } else
566 Out << ", ";
567
568 if (BMInitializer->isAnyMemberInitializer()) {
569 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000570 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000571 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000572 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000573 }
574
575 Out << "(";
576 if (!BMInitializer->getInit()) {
577 // Nothing to print
578 } else {
579 Expr *Init = BMInitializer->getInit();
580 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
581 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000582
Richard Smith938f40b2011-06-11 17:19:42 +0000583 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000584
585 Expr *SimpleInit = nullptr;
586 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000587 unsigned NumArgs = 0;
588 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
589 Args = ParenList->getExprs();
590 NumArgs = ParenList->getNumExprs();
591 } else if (CXXConstructExpr *Construct
592 = dyn_cast<CXXConstructExpr>(Init)) {
593 Args = Construct->getArgs();
594 NumArgs = Construct->getNumArgs();
595 } else
596 SimpleInit = Init;
597
598 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000599 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000600 else {
601 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000602 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000603 if (isa<CXXDefaultArgExpr>(Args[I]))
604 break;
605
606 if (I)
607 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000608 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000609 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000610 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000611 }
Richard Smith938f40b2011-06-11 17:19:42 +0000612 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000613 if (BMInitializer->isPackExpansion())
614 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000615 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000616 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000617 if (FT && FT->hasTrailingReturn()) {
618 Out << "auto " << Proto << " -> ";
619 Proto.clear();
620 }
Alp Toker314cc812014-01-25 16:55:45 +0000621 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000622 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000623 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000624 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000625 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000626 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000627 }
628
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000629 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000630
631 if (D->isPure())
632 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000633 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000634 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000635 else if (D->isExplicitlyDefaulted())
636 Out << " = default";
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000637 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000638 if (!D->hasPrototype() && D->getNumParams()) {
639 // This is a K&R function definition, so we need to print the
640 // parameters.
641 Out << '\n';
Richard Smith235341b2012-08-16 03:56:14 +0000642 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000643 Indentation += Policy.Indentation;
644 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
645 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000646 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000647 Out << ";\n";
648 }
649 Indentation -= Policy.Indentation;
650 } else
651 Out << ' ';
652
Richard Trieuddd01ce2014-06-09 22:53:25 +0000653 if (D->getBody())
654 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000655 Out << '\n';
656 }
657}
658
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000659void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
660 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000661 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
662 for (unsigned i = 0; i < NumTPLists; ++i)
663 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000664 Out << "friend ";
665 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000666 }
667 else if (FunctionDecl *FD =
668 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
669 Out << "friend ";
670 VisitFunctionDecl(FD);
671 }
672 else if (FunctionTemplateDecl *FTD =
673 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
674 Out << "friend ";
675 VisitFunctionTemplateDecl(FTD);
676 }
677 else if (ClassTemplateDecl *CTD =
678 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
679 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000680 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000681 }
682}
683
Douglas Gregor278f52e2009-05-30 00:08:05 +0000684void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000685 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000686 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000687 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000688 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
689 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000690
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000691 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000692 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000693
694 if (D->isBitField()) {
695 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000696 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000697 }
Richard Smith938f40b2011-06-11 17:19:42 +0000698
699 Expr *Init = D->getInClassInitializer();
700 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000701 if (D->getInClassInitStyle() == ICIS_ListInit)
702 Out << " ";
703 else
704 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000705 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000706 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000707 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000708}
709
Chris Lattnercab02a62011-02-17 20:34:02 +0000710void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000711 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000712}
713
Douglas Gregor278f52e2009-05-30 00:08:05 +0000714void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000715 prettyPrintPragmas(D);
Richard Smithfd3834f2013-04-13 02:43:54 +0000716 if (!Policy.SuppressSpecifiers) {
717 StorageClass SC = D->getStorageClass();
718 if (SC != SC_None)
719 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000720
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000721 switch (D->getTSCSpec()) {
722 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000723 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000724 case TSCS___thread:
725 Out << "__thread ";
726 break;
727 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000728 Out << "_Thread_local ";
729 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000730 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000731 Out << "thread_local ";
732 break;
733 }
734
735 if (D->isModulePrivate())
736 Out << "__module_private__ ";
737 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000738
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000739 QualType T = D->getTypeSourceInfo()
740 ? D->getTypeSourceInfo()->getType()
741 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
Richard Smitha4bb2922014-07-23 03:17:06 +0000742 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000743 Expr *Init = D->getInit();
744 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000745 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000746 if (CXXConstructExpr *Construct =
747 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000748 if (D->getInitStyle() == VarDecl::CallInit &&
749 !Construct->isListInitialization()) {
750 ImplicitInit = Construct->getNumArgs() == 0 ||
751 Construct->getArg(0)->isDefaultArgument();
752 }
753 }
Sebastian Redla9351792012-02-11 23:51:47 +0000754 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000755 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000756 Out << "(";
757 else if (D->getInitStyle() == VarDecl::CInit) {
758 Out << " = ";
759 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000760 PrintingPolicy SubPolicy(Policy);
761 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000762 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000763 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000764 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000765 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000766 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000767 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000768 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000769}
770
771void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
772 VisitVarDecl(D);
773}
774
775void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
776 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000777 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000778 Out << ")";
779}
780
Douglas Gregorba345522011-12-02 23:23:56 +0000781void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000782 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000783 << ";\n";
784}
785
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000786void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
787 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000788 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000789 if (StringLiteral *SL = D->getMessage()) {
790 Out << ", ";
791 SL->printPretty(Out, nullptr, Policy, Indentation);
792 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000793 Out << ")";
794}
795
Douglas Gregor278f52e2009-05-30 00:08:05 +0000796//----------------------------------------------------------------------------
797// C++ declarations
798//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000799void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000800 if (D->isInline())
801 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000802 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000803 VisitDeclContext(D);
804 Indent() << "}";
805}
806
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000807void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
808 Out << "using namespace ";
809 if (D->getQualifier())
810 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000811 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000812}
813
Douglas Gregor18231932009-05-30 06:48:27 +0000814void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000815 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000816 if (D->getQualifier())
817 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000818 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000819}
820
Michael Han84324352013-02-22 17:15:32 +0000821void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
822 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000823}
824
Douglas Gregor5f478b72009-05-30 06:58:37 +0000825void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000826 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000827 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
828 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000829 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000830
831 prettyPrintAttributes(D);
832
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000833 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000834 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000835
John McCallf937c022011-10-07 06:10:15 +0000836 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000837 // Print the base classes
838 if (D->getNumBases()) {
839 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000840 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
841 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000842 if (Base != D->bases_begin())
843 Out << ", ";
844
845 if (Base->isVirtual())
846 Out << "virtual ";
847
Anders Carlsson6df9e072009-08-29 20:36:12 +0000848 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000849 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000850 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000851 Out << " ";
852 }
853 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000854
855 if (Base->isPackExpansion())
856 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000857 }
858 }
859
860 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000861 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000862 Out << " {\n";
863 VisitDeclContext(D);
864 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000865 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000866}
867
868void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
869 const char *l;
870 if (D->getLanguage() == LinkageSpecDecl::lang_c)
871 l = "C";
872 else {
873 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
874 "unknown language in linkage specification");
875 l = "C++";
876 }
877
878 Out << "extern \"" << l << "\" ";
879 if (D->hasBraces()) {
880 Out << "{\n";
881 VisitDeclContext(D);
882 Indent() << "}";
883 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000884 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000885}
886
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000887void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
888 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000889 assert(Params);
890 assert(!Args || Params->size() == Args->size());
891
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000892 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000893
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000894 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
895 if (i != 0)
896 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000897
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000898 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000899 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000900 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000901
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000902 if (TTP->wasDeclaredWithTypename())
903 Out << "typename ";
904 else
905 Out << "class ";
906
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000907 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000908 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000909
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000910 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000911
Richard Trieu82398f92011-07-28 00:19:05 +0000912 if (Args) {
913 Out << " = ";
914 Args->get(i).print(Policy, Out);
915 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000916 Out << " = ";
917 Out << TTP->getDefaultArgument().getAsString(Policy);
918 };
Mike Stump11289f42009-09-09 15:08:12 +0000919 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000920 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000921 StringRef Name;
922 if (IdentifierInfo *II = NTTP->getIdentifier())
923 Name = II->getName();
924 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000925
Richard Trieu82398f92011-07-28 00:19:05 +0000926 if (Args) {
927 Out << " = ";
928 Args->get(i).print(Policy, Out);
929 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000930 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000931 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
932 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000933 }
Richard Smith030f4992011-04-15 13:38:57 +0000934 } else if (const TemplateTemplateParmDecl *TTPD =
935 dyn_cast<TemplateTemplateParmDecl>(Param)) {
936 VisitTemplateDecl(TTPD);
937 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000938 }
939 }
Mike Stump11289f42009-09-09 15:08:12 +0000940
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000941 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000942}
943
944void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
945 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000946
Richard Smith030f4992011-04-15 13:38:57 +0000947 if (const TemplateTemplateParmDecl *TTP =
948 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000949 Out << "class ";
950 if (TTP->isParameterPack())
951 Out << "...";
952 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000953 } else {
954 Visit(D->getTemplatedDecl());
955 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000956}
957
Richard Trieu82398f92011-07-28 00:19:05 +0000958void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
959 if (PrintInstantiation) {
960 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000961 for (auto *I : D->specializations()) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000962 prettyPrintPragmas(I);
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000963 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
964 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000965 }
966 }
967
Alexey Bataev6d455322015-10-12 06:59:48 +0000968 prettyPrintPragmas(D->getTemplatedDecl());
Richard Trieu82398f92011-07-28 00:19:05 +0000969 return VisitRedeclarableTemplateDecl(D);
970}
971
972void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
973 if (PrintInstantiation) {
974 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000975 for (auto *I : D->specializations()) {
976 PrintTemplateParameters(Params, &I->getTemplateArgs());
977 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000978 Out << '\n';
979 }
980 }
981
982 return VisitRedeclarableTemplateDecl(D);
983}
984
Douglas Gregor278f52e2009-05-30 00:08:05 +0000985//----------------------------------------------------------------------------
986// Objective-C declarations
987//----------------------------------------------------------------------------
988
Douglas Gregor813a0662015-06-19 18:14:38 +0000989void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
990 Decl::ObjCDeclQualifier Quals,
991 QualType T) {
992 Out << '(';
993 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
994 Out << "in ";
995 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
996 Out << "inout ";
997 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
998 Out << "out ";
999 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1000 Out << "bycopy ";
1001 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1002 Out << "byref ";
1003 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1004 Out << "oneway ";
1005 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001006 if (auto nullability = AttributedType::stripOuterNullability(T))
1007 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001008 }
1009
1010 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1011 Out << ')';
1012}
1013
Douglas Gregor85f3f952015-07-07 03:57:15 +00001014void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1015 Out << "<";
1016 unsigned First = true;
1017 for (auto *Param : *Params) {
1018 if (First) {
1019 First = false;
1020 } else {
1021 Out << ", ";
1022 }
1023
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001024 switch (Param->getVariance()) {
1025 case ObjCTypeParamVariance::Invariant:
1026 break;
1027
1028 case ObjCTypeParamVariance::Covariant:
1029 Out << "__covariant ";
1030 break;
1031
1032 case ObjCTypeParamVariance::Contravariant:
1033 Out << "__contravariant ";
1034 break;
1035 }
1036
Douglas Gregor85f3f952015-07-07 03:57:15 +00001037 Out << Param->getDeclName().getAsString();
1038
1039 if (Param->hasExplicitBound()) {
1040 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1041 }
1042 }
1043 Out << ">";
1044}
1045
Douglas Gregor278f52e2009-05-30 00:08:05 +00001046void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1047 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001048 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001049 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001050 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001051 if (!OMD->getReturnType().isNull()) {
1052 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1053 OMD->getReturnType());
1054 }
Mike Stump11289f42009-09-09 15:08:12 +00001055
Douglas Gregor278f52e2009-05-30 00:08:05 +00001056 std::string name = OMD->getSelector().getAsString();
1057 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001058 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001059 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001060 pos = name.find_first_of(':', lastPos);
Douglas Gregor813a0662015-06-19 18:14:38 +00001061 Out << " " << name.substr(lastPos, pos - lastPos) << ':';
1062 PrintObjCMethodType(OMD->getASTContext(),
1063 PI->getObjCDeclQualifier(),
1064 PI->getType());
1065 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001066 lastPos = pos + 1;
1067 }
Mike Stump11289f42009-09-09 15:08:12 +00001068
Douglas Gregor278f52e2009-05-30 00:08:05 +00001069 if (OMD->param_begin() == OMD->param_end())
1070 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +00001071
Douglas Gregor278f52e2009-05-30 00:08:05 +00001072 if (OMD->isVariadic())
1073 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001074
1075 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001076
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001077 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001078 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001079 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001080 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001081 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001082 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001083}
1084
1085void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1086 std::string I = OID->getNameAsString();
1087 ObjCInterfaceDecl *SID = OID->getSuperClass();
1088
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001089 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001090 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001091 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001092 else
1093 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001094
1095 if (OID->ivar_size() > 0) {
1096 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001097 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001098 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001099 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001100 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001101 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001102 }
1103 Indentation -= Policy.Indentation;
1104 Out << "}\n";
1105 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001106 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1107 Out << "\n";
1108 eolnOut = true;
1109 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001110 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001111 if (!eolnOut)
1112 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001113 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001114}
1115
1116void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1117 std::string I = OID->getNameAsString();
1118 ObjCInterfaceDecl *SID = OID->getSuperClass();
1119
Douglas Gregordc9166c2011-12-15 20:29:51 +00001120 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001121 Out << "@class " << I;
1122
1123 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1124 PrintObjCTypeParams(TypeParams);
1125 }
1126
1127 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001128 return;
1129 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001130 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001131 Out << "@interface " << I;
1132
1133 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1134 PrintObjCTypeParams(TypeParams);
1135 }
1136
Douglas Gregor278f52e2009-05-30 00:08:05 +00001137 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001138 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001139
Douglas Gregor278f52e2009-05-30 00:08:05 +00001140 // Protocols?
1141 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1142 if (!Protocols.empty()) {
1143 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1144 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001145 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001146 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001147 }
Mike Stump11289f42009-09-09 15:08:12 +00001148
Douglas Gregor278f52e2009-05-30 00:08:05 +00001149 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001150 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001151 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001152 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001153 for (const auto *I : OID->ivars()) {
1154 Indent() << I->getASTContext()
1155 .getUnqualifiedObjCPointerType(I->getType())
1156 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001157 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001158 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001159 Out << "}\n";
1160 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001161 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001162 Out << "\n";
1163 eolnOut = true;
1164 }
Mike Stump11289f42009-09-09 15:08:12 +00001165
Douglas Gregor278f52e2009-05-30 00:08:05 +00001166 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001167 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001168 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001169 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001170 // FIXME: implement the rest...
1171}
1172
Douglas Gregor278f52e2009-05-30 00:08:05 +00001173void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001174 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001175 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001176 return;
1177 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001178 // Protocols?
1179 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1180 if (!Protocols.empty()) {
1181 Out << "@protocol " << *PID;
1182 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1183 E = Protocols.end(); I != E; ++I)
1184 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1185 Out << ">\n";
1186 } else
1187 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001188 VisitDeclContext(PID, false);
1189 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001190}
1191
1192void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001193 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001194
1195 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001196 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001197 // FIXME: implement the rest...
1198}
1199
1200void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001201 Out << "@interface " << *PID->getClassInterface();
1202 if (auto TypeParams = PID->getTypeParamList()) {
1203 PrintObjCTypeParams(TypeParams);
1204 }
1205 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001206 if (PID->ivar_size() > 0) {
1207 Out << "{\n";
1208 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001209 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001210 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001211 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001212 Indentation -= Policy.Indentation;
1213 Out << "}\n";
1214 }
1215
Douglas Gregor278f52e2009-05-30 00:08:05 +00001216 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001217 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001218
Douglas Gregor278f52e2009-05-30 00:08:05 +00001219 // FIXME: implement the rest...
1220}
1221
1222void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001223 Out << "@compatibility_alias " << *AID
1224 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001225}
1226
1227/// PrintObjCPropertyDecl - print a property declaration.
1228///
1229void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1230 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1231 Out << "@required\n";
1232 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1233 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001234
Douglas Gregor813a0662015-06-19 18:14:38 +00001235 QualType T = PDecl->getType();
1236
Douglas Gregor278f52e2009-05-30 00:08:05 +00001237 Out << "@property";
1238 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1239 bool first = true;
1240 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001241 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001242 ObjCPropertyDecl::OBJC_PR_readonly) {
1243 Out << (first ? ' ' : ',') << "readonly";
1244 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001245 }
Mike Stump11289f42009-09-09 15:08:12 +00001246
Ted Kremenek897af912011-08-17 21:09:35 +00001247 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001248 Out << (first ? ' ' : ',') << "getter = ";
1249 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001250 first = false;
1251 }
1252 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001253 Out << (first ? ' ' : ',') << "setter = ";
1254 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001255 first = false;
1256 }
Mike Stump11289f42009-09-09 15:08:12 +00001257
Ted Kremenek897af912011-08-17 21:09:35 +00001258 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1259 Out << (first ? ' ' : ',') << "assign";
1260 first = false;
1261 }
Mike Stump11289f42009-09-09 15:08:12 +00001262
Ted Kremenek897af912011-08-17 21:09:35 +00001263 if (PDecl->getPropertyAttributes() &
1264 ObjCPropertyDecl::OBJC_PR_readwrite) {
1265 Out << (first ? ' ' : ',') << "readwrite";
1266 first = false;
1267 }
Mike Stump11289f42009-09-09 15:08:12 +00001268
Ted Kremenek897af912011-08-17 21:09:35 +00001269 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1270 Out << (first ? ' ' : ',') << "retain";
1271 first = false;
1272 }
Mike Stump11289f42009-09-09 15:08:12 +00001273
Ted Kremenek897af912011-08-17 21:09:35 +00001274 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1275 Out << (first ? ' ' : ',') << "strong";
1276 first = false;
1277 }
John McCall31168b02011-06-15 23:02:42 +00001278
Ted Kremenek897af912011-08-17 21:09:35 +00001279 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1280 Out << (first ? ' ' : ',') << "copy";
1281 first = false;
1282 }
Mike Stump11289f42009-09-09 15:08:12 +00001283
Ted Kremenek897af912011-08-17 21:09:35 +00001284 if (PDecl->getPropertyAttributes() &
1285 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1286 Out << (first ? ' ' : ',') << "nonatomic";
1287 first = false;
1288 }
1289 if (PDecl->getPropertyAttributes() &
1290 ObjCPropertyDecl::OBJC_PR_atomic) {
1291 Out << (first ? ' ' : ',') << "atomic";
1292 first = false;
1293 }
1294
Douglas Gregor813a0662015-06-19 18:14:38 +00001295 if (PDecl->getPropertyAttributes() &
1296 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001297 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001298 if (*nullability == NullabilityKind::Unspecified &&
1299 (PDecl->getPropertyAttributes() &
1300 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1301 Out << (first ? ' ' : ',') << "null_resettable";
1302 } else {
1303 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001304 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001305 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001306 first = false;
1307 }
1308 }
1309
Manman Ren387ff7f2016-01-26 18:52:43 +00001310 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1311 Out << (first ? ' ' : ',') << "class";
1312 first = false;
1313 }
1314
Ted Kremenek897af912011-08-17 21:09:35 +00001315 (void) first; // Silence dead store warning due to idiomatic code.
1316 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001317 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001318 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001319 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001320 if (Policy.PolishForDeclaration)
1321 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001322}
1323
1324void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1325 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001326 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001327 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001328 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001329 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001330 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001331 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001332}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001333
1334void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001335 if (!D->isAccessDeclaration())
1336 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001337 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001338 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001339 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001340 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001341}
1342
John McCalle61f2ba2009-11-18 02:36:19 +00001343void
1344DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1345 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001346 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001347 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001348}
1349
1350void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001351 if (!D->isAccessDeclaration())
1352 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001353 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001354 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001355}
John McCall3f746822009-11-17 05:59:44 +00001356
1357void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1358 // ignore
1359}
Alexey Bataeva769e072013-03-22 06:34:35 +00001360
1361void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1362 Out << "#pragma omp threadprivate";
1363 if (!D->varlist_empty()) {
1364 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1365 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001366 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001367 Out << (I == D->varlist_begin() ? '(' : ',');
1368 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1369 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001370 }
1371 Out << ")";
1372 }
1373}
1374
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001375void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1376 if (!D->isInvalidDecl()) {
1377 Out << "#pragma omp declare reduction (";
1378 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1379 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1380 nullptr,
1381#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1382 Spelling,
1383#include "clang/Basic/OperatorKinds.def"
1384 };
1385 const char *OpName =
1386 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1387 assert(OpName && "not an overloaded operator");
1388 Out << OpName;
1389 } else {
1390 assert(D->getDeclName().isIdentifier());
1391 D->printName(Out);
1392 }
1393 Out << " : ";
1394 D->getType().print(Out, Policy);
1395 Out << " : ";
1396 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1397 Out << ")";
1398 if (auto *Init = D->getInitializer()) {
1399 Out << " initializer(";
1400 Init->printPretty(Out, nullptr, Policy, 0);
1401 Out << ")";
1402 }
1403 }
1404}
1405
Alexey Bataev4244be22016-02-11 05:35:55 +00001406void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001407 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1408}
1409