blob: e2a66fb8a74a0434ef20bb012f6653d4dc92a3f8 [file] [log] [blame]
Douglas Gregor4fe0c8e2009-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 Kornienkod538ed92012-12-20 02:09:13 +000010// This file implements the Decl::print method, which pretty prints the
Douglas Gregor4fe0c8e2009-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 Kramer2fa67ef2012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000020#include "clang/AST/Expr.h"
Douglas Gregor9db7dbb2010-01-31 09:12:51 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000022#include "clang/AST/PrettyPrinter.h"
Douglas Gregor15de72c2011-12-02 23:23:56 +000023#include "clang/Basic/Module.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000024#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26
27namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000028 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000029 raw_ostream &Out;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000030 PrintingPolicy Policy;
31 unsigned Indentation;
Richard Trieu5cb3d692011-07-28 00:19:05 +000032 bool PrintInstantiation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000033
Chris Lattner5f9e2722011-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 Gregor4fe0c8e2009-05-30 00:08:05 +000037
Anders Carlsson0d592922009-08-28 22:39:52 +000038 void Print(AccessSpecifier AS);
Mike Stump1eb44332009-09-09 15:08:12 +000039
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000040 public:
Richard Smithd1420c62012-08-16 03:56:14 +000041 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
42 unsigned Indentation = 0, bool PrintInstantiation = false)
43 : Out(Out), Policy(Policy), Indentation(Indentation),
Richard Trieu5cb3d692011-07-28 00:19:05 +000044 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000045
46 void VisitDeclContext(DeclContext *DC, bool Indent = true);
47
48 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
49 void VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +000050 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000051 void VisitEnumDecl(EnumDecl *D);
52 void VisitRecordDecl(RecordDecl *D);
53 void VisitEnumConstantDecl(EnumConstantDecl *D);
Michael Han684aa732013-02-22 17:15:32 +000054 void VisitEmptyDecl(EmptyDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000055 void VisitFunctionDecl(FunctionDecl *D);
Fariborz Jahanian8920eb72012-12-05 00:38:44 +000056 void VisitFriendDecl(FriendDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000057 void VisitFieldDecl(FieldDecl *D);
58 void VisitVarDecl(VarDecl *D);
Chris Lattner57ad3782011-02-17 20:34:02 +000059 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000060 void VisitParmVarDecl(ParmVarDecl *D);
61 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor15de72c2011-12-02 23:23:56 +000062 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +000063 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000064 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000065 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000066 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000067 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000068 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith98a57862011-04-15 13:38:57 +000069 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000070 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
71 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000072 void VisitObjCMethodDecl(ObjCMethodDecl *D);
73 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
74 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000075 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
76 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
77 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
78 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
79 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
80 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000081 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
82 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000083 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000084 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000085
86 void PrintTemplateParameters(const TemplateParameterList *Params,
Enea Zaffanella8c840282013-01-31 09:54:08 +000087 const TemplateArgumentList *Args = 0);
Douglas Gregor1bea8802011-11-19 19:22:57 +000088 void prettyPrintAttributes(Decl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000089 };
90}
91
Richard Trieu5cb3d692011-07-28 00:19:05 +000092void Decl::print(raw_ostream &Out, unsigned Indentation,
93 bool PrintInstantiation) const {
Douglas Gregor30c42402011-09-27 22:38:19 +000094 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000095}
96
Chris Lattner5f9e2722011-07-23 10:55:15 +000097void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +000098 unsigned Indentation, bool PrintInstantiation) const {
Richard Smithd1420c62012-08-16 03:56:14 +000099 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlssonf88df862009-09-26 21:58:53 +0000100 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000101}
102
Eli Friedman42f42c02009-05-30 04:20:30 +0000103static QualType GetBaseType(QualType T) {
104 // FIXME: This should be on the Type class!
105 QualType BaseType = T;
106 while (!BaseType->isSpecifierType()) {
107 if (isa<TypedefType>(BaseType))
108 break;
Ted Kremenek6217b802009-07-29 21:53:49 +0000109 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000110 BaseType = PTy->getPointeeType();
Ted Kremenek41c1f212012-10-11 20:58:14 +0000111 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
112 BaseType = BPy->getPointeeType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000113 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
114 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +0000115 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000116 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000117 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000118 BaseType = VTy->getElementType();
Eli Friedman15631b42012-08-08 03:47:15 +0000119 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
120 BaseType = RTy->getPointeeType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000121 else
David Blaikieb219cfc2011-09-23 05:06:16 +0000122 llvm_unreachable("Unknown declarator!");
Eli Friedman42f42c02009-05-30 04:20:30 +0000123 }
124 return BaseType;
125}
126
127static QualType getDeclType(Decl* D) {
Richard Smith162e1c12011-04-15 14:24:37 +0000128 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman42f42c02009-05-30 04:20:30 +0000129 return TDD->getUnderlyingType();
130 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
131 return VD->getType();
132 return QualType();
133}
134
135void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000136 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000137 unsigned Indentation) {
138 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000139 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000140 return;
141 }
142
143 Decl** End = Begin + NumDecls;
144 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
145 if (TD)
146 ++Begin;
147
148 PrintingPolicy SubPolicy(Policy);
John McCall5e1cdac2011-10-07 06:10:15 +0000149 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000150 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000151 Out << " ";
152 SubPolicy.SuppressTag = true;
153 }
154
155 bool isFirst = true;
156 for ( ; Begin != End; ++Begin) {
157 if (isFirst) {
158 SubPolicy.SuppressSpecifiers = false;
159 isFirst = false;
160 } else {
161 if (!isFirst) Out << ", ";
162 SubPolicy.SuppressSpecifiers = true;
163 }
164
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000165 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000166 }
167}
168
Anders Carlsson84834432009-12-14 00:51:04 +0000169void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000170 // Get the translation unit
171 const DeclContext *DC = this;
172 while (!DC->isTranslationUnit())
173 DC = DC->getParent();
174
175 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smithd1420c62012-08-16 03:56:14 +0000176 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000177 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
178}
179
Chris Lattner5f9e2722011-07-23 10:55:15 +0000180raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar512ce602009-11-21 09:12:06 +0000181 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000182 Out << " ";
183 return Out;
184}
185
Douglas Gregor1bea8802011-11-19 19:22:57 +0000186void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian40902d82012-12-19 23:36:00 +0000187 if (Policy.PolishForDeclaration)
Fariborz Jahanian1bfb00d2012-10-17 21:58:03 +0000188 return;
189
Douglas Gregor1bea8802011-11-19 19:22:57 +0000190 if (D->hasAttrs()) {
191 AttrVec &Attrs = D->getAttrs();
192 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
Richard Smith0dae7292012-08-16 02:43:29 +0000193 Attr *A = *i;
194 A->printPretty(Out, Policy);
Douglas Gregor1bea8802011-11-19 19:22:57 +0000195 }
196 }
197}
198
Chris Lattner5f9e2722011-07-23 10:55:15 +0000199void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000200 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000201 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000202 Out << ";\n";
203 Decls.clear();
204
205}
206
Anders Carlsson0d592922009-08-28 22:39:52 +0000207void DeclPrinter::Print(AccessSpecifier AS) {
208 switch(AS) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000209 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson0d592922009-08-28 22:39:52 +0000210 case AS_public: Out << "public"; break;
211 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000212 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000213 }
214}
215
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000216//----------------------------------------------------------------------------
217// Common C declarations
218//----------------------------------------------------------------------------
219
220void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkod1fc82e2012-08-21 17:36:32 +0000221 if (Policy.TerseOutput)
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000222 return;
223
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000224 if (Indent)
225 Indentation += Policy.Indentation;
226
Chris Lattner5f9e2722011-07-23 10:55:15 +0000227 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000228 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000229 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-07-30 00:47:46 +0000230
231 // Don't print ObjCIvarDecls, as they are printed when visiting the
232 // containing ObjCInterfaceDecl.
233 if (isa<ObjCIvarDecl>(*D))
234 continue;
235
Alexander Kornienkod538ed92012-12-20 02:09:13 +0000236 // Skip over implicit declarations in pretty-printing mode.
237 if (D->isImplicit())
238 continue;
239
240 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
241 // of __builtin_va_list or __[u]int128_t. There should be some other way
242 // to check that.
243 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
244 if (IdentifierInfo *II = ND->getIdentifier()) {
245 if (II->isStr("__builtin_va_list") ||
246 II->isStr("__int128_t") || II->isStr("__uint128_t"))
247 continue;
Argyrios Kyrtzidis2574f6f2010-06-17 10:52:11 +0000248 }
Eli Friedman48d14a22009-05-30 05:03:24 +0000249 }
250
Eli Friedman42f42c02009-05-30 04:20:30 +0000251 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
252 // forced to merge the declarations because there's no other way to
253 // refer to the struct in question. This limited merging is safe without
254 // a bunch of other checks because it only merges declarations directly
255 // referring to the tag, not typedefs.
256 //
257 // Check whether the current declaration should be grouped with a previous
258 // unnamed struct.
259 QualType CurDeclType = getDeclType(*D);
260 if (!Decls.empty() && !CurDeclType.isNull()) {
261 QualType BaseType = GetBaseType(CurDeclType);
262 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
263 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
264 Decls.push_back(*D);
265 continue;
266 }
267 }
268
269 // If we have a merged group waiting to be handled, handle it now.
270 if (!Decls.empty())
271 ProcessDeclGroup(Decls);
272
273 // If the current declaration is an unnamed tag type, save it
274 // so we can merge it with the subsequent declaration(s) using it.
275 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
276 Decls.push_back(*D);
277 continue;
278 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000279
280 if (isa<AccessSpecDecl>(*D)) {
281 Indentation -= Policy.Indentation;
282 this->Indent();
283 Print(D->getAccess());
284 Out << ":\n";
285 Indentation += Policy.Indentation;
286 continue;
287 }
288
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000289 this->Indent();
290 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
292 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000293 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000294 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000295 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
296 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000297 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
298 Terminator = 0;
299 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000300 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000301 isa<ObjCInterfaceDecl>(*D) ||
302 isa<ObjCProtocolDecl>(*D) ||
303 isa<ObjCCategoryImplDecl>(*D) ||
304 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000305 Terminator = 0;
306 else if (isa<EnumConstantDecl>(*D)) {
307 DeclContext::decl_iterator Next = D;
308 ++Next;
309 if (Next != DEnd)
310 Terminator = ",";
311 } else
312 Terminator = ";";
313
314 if (Terminator)
315 Out << Terminator;
316 Out << "\n";
317 }
318
Eli Friedman42f42c02009-05-30 04:20:30 +0000319 if (!Decls.empty())
320 ProcessDeclGroup(Decls);
321
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000322 if (Indent)
323 Indentation -= Policy.Indentation;
324}
325
326void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
327 VisitDeclContext(D, false);
328}
329
330void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000331 if (!Policy.SuppressSpecifiers) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000332 Out << "typedef ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000333
334 if (D->isModulePrivate())
335 Out << "__module_private__ ";
336 }
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000337 D->getUnderlyingType().print(Out, Policy, D->getName());
Douglas Gregor1bea8802011-11-19 19:22:57 +0000338 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000339}
340
Richard Smith162e1c12011-04-15 14:24:37 +0000341void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000342 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy);
Richard Smith162e1c12011-04-15 14:24:37 +0000343}
344
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000345void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000346 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
347 Out << "__module_private__ ";
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000348 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000349 if (D->isScoped()) {
350 if (D->isScopedUsingClassTag())
351 Out << "class ";
352 else
353 Out << "struct ";
354 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000355 Out << *D;
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000356
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000357 if (D->isFixed())
358 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000359
John McCall5e1cdac2011-10-07 06:10:15 +0000360 if (D->isCompleteDefinition()) {
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000361 Out << " {\n";
362 VisitDeclContext(D);
363 Indent() << "}";
364 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000365 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000366}
367
368void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000369 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
370 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000371 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000372 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000373 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
John McCall5e1cdac2011-10-07 06:10:15 +0000375 if (D->isCompleteDefinition()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000376 Out << " {\n";
377 VisitDeclContext(D);
378 Indent() << "}";
379 }
380}
381
382void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000383 Out << *D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000384 if (Expr *Init = D->getInitExpr()) {
385 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000386 Init->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000387 }
388}
389
Mike Stump1eb44332009-09-09 15:08:12 +0000390void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Fariborz Jahanian65bcdab2012-12-05 22:19:06 +0000391 CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
Eli Friedman42f42c02009-05-30 04:20:30 +0000392 if (!Policy.SuppressSpecifiers) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000393 switch (D->getStorageClassAsWritten()) {
John McCalld931b082010-08-26 03:08:43 +0000394 case SC_None: break;
395 case SC_Extern: Out << "extern "; break;
396 case SC_Static: Out << "static "; break;
397 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000398 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
399 llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000400 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000401
Douglas Gregor8d267c52011-09-09 02:06:17 +0000402 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000403 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000404 if (D->isModulePrivate()) Out << "__module_private__ ";
Fariborz Jahanian65bcdab2012-12-05 22:19:06 +0000405 if (CDecl && CDecl->isExplicitSpecified())
406 Out << "explicit ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000407 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000408
Douglas Gregor6620a622009-05-30 05:39:39 +0000409 PrintingPolicy SubPolicy(Policy);
410 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000411 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000412
Abramo Bagnara723df242010-12-14 22:11:44 +0000413 QualType Ty = D->getType();
John McCallf4c73712011-01-19 06:33:43 +0000414 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000415 Proto = '(' + Proto + ')';
416 Ty = PT->getInnerType();
417 }
418
419 if (isa<FunctionType>(Ty)) {
420 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000421 const FunctionProtoType *FT = 0;
422 if (D->hasWrittenPrototype())
423 FT = dyn_cast<FunctionProtoType>(AFT);
424
425 Proto += "(";
426 if (FT) {
427 llvm::raw_string_ostream POut(Proto);
Richard Smithd1420c62012-08-16 03:56:14 +0000428 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000429 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
430 if (i) POut << ", ";
431 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000434 if (FT->isVariadic()) {
435 if (D->getNumParams()) POut << ", ";
436 POut << "...";
437 }
Sean Hunt10620eb2011-05-06 20:44:56 +0000438 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000439 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
440 if (i)
441 Proto += ", ";
442 Proto += D->getParamDecl(i)->getNameAsString();
443 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000444 }
445
446 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000447
David Blaikie4ef832f2012-08-10 00:55:35 +0000448 if (FT) {
449 if (FT->isConst())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000450 Proto += " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000451 if (FT->isVolatile())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000452 Proto += " volatile";
David Blaikie4ef832f2012-08-10 00:55:35 +0000453 if (FT->isRestrict())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000454 Proto += " restrict";
455 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000456
457 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000458 Proto += " throw(";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000459 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000460 Proto += "...";
461 else
462 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
463 if (I)
464 Proto += ", ";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000465
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000466 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000467 }
468 Proto += ")";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000469 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
470 Proto += " noexcept";
471 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
472 Proto += "(";
473 llvm::raw_string_ostream EOut(Proto);
Richard Smithd1420c62012-08-16 03:56:14 +0000474 FT->getNoexceptExpr()->printPretty(EOut, 0, SubPolicy,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000475 Indentation);
476 EOut.flush();
477 Proto += EOut.str();
478 Proto += ")";
479 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000480 }
481
Fariborz Jahanian65bcdab2012-12-05 22:19:06 +0000482 if (CDecl) {
Richard Smith7a614d82011-06-11 17:19:42 +0000483 bool HasInitializerList = false;
484 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
485 E = CDecl->init_end();
486 B != E; ++B) {
487 CXXCtorInitializer * BMInitializer = (*B);
488 if (BMInitializer->isInClassMemberInitializer())
489 continue;
490
491 if (!HasInitializerList) {
492 Proto += " : ";
493 Out << Proto;
494 Proto.clear();
495 HasInitializerList = true;
496 } else
497 Out << ", ";
498
499 if (BMInitializer->isAnyMemberInitializer()) {
500 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000501 Out << *FD;
Richard Smith7a614d82011-06-11 17:19:42 +0000502 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000503 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith7a614d82011-06-11 17:19:42 +0000504 }
505
506 Out << "(";
507 if (!BMInitializer->getInit()) {
508 // Nothing to print
509 } else {
510 Expr *Init = BMInitializer->getInit();
511 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
512 Init = Tmp->getSubExpr();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000513
Richard Smith7a614d82011-06-11 17:19:42 +0000514 Init = Init->IgnoreParens();
515
516 Expr *SimpleInit = 0;
517 Expr **Args = 0;
518 unsigned NumArgs = 0;
519 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
520 Args = ParenList->getExprs();
521 NumArgs = ParenList->getNumExprs();
522 } else if (CXXConstructExpr *Construct
523 = dyn_cast<CXXConstructExpr>(Init)) {
524 Args = Construct->getArgs();
525 NumArgs = Construct->getNumArgs();
526 } else
527 SimpleInit = Init;
528
529 if (SimpleInit)
Richard Smithd1420c62012-08-16 03:56:14 +0000530 SimpleInit->printPretty(Out, 0, Policy, Indentation);
Richard Smith7a614d82011-06-11 17:19:42 +0000531 else {
532 for (unsigned I = 0; I != NumArgs; ++I) {
533 if (isa<CXXDefaultArgExpr>(Args[I]))
534 break;
535
536 if (I)
537 Out << ", ";
Richard Smithd1420c62012-08-16 03:56:14 +0000538 Args[I]->printPretty(Out, 0, Policy, Indentation);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000539 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000540 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000541 }
Richard Smith7a614d82011-06-11 17:19:42 +0000542 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000543 }
Fariborz Jahanian02a2e5a2012-12-05 19:54:11 +0000544 if (!Proto.empty())
545 Out << Proto;
Richard Smith425663a2013-02-22 05:54:51 +0000546 } else {
547 if (FT && FT->hasTrailingReturn()) {
548 Out << "auto " << Proto << " -> ";
549 Proto.clear();
550 }
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000551 AFT->getResultType().print(Out, Policy, Proto);
Richard Smith425663a2013-02-22 05:54:51 +0000552 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000553 } else {
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000554 Ty.print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000555 }
556
Douglas Gregor1bea8802011-11-19 19:22:57 +0000557 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000558
559 if (D->isPure())
560 Out << " = 0";
Sean Hunt10620eb2011-05-06 20:44:56 +0000561 else if (D->isDeletedAsWritten())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000562 Out << " = delete";
Fariborz Jahanianddb29682012-12-05 22:53:06 +0000563 else if (D->isExplicitlyDefaulted())
564 Out << " = default";
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000565 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000566 if (!D->hasPrototype() && D->getNumParams()) {
567 // This is a K&R function definition, so we need to print the
568 // parameters.
569 Out << '\n';
Richard Smithd1420c62012-08-16 03:56:14 +0000570 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000571 Indentation += Policy.Indentation;
572 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
573 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000574 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000575 Out << ";\n";
576 }
577 Indentation -= Policy.Indentation;
578 } else
579 Out << ' ';
580
Richard Smithd1420c62012-08-16 03:56:14 +0000581 D->getBody()->printPretty(Out, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000582 Out << '\n';
583 }
584}
585
Fariborz Jahanian8920eb72012-12-05 00:38:44 +0000586void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
587 if (TypeSourceInfo *TSI = D->getFriendType()) {
Enea Zaffanella8c840282013-01-31 09:54:08 +0000588 unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
589 for (unsigned i = 0; i < NumTPLists; ++i)
590 PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
Fariborz Jahaniand4ae6532012-12-21 21:43:05 +0000591 Out << "friend ";
592 Out << " " << TSI->getType().getAsString(Policy);
Fariborz Jahanian8920eb72012-12-05 00:38:44 +0000593 }
594 else if (FunctionDecl *FD =
595 dyn_cast<FunctionDecl>(D->getFriendDecl())) {
596 Out << "friend ";
597 VisitFunctionDecl(FD);
598 }
599 else if (FunctionTemplateDecl *FTD =
600 dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
601 Out << "friend ";
602 VisitFunctionTemplateDecl(FTD);
603 }
604 else if (ClassTemplateDecl *CTD =
605 dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
606 Out << "friend ";
Fariborz Jahaniand4ae6532012-12-21 21:43:05 +0000607 VisitRedeclarableTemplateDecl(CTD);
Fariborz Jahanian8920eb72012-12-05 00:38:44 +0000608 }
609}
610
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000611void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000612 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000613 Out << "mutable ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000614 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
615 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000616
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000617 Out << D->getType().stream(Policy, D->getName());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000618
619 if (D->isBitField()) {
620 Out << " : ";
Richard Smithd1420c62012-08-16 03:56:14 +0000621 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000622 }
Richard Smith7a614d82011-06-11 17:19:42 +0000623
624 Expr *Init = D->getInClassInitializer();
625 if (!Policy.SuppressInitializers && Init) {
Richard Smithca523302012-06-10 03:12:00 +0000626 if (D->getInClassInitStyle() == ICIS_ListInit)
627 Out << " ";
628 else
629 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000630 Init->printPretty(Out, 0, Policy, Indentation);
Richard Smith7a614d82011-06-11 17:19:42 +0000631 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000632 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000633}
634
Chris Lattner57ad3782011-02-17 20:34:02 +0000635void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000636 Out << *D << ":";
Chris Lattner57ad3782011-02-17 20:34:02 +0000637}
638
639
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000640void DeclPrinter::VisitVarDecl(VarDecl *D) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000641 StorageClass SCAsWritten = D->getStorageClassAsWritten();
642 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None)
643 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000644
Eli Friedman42f42c02009-05-30 04:20:30 +0000645 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000646 Out << "__thread ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000647 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
648 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000649
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000650 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000651 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000652 T = Parm->getOriginalType();
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000653 T.print(Out, Policy, D->getName());
Richard Smithad762fc2011-04-14 22:09:26 +0000654 Expr *Init = D->getInit();
655 if (!Policy.SuppressInitializers && Init) {
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000656 bool ImplicitInit = false;
Dmitri Gribenko93aa2db2013-02-03 23:02:47 +0000657 if (CXXConstructExpr *Construct =
658 dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
Dmitri Gribenko5250e2b2013-01-27 21:28:24 +0000659 if (D->getInitStyle() == VarDecl::CallInit &&
660 !Construct->isListInitialization()) {
661 ImplicitInit = Construct->getNumArgs() == 0 ||
662 Construct->getArg(0)->isDefaultArgument();
663 }
664 }
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000665 if (!ImplicitInit) {
Eli Friedmane1aebe12012-10-19 20:36:44 +0000666 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000667 Out << "(";
668 else if (D->getInitStyle() == VarDecl::CInit) {
669 Out << " = ";
670 }
Richard Smithd1420c62012-08-16 03:56:14 +0000671 Init->printPretty(Out, 0, Policy, Indentation);
Eli Friedmane1aebe12012-10-19 20:36:44 +0000672 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000673 Out << ")";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000674 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000675 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000676 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000677}
678
679void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
680 VisitVarDecl(D);
681}
682
683void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
684 Out << "__asm (";
Richard Smithd1420c62012-08-16 03:56:14 +0000685 D->getAsmString()->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000686 Out << ")";
687}
688
Douglas Gregor15de72c2011-12-02 23:23:56 +0000689void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Douglas Gregor1b257af2012-12-11 22:11:52 +0000690 Out << "@import " << D->getImportedModule()->getFullModuleName()
Douglas Gregor15de72c2011-12-02 23:23:56 +0000691 << ";\n";
692}
693
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000694void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
695 Out << "static_assert(";
Richard Smithd1420c62012-08-16 03:56:14 +0000696 D->getAssertExpr()->printPretty(Out, 0, Policy, Indentation);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000697 Out << ", ";
Richard Smithd1420c62012-08-16 03:56:14 +0000698 D->getMessage()->printPretty(Out, 0, Policy, Indentation);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000699 Out << ")";
700}
701
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000702//----------------------------------------------------------------------------
703// C++ declarations
704//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000705void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorc30636a2012-05-01 01:43:38 +0000706 if (D->isInline())
707 Out << "inline ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000708 Out << "namespace " << *D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000709 VisitDeclContext(D);
710 Indent() << "}";
711}
712
Douglas Gregor8419fa32009-05-30 06:31:56 +0000713void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
714 Out << "using namespace ";
715 if (D->getQualifier())
716 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000717 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000718}
719
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000720void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000721 Out << "namespace " << *D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000722 if (D->getQualifier())
723 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000724 Out << *D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000725}
726
Michael Han684aa732013-02-22 17:15:32 +0000727void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
728 prettyPrintAttributes(D);
Michael Han684aa732013-02-22 17:15:32 +0000729}
730
Douglas Gregor59e63572009-05-30 06:58:37 +0000731void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000732 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
733 Out << "__module_private__ ";
Douglas Gregor59e63572009-05-30 06:58:37 +0000734 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000735 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000736 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000737
John McCall5e1cdac2011-10-07 06:10:15 +0000738 if (D->isCompleteDefinition()) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000739 // Print the base classes
740 if (D->getNumBases()) {
741 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000742 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
743 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000744 if (Base != D->bases_begin())
745 Out << ", ";
746
747 if (Base->isVirtual())
748 Out << "virtual ";
749
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000750 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
751 if (AS != AS_none)
752 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000753 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor34a99e72011-04-27 17:07:55 +0000754
755 if (Base->isPackExpansion())
756 Out << "...";
Douglas Gregor59e63572009-05-30 06:58:37 +0000757 }
758 }
759
760 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000761 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000762 Out << " {\n";
763 VisitDeclContext(D);
764 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000765 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000766}
767
768void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
769 const char *l;
770 if (D->getLanguage() == LinkageSpecDecl::lang_c)
771 l = "C";
772 else {
773 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
774 "unknown language in linkage specification");
775 l = "C++";
776 }
777
778 Out << "extern \"" << l << "\" ";
779 if (D->hasBraces()) {
780 Out << "{\n";
781 VisitDeclContext(D);
782 Indent() << "}";
783 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000784 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000785}
786
Enea Zaffanella8c840282013-01-31 09:54:08 +0000787void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
788 const TemplateArgumentList *Args) {
Richard Trieu5cb3d692011-07-28 00:19:05 +0000789 assert(Params);
790 assert(!Args || Params->size() == Args->size());
791
Anders Carlsson0487f662009-06-04 05:37:43 +0000792 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Anders Carlsson0487f662009-06-04 05:37:43 +0000794 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
795 if (i != 0)
796 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Anders Carlsson0487f662009-06-04 05:37:43 +0000798 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000799 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000800 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Anders Carlsson0487f662009-06-04 05:37:43 +0000802 if (TTP->wasDeclaredWithTypename())
803 Out << "typename ";
804 else
805 Out << "class ";
806
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000807 if (TTP->isParameterPack())
808 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000810 Out << *TTP;
Anders Carlsson0487f662009-06-04 05:37:43 +0000811
Richard Trieu5cb3d692011-07-28 00:19:05 +0000812 if (Args) {
813 Out << " = ";
814 Args->get(i).print(Policy, Out);
815 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000816 Out << " = ";
817 Out << TTP->getDefaultArgument().getAsString(Policy);
818 };
Mike Stump1eb44332009-09-09 15:08:12 +0000819 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000820 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
821 Out << NTTP->getType().getAsString(Policy);
822
Douglas Gregor56bc9832010-12-24 00:15:10 +0000823 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
824 Out << "...";
825
Anders Carlsson0487f662009-06-04 05:37:43 +0000826 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
827 Out << ' ';
828 Out << Name->getName();
829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Richard Trieu5cb3d692011-07-28 00:19:05 +0000831 if (Args) {
832 Out << " = ";
833 Args->get(i).print(Policy, Out);
834 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000835 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000836 NTTP->getDefaultArgument()->printPretty(Out, 0, Policy, Indentation);
Anders Carlsson0487f662009-06-04 05:37:43 +0000837 }
Richard Smith98a57862011-04-15 13:38:57 +0000838 } else if (const TemplateTemplateParmDecl *TTPD =
839 dyn_cast<TemplateTemplateParmDecl>(Param)) {
840 VisitTemplateDecl(TTPD);
841 // FIXME: print the default argument, if present.
Anders Carlsson0487f662009-06-04 05:37:43 +0000842 }
843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Anders Carlsson0487f662009-06-04 05:37:43 +0000845 Out << "> ";
Richard Trieu5cb3d692011-07-28 00:19:05 +0000846}
847
848void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
849 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson0487f662009-06-04 05:37:43 +0000850
Richard Smith98a57862011-04-15 13:38:57 +0000851 if (const TemplateTemplateParmDecl *TTP =
852 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000853 Out << "class ";
854 if (TTP->isParameterPack())
855 Out << "...";
856 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000857 } else {
858 Visit(D->getTemplatedDecl());
859 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000860}
861
Richard Trieu5cb3d692011-07-28 00:19:05 +0000862void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
863 if (PrintInstantiation) {
864 TemplateParameterList *Params = D->getTemplateParameters();
865 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
866 I != E; ++I) {
867 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
868 Visit(*I);
869 }
870 }
871
872 return VisitRedeclarableTemplateDecl(D);
873}
874
875void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
876 if (PrintInstantiation) {
877 TemplateParameterList *Params = D->getTemplateParameters();
878 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
879 I != E; ++I) {
880 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
881 Visit(*I);
882 Out << '\n';
883 }
884 }
885
886 return VisitRedeclarableTemplateDecl(D);
887}
888
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000889//----------------------------------------------------------------------------
890// Objective-C declarations
891//----------------------------------------------------------------------------
892
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000893void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
894 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000895 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000896 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000897 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000898 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000899 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000901 std::string name = OMD->getSelector().getAsString();
902 std::string::size_type pos, lastPos = 0;
903 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
904 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000905 // FIXME: selector is missing here!
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000906 pos = name.find_first_of(':', lastPos);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000907 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000908 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000909 lastPos = pos + 1;
910 }
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000912 if (OMD->param_begin() == OMD->param_end())
913 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000915 if (OMD->isVariadic())
916 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Fariborz Jahanian1bfb00d2012-10-17 21:58:03 +0000918 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000919 Out << ' ';
Richard Smithd1420c62012-08-16 03:56:14 +0000920 OMD->getBody()->printPretty(Out, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000921 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000922 }
Fariborz Jahanian40902d82012-12-19 23:36:00 +0000923 else if (Policy.PolishForDeclaration)
Fariborz Jahanian88b95212012-12-18 23:02:59 +0000924 Out << ';';
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000925}
926
927void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
928 std::string I = OID->getNameAsString();
929 ObjCInterfaceDecl *SID = OID->getSuperClass();
930
931 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000932 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000933 else
934 Out << "@implementation " << I;
Fariborz Jahanian482b4fd2012-12-04 00:47:33 +0000935
936 if (OID->ivar_size() > 0) {
937 Out << "{\n";
938 Indentation += Policy.Indentation;
939 for (ObjCImplementationDecl::ivar_iterator I = OID->ivar_begin(),
940 E = OID->ivar_end(); I != E; ++I) {
941 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n";
942 }
943 Indentation -= Policy.Indentation;
944 Out << "}\n";
945 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000946 VisitDeclContext(OID, false);
947 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000948}
949
950void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
951 std::string I = OID->getNameAsString();
952 ObjCInterfaceDecl *SID = OID->getSuperClass();
953
Douglas Gregor7723fec2011-12-15 20:29:51 +0000954 if (!OID->isThisDeclarationADefinition()) {
955 Out << "@class " << I << ";";
956 return;
957 }
Fariborz Jahanian40902d82012-12-19 23:36:00 +0000958 bool eolnOut = false;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000959 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000960 Out << "@interface " << I << " : " << *SID;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000961 else
962 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000964 // Protocols?
965 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
966 if (!Protocols.empty()) {
967 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
968 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000969 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000970 Out << "> ";
Fariborz Jahanian40902d82012-12-19 23:36:00 +0000971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000973 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000974 Out << "{\n";
Fariborz Jahanian40902d82012-12-19 23:36:00 +0000975 eolnOut = true;
Douglas Gregor64f65002009-05-30 00:56:08 +0000976 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000977 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
978 E = OID->ivar_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +0000979 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000980 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000981 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000982 Out << "}\n";
983 }
Fariborz Jahanian40902d82012-12-19 23:36:00 +0000984 else if (SID) {
985 Out << "\n";
986 eolnOut = true;
987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000989 VisitDeclContext(OID, false);
Fariborz Jahanian40902d82012-12-19 23:36:00 +0000990 if (!eolnOut)
991 Out << ' ';
Douglas Gregor64f65002009-05-30 00:56:08 +0000992 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000993 // FIXME: implement the rest...
994}
995
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000996void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000997 if (!PID->isThisDeclarationADefinition()) {
Fariborz Jahanian40902d82012-12-19 23:36:00 +0000998 Out << "@protocol " << *PID << ";\n";
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000999 return;
1000 }
Fariborz Jahanian40902d82012-12-19 23:36:00 +00001001 // Protocols?
1002 const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1003 if (!Protocols.empty()) {
1004 Out << "@protocol " << *PID;
1005 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1006 E = Protocols.end(); I != E; ++I)
1007 Out << (I == Protocols.begin() ? '<' : ',') << **I;
1008 Out << ">\n";
1009 } else
1010 Out << "@protocol " << *PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +00001011 VisitDeclContext(PID, false);
1012 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001013}
1014
1015void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001016 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001017
1018 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +00001019 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001020 // FIXME: implement the rest...
1021}
1022
1023void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001024 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Fariborz Jahanianff685c52012-12-04 17:20:57 +00001025 if (PID->ivar_size() > 0) {
1026 Out << "{\n";
1027 Indentation += Policy.Indentation;
1028 for (ObjCCategoryDecl::ivar_iterator I = PID->ivar_begin(),
1029 E = PID->ivar_end(); I != E; ++I) {
1030 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n";
1031 }
1032 Indentation -= Policy.Indentation;
1033 Out << "}\n";
1034 }
1035
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001036 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +00001037 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001039 // FIXME: implement the rest...
1040}
1041
1042void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001043 Out << "@compatibility_alias " << *AID
1044 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001045}
1046
1047/// PrintObjCPropertyDecl - print a property declaration.
1048///
1049void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1050 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1051 Out << "@required\n";
1052 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1053 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001055 Out << "@property";
1056 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
1057 bool first = true;
1058 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +00001059 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001060 ObjCPropertyDecl::OBJC_PR_readonly) {
1061 Out << (first ? ' ' : ',') << "readonly";
1062 first = false;
Ted Kremenek07c682a2011-08-17 21:09:35 +00001063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Ted Kremenek07c682a2011-08-17 21:09:35 +00001065 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1066 Out << (first ? ' ' : ',') << "getter = "
1067 << PDecl->getGetterName().getAsString();
1068 first = false;
1069 }
1070 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1071 Out << (first ? ' ' : ',') << "setter = "
1072 << PDecl->getSetterName().getAsString();
1073 first = false;
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Ted Kremenek07c682a2011-08-17 21:09:35 +00001076 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1077 Out << (first ? ' ' : ',') << "assign";
1078 first = false;
1079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Ted Kremenek07c682a2011-08-17 21:09:35 +00001081 if (PDecl->getPropertyAttributes() &
1082 ObjCPropertyDecl::OBJC_PR_readwrite) {
1083 Out << (first ? ' ' : ',') << "readwrite";
1084 first = false;
1085 }
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Ted Kremenek07c682a2011-08-17 21:09:35 +00001087 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1088 Out << (first ? ' ' : ',') << "retain";
1089 first = false;
1090 }
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Ted Kremenek07c682a2011-08-17 21:09:35 +00001092 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1093 Out << (first ? ' ' : ',') << "strong";
1094 first = false;
1095 }
John McCallf85e1932011-06-15 23:02:42 +00001096
Ted Kremenek07c682a2011-08-17 21:09:35 +00001097 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1098 Out << (first ? ' ' : ',') << "copy";
1099 first = false;
1100 }
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Ted Kremenek07c682a2011-08-17 21:09:35 +00001102 if (PDecl->getPropertyAttributes() &
1103 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1104 Out << (first ? ' ' : ',') << "nonatomic";
1105 first = false;
1106 }
1107 if (PDecl->getPropertyAttributes() &
1108 ObjCPropertyDecl::OBJC_PR_atomic) {
1109 Out << (first ? ' ' : ',') << "atomic";
1110 first = false;
1111 }
1112
1113 (void) first; // Silence dead store warning due to idiomatic code.
1114 Out << " )";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001115 }
Fariborz Jahanian40902d82012-12-19 23:36:00 +00001116 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
1117 if (Policy.PolishForDeclaration)
1118 Out << ';';
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001119}
1120
1121void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1122 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +00001123 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001124 else
Douglas Gregor64f65002009-05-30 00:56:08 +00001125 Out << "@dynamic ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001126 Out << *PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001127 if (PID->getPropertyIvarDecl())
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001128 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001129}
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001130
1131void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1132 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001133 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001134 Out << *D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001135}
1136
John McCall7ba107a2009-11-18 02:36:19 +00001137void
1138DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1139 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +00001140 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001141 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +00001142}
1143
1144void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001145 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001146 D->getQualifier()->print(Out, Policy);
Fariborz Jahanian5e0ea192012-12-06 00:09:40 +00001147 Out << D->getName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001148}
John McCall9488ea12009-11-17 05:59:44 +00001149
1150void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1151 // ignore
1152}