blob: 4a8db43817cdead1fdda44275a4d1c28fca69f24 [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;
Serge Pavlovdc586c42016-10-31 05:11:12 +0000340 else if (isa<FunctionDecl>(*D) && cast<FunctionDecl>(*D)->hasBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000341 Terminator = nullptr;
Serge Pavlovdc586c42016-10-31 05:11:12 +0000342 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
Yaron Keren51db8772014-05-07 09:53:02 +0000343 Terminator = nullptr;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000344 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000345 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000346 isa<ObjCInterfaceDecl>(*D) ||
347 isa<ObjCProtocolDecl>(*D) ||
348 isa<ObjCCategoryImplDecl>(*D) ||
349 isa<ObjCCategoryDecl>(*D))
Yaron Keren51db8772014-05-07 09:53:02 +0000350 Terminator = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000351 else if (isa<EnumConstantDecl>(*D)) {
352 DeclContext::decl_iterator Next = D;
353 ++Next;
354 if (Next != DEnd)
355 Terminator = ",";
356 } else
357 Terminator = ";";
358
359 if (Terminator)
360 Out << Terminator;
361 Out << "\n";
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000362
363 // Declare target attribute is special one, natural spelling for the pragma
364 // assumes "ending" construct so print it here.
365 if (D->hasAttr<OMPDeclareTargetDeclAttr>())
366 Out << "#pragma omp end declare target\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000367 }
368
Eli Friedman79635842009-05-30 04:20:30 +0000369 if (!Decls.empty())
370 ProcessDeclGroup(Decls);
371
Douglas Gregor278f52e2009-05-30 00:08:05 +0000372 if (Indent)
373 Indentation -= Policy.Indentation;
374}
375
376void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
377 VisitDeclContext(D, false);
378}
379
380void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000381 if (!Policy.SuppressSpecifiers) {
Eli Friedman79635842009-05-30 04:20:30 +0000382 Out << "typedef ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000383
384 if (D->isModulePrivate())
385 Out << "__module_private__ ";
386 }
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000387 QualType Ty = D->getTypeSourceInfo()->getType();
388 Ty.print(Out, Policy, D->getName(), Indentation);
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000389 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000390}
391
Richard Smithdda56e42011-04-15 14:24:37 +0000392void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000393 Out << "using " << *D;
394 prettyPrintAttributes(D);
395 Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
Richard Smithdda56e42011-04-15 14:24:37 +0000396}
397
Douglas Gregor278f52e2009-05-30 00:08:05 +0000398void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000399 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
400 Out << "__module_private__ ";
Douglas Gregorec0e3662010-12-01 16:01:08 +0000401 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000402 if (D->isScoped()) {
403 if (D->isScopedUsingClassTag())
404 Out << "class ";
405 else
406 Out << "struct ";
407 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000408 Out << *D;
Douglas Gregorec0e3662010-12-01 16:01:08 +0000409
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000410 if (D->isFixed())
411 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregorec0e3662010-12-01 16:01:08 +0000412
John McCallf937c022011-10-07 06:10:15 +0000413 if (D->isCompleteDefinition()) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000414 Out << " {\n";
415 VisitDeclContext(D);
416 Indent() << "}";
417 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000418 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000419}
420
421void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor26701a42011-09-09 02:06:17 +0000422 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
423 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000424 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000425
426 prettyPrintAttributes(D);
427
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000428 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000429 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000430
John McCallf937c022011-10-07 06:10:15 +0000431 if (D->isCompleteDefinition()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000432 Out << " {\n";
433 VisitDeclContext(D);
434 Indent() << "}";
435 }
436}
437
438void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000439 Out << *D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000440 if (Expr *Init = D->getInitExpr()) {
441 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000442 Init->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000443 }
444}
445
Mike Stump11289f42009-09-09 15:08:12 +0000446void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000447 if (!D->getDescribedFunctionTemplate() &&
448 !D->isFunctionTemplateSpecialization())
449 prettyPrintPragmas(D);
450
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000451 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000452 CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
Eli Friedman79635842009-05-30 04:20:30 +0000453 if (!Policy.SuppressSpecifiers) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000454 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000455 case SC_None: break;
456 case SC_Extern: Out << "extern "; break;
457 case SC_Static: Out << "static "; break;
458 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Anastasia Stulovabcea6962015-09-30 14:08:20 +0000459 case SC_Auto: case SC_Register:
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000460 llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000461 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000462
Douglas Gregor26701a42011-09-09 02:06:17 +0000463 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000464 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000465 if (D->isModulePrivate()) Out << "__module_private__ ";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000466 if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000467 if ((CDecl && CDecl->isExplicitSpecified()) ||
468 (ConversionDecl && ConversionDecl->isExplicit()))
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000469 Out << "explicit ";
Eli Friedman79635842009-05-30 04:20:30 +0000470 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000471
Douglas Gregor2d042f12009-05-30 05:39:39 +0000472 PrintingPolicy SubPolicy(Policy);
473 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000474 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000475
Abramo Bagnara6d810632010-12-14 22:11:44 +0000476 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000477 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000478 Proto = '(' + Proto + ')';
479 Ty = PT->getInnerType();
480 }
481
Reid Kleckner0503a872013-12-05 01:23:43 +0000482 if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000483 const FunctionProtoType *FT = nullptr;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000484 if (D->hasWrittenPrototype())
485 FT = dyn_cast<FunctionProtoType>(AFT);
486
487 Proto += "(";
488 if (FT) {
489 llvm::raw_string_ostream POut(Proto);
Richard Smith235341b2012-08-16 03:56:14 +0000490 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000491 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
492 if (i) POut << ", ";
493 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
494 }
Mike Stump11289f42009-09-09 15:08:12 +0000495
Douglas Gregor278f52e2009-05-30 00:08:05 +0000496 if (FT->isVariadic()) {
497 if (D->getNumParams()) POut << ", ";
498 POut << "...";
499 }
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000500 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman79635842009-05-30 04:20:30 +0000501 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
502 if (i)
503 Proto += ", ";
504 Proto += D->getParamDecl(i)->getNameAsString();
505 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000506 }
507
508 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000509
David Blaikief5697e52012-08-10 00:55:35 +0000510 if (FT) {
511 if (FT->isConst())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000512 Proto += " const";
David Blaikief5697e52012-08-10 00:55:35 +0000513 if (FT->isVolatile())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000514 Proto += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +0000515 if (FT->isRestrict())
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000516 Proto += " restrict";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000517
518 switch (FT->getRefQualifier()) {
519 case RQ_None:
520 break;
521 case RQ_LValue:
522 Proto += " &";
523 break;
524 case RQ_RValue:
525 Proto += " &&";
526 break;
527 }
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000528 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000529
530 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000531 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000532 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000533 Proto += "...";
534 else
535 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
536 if (I)
537 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000538
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000539 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor049bdca2009-12-08 17:45:32 +0000540 }
541 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000542 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
543 Proto += " noexcept";
544 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
545 Proto += "(";
546 llvm::raw_string_ostream EOut(Proto);
Craig Topper36250ad2014-05-12 05:36:57 +0000547 FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000548 Indentation);
549 EOut.flush();
550 Proto += EOut.str();
551 Proto += ")";
552 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000553 }
554
Fariborz Jahanian69c403c2012-12-05 22:19:06 +0000555 if (CDecl) {
Richard Smith938f40b2011-06-11 17:19:42 +0000556 bool HasInitializerList = false;
Aaron Ballman0ad78302014-03-13 17:34:31 +0000557 for (const auto *BMInitializer : CDecl->inits()) {
Richard Smith938f40b2011-06-11 17:19:42 +0000558 if (BMInitializer->isInClassMemberInitializer())
559 continue;
560
561 if (!HasInitializerList) {
562 Proto += " : ";
563 Out << Proto;
564 Proto.clear();
565 HasInitializerList = true;
566 } else
567 Out << ", ";
568
569 if (BMInitializer->isAnyMemberInitializer()) {
570 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000571 Out << *FD;
Richard Smith938f40b2011-06-11 17:19:42 +0000572 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000573 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith938f40b2011-06-11 17:19:42 +0000574 }
575
576 Out << "(";
577 if (!BMInitializer->getInit()) {
578 // Nothing to print
579 } else {
580 Expr *Init = BMInitializer->getInit();
581 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
582 Init = Tmp->getSubExpr();
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000583
Richard Smith938f40b2011-06-11 17:19:42 +0000584 Init = Init->IgnoreParens();
Craig Topper36250ad2014-05-12 05:36:57 +0000585
586 Expr *SimpleInit = nullptr;
587 Expr **Args = nullptr;
Richard Smith938f40b2011-06-11 17:19:42 +0000588 unsigned NumArgs = 0;
589 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
590 Args = ParenList->getExprs();
591 NumArgs = ParenList->getNumExprs();
592 } else if (CXXConstructExpr *Construct
593 = dyn_cast<CXXConstructExpr>(Init)) {
594 Args = Construct->getArgs();
595 NumArgs = Construct->getNumArgs();
596 } else
597 SimpleInit = Init;
598
599 if (SimpleInit)
Craig Topper36250ad2014-05-12 05:36:57 +0000600 SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000601 else {
602 for (unsigned I = 0; I != NumArgs; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000603 assert(Args[I] != nullptr && "Expected non-null Expr");
Richard Smith938f40b2011-06-11 17:19:42 +0000604 if (isa<CXXDefaultArgExpr>(Args[I]))
605 break;
606
607 if (I)
608 Out << ", ";
Craig Topper36250ad2014-05-12 05:36:57 +0000609 Args[I]->printPretty(Out, nullptr, Policy, Indentation);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000610 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000611 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000612 }
Richard Smith938f40b2011-06-11 17:19:42 +0000613 Out << ")";
Benjamin Kramer2907b082014-02-25 18:49:49 +0000614 if (BMInitializer->isPackExpansion())
615 Out << "...";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000616 }
Benjamin Kramer2907b082014-02-25 18:49:49 +0000617 } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
Richard Smithe6560762013-02-22 05:54:51 +0000618 if (FT && FT->hasTrailingReturn()) {
619 Out << "auto " << Proto << " -> ";
620 Proto.clear();
621 }
Alp Toker314cc812014-01-25 16:55:45 +0000622 AFT->getReturnType().print(Out, Policy, Proto);
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000623 Proto.clear();
Richard Smithe6560762013-02-22 05:54:51 +0000624 }
Benjamin Kramer00e8a192014-02-25 18:03:55 +0000625 Out << Proto;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000626 } else {
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000627 Ty.print(Out, Policy, Proto);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000628 }
629
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000630 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000631
632 if (D->isPure())
633 Out << " = 0";
Alexis Hunt4a8ea102011-05-06 20:44:56 +0000634 else if (D->isDeletedAsWritten())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000635 Out << " = delete";
Fariborz Jahaniande872af2012-12-05 22:53:06 +0000636 else if (D->isExplicitlyDefaulted())
637 Out << " = default";
Dmitri Gribenkoa9a2af72012-08-21 17:47:24 +0000638 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000639 if (!D->hasPrototype() && D->getNumParams()) {
640 // This is a K&R function definition, so we need to print the
641 // parameters.
642 Out << '\n';
Richard Smith235341b2012-08-16 03:56:14 +0000643 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000644 Indentation += Policy.Indentation;
645 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
646 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000647 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000648 Out << ";\n";
649 }
650 Indentation -= Policy.Indentation;
651 } else
652 Out << ' ';
653
Richard Trieuddd01ce2014-06-09 22:53:25 +0000654 if (D->getBody())
655 D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000656 Out << '\n';
657 }
658}
659
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000660void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
661 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000662 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
663 for (unsigned i = 0; i < NumTPLists; ++i)
664 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000665 Out << "friend ";
666 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000667 }
668 else if (FunctionDecl *FD =
669 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
670 Out << "friend ";
671 VisitFunctionDecl(FD);
672 }
673 else if (FunctionTemplateDecl *FTD =
674 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
675 Out << "friend ";
676 VisitFunctionTemplateDecl(FTD);
677 }
678 else if (ClassTemplateDecl *CTD =
679 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
680 Out << "friend ";
Fariborz Jahanian7a16a022012-12-21 21:43:05 +0000681 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanianbfc3ef52012-12-05 00:38:44 +0000682 }
683}
684
Douglas Gregor278f52e2009-05-30 00:08:05 +0000685void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000686 // FIXME: add printing of pragma attributes if required.
Eli Friedman79635842009-05-30 04:20:30 +0000687 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000688 Out << "mutable ";
Douglas Gregor26701a42011-09-09 02:06:17 +0000689 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
690 Out << "__module_private__ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000691
Fariborz Jahanianb5f34682013-05-01 20:53:21 +0000692 Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000693 stream(Policy, D->getName(), Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000694
695 if (D->isBitField()) {
696 Out << " : ";
Craig Topper36250ad2014-05-12 05:36:57 +0000697 D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000698 }
Richard Smith938f40b2011-06-11 17:19:42 +0000699
700 Expr *Init = D->getInClassInitializer();
701 if (!Policy.SuppressInitializers && Init) {
Richard Smith2b013182012-06-10 03:12:00 +0000702 if (D->getInClassInitStyle() == ICIS_ListInit)
703 Out << " ";
704 else
705 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000706 Init->printPretty(Out, nullptr, Policy, Indentation);
Richard Smith938f40b2011-06-11 17:19:42 +0000707 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000708 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000709}
710
Chris Lattnercab02a62011-02-17 20:34:02 +0000711void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000712 Out << *D << ":";
Chris Lattnercab02a62011-02-17 20:34:02 +0000713}
714
Douglas Gregor278f52e2009-05-30 00:08:05 +0000715void DeclPrinter::VisitVarDecl(VarDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000716 prettyPrintPragmas(D);
Vassil Vassilev10023732016-07-08 21:09:08 +0000717
718 QualType T = D->getTypeSourceInfo()
719 ? D->getTypeSourceInfo()->getType()
720 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
721
Richard Smithfd3834f2013-04-13 02:43:54 +0000722 if (!Policy.SuppressSpecifiers) {
723 StorageClass SC = D->getStorageClass();
724 if (SC != SC_None)
725 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000726
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000727 switch (D->getTSCSpec()) {
728 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000729 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000730 case TSCS___thread:
731 Out << "__thread ";
732 break;
733 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000734 Out << "_Thread_local ";
735 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000736 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000737 Out << "thread_local ";
738 break;
739 }
740
741 if (D->isModulePrivate())
742 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000743
744 if (D->isConstexpr()) {
745 Out << "constexpr ";
746 T.removeLocalConst();
747 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000748 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000749
Richard Smitha4bb2922014-07-23 03:17:06 +0000750 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000751 Expr *Init = D->getInit();
752 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000753 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000754 if (CXXConstructExpr *Construct =
755 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000756 if (D->getInitStyle() == VarDecl::CallInit &&
757 !Construct->isListInitialization()) {
758 ImplicitInit = Construct->getNumArgs() == 0 ||
759 Construct->getArg(0)->isDefaultArgument();
760 }
761 }
Sebastian Redla9351792012-02-11 23:51:47 +0000762 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000763 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000764 Out << "(";
765 else if (D->getInitStyle() == VarDecl::CInit) {
766 Out << " = ";
767 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000768 PrintingPolicy SubPolicy(Policy);
769 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000770 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000771 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000772 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000773 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000774 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000775 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000776 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000777}
778
779void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
780 VisitVarDecl(D);
781}
782
783void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
784 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000785 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000786 Out << ")";
787}
788
Douglas Gregorba345522011-12-02 23:23:56 +0000789void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000790 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000791 << ";\n";
792}
793
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000794void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
795 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000796 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000797 if (StringLiteral *SL = D->getMessage()) {
798 Out << ", ";
799 SL->printPretty(Out, nullptr, Policy, Indentation);
800 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000801 Out << ")";
802}
803
Douglas Gregor278f52e2009-05-30 00:08:05 +0000804//----------------------------------------------------------------------------
805// C++ declarations
806//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000807void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000808 if (D->isInline())
809 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000810 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000811 VisitDeclContext(D);
812 Indent() << "}";
813}
814
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000815void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
816 Out << "using namespace ";
817 if (D->getQualifier())
818 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000819 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000820}
821
Douglas Gregor18231932009-05-30 06:48:27 +0000822void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000823 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000824 if (D->getQualifier())
825 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000826 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000827}
828
Michael Han84324352013-02-22 17:15:32 +0000829void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
830 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000831}
832
Douglas Gregor5f478b72009-05-30 06:58:37 +0000833void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000834 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000835 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
836 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000837 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000838
839 prettyPrintAttributes(D);
840
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000841 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000842 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000843
John McCallf937c022011-10-07 06:10:15 +0000844 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000845 // Print the base classes
846 if (D->getNumBases()) {
847 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000848 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
849 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000850 if (Base != D->bases_begin())
851 Out << ", ";
852
853 if (Base->isVirtual())
854 Out << "virtual ";
855
Anders Carlsson6df9e072009-08-29 20:36:12 +0000856 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000857 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000858 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000859 Out << " ";
860 }
861 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000862
863 if (Base->isPackExpansion())
864 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000865 }
866 }
867
868 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000869 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000870 Out << " {\n";
871 VisitDeclContext(D);
872 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000873 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000874}
875
876void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
877 const char *l;
878 if (D->getLanguage() == LinkageSpecDecl::lang_c)
879 l = "C";
880 else {
881 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
882 "unknown language in linkage specification");
883 l = "C++";
884 }
885
886 Out << "extern \"" << l << "\" ";
887 if (D->hasBraces()) {
888 Out << "{\n";
889 VisitDeclContext(D);
890 Indent() << "}";
891 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000892 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000893}
894
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000895void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
896 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000897 assert(Params);
898 assert(!Args || Params->size() == Args->size());
899
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000900 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000901
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000902 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
903 if (i != 0)
904 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000905
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000906 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000907 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000908 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000909
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000910 if (TTP->wasDeclaredWithTypename())
911 Out << "typename ";
912 else
913 Out << "class ";
914
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000915 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000916 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000917
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000918 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000919
Richard Trieu82398f92011-07-28 00:19:05 +0000920 if (Args) {
921 Out << " = ";
922 Args->get(i).print(Policy, Out);
923 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000924 Out << " = ";
925 Out << TTP->getDefaultArgument().getAsString(Policy);
926 };
Mike Stump11289f42009-09-09 15:08:12 +0000927 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000928 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000929 StringRef Name;
930 if (IdentifierInfo *II = NTTP->getIdentifier())
931 Name = II->getName();
932 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000933
Richard Trieu82398f92011-07-28 00:19:05 +0000934 if (Args) {
935 Out << " = ";
936 Args->get(i).print(Policy, Out);
937 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000938 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000939 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
940 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000941 }
Richard Smith030f4992011-04-15 13:38:57 +0000942 } else if (const TemplateTemplateParmDecl *TTPD =
943 dyn_cast<TemplateTemplateParmDecl>(Param)) {
944 VisitTemplateDecl(TTPD);
945 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000946 }
947 }
Mike Stump11289f42009-09-09 15:08:12 +0000948
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000949 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000950}
951
952void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
953 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000954
Richard Smith030f4992011-04-15 13:38:57 +0000955 if (const TemplateTemplateParmDecl *TTP =
956 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000957 Out << "class ";
958 if (TTP->isParameterPack())
959 Out << "...";
960 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000961 } else {
962 Visit(D->getTemplatedDecl());
963 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000964}
965
Richard Trieu82398f92011-07-28 00:19:05 +0000966void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
967 if (PrintInstantiation) {
968 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000969 for (auto *I : D->specializations()) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000970 prettyPrintPragmas(I);
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000971 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
972 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000973 }
974 }
975
Alexey Bataev6d455322015-10-12 06:59:48 +0000976 prettyPrintPragmas(D->getTemplatedDecl());
Richard Trieu82398f92011-07-28 00:19:05 +0000977 return VisitRedeclarableTemplateDecl(D);
978}
979
980void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
981 if (PrintInstantiation) {
982 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000983 for (auto *I : D->specializations()) {
984 PrintTemplateParameters(Params, &I->getTemplateArgs());
985 Visit(I);
Serge Pavlovdc586c42016-10-31 05:11:12 +0000986 Out << ";\n";
Richard Trieu82398f92011-07-28 00:19:05 +0000987 }
988 }
989
990 return VisitRedeclarableTemplateDecl(D);
991}
992
Douglas Gregor278f52e2009-05-30 00:08:05 +0000993//----------------------------------------------------------------------------
994// Objective-C declarations
995//----------------------------------------------------------------------------
996
Douglas Gregor813a0662015-06-19 18:14:38 +0000997void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
998 Decl::ObjCDeclQualifier Quals,
999 QualType T) {
1000 Out << '(';
1001 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1002 Out << "in ";
1003 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1004 Out << "inout ";
1005 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1006 Out << "out ";
1007 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1008 Out << "bycopy ";
1009 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1010 Out << "byref ";
1011 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1012 Out << "oneway ";
1013 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001014 if (auto nullability = AttributedType::stripOuterNullability(T))
1015 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001016 }
1017
1018 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1019 Out << ')';
1020}
1021
Douglas Gregor85f3f952015-07-07 03:57:15 +00001022void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1023 Out << "<";
1024 unsigned First = true;
1025 for (auto *Param : *Params) {
1026 if (First) {
1027 First = false;
1028 } else {
1029 Out << ", ";
1030 }
1031
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001032 switch (Param->getVariance()) {
1033 case ObjCTypeParamVariance::Invariant:
1034 break;
1035
1036 case ObjCTypeParamVariance::Covariant:
1037 Out << "__covariant ";
1038 break;
1039
1040 case ObjCTypeParamVariance::Contravariant:
1041 Out << "__contravariant ";
1042 break;
1043 }
1044
Douglas Gregor85f3f952015-07-07 03:57:15 +00001045 Out << Param->getDeclName().getAsString();
1046
1047 if (Param->hasExplicitBound()) {
1048 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1049 }
1050 }
1051 Out << ">";
1052}
1053
Douglas Gregor278f52e2009-05-30 00:08:05 +00001054void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1055 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001056 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001057 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001058 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001059 if (!OMD->getReturnType().isNull()) {
1060 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1061 OMD->getReturnType());
1062 }
Mike Stump11289f42009-09-09 15:08:12 +00001063
Douglas Gregor278f52e2009-05-30 00:08:05 +00001064 std::string name = OMD->getSelector().getAsString();
1065 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001066 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001067 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001068 pos = name.find_first_of(':', lastPos);
Douglas Gregor813a0662015-06-19 18:14:38 +00001069 Out << " " << name.substr(lastPos, pos - lastPos) << ':';
1070 PrintObjCMethodType(OMD->getASTContext(),
1071 PI->getObjCDeclQualifier(),
1072 PI->getType());
1073 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001074 lastPos = pos + 1;
1075 }
Mike Stump11289f42009-09-09 15:08:12 +00001076
Douglas Gregor278f52e2009-05-30 00:08:05 +00001077 if (OMD->param_begin() == OMD->param_end())
1078 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +00001079
Douglas Gregor278f52e2009-05-30 00:08:05 +00001080 if (OMD->isVariadic())
1081 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001082
1083 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001084
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001085 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001086 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001087 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001088 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001089 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001090 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001091}
1092
1093void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1094 std::string I = OID->getNameAsString();
1095 ObjCInterfaceDecl *SID = OID->getSuperClass();
1096
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001097 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001098 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001099 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001100 else
1101 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001102
1103 if (OID->ivar_size() > 0) {
1104 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001105 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001106 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001107 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001108 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001109 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001110 }
1111 Indentation -= Policy.Indentation;
1112 Out << "}\n";
1113 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001114 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1115 Out << "\n";
1116 eolnOut = true;
1117 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001118 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001119 if (!eolnOut)
1120 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001121 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001122}
1123
1124void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1125 std::string I = OID->getNameAsString();
1126 ObjCInterfaceDecl *SID = OID->getSuperClass();
1127
Douglas Gregordc9166c2011-12-15 20:29:51 +00001128 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001129 Out << "@class " << I;
1130
1131 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1132 PrintObjCTypeParams(TypeParams);
1133 }
1134
1135 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001136 return;
1137 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001138 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001139 Out << "@interface " << I;
1140
1141 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1142 PrintObjCTypeParams(TypeParams);
1143 }
1144
Douglas Gregor278f52e2009-05-30 00:08:05 +00001145 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001146 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001147
Douglas Gregor278f52e2009-05-30 00:08:05 +00001148 // Protocols?
1149 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1150 if (!Protocols.empty()) {
1151 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1152 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001153 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001154 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001155 }
Mike Stump11289f42009-09-09 15:08:12 +00001156
Douglas Gregor278f52e2009-05-30 00:08:05 +00001157 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001158 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001159 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001160 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001161 for (const auto *I : OID->ivars()) {
1162 Indent() << I->getASTContext()
1163 .getUnqualifiedObjCPointerType(I->getType())
1164 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001165 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001166 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001167 Out << "}\n";
1168 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001169 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001170 Out << "\n";
1171 eolnOut = true;
1172 }
Mike Stump11289f42009-09-09 15:08:12 +00001173
Douglas Gregor278f52e2009-05-30 00:08:05 +00001174 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001175 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001176 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001177 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001178 // FIXME: implement the rest...
1179}
1180
Douglas Gregor278f52e2009-05-30 00:08:05 +00001181void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001182 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001183 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001184 return;
1185 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001186 // Protocols?
1187 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1188 if (!Protocols.empty()) {
1189 Out << "@protocol " << *PID;
1190 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1191 E = Protocols.end(); I != E; ++I)
1192 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1193 Out << ">\n";
1194 } else
1195 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001196 VisitDeclContext(PID, false);
1197 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001198}
1199
1200void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001201 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001202
1203 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001204 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001205 // FIXME: implement the rest...
1206}
1207
1208void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001209 Out << "@interface " << *PID->getClassInterface();
1210 if (auto TypeParams = PID->getTypeParamList()) {
1211 PrintObjCTypeParams(TypeParams);
1212 }
1213 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001214 if (PID->ivar_size() > 0) {
1215 Out << "{\n";
1216 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001217 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001218 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001219 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001220 Indentation -= Policy.Indentation;
1221 Out << "}\n";
1222 }
1223
Douglas Gregor278f52e2009-05-30 00:08:05 +00001224 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001225 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001226
Douglas Gregor278f52e2009-05-30 00:08:05 +00001227 // FIXME: implement the rest...
1228}
1229
1230void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001231 Out << "@compatibility_alias " << *AID
1232 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001233}
1234
1235/// PrintObjCPropertyDecl - print a property declaration.
1236///
1237void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1238 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1239 Out << "@required\n";
1240 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1241 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001242
Douglas Gregor813a0662015-06-19 18:14:38 +00001243 QualType T = PDecl->getType();
1244
Douglas Gregor278f52e2009-05-30 00:08:05 +00001245 Out << "@property";
1246 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1247 bool first = true;
1248 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001249 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001250 ObjCPropertyDecl::OBJC_PR_readonly) {
1251 Out << (first ? ' ' : ',') << "readonly";
1252 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001253 }
Mike Stump11289f42009-09-09 15:08:12 +00001254
Ted Kremenek897af912011-08-17 21:09:35 +00001255 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001256 Out << (first ? ' ' : ',') << "getter = ";
1257 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001258 first = false;
1259 }
1260 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001261 Out << (first ? ' ' : ',') << "setter = ";
1262 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001263 first = false;
1264 }
Mike Stump11289f42009-09-09 15:08:12 +00001265
Ted Kremenek897af912011-08-17 21:09:35 +00001266 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1267 Out << (first ? ' ' : ',') << "assign";
1268 first = false;
1269 }
Mike Stump11289f42009-09-09 15:08:12 +00001270
Ted Kremenek897af912011-08-17 21:09:35 +00001271 if (PDecl->getPropertyAttributes() &
1272 ObjCPropertyDecl::OBJC_PR_readwrite) {
1273 Out << (first ? ' ' : ',') << "readwrite";
1274 first = false;
1275 }
Mike Stump11289f42009-09-09 15:08:12 +00001276
Ted Kremenek897af912011-08-17 21:09:35 +00001277 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1278 Out << (first ? ' ' : ',') << "retain";
1279 first = false;
1280 }
Mike Stump11289f42009-09-09 15:08:12 +00001281
Ted Kremenek897af912011-08-17 21:09:35 +00001282 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1283 Out << (first ? ' ' : ',') << "strong";
1284 first = false;
1285 }
John McCall31168b02011-06-15 23:02:42 +00001286
Ted Kremenek897af912011-08-17 21:09:35 +00001287 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1288 Out << (first ? ' ' : ',') << "copy";
1289 first = false;
1290 }
Mike Stump11289f42009-09-09 15:08:12 +00001291
Ted Kremenek897af912011-08-17 21:09:35 +00001292 if (PDecl->getPropertyAttributes() &
1293 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1294 Out << (first ? ' ' : ',') << "nonatomic";
1295 first = false;
1296 }
1297 if (PDecl->getPropertyAttributes() &
1298 ObjCPropertyDecl::OBJC_PR_atomic) {
1299 Out << (first ? ' ' : ',') << "atomic";
1300 first = false;
1301 }
1302
Douglas Gregor813a0662015-06-19 18:14:38 +00001303 if (PDecl->getPropertyAttributes() &
1304 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001305 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001306 if (*nullability == NullabilityKind::Unspecified &&
1307 (PDecl->getPropertyAttributes() &
1308 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1309 Out << (first ? ' ' : ',') << "null_resettable";
1310 } else {
1311 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001312 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001313 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001314 first = false;
1315 }
1316 }
1317
Manman Ren387ff7f2016-01-26 18:52:43 +00001318 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1319 Out << (first ? ' ' : ',') << "class";
1320 first = false;
1321 }
1322
Ted Kremenek897af912011-08-17 21:09:35 +00001323 (void) first; // Silence dead store warning due to idiomatic code.
1324 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001325 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001326 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001327 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001328 if (Policy.PolishForDeclaration)
1329 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001330}
1331
1332void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1333 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001334 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001335 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001336 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001337 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001338 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001339 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001340}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001341
1342void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001343 if (!D->isAccessDeclaration())
1344 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001345 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001346 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001347 D->getQualifier()->print(Out, Policy);
Alex Lorenzea9b59a2016-10-03 12:22:17 +00001348
1349 // Use the correct record name when the using declaration is used for
1350 // inheriting constructors.
1351 for (const auto *Shadow : D->shadows()) {
1352 if (const auto *ConstructorShadow =
1353 dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1354 assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1355 Out << *ConstructorShadow->getNominatedBaseClass();
1356 return;
1357 }
1358 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001359 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001360}
1361
John McCalle61f2ba2009-11-18 02:36:19 +00001362void
1363DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1364 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001365 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001366 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001367}
1368
1369void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001370 if (!D->isAccessDeclaration())
1371 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001372 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001373 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001374}
John McCall3f746822009-11-17 05:59:44 +00001375
1376void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1377 // ignore
1378}
Alexey Bataeva769e072013-03-22 06:34:35 +00001379
1380void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1381 Out << "#pragma omp threadprivate";
1382 if (!D->varlist_empty()) {
1383 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1384 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001385 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001386 Out << (I == D->varlist_begin() ? '(' : ',');
1387 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1388 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001389 }
1390 Out << ")";
1391 }
1392}
1393
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001394void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1395 if (!D->isInvalidDecl()) {
1396 Out << "#pragma omp declare reduction (";
1397 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1398 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1399 nullptr,
1400#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1401 Spelling,
1402#include "clang/Basic/OperatorKinds.def"
1403 };
1404 const char *OpName =
1405 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1406 assert(OpName && "not an overloaded operator");
1407 Out << OpName;
1408 } else {
1409 assert(D->getDeclName().isIdentifier());
1410 D->printName(Out);
1411 }
1412 Out << " : ";
1413 D->getType().print(Out, Policy);
1414 Out << " : ";
1415 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1416 Out << ")";
1417 if (auto *Init = D->getInitializer()) {
1418 Out << " initializer(";
1419 Init->printPretty(Out, nullptr, Policy, 0);
1420 Out << ")";
1421 }
1422 }
1423}
1424
Alexey Bataev4244be22016-02-11 05:35:55 +00001425void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001426 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1427}
1428