blob: 7e786990becb1724a037488c8dbd69a650e3256d [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);
Vassil Vassilev10023732016-07-08 21:09:08 +0000718
719 QualType T = D->getTypeSourceInfo()
720 ? D->getTypeSourceInfo()->getType()
721 : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
722
Richard Smithfd3834f2013-04-13 02:43:54 +0000723 if (!Policy.SuppressSpecifiers) {
724 StorageClass SC = D->getStorageClass();
725 if (SC != SC_None)
726 Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000727
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000728 switch (D->getTSCSpec()) {
729 case TSCS_unspecified:
Richard Smithfd3834f2013-04-13 02:43:54 +0000730 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000731 case TSCS___thread:
732 Out << "__thread ";
733 break;
734 case TSCS__Thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000735 Out << "_Thread_local ";
736 break;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000737 case TSCS_thread_local:
Richard Smithfd3834f2013-04-13 02:43:54 +0000738 Out << "thread_local ";
739 break;
740 }
741
742 if (D->isModulePrivate())
743 Out << "__module_private__ ";
Vassil Vassilev10023732016-07-08 21:09:08 +0000744
745 if (D->isConstexpr()) {
746 Out << "constexpr ";
747 T.removeLocalConst();
748 }
Richard Smithfd3834f2013-04-13 02:43:54 +0000749 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000750
Richard Smitha4bb2922014-07-23 03:17:06 +0000751 printDeclType(T, D->getName());
Richard Smith02e85f32011-04-14 22:09:26 +0000752 Expr *Init = D->getInit();
753 if (!Policy.SuppressInitializers && Init) {
Sebastian Redla9351792012-02-11 23:51:47 +0000754 bool ImplicitInit = false;
Dmitri Gribenkob614fab2013-02-03 23:02:47 +0000755 if (CXXConstructExpr *Construct =
756 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko6835e372013-01-27 21:28:24 +0000757 if (D->getInitStyle() == VarDecl::CallInit &&
758 !Construct->isListInitialization()) {
759 ImplicitInit = Construct->getNumArgs() == 0 ||
760 Construct->getArg(0)->isDefaultArgument();
761 }
762 }
Sebastian Redla9351792012-02-11 23:51:47 +0000763 if (!ImplicitInit) {
Eli Friedman92125c42012-10-19 20:36:44 +0000764 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000765 Out << "(";
766 else if (D->getInitStyle() == VarDecl::CInit) {
767 Out << " = ";
768 }
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000769 PrintingPolicy SubPolicy(Policy);
770 SubPolicy.SuppressSpecifiers = false;
Steven Watanabe9359b8f2016-03-18 21:35:59 +0000771 SubPolicy.IncludeTagDefinition = false;
Benjamin Kramer54f81ed2016-01-25 10:34:06 +0000772 Init->printPretty(Out, nullptr, SubPolicy, Indentation);
Eli Friedman92125c42012-10-19 20:36:44 +0000773 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redla9351792012-02-11 23:51:47 +0000774 Out << ")";
Ted Kremenek35869382010-09-17 23:04:38 +0000775 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000776 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000777 prettyPrintAttributes(D);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000778}
779
780void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
781 VisitVarDecl(D);
782}
783
784void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
785 Out << "__asm (";
Craig Topper36250ad2014-05-12 05:36:57 +0000786 D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000787 Out << ")";
788}
789
Douglas Gregorba345522011-12-02 23:23:56 +0000790void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregorc50d4922012-12-11 22:11:52 +0000791 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregorba345522011-12-02 23:23:56 +0000792 << ";\n";
793}
794
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000795void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
796 Out << "static_assert(";
Craig Topper36250ad2014-05-12 05:36:57 +0000797 D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
David Majnemercdffc362015-06-05 18:03:58 +0000798 if (StringLiteral *SL = D->getMessage()) {
799 Out << ", ";
800 SL->printPretty(Out, nullptr, Policy, Indentation);
801 }
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000802 Out << ")";
803}
804
Douglas Gregor278f52e2009-05-30 00:08:05 +0000805//----------------------------------------------------------------------------
806// C++ declarations
807//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000808void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorb5263702012-05-01 01:43:38 +0000809 if (D->isInline())
810 Out << "inline ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000811 Out << "namespace " << *D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000812 VisitDeclContext(D);
813 Indent() << "}";
814}
815
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000816void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
817 Out << "using namespace ";
818 if (D->getQualifier())
819 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000820 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000821}
822
Douglas Gregor18231932009-05-30 06:48:27 +0000823void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000824 Out << "namespace " << *D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000825 if (D->getQualifier())
826 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000827 Out << *D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000828}
829
Michael Han84324352013-02-22 17:15:32 +0000830void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
831 prettyPrintAttributes(D);
Michael Han84324352013-02-22 17:15:32 +0000832}
833
Douglas Gregor5f478b72009-05-30 06:58:37 +0000834void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000835 // FIXME: add printing of pragma attributes if required.
Douglas Gregor26701a42011-09-09 02:06:17 +0000836 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
837 Out << "__module_private__ ";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000838 Out << D->getKindName();
Aaron Ballman53885382014-09-15 16:45:30 +0000839
840 prettyPrintAttributes(D);
841
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000842 if (D->getIdentifier())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000843 Out << ' ' << *D;
Mike Stump11289f42009-09-09 15:08:12 +0000844
John McCallf937c022011-10-07 06:10:15 +0000845 if (D->isCompleteDefinition()) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000846 // Print the base classes
847 if (D->getNumBases()) {
848 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000849 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
850 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000851 if (Base != D->bases_begin())
852 Out << ", ";
853
854 if (Base->isVirtual())
855 Out << "virtual ";
856
Anders Carlsson6df9e072009-08-29 20:36:12 +0000857 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
Richard Smitha5934192014-07-23 03:22:10 +0000858 if (AS != AS_none) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000859 Print(AS);
Richard Smitha5934192014-07-23 03:22:10 +0000860 Out << " ";
861 }
862 Out << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000863
864 if (Base->isPackExpansion())
865 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000866 }
867 }
868
869 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000870 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000871 Out << " {\n";
872 VisitDeclContext(D);
873 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000874 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000875}
876
877void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
878 const char *l;
879 if (D->getLanguage() == LinkageSpecDecl::lang_c)
880 l = "C";
881 else {
882 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
883 "unknown language in linkage specification");
884 l = "C++";
885 }
886
887 Out << "extern \"" << l << "\" ";
888 if (D->hasBraces()) {
889 Out << "{\n";
890 VisitDeclContext(D);
891 Indent() << "}";
892 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000893 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000894}
895
Enea Zaffanellaeb22c872013-01-31 09:54:08 +0000896void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
897 const TemplateArgumentList *Args) {
Richard Trieu82398f92011-07-28 00:19:05 +0000898 assert(Params);
899 assert(!Args || Params->size() == Args->size());
900
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000901 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000902
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000903 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
904 if (i != 0)
905 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000906
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000907 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000908 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000909 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000910
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000911 if (TTP->wasDeclaredWithTypename())
912 Out << "typename ";
913 else
914 Out << "class ";
915
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000916 if (TTP->isParameterPack())
Richard Smitha4bb2922014-07-23 03:17:06 +0000917 Out << "...";
Mike Stump11289f42009-09-09 15:08:12 +0000918
Benjamin Kramerdb0fc512012-02-07 11:57:57 +0000919 Out << *TTP;
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000920
Richard Trieu82398f92011-07-28 00:19:05 +0000921 if (Args) {
922 Out << " = ";
923 Args->get(i).print(Policy, Out);
924 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000925 Out << " = ";
926 Out << TTP->getDefaultArgument().getAsString(Policy);
927 };
Mike Stump11289f42009-09-09 15:08:12 +0000928 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000929 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Richard Smitha4bb2922014-07-23 03:17:06 +0000930 StringRef Name;
931 if (IdentifierInfo *II = NTTP->getIdentifier())
932 Name = II->getName();
933 printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
Mike Stump11289f42009-09-09 15:08:12 +0000934
Richard Trieu82398f92011-07-28 00:19:05 +0000935 if (Args) {
936 Out << " = ";
937 Args->get(i).print(Policy, Out);
938 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000939 Out << " = ";
Craig Topper36250ad2014-05-12 05:36:57 +0000940 NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
941 Indentation);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000942 }
Richard Smith030f4992011-04-15 13:38:57 +0000943 } else if (const TemplateTemplateParmDecl *TTPD =
944 dyn_cast<TemplateTemplateParmDecl>(Param)) {
945 VisitTemplateDecl(TTPD);
946 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000947 }
948 }
Mike Stump11289f42009-09-09 15:08:12 +0000949
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000950 Out << "> ";
Richard Trieu82398f92011-07-28 00:19:05 +0000951}
952
953void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
954 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000955
Richard Smith030f4992011-04-15 13:38:57 +0000956 if (const TemplateTemplateParmDecl *TTP =
957 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000958 Out << "class ";
959 if (TTP->isParameterPack())
960 Out << "...";
961 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000962 } else {
963 Visit(D->getTemplatedDecl());
964 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000965}
966
Richard Trieu82398f92011-07-28 00:19:05 +0000967void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
968 if (PrintInstantiation) {
969 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000970 for (auto *I : D->specializations()) {
Alexey Bataev6d455322015-10-12 06:59:48 +0000971 prettyPrintPragmas(I);
Aaron Ballmanb8733c52014-03-14 16:05:56 +0000972 PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
973 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000974 }
975 }
976
Alexey Bataev6d455322015-10-12 06:59:48 +0000977 prettyPrintPragmas(D->getTemplatedDecl());
Richard Trieu82398f92011-07-28 00:19:05 +0000978 return VisitRedeclarableTemplateDecl(D);
979}
980
981void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
982 if (PrintInstantiation) {
983 TemplateParameterList *Params = D->getTemplateParameters();
Aaron Ballman702fa312014-03-14 16:13:33 +0000984 for (auto *I : D->specializations()) {
985 PrintTemplateParameters(Params, &I->getTemplateArgs());
986 Visit(I);
Richard Trieu82398f92011-07-28 00:19:05 +0000987 Out << '\n';
988 }
989 }
990
991 return VisitRedeclarableTemplateDecl(D);
992}
993
Douglas Gregor278f52e2009-05-30 00:08:05 +0000994//----------------------------------------------------------------------------
995// Objective-C declarations
996//----------------------------------------------------------------------------
997
Douglas Gregor813a0662015-06-19 18:14:38 +0000998void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
999 Decl::ObjCDeclQualifier Quals,
1000 QualType T) {
1001 Out << '(';
1002 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1003 Out << "in ";
1004 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1005 Out << "inout ";
1006 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1007 Out << "out ";
1008 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1009 Out << "bycopy ";
1010 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1011 Out << "byref ";
1012 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1013 Out << "oneway ";
1014 if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001015 if (auto nullability = AttributedType::stripOuterNullability(T))
1016 Out << getNullabilitySpelling(*nullability, true) << ' ';
Douglas Gregor813a0662015-06-19 18:14:38 +00001017 }
1018
1019 Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1020 Out << ')';
1021}
1022
Douglas Gregor85f3f952015-07-07 03:57:15 +00001023void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1024 Out << "<";
1025 unsigned First = true;
1026 for (auto *Param : *Params) {
1027 if (First) {
1028 First = false;
1029 } else {
1030 Out << ", ";
1031 }
1032
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001033 switch (Param->getVariance()) {
1034 case ObjCTypeParamVariance::Invariant:
1035 break;
1036
1037 case ObjCTypeParamVariance::Covariant:
1038 Out << "__covariant ";
1039 break;
1040
1041 case ObjCTypeParamVariance::Contravariant:
1042 Out << "__contravariant ";
1043 break;
1044 }
1045
Douglas Gregor85f3f952015-07-07 03:57:15 +00001046 Out << Param->getDeclName().getAsString();
1047
1048 if (Param->hasExplicitBound()) {
1049 Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1050 }
1051 }
1052 Out << ">";
1053}
1054
Douglas Gregor278f52e2009-05-30 00:08:05 +00001055void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1056 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +00001057 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +00001058 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001059 Out << "+ ";
Douglas Gregor813a0662015-06-19 18:14:38 +00001060 if (!OMD->getReturnType().isNull()) {
1061 PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1062 OMD->getReturnType());
1063 }
Mike Stump11289f42009-09-09 15:08:12 +00001064
Douglas Gregor278f52e2009-05-30 00:08:05 +00001065 std::string name = OMD->getSelector().getAsString();
1066 std::string::size_type pos, lastPos = 0;
David Majnemer59f77922016-06-24 04:05:48 +00001067 for (const auto *PI : OMD->parameters()) {
Mike Stump11289f42009-09-09 15:08:12 +00001068 // FIXME: selector is missing here!
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001069 pos = name.find_first_of(':', lastPos);
Douglas Gregor813a0662015-06-19 18:14:38 +00001070 Out << " " << name.substr(lastPos, pos - lastPos) << ':';
1071 PrintObjCMethodType(OMD->getASTContext(),
1072 PI->getObjCDeclQualifier(),
1073 PI->getType());
1074 Out << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001075 lastPos = pos + 1;
1076 }
Mike Stump11289f42009-09-09 15:08:12 +00001077
Douglas Gregor278f52e2009-05-30 00:08:05 +00001078 if (OMD->param_begin() == OMD->param_end())
1079 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +00001080
Douglas Gregor278f52e2009-05-30 00:08:05 +00001081 if (OMD->isVariadic())
1082 Out << ", ...";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001083
1084 prettyPrintAttributes(OMD);
Mike Stump11289f42009-09-09 15:08:12 +00001085
Fariborz Jahaniana7d76d22012-10-17 21:58:03 +00001086 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor278f52e2009-05-30 00:08:05 +00001087 Out << ' ';
Craig Topper36250ad2014-05-12 05:36:57 +00001088 OMD->getBody()->printPretty(Out, nullptr, Policy);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001089 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001090 else if (Policy.PolishForDeclaration)
Fariborz Jahanian9b7ab872012-12-18 23:02:59 +00001091 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001092}
1093
1094void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1095 std::string I = OID->getNameAsString();
1096 ObjCInterfaceDecl *SID = OID->getSuperClass();
1097
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001098 bool eolnOut = false;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001099 if (SID)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001100 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001101 else
1102 Out << "@implementation " << I;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001103
1104 if (OID->ivar_size() > 0) {
1105 Out << "{\n";
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001106 eolnOut = true;
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001107 Indentation += Policy.Indentation;
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001108 for (const auto *I : OID->ivars()) {
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001109 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballmand6d25de2014-03-14 15:16:45 +00001110 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian95759ff2012-12-04 00:47:33 +00001111 }
1112 Indentation -= Policy.Indentation;
1113 Out << "}\n";
1114 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001115 else if (SID || (OID->decls_begin() != OID->decls_end())) {
1116 Out << "\n";
1117 eolnOut = true;
1118 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001119 VisitDeclContext(OID, false);
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001120 if (!eolnOut)
1121 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001122 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001123}
1124
1125void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1126 std::string I = OID->getNameAsString();
1127 ObjCInterfaceDecl *SID = OID->getSuperClass();
1128
Douglas Gregordc9166c2011-12-15 20:29:51 +00001129 if (!OID->isThisDeclarationADefinition()) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001130 Out << "@class " << I;
1131
1132 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1133 PrintObjCTypeParams(TypeParams);
1134 }
1135
1136 Out << ";";
Douglas Gregordc9166c2011-12-15 20:29:51 +00001137 return;
1138 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001139 bool eolnOut = false;
Douglas Gregor85f3f952015-07-07 03:57:15 +00001140 Out << "@interface " << I;
1141
1142 if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1143 PrintObjCTypeParams(TypeParams);
1144 }
1145
Douglas Gregor278f52e2009-05-30 00:08:05 +00001146 if (SID)
Bob Wilson1f24ea152015-10-01 00:53:13 +00001147 Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001148
Douglas Gregor278f52e2009-05-30 00:08:05 +00001149 // Protocols?
1150 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1151 if (!Protocols.empty()) {
1152 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1153 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001154 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001155 Out << "> ";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001156 }
Mike Stump11289f42009-09-09 15:08:12 +00001157
Douglas Gregor278f52e2009-05-30 00:08:05 +00001158 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001159 Out << "{\n";
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001160 eolnOut = true;
Douglas Gregor36098ff2009-05-30 00:56:08 +00001161 Indentation += Policy.Indentation;
Aaron Ballman59abbd42014-03-13 21:09:43 +00001162 for (const auto *I : OID->ivars()) {
1163 Indent() << I->getASTContext()
1164 .getUnqualifiedObjCPointerType(I->getType())
1165 .getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001166 }
Douglas Gregor36098ff2009-05-30 00:56:08 +00001167 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +00001168 Out << "}\n";
1169 }
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001170 else if (SID || (OID->decls_begin() != OID->decls_end())) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001171 Out << "\n";
1172 eolnOut = true;
1173 }
Mike Stump11289f42009-09-09 15:08:12 +00001174
Douglas Gregor278f52e2009-05-30 00:08:05 +00001175 VisitDeclContext(OID, false);
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001176 if (!eolnOut)
Fariborz Jahanianaae7fef2014-10-03 20:05:33 +00001177 Out << "\n";
Douglas Gregor36098ff2009-05-30 00:56:08 +00001178 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001179 // FIXME: implement the rest...
1180}
1181
Douglas Gregor278f52e2009-05-30 00:08:05 +00001182void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorf6102672012-01-01 21:23:57 +00001183 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001184 Out << "@protocol " << *PID << ";\n";
Douglas Gregorf6102672012-01-01 21:23:57 +00001185 return;
1186 }
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001187 // Protocols?
1188 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1189 if (!Protocols.empty()) {
1190 Out << "@protocol " << *PID;
1191 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1192 E = Protocols.end(); I != E; ++I)
1193 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1194 Out << ">\n";
1195 } else
1196 Out << "@protocol " << *PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +00001197 VisitDeclContext(PID, false);
1198 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001199}
1200
1201void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001202 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001203
1204 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001205 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001206 // FIXME: implement the rest...
1207}
1208
1209void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00001210 Out << "@interface " << *PID->getClassInterface();
1211 if (auto TypeParams = PID->getTypeParamList()) {
1212 PrintObjCTypeParams(TypeParams);
1213 }
1214 Out << "(" << *PID << ")\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001215 if (PID->ivar_size() > 0) {
1216 Out << "{\n";
1217 Indentation += Policy.Indentation;
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001218 for (const auto *I : PID->ivars())
Fariborz Jahanianb5f34682013-05-01 20:53:21 +00001219 Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
Aaron Ballman865fbcd2014-03-14 13:13:27 +00001220 getAsString(Policy) << ' ' << *I << ";\n";
Fariborz Jahanian4cf177e2012-12-04 17:20:57 +00001221 Indentation -= Policy.Indentation;
1222 Out << "}\n";
1223 }
1224
Douglas Gregor278f52e2009-05-30 00:08:05 +00001225 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +00001226 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +00001227
Douglas Gregor278f52e2009-05-30 00:08:05 +00001228 // FIXME: implement the rest...
1229}
1230
1231void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001232 Out << "@compatibility_alias " << *AID
1233 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001234}
1235
1236/// PrintObjCPropertyDecl - print a property declaration.
1237///
1238void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1239 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1240 Out << "@required\n";
1241 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1242 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +00001243
Douglas Gregor813a0662015-06-19 18:14:38 +00001244 QualType T = PDecl->getType();
1245
Douglas Gregor278f52e2009-05-30 00:08:05 +00001246 Out << "@property";
1247 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1248 bool first = true;
1249 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +00001250 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +00001251 ObjCPropertyDecl::OBJC_PR_readonly) {
1252 Out << (first ? ' ' : ',') << "readonly";
1253 first = false;
Ted Kremenek897af912011-08-17 21:09:35 +00001254 }
Mike Stump11289f42009-09-09 15:08:12 +00001255
Ted Kremenek897af912011-08-17 21:09:35 +00001256 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001257 Out << (first ? ' ' : ',') << "getter = ";
1258 PDecl->getGetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001259 first = false;
1260 }
1261 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001262 Out << (first ? ' ' : ',') << "setter = ";
1263 PDecl->getSetterName().print(Out);
Ted Kremenek897af912011-08-17 21:09:35 +00001264 first = false;
1265 }
Mike Stump11289f42009-09-09 15:08:12 +00001266
Ted Kremenek897af912011-08-17 21:09:35 +00001267 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1268 Out << (first ? ' ' : ',') << "assign";
1269 first = false;
1270 }
Mike Stump11289f42009-09-09 15:08:12 +00001271
Ted Kremenek897af912011-08-17 21:09:35 +00001272 if (PDecl->getPropertyAttributes() &
1273 ObjCPropertyDecl::OBJC_PR_readwrite) {
1274 Out << (first ? ' ' : ',') << "readwrite";
1275 first = false;
1276 }
Mike Stump11289f42009-09-09 15:08:12 +00001277
Ted Kremenek897af912011-08-17 21:09:35 +00001278 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1279 Out << (first ? ' ' : ',') << "retain";
1280 first = false;
1281 }
Mike Stump11289f42009-09-09 15:08:12 +00001282
Ted Kremenek897af912011-08-17 21:09:35 +00001283 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1284 Out << (first ? ' ' : ',') << "strong";
1285 first = false;
1286 }
John McCall31168b02011-06-15 23:02:42 +00001287
Ted Kremenek897af912011-08-17 21:09:35 +00001288 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1289 Out << (first ? ' ' : ',') << "copy";
1290 first = false;
1291 }
Mike Stump11289f42009-09-09 15:08:12 +00001292
Ted Kremenek897af912011-08-17 21:09:35 +00001293 if (PDecl->getPropertyAttributes() &
1294 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1295 Out << (first ? ' ' : ',') << "nonatomic";
1296 first = false;
1297 }
1298 if (PDecl->getPropertyAttributes() &
1299 ObjCPropertyDecl::OBJC_PR_atomic) {
1300 Out << (first ? ' ' : ',') << "atomic";
1301 first = false;
1302 }
1303
Douglas Gregor813a0662015-06-19 18:14:38 +00001304 if (PDecl->getPropertyAttributes() &
1305 ObjCPropertyDecl::OBJC_PR_nullability) {
Douglas Gregor86b42682015-06-19 18:27:52 +00001306 if (auto nullability = AttributedType::stripOuterNullability(T)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001307 if (*nullability == NullabilityKind::Unspecified &&
1308 (PDecl->getPropertyAttributes() &
1309 ObjCPropertyDecl::OBJC_PR_null_resettable)) {
1310 Out << (first ? ' ' : ',') << "null_resettable";
1311 } else {
1312 Out << (first ? ' ' : ',')
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001313 << getNullabilitySpelling(*nullability, true);
Douglas Gregor849ebc22015-06-19 18:14:46 +00001314 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001315 first = false;
1316 }
1317 }
1318
Manman Ren387ff7f2016-01-26 18:52:43 +00001319 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) {
1320 Out << (first ? ' ' : ',') << "class";
1321 first = false;
1322 }
1323
Ted Kremenek897af912011-08-17 21:09:35 +00001324 (void) first; // Silence dead store warning due to idiomatic code.
1325 Out << " )";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001326 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001327 Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
Fariborz Jahanian9e075842013-05-01 17:28:37 +00001328 getAsString(Policy) << ' ' << *PDecl;
Fariborz Jahanian0389e522012-12-19 23:36:00 +00001329 if (Policy.PolishForDeclaration)
1330 Out << ';';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001331}
1332
1333void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1334 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +00001335 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +00001336 else
Douglas Gregor36098ff2009-05-30 00:56:08 +00001337 Out << "@dynamic ";
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001338 Out << *PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001339 if (PID->getPropertyIvarDecl())
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001340 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +00001341}
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001342
1343void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001344 if (!D->isAccessDeclaration())
1345 Out << "using ";
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001346 if (D->hasTypename())
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001347 Out << "typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001348 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001349 Out << *D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001350}
1351
John McCalle61f2ba2009-11-18 02:36:19 +00001352void
1353DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1354 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001355 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001356 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +00001357}
1358
1359void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Enea Zaffanellac70b2512013-07-17 17:28:56 +00001360 if (!D->isAccessDeclaration())
1361 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001362 D->getQualifier()->print(Out, Policy);
Benjamin Kramer36d514e2015-09-23 13:43:16 +00001363 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +00001364}
John McCall3f746822009-11-17 05:59:44 +00001365
1366void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1367 // ignore
1368}
Alexey Bataeva769e072013-03-22 06:34:35 +00001369
1370void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1371 Out << "#pragma omp threadprivate";
1372 if (!D->varlist_empty()) {
1373 for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1374 E = D->varlist_end();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001375 I != E; ++I) {
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001376 Out << (I == D->varlist_begin() ? '(' : ',');
1377 NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
1378 ND->printQualifiedName(Out);
Alexey Bataeva769e072013-03-22 06:34:35 +00001379 }
1380 Out << ")";
1381 }
1382}
1383
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001384void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1385 if (!D->isInvalidDecl()) {
1386 Out << "#pragma omp declare reduction (";
1387 if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1388 static const char *const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
1389 nullptr,
1390#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1391 Spelling,
1392#include "clang/Basic/OperatorKinds.def"
1393 };
1394 const char *OpName =
1395 OperatorNames[D->getDeclName().getCXXOverloadedOperator()];
1396 assert(OpName && "not an overloaded operator");
1397 Out << OpName;
1398 } else {
1399 assert(D->getDeclName().isIdentifier());
1400 D->printName(Out);
1401 }
1402 Out << " : ";
1403 D->getType().print(Out, Policy);
1404 Out << " : ";
1405 D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1406 Out << ")";
1407 if (auto *Init = D->getInitializer()) {
1408 Out << " initializer(";
1409 Init->printPretty(Out, nullptr, Policy, 0);
1410 Out << ")";
1411 }
1412 }
1413}
1414
Alexey Bataev4244be22016-02-11 05:35:55 +00001415void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00001416 D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1417}
1418