blob: bfdb47b90d8b513dc6936c1b606f142becc0a668 [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();
Vassil Vassilevcdaa31f2016-07-08 16:04:22 +0000135 else if (const AutoType *ATy = BaseType->getAs<AutoType>())
136 BaseType = ATy->getDeducedType();
Eli Friedman79635842009-05-30 04:20:30 +0000137 else
David Blaikie83d382b2011-09-23 05:06:16 +0000138 llvm_unreachable("Unknown declarator!");
Eli Friedman79635842009-05-30 04:20:30 +0000139 }
140 return BaseType;
141}
142
143static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000144 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000145 return TDD->getUnderlyingType();
146 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
147 return VD->getType();
148 return QualType();
149}
150
151void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000152 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000153 unsigned Indentation) {
154 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000155 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000156 return;
157 }
158
159 Decl** End = Begin + NumDecls;
160 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
161 if (TD)
162 ++Begin;
163
164 PrintingPolicy SubPolicy(Policy);
Eli Friedman79635842009-05-30 04:20:30 +0000165
166 bool isFirst = true;
167 for ( ; Begin != End; ++Begin) {
168 if (isFirst) {
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000169 if(TD)
170 SubPolicy.IncludeTagDefinition = true;
Eli Friedman79635842009-05-30 04:20:30 +0000171 SubPolicy.SuppressSpecifiers = false;
172 isFirst = false;
173 } else {
174 if (!isFirst) Out << ", ";
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000175 SubPolicy.IncludeTagDefinition = false;
Eli Friedman79635842009-05-30 04:20:30 +0000176 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 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000249 T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
Richard Smitha4bb2922014-07-23 03:17:06 +0000250}
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";
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000363
364 // Declare target attribute is special one, natural spelling for the pragma
365 // assumes "ending" construct so print it here.
366 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
367 Out << "#pragma omp end declare target\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000368 }
369
Eli Friedman79635842009-05-30 04:20:30 +0000370 if (!Decls.empty())
371 ProcessDeclGroup(Decls);
372
Douglas Gregor278f52e2009-05-30 00:08:05 +0000373 if (Indent)
374 Indentation -= Policy.Indentation;
375}
376
377void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
378 VisitDeclContext(D, false);
379}
380
381void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000382 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000383 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000384
385 if (D->isModulePrivate())
386 Out << "__module_private__ ";
387 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000388 QualType Ty = D->getTypeSourceInfo()->getType();
389 Ty.print(Out, Policy, D->getName(), Indentation);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000390 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000391}
392
Richard Smithdda56e42011-04-15 14:24:37 +0000393void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000394 Out << "using " << *D;
395 prettyPrintAttributes(D);
396 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000397}
398
Douglas Gregor278f52e2009-05-30 00:08:05 +0000399void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000400 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
401 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000402 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000403 if (D->isScoped()) {
404 if (D->isScopedUsingClassTag())
405 Out << "class ";
406 else
407 Out << "struct ";
408 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000409 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000410
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000411 if (D->isFixed())
412 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000413
John McCallf937c022011-10-07 06:10:15 +0000414 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000415 Out << " {\n";
416 VisitDeclContext(D);
417 Indent() << "}";
418 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000419 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000420}
421
422void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000423 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
424 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000425 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000426
427 prettyPrintAttributes(D);
428
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000429 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000430 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000431
John McCallf937c022011-10-07 06:10:15 +0000432 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000433 Out << " {\n";
434 VisitDeclContext(D);
435 Indent() << "}";
436 }
437}
438
439void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000440 Out << *D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000441 if (Expr *Init = D->getInitExpr()) {
442 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000443 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000444 }
445}
446
Mike Stump11289f42009-09-09 15:08:12 +0000447void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000448 if (!D->getDescribedFunctionTemplate() &&
449 !D->isFunctionTemplateSpecialization())
450 prettyPrintPragmas(D);
451
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000452 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000453 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000454 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000455 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000456 case SC_None: break;
457 case SC_Extern: Out << "extern "; break;
458 case SC_Static: Out << "static "; break;
459 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000460 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000461 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000462 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000463
Douglas Gregor26701a42011-09-09 02:06:17 +0000464 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000465 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000466 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000467 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000468 if ((CDecl && CDecl->isExplicitSpecified()) ||
469 (ConversionDecl && ConversionDecl->isExplicit()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000470 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000471 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000472
Douglas Gregor2d042f12009-05-30 05:39:39 +0000473 PrintingPolicy SubPolicy(Policy);
474 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000475 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000476
Abramo Bagnara6d810632010-12-14 22:11:44 +0000477 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000478 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000479 Proto = '(' + Proto + ')';
480 Ty = PT->getInnerType();
481 }
482
Reid Kleckner0503a872013-12-05 01:23:43 +0000483 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000484 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000485 if (D->hasWrittenPrototype())
486 FT = dyn_cast<FunctionProtoType>(AFT);
487
488 Proto += "(";
489 if (FT) {
490 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000491 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000492 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
493 if (i) POut << ", ";
494 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
495 }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Douglas Gregor278f52e2009-05-30 00:08:05 +0000497 if (FT->isVariadic()) {
498 if (D->getNumParams()) POut << ", ";
499 POut << "...";
500 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000501 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000502 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
503 if (i)
504 Proto += ", ";
505 Proto += D->getParamDecl(i)->getNameAsString();
506 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000507 }
508
509 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000510
David Blaikief5697e52012-08-10 00:55:35 +0000511 if (FT) {
512 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000513 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000514 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000515 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000516 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000517 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000518
519 switch (FT->getRefQualifier()) {
520 case RQ_None:
521 break;
522 case RQ_LValue:
523 Proto += " &";
524 break;
525 case RQ_RValue:
526 Proto += " &&";
527 break;
528 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000529 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000530
531 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000532 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000533 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000534 Proto += "...";
535 else
536 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
537 if (I)
538 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000539
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000540 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000541 }
542 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000543 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
544 Proto += " noexcept";
545 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
546 Proto += "(";
547 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000548 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000549 Indentation);
550 EOut.flush();
551 Proto += EOut.str();
552 Proto += ")";
553 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000554 }
555
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000556 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000557 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000558 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000559 if (BMInitializer->isInClassMemberInitializer())
560 continue;
561
562 if (!HasInitializerList) {
563 Proto += " : ";
564 Out << Proto;
565 Proto.clear();
566 HasInitializerList = true;
567 } else
568 Out << ", ";
569
570 if (BMInitializer->isAnyMemberInitializer()) {
571 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000572 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000573 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000574 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000575 }
576
577 Out << "(";
578 if (!BMInitializer->getInit()) {
579 // Nothing to print
580 } else {
581 Expr *Init = BMInitializer->getInit();
582 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
583 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000584
Richard Smith938f40b2011-06-11 17:19:42 +0000585 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000586
587 Expr *SimpleInit = nullptr;
588 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000589 unsigned NumArgs = 0;
590 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
591 Args = ParenList->getExprs();
592 NumArgs = ParenList->getNumExprs();
593 } else if (CXXConstructExpr *Construct
594 = dyn_cast<CXXConstructExpr>(Init)) {
595 Args = Construct->getArgs();
596 NumArgs = Construct->getNumArgs();
597 } else
598 SimpleInit = Init;
599
600 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000601 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000602 else {
603 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000604 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000605 if (isa<CXXDefaultArgExpr>(Args[I]))
606 break;
607
608 if (I)
609 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000610 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000611 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000612 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000613 }
Richard Smith938f40b2011-06-11 17:19:42 +0000614 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000615 if (BMInitializer->isPackExpansion())
616 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000617 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000618 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000619 if (FT && FT->hasTrailingReturn()) {
620 Out << "auto " << Proto << " -> ";
621 Proto.clear();
622 }
Alp Toker314cc812014-01-25 16:55:45 +0000623 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000624 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000625 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000626 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000627 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000628 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000629 }
630
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000631 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000632
633 if (D->isPure())
634 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000635 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000636 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000637 else if (D->isExplicitlyDefaulted())
638 Out << " = default";
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000639 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000640 if (!D->hasPrototype() && D->getNumParams()) {
641 // This is a K&R function definition, so we need to print the
642 // parameters.
643 Out << '\n';
Richard Smith235341b2012-08-16 03:56:14 +0000644 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000645 Indentation += Policy.Indentation;
646 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
647 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000648 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000649 Out << ";\n";
650 }
651 Indentation -= Policy.Indentation;
652 } else
653 Out << ' ';
654
Richard Trieuddd01ce2014-06-09 22:53:25 +0000655 if (D->getBody())
656 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000657 Out << '\n';
658 }
659}
660
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000661void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
662 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000663 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
664 for (unsigned i = 0; i < NumTPLists; ++i)
665 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000666 Out << "friend ";
667 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000668 }
669 else if (FunctionDecl *FD =
670 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
671 Out << "friend ";
672 VisitFunctionDecl(FD);
673 }
674 else if (FunctionTemplateDecl *FTD =
675 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
676 Out << "friend ";
677 VisitFunctionTemplateDecl(FTD);
678 }
679 else if (ClassTemplateDecl *CTD =
680 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
681 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000682 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000683 }
684}
685
Douglas Gregor278f52e2009-05-30 00:08:05 +0000686void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000687 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000688 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000689 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000690 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
691 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000692
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000693 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000694 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000695
696 if (D->isBitField()) {
697 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000698 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000699 }
Richard Smith938f40b2011-06-11 17:19:42 +0000700
701 Expr *Init = D->getInClassInitializer();
702 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000703 if (D->getInClassInitStyle() == ICIS_ListInit)
704 Out << " ";
705 else
706 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000707 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000708 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000709 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000710}
711
Chris Lattnercab02a62011-02-17 20:34:02 +0000712void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000713 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000714}
715
Douglas Gregor278f52e2009-05-30 00:08:05 +0000716void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000717 prettyPrintPragmas(D);
Richard Smithfd3834f2013-04-13 02:43:54 +0000718 if (!Policy.SuppressSpecifiers) {
719 StorageClass SC = D->getStorageClass();
720 if (SC != SC_None)
721 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000722
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000723 switch (D->getTSCSpec()) {
724 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000725 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000726 case TSCS___thread:
727 Out << "__thread ";
728 break;
729 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000730 Out << "_Thread_local ";
731 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000732 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000733 Out << "thread_local ";
734 break;
735 }
736
737 if (D->isModulePrivate())
738 Out << "__module_private__ ";
739 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000740
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000741 QualType T = D->getTypeSourceInfo()
742 ? D->getTypeSourceInfo()->getType()
743 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
Richard Smitha4bb2922014-07-23 03:17:06 +0000744 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000745 Expr *Init = D->getInit();
746 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000747 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000748 if (CXXConstructExpr *Construct =
749 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000750 if (D->getInitStyle() == VarDecl::CallInit &&
751 !Construct->isListInitialization()) {
752 ImplicitInit = Construct->getNumArgs() == 0 ||
753 Construct->getArg(0)->isDefaultArgument();
754 }
755 }
Sebastian Redla9351792012-02-11 23:51:47 +0000756 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000757 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000758 Out << "(";
759 else if (D->getInitStyle() == VarDecl::CInit) {
760 Out << " = ";
761 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000762 PrintingPolicy SubPolicy(Policy);
763 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000764 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000765 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000766 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000767 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000768 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000769 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000770 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000771}
772
773void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
774 VisitVarDecl(D);
775}
776
777void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
778 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000779 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000780 Out << ")";
781}
782
Douglas Gregorba345522011-12-02 23:23:56 +0000783void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000784 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000785 << ";\n";
786}
787
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000788void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
789 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000790 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000791 if (StringLiteral *SL = D->getMessage()) {
792 Out << ", ";
793 SL->printPretty(Out, nullptr, Policy, Indentation);
794 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000795 Out << ")";
796}
797
Douglas Gregor278f52e2009-05-30 00:08:05 +0000798//----------------------------------------------------------------------------
799// C++ declarations
800//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000801void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000802 if (D->isInline())
803 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000804 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000805 VisitDeclContext(D);
806 Indent() << "}";
807}
808
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000809void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
810 Out << "using namespace ";
811 if (D->getQualifier())
812 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000813 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000814}
815
Douglas Gregor18231932009-05-30 06:48:27 +0000816void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000817 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000818 if (D->getQualifier())
819 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000820 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000821}
822
Michael Han84324352013-02-22 17:15:32 +0000823void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
824 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000825}
826
Douglas Gregor5f478b72009-05-30 06:58:37 +0000827void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000828 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000829 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
830 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000831 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000832
833 prettyPrintAttributes(D);
834
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000835 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000836 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000837
John McCallf937c022011-10-07 06:10:15 +0000838 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000839 // Print the base classes
840 if (D->getNumBases()) {
841 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000842 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
843 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000844 if (Base != D->bases_begin())
845 Out << ", ";
846
847 if (Base->isVirtual())
848 Out << "virtual ";
849
Anders Carlsson6df9e072009-08-29 20:36:12 +0000850 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000851 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000852 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000853 Out << " ";
854 }
855 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000856
857 if (Base->isPackExpansion())
858 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000859 }
860 }
861
862 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000863 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000864 Out << " {\n";
865 VisitDeclContext(D);
866 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000867 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000868}
869
870void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
871 const char *l;
872 if (D->getLanguage() == LinkageSpecDecl::lang_c)
873 l = "C";
874 else {
875 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
876 "unknown language in linkage specification");
877 l = "C++";
878 }
879
880 Out << "extern \"" << l << "\" ";
881 if (D->hasBraces()) {
882 Out << "{\n";
883 VisitDeclContext(D);
884 Indent() << "}";
885 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000886 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000887}
888
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000889void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
890 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000891 assert(Params);
892 assert(!Args || Params->size() == Args->size());
893
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000894 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000895
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000896 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
897 if (i != 0)
898 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000899
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000900 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000901 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000902 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000903
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000904 if (TTP->wasDeclaredWithTypename())
905 Out << "typename ";
906 else
907 Out << "class ";
908
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000909 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000910 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000911
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000912 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000913
Richard Trieu82398f92011-07-28 00:19:05 +0000914 if (Args) {
915 Out << " = ";
916 Args->get(i).print(Policy, Out);
917 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000918 Out << " = ";
919 Out << TTP->getDefaultArgument().getAsString(Policy);
920 };
Mike Stump11289f42009-09-09 15:08:12 +0000921 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000922 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000923 StringRef Name;
924 if (IdentifierInfo *II = NTTP->getIdentifier())
925 Name = II->getName();
926 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000927
Richard Trieu82398f92011-07-28 00:19:05 +0000928 if (Args) {
929 Out << " = ";
930 Args->get(i).print(Policy, Out);
931 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000932 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000933 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
934 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000935 }
Richard Smith030f4992011-04-15 13:38:57 +0000936 } else if (const TemplateTemplateParmDecl *TTPD =
937 dyn_cast<TemplateTemplateParmDecl>(Param)) {
938 VisitTemplateDecl(TTPD);
939 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000940 }
941 }
Mike Stump11289f42009-09-09 15:08:12 +0000942
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000943 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000944}
945
946void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
947 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000948
Richard Smith030f4992011-04-15 13:38:57 +0000949 if (const TemplateTemplateParmDecl *TTP =
950 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000951 Out << "class ";
952 if (TTP->isParameterPack())
953 Out << "...";
954 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000955 } else {
956 Visit(D->getTemplatedDecl());
957 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000958}
959
Richard Trieu82398f92011-07-28 00:19:05 +0000960void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
961 if (PrintInstantiation) {
962 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000963 for (auto *I : D->specializations()) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000964 prettyPrintPragmas(I);
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000965 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
966 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000967 }
968 }
969
Alexey Bataev6d455322015-10-12 06:59:48 +0000970 prettyPrintPragmas(D->getTemplatedDecl());
Richard Trieu82398f92011-07-28 00:19:05 +0000971 return VisitRedeclarableTemplateDecl(D);
972}
973
974void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
975 if (PrintInstantiation) {
976 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000977 for (auto *I : D->specializations()) {
978 PrintTemplateParameters(Params, &I->getTemplateArgs());
979 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000980 Out << '\n';
981 }
982 }
983
984 return VisitRedeclarableTemplateDecl(D);
985}
986
Douglas Gregor278f52e2009-05-30 00:08:05 +0000987//----------------------------------------------------------------------------
988// Objective-C declarations
989//----------------------------------------------------------------------------
990
Douglas Gregor813a0662015-06-19 18:14:38 +0000991void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
992 Decl::ObjCDeclQualifier Quals,
993 QualType T) {
994 Out << '(';
995 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
996 Out << "in ";
997 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
998 Out << "inout ";
999 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1000 Out << "out ";
1001 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1002 Out << "bycopy ";
1003 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1004 Out << "byref ";
1005 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1006 Out << "oneway ";
1007 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001008 if (auto nullability = AttributedType::stripOuterNullability(T))
1009 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001010 }
1011
1012 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1013 Out << ')';
1014}
1015
Douglas Gregor85f3f952015-07-07 03:57:15 +00001016void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1017 Out << "<";
1018 unsigned First = true;
1019 for (auto *Param : *Params) {
1020 if (First) {
1021 First = false;
1022 } else {
1023 Out << ", ";
1024 }
1025
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001026 switch (Param->getVariance()) {
1027 case ObjCTypeParamVariance::Invariant:
1028 break;
1029
1030 case ObjCTypeParamVariance::Covariant:
1031 Out << "__covariant ";
1032 break;
1033
1034 case ObjCTypeParamVariance::Contravariant:
1035 Out << "__contravariant ";
1036 break;
1037 }
1038
Douglas Gregor85f3f952015-07-07 03:57:15 +00001039 Out << Param->getDeclName().getAsString();
1040
1041 if (Param->hasExplicitBound()) {
1042 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1043 }
1044 }
1045 Out << ">";
1046}
1047
Douglas Gregor278f52e2009-05-30 00:08:05 +00001048void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1049 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001050 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001051 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001052 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001053 if (!OMD->getReturnType().isNull()) {
1054 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1055 OMD->getReturnType());
1056 }
Mike Stump11289f42009-09-09 15:08:12 +00001057
Douglas Gregor278f52e2009-05-30 00:08:05 +00001058 std::string name = OMD->getSelector().getAsString();
1059 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001060 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001061 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001062 pos = name.find_first_of(':', lastPos);
Douglas Gregor813a0662015-06-19 18:14:38 +00001063 Out << " " << name.substr(lastPos, pos - lastPos) << ':';
1064 PrintObjCMethodType(OMD->getASTContext(),
1065 PI->getObjCDeclQualifier(),
1066 PI->getType());
1067 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001068 lastPos = pos + 1;
1069 }
Mike Stump11289f42009-09-09 15:08:12 +00001070
Douglas Gregor278f52e2009-05-30 00:08:05 +00001071 if (OMD->param_begin() == OMD->param_end())
1072 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +00001073
Douglas Gregor278f52e2009-05-30 00:08:05 +00001074 if (OMD->isVariadic())
1075 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001076
1077 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001078
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001079 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001080 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001081 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001082 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001083 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001084 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001085}
1086
1087void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1088 std::string I = OID->getNameAsString();
1089 ObjCInterfaceDecl *SID = OID->getSuperClass();
1090
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001091 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001092 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001093 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001094 else
1095 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001096
1097 if (OID->ivar_size() > 0) {
1098 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001099 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001100 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001101 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001102 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001103 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001104 }
1105 Indentation -= Policy.Indentation;
1106 Out << "}\n";
1107 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001108 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1109 Out << "\n";
1110 eolnOut = true;
1111 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001112 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001113 if (!eolnOut)
1114 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001115 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001116}
1117
1118void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1119 std::string I = OID->getNameAsString();
1120 ObjCInterfaceDecl *SID = OID->getSuperClass();
1121
Douglas Gregordc9166c2011-12-15 20:29:51 +00001122 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001123 Out << "@class " << I;
1124
1125 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1126 PrintObjCTypeParams(TypeParams);
1127 }
1128
1129 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001130 return;
1131 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001132 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001133 Out << "@interface " << I;
1134
1135 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1136 PrintObjCTypeParams(TypeParams);
1137 }
1138
Douglas Gregor278f52e2009-05-30 00:08:05 +00001139 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001140 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001141
Douglas Gregor278f52e2009-05-30 00:08:05 +00001142 // Protocols?
1143 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1144 if (!Protocols.empty()) {
1145 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1146 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001147 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001148 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001149 }
Mike Stump11289f42009-09-09 15:08:12 +00001150
Douglas Gregor278f52e2009-05-30 00:08:05 +00001151 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001152 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001153 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001154 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001155 for (const auto *I : OID->ivars()) {
1156 Indent() << I->getASTContext()
1157 .getUnqualifiedObjCPointerType(I->getType())
1158 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001159 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001160 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001161 Out << "}\n";
1162 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001163 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001164 Out << "\n";
1165 eolnOut = true;
1166 }
Mike Stump11289f42009-09-09 15:08:12 +00001167
Douglas Gregor278f52e2009-05-30 00:08:05 +00001168 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001169 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001170 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001171 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001172 // FIXME: implement the rest...
1173}
1174
Douglas Gregor278f52e2009-05-30 00:08:05 +00001175void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001176 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001177 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001178 return;
1179 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001180 // Protocols?
1181 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1182 if (!Protocols.empty()) {
1183 Out << "@protocol " << *PID;
1184 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1185 E = Protocols.end(); I != E; ++I)
1186 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1187 Out << ">\n";
1188 } else
1189 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001190 VisitDeclContext(PID, false);
1191 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001192}
1193
1194void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001195 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001196
1197 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001198 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001199 // FIXME: implement the rest...
1200}
1201
1202void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001203 Out << "@interface " << *PID->getClassInterface();
1204 if (auto TypeParams = PID->getTypeParamList()) {
1205 PrintObjCTypeParams(TypeParams);
1206 }
1207 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001208 if (PID->ivar_size() > 0) {
1209 Out << "{\n";
1210 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001211 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001212 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001213 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001214 Indentation -= Policy.Indentation;
1215 Out << "}\n";
1216 }
1217
Douglas Gregor278f52e2009-05-30 00:08:05 +00001218 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001219 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001220
Douglas Gregor278f52e2009-05-30 00:08:05 +00001221 // FIXME: implement the rest...
1222}
1223
1224void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001225 Out << "@compatibility_alias " << *AID
1226 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001227}
1228
1229/// PrintObjCPropertyDecl - print a property declaration.
1230///
1231void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1232 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1233 Out << "@required\n";
1234 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1235 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001236
Douglas Gregor813a0662015-06-19 18:14:38 +00001237 QualType T = PDecl->getType();
1238
Douglas Gregor278f52e2009-05-30 00:08:05 +00001239 Out << "@property";
1240 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1241 bool first = true;
1242 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001243 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001244 ObjCPropertyDecl::OBJC_PR_readonly) {
1245 Out << (first ? ' ' : ',') << "readonly";
1246 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001247 }
Mike Stump11289f42009-09-09 15:08:12 +00001248
Ted Kremenek897af912011-08-17 21:09:35 +00001249 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001250 Out << (first ? ' ' : ',') << "getter = ";
1251 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001252 first = false;
1253 }
1254 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001255 Out << (first ? ' ' : ',') << "setter = ";
1256 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001257 first = false;
1258 }
Mike Stump11289f42009-09-09 15:08:12 +00001259
Ted Kremenek897af912011-08-17 21:09:35 +00001260 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1261 Out << (first ? ' ' : ',') << "assign";
1262 first = false;
1263 }
Mike Stump11289f42009-09-09 15:08:12 +00001264
Ted Kremenek897af912011-08-17 21:09:35 +00001265 if (PDecl->getPropertyAttributes() &
1266 ObjCPropertyDecl::OBJC_PR_readwrite) {
1267 Out << (first ? ' ' : ',') << "readwrite";
1268 first = false;
1269 }
Mike Stump11289f42009-09-09 15:08:12 +00001270
Ted Kremenek897af912011-08-17 21:09:35 +00001271 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1272 Out << (first ? ' ' : ',') << "retain";
1273 first = false;
1274 }
Mike Stump11289f42009-09-09 15:08:12 +00001275
Ted Kremenek897af912011-08-17 21:09:35 +00001276 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1277 Out << (first ? ' ' : ',') << "strong";
1278 first = false;
1279 }
John McCall31168b02011-06-15 23:02:42 +00001280
Ted Kremenek897af912011-08-17 21:09:35 +00001281 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1282 Out << (first ? ' ' : ',') << "copy";
1283 first = false;
1284 }
Mike Stump11289f42009-09-09 15:08:12 +00001285
Ted Kremenek897af912011-08-17 21:09:35 +00001286 if (PDecl->getPropertyAttributes() &
1287 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1288 Out << (first ? ' ' : ',') << "nonatomic";
1289 first = false;
1290 }
1291 if (PDecl->getPropertyAttributes() &
1292 ObjCPropertyDecl::OBJC_PR_atomic) {
1293 Out << (first ? ' ' : ',') << "atomic";
1294 first = false;
1295 }
1296
Douglas Gregor813a0662015-06-19 18:14:38 +00001297 if (PDecl->getPropertyAttributes() &
1298 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001299 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001300 if (*nullability == NullabilityKind::Unspecified &&
1301 (PDecl->getPropertyAttributes() &
1302 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1303 Out << (first ? ' ' : ',') << "null_resettable";
1304 } else {
1305 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001306 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001307 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001308 first = false;
1309 }
1310 }
1311
Manman Ren387ff7f2016-01-26 18:52:43 +00001312 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1313 Out << (first ? ' ' : ',') << "class";
1314 first = false;
1315 }
1316
Ted Kremenek897af912011-08-17 21:09:35 +00001317 (void) first; // Silence dead store warning due to idiomatic code.
1318 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001319 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001320 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001321 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001322 if (Policy.PolishForDeclaration)
1323 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001324}
1325
1326void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1327 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001328 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001329 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001330 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001331 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001332 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001333 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001334}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001335
1336void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001337 if (!D->isAccessDeclaration())
1338 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001339 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001340 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001341 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001342 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001343}
1344
John McCalle61f2ba2009-11-18 02:36:19 +00001345void
1346DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1347 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001348 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001349 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001350}
1351
1352void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001353 if (!D->isAccessDeclaration())
1354 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001355 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001356 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001357}
John McCall3f746822009-11-17 05:59:44 +00001358
1359void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1360 // ignore
1361}
Alexey Bataeva769e072013-03-22 06:34:35 +00001362
1363void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1364 Out << "#pragma omp threadprivate";
1365 if (!D->varlist_empty()) {
1366 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1367 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001368 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001369 Out << (I == D->varlist_begin() ? '(' : ',');
1370 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1371 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001372 }
1373 Out << ")";
1374 }
1375}
1376
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001377void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1378 if (!D->isInvalidDecl()) {
1379 Out << "#pragma omp declare reduction (";
1380 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1381 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1382 nullptr,
1383#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1384 Spelling,
1385#include "clang/Basic/OperatorKinds.def"
1386 };
1387 const char *OpName =
1388 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1389 assert(OpName && "not an overloaded operator");
1390 Out << OpName;
1391 } else {
1392 assert(D->getDeclName().isIdentifier());
1393 D->printName(Out);
1394 }
1395 Out << " : ";
1396 D->getType().print(Out, Policy);
1397 Out << " : ";
1398 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1399 Out << ")";
1400 if (auto *Init = D->getInitializer()) {
1401 Out << " initializer(";
1402 Init->printPretty(Out, nullptr, Policy, 0);
1403 Out << ")";
1404 }
1405 }
1406}
1407
Alexey Bataev4244be22016-02-11 05:35:55 +00001408void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001409 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1410}
1411