blob: 2fb6cb1d311dd96f28d573a992e6a7a2c5c9bda3 [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//
10// This file implements the Decl::dump method, which pretty print the
11// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclVisitor.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Douglas Gregor9db7dbb2010-01-31 09:12:51 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000021#include "clang/AST/PrettyPrinter.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000022#include "llvm/Support/raw_ostream.h"
23using namespace clang;
24
25namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000026 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000027 llvm::raw_ostream &Out;
28 ASTContext &Context;
29 PrintingPolicy Policy;
30 unsigned Indentation;
31
Daniel Dunbar512ce602009-11-21 09:12:06 +000032 llvm::raw_ostream& Indent() { return Indent(Indentation); }
33 llvm::raw_ostream& Indent(unsigned Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +000034 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000035
Anders Carlsson0d592922009-08-28 22:39:52 +000036 void Print(AccessSpecifier AS);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000038 public:
Mike Stump1eb44332009-09-09 15:08:12 +000039 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000040 const PrintingPolicy &Policy,
41 unsigned Indentation = 0)
42 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
43
44 void VisitDeclContext(DeclContext *DC, bool Indent = true);
45
46 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
47 void VisitTypedefDecl(TypedefDecl *D);
48 void VisitEnumDecl(EnumDecl *D);
49 void VisitRecordDecl(RecordDecl *D);
50 void VisitEnumConstantDecl(EnumConstantDecl *D);
51 void VisitFunctionDecl(FunctionDecl *D);
52 void VisitFieldDecl(FieldDecl *D);
53 void VisitVarDecl(VarDecl *D);
54 void VisitParmVarDecl(ParmVarDecl *D);
55 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000056 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000057 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000058 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000059 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000060 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
61 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000062 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000063 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000064 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
65 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
66 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
67 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
68 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
69 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
70 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
71 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
72 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000073 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
74 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000075 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000076 void VisitUsingShadowDecl(UsingShadowDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000077 };
78}
79
Anders Carlssonf88df862009-09-26 21:58:53 +000080void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000081 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000082}
83
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000084void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Anders Carlssonf88df862009-09-26 21:58:53 +000085 unsigned Indentation) const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000086 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Anders Carlssonf88df862009-09-26 21:58:53 +000087 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000088}
89
Eli Friedman42f42c02009-05-30 04:20:30 +000090static QualType GetBaseType(QualType T) {
91 // FIXME: This should be on the Type class!
92 QualType BaseType = T;
93 while (!BaseType->isSpecifierType()) {
94 if (isa<TypedefType>(BaseType))
95 break;
Ted Kremenek6217b802009-07-29 21:53:49 +000096 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +000097 BaseType = PTy->getPointeeType();
98 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
99 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +0000100 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000101 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000102 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000103 BaseType = VTy->getElementType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000104 else
105 assert(0 && "Unknown declarator!");
106 }
107 return BaseType;
108}
109
110static QualType getDeclType(Decl* D) {
111 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
112 return TDD->getUnderlyingType();
113 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
114 return VD->getType();
115 return QualType();
116}
117
118void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000119 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000120 unsigned Indentation) {
121 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000122 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000123 return;
124 }
125
126 Decl** End = Begin + NumDecls;
127 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
128 if (TD)
129 ++Begin;
130
131 PrintingPolicy SubPolicy(Policy);
132 if (TD && TD->isDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000133 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000134 Out << " ";
135 SubPolicy.SuppressTag = true;
136 }
137
138 bool isFirst = true;
139 for ( ; Begin != End; ++Begin) {
140 if (isFirst) {
141 SubPolicy.SuppressSpecifiers = false;
142 isFirst = false;
143 } else {
144 if (!isFirst) Out << ", ";
145 SubPolicy.SuppressSpecifiers = true;
146 }
147
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000148 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000149 }
150}
151
Anders Carlsson84834432009-12-14 00:51:04 +0000152void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000153 // Get the translation unit
154 const DeclContext *DC = this;
155 while (!DC->isTranslationUnit())
156 DC = DC->getParent();
157
158 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
159 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
160 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
161}
162
Anders Carlssonf88df862009-09-26 21:58:53 +0000163void Decl::dump() const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000164 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000165}
166
Daniel Dunbar512ce602009-11-21 09:12:06 +0000167llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
168 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000169 Out << " ";
170 return Out;
171}
172
Eli Friedman42f42c02009-05-30 04:20:30 +0000173void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
174 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000175 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000176 Out << ";\n";
177 Decls.clear();
178
179}
180
Anders Carlsson0d592922009-08-28 22:39:52 +0000181void DeclPrinter::Print(AccessSpecifier AS) {
182 switch(AS) {
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000183 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000184 case AS_public: Out << "public"; break;
185 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000186 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000187 }
188}
189
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000190//----------------------------------------------------------------------------
191// Common C declarations
192//----------------------------------------------------------------------------
193
194void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
195 if (Indent)
196 Indentation += Policy.Indentation;
197
Eli Friedman42f42c02009-05-30 04:20:30 +0000198 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000199 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000200 D != DEnd; ++D) {
Eli Friedman48d14a22009-05-30 05:03:24 +0000201 if (!Policy.Dump) {
202 // Skip over implicit declarations in pretty-printing mode.
203 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000204 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
205 // of __builtin_va_list. There should be some other way to check that.
206 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
207 "__builtin_va_list")
208 continue;
Eli Friedman48d14a22009-05-30 05:03:24 +0000209 }
210
Eli Friedman42f42c02009-05-30 04:20:30 +0000211 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
212 // forced to merge the declarations because there's no other way to
213 // refer to the struct in question. This limited merging is safe without
214 // a bunch of other checks because it only merges declarations directly
215 // referring to the tag, not typedefs.
216 //
217 // Check whether the current declaration should be grouped with a previous
218 // unnamed struct.
219 QualType CurDeclType = getDeclType(*D);
220 if (!Decls.empty() && !CurDeclType.isNull()) {
221 QualType BaseType = GetBaseType(CurDeclType);
222 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
223 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
224 Decls.push_back(*D);
225 continue;
226 }
227 }
228
229 // If we have a merged group waiting to be handled, handle it now.
230 if (!Decls.empty())
231 ProcessDeclGroup(Decls);
232
233 // If the current declaration is an unnamed tag type, save it
234 // so we can merge it with the subsequent declaration(s) using it.
235 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
236 Decls.push_back(*D);
237 continue;
238 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000239
240 if (isa<AccessSpecDecl>(*D)) {
241 Indentation -= Policy.Indentation;
242 this->Indent();
243 Print(D->getAccess());
244 Out << ":\n";
245 Indentation += Policy.Indentation;
246 continue;
247 }
248
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000249 this->Indent();
250 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
252 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000253 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000254 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000255 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
256 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000257 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
258 Terminator = 0;
259 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000260 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000261 isa<ObjCInterfaceDecl>(*D) ||
262 isa<ObjCProtocolDecl>(*D) ||
263 isa<ObjCCategoryImplDecl>(*D) ||
264 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000265 Terminator = 0;
266 else if (isa<EnumConstantDecl>(*D)) {
267 DeclContext::decl_iterator Next = D;
268 ++Next;
269 if (Next != DEnd)
270 Terminator = ",";
271 } else
272 Terminator = ";";
273
274 if (Terminator)
275 Out << Terminator;
276 Out << "\n";
277 }
278
Eli Friedman42f42c02009-05-30 04:20:30 +0000279 if (!Decls.empty())
280 ProcessDeclGroup(Decls);
281
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000282 if (Indent)
283 Indentation -= Policy.Indentation;
284}
285
286void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
287 VisitDeclContext(D, false);
288}
289
290void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
291 std::string S = D->getNameAsString();
292 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +0000293 if (!Policy.SuppressSpecifiers)
294 Out << "typedef ";
295 Out << S;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000296}
297
298void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000299 Out << "enum " << D << " {\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000300 VisitDeclContext(D);
301 Indent() << "}";
302}
303
304void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000305 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000306 if (D->getIdentifier())
307 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000309 if (D->isDefinition()) {
310 Out << " {\n";
311 VisitDeclContext(D);
312 Indent() << "}";
313 }
314}
315
316void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000317 Out << D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000318 if (Expr *Init = D->getInitExpr()) {
319 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000320 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000321 }
322}
323
Mike Stump1eb44332009-09-09 15:08:12 +0000324void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000325 if (!Policy.SuppressSpecifiers) {
326 switch (D->getStorageClass()) {
327 case FunctionDecl::None: break;
328 case FunctionDecl::Extern: Out << "extern "; break;
329 case FunctionDecl::Static: Out << "static "; break;
330 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
331 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000332
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000333 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000334 if (D->isVirtualAsWritten()) Out << "virtual ";
335 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000336
Douglas Gregor6620a622009-05-30 05:39:39 +0000337 PrintingPolicy SubPolicy(Policy);
338 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000339 std::string Proto = D->getNameAsString();
340 if (isa<FunctionType>(D->getType().getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +0000341 const FunctionType *AFT = D->getType()->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000342
343 const FunctionProtoType *FT = 0;
344 if (D->hasWrittenPrototype())
345 FT = dyn_cast<FunctionProtoType>(AFT);
346
347 Proto += "(";
348 if (FT) {
349 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000350 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000351 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
352 if (i) POut << ", ";
353 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000356 if (FT->isVariadic()) {
357 if (D->getNumParams()) POut << ", ";
358 POut << "...";
359 }
Eli Friedman42f42c02009-05-30 04:20:30 +0000360 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
361 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
362 if (i)
363 Proto += ", ";
364 Proto += D->getParamDecl(i)->getNameAsString();
365 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000366 }
367
368 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000369
370 if (FT && FT->hasExceptionSpec()) {
371 Proto += " throw(";
372 if (FT->hasAnyExceptionSpec())
373 Proto += "...";
374 else
375 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
376 if (I)
377 Proto += ", ";
378
379
380 std::string ExceptionType;
381 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
382 Proto += ExceptionType;
383 }
384 Proto += ")";
385 }
386
Mike Stumpfd350b52009-07-27 21:33:40 +0000387 if (D->hasAttr<NoReturnAttr>())
388 Proto += " __attribute((noreturn))";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000389 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
390 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
391 Proto += " : ";
392 Out << Proto;
393 Proto.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000394 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000395 E = CDecl->init_end();
396 B != E; ++B) {
397 CXXBaseOrMemberInitializer * BMInitializer = (*B);
398 if (B != CDecl->init_begin())
399 Out << ", ";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000400 if (BMInitializer->isMemberInitializer()) {
401 FieldDecl *FD = BMInitializer->getMember();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000402 Out << FD;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000403 } else {
404 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString();
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000405 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000406
407 Out << "(";
408 if (!BMInitializer->getInit()) {
409 // Nothing to print
410 } else {
411 Expr *Init = BMInitializer->getInit();
412 if (CXXExprWithTemporaries *Tmp
413 = dyn_cast<CXXExprWithTemporaries>(Init))
414 Init = Tmp->getSubExpr();
415
416 Init = Init->IgnoreParens();
417
418 Expr *SimpleInit = 0;
419 Expr **Args = 0;
420 unsigned NumArgs = 0;
421 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
422 Args = ParenList->getExprs();
423 NumArgs = ParenList->getNumExprs();
424 } else if (CXXConstructExpr *Construct
425 = dyn_cast<CXXConstructExpr>(Init)) {
426 Args = Construct->getArgs();
427 NumArgs = Construct->getNumArgs();
428 } else
429 SimpleInit = Init;
430
431 if (SimpleInit)
432 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
433 else {
434 for (unsigned I = 0; I != NumArgs; ++I) {
435 if (isa<CXXDefaultArgExpr>(Args[I]))
436 break;
437
438 if (I)
439 Out << ", ";
440 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
441 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000442 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000443 }
444 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000445 }
446 }
447 }
448 else
449 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000450 } else {
451 D->getType().getAsStringInternal(Proto, Policy);
452 }
453
454 Out << Proto;
455
456 if (D->isPure())
457 Out << " = 0";
458 else if (D->isDeleted())
459 Out << " = delete";
460 else if (D->isThisDeclarationADefinition()) {
461 if (!D->hasPrototype() && D->getNumParams()) {
462 // This is a K&R function definition, so we need to print the
463 // parameters.
464 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000465 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000466 Indentation += Policy.Indentation;
467 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
468 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000469 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000470 Out << ";\n";
471 }
472 Indentation -= Policy.Indentation;
473 } else
474 Out << ' ';
475
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000476 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000477 Out << '\n';
478 }
479}
480
481void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000482 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000483 Out << "mutable ";
484
485 std::string Name = D->getNameAsString();
486 D->getType().getAsStringInternal(Name, Policy);
487 Out << Name;
488
489 if (D->isBitField()) {
490 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000491 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000492 }
493}
494
495void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000496 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000497 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
498
Eli Friedman42f42c02009-05-30 04:20:30 +0000499 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000500 Out << "__thread ";
501
502 std::string Name = D->getNameAsString();
503 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000504 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000505 T = Parm->getOriginalType();
506 T.getAsStringInternal(Name, Policy);
507 Out << Name;
508 if (D->getInit()) {
509 if (D->hasCXXDirectInitializer())
510 Out << "(";
511 else
512 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000513 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000514 if (D->hasCXXDirectInitializer())
515 Out << ")";
516 }
517}
518
519void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
520 VisitVarDecl(D);
521}
522
523void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
524 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000525 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000526 Out << ")";
527}
528
529//----------------------------------------------------------------------------
530// C++ declarations
531//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000532void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000533 Out << "namespace " << D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000534 VisitDeclContext(D);
535 Indent() << "}";
536}
537
Douglas Gregor8419fa32009-05-30 06:31:56 +0000538void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
539 Out << "using namespace ";
540 if (D->getQualifier())
541 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000542 Out << D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000543}
544
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000545void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000546 Out << "namespace " << D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000547 if (D->getQualifier())
548 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000549 Out << D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000550}
551
Douglas Gregor59e63572009-05-30 06:58:37 +0000552void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
553 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000554 if (D->getIdentifier())
555 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor59e63572009-05-30 06:58:37 +0000557 if (D->isDefinition()) {
558 // Print the base classes
559 if (D->getNumBases()) {
560 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000561 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
562 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000563 if (Base != D->bases_begin())
564 Out << ", ";
565
566 if (Base->isVirtual())
567 Out << "virtual ";
568
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000569 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
570 if (AS != AS_none)
571 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000572 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor59e63572009-05-30 06:58:37 +0000573 }
574 }
575
576 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000577 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000578 Out << " {\n";
579 VisitDeclContext(D);
580 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000581 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000582}
583
584void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
585 const char *l;
586 if (D->getLanguage() == LinkageSpecDecl::lang_c)
587 l = "C";
588 else {
589 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
590 "unknown language in linkage specification");
591 l = "C++";
592 }
593
594 Out << "extern \"" << l << "\" ";
595 if (D->hasBraces()) {
596 Out << "{\n";
597 VisitDeclContext(D);
598 Indent() << "}";
599 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000600 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000601}
602
603void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000604 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Anders Carlsson0487f662009-06-04 05:37:43 +0000606 TemplateParameterList *Params = D->getTemplateParameters();
607 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
608 if (i != 0)
609 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Anders Carlsson0487f662009-06-04 05:37:43 +0000611 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000612 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000613 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000614
615 QualType ParamType =
Anders Carlsson0487f662009-06-04 05:37:43 +0000616 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
617
618 if (TTP->wasDeclaredWithTypename())
619 Out << "typename ";
620 else
621 Out << "class ";
622
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000623 if (TTP->isParameterPack())
624 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Douglas Gregorefed5c82010-06-16 15:23:05 +0000626 Out << ParamType.getAsString(Policy);
Anders Carlsson0487f662009-06-04 05:37:43 +0000627
628 if (TTP->hasDefaultArgument()) {
629 Out << " = ";
630 Out << TTP->getDefaultArgument().getAsString(Policy);
631 };
Mike Stump1eb44332009-09-09 15:08:12 +0000632 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000633 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
634 Out << NTTP->getType().getAsString(Policy);
635
636 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
637 Out << ' ';
638 Out << Name->getName();
639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Anders Carlsson0487f662009-06-04 05:37:43 +0000641 if (NTTP->hasDefaultArgument()) {
642 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000643 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000644 Indentation);
645 }
646 }
647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Anders Carlsson0487f662009-06-04 05:37:43 +0000649 Out << "> ";
650
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000651 Visit(D->getTemplatedDecl());
652}
653
654//----------------------------------------------------------------------------
655// Objective-C declarations
656//----------------------------------------------------------------------------
657
658void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
659 Out << "@class ";
660 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
661 I != E; ++I) {
662 if (I != D->begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000663 Out << I->getInterface();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000664 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000665}
666
667void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
668 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000669 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000670 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000671 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000672 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000673 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000675 std::string name = OMD->getSelector().getAsString();
676 std::string::size_type pos, lastPos = 0;
677 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
678 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000679 // FIXME: selector is missing here!
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000680 pos = name.find_first_of(":", lastPos);
681 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000682 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000683 lastPos = pos + 1;
684 }
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000686 if (OMD->param_begin() == OMD->param_end())
687 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000689 if (OMD->isVariadic())
690 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000692 if (OMD->getBody()) {
693 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000694 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000695 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000696 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000697}
698
699void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
700 std::string I = OID->getNameAsString();
701 ObjCInterfaceDecl *SID = OID->getSuperClass();
702
703 if (SID)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000704 Out << "@implementation " << I << " : " << SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000705 else
706 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000707 Out << "\n";
708 VisitDeclContext(OID, false);
709 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000710}
711
712void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
713 std::string I = OID->getNameAsString();
714 ObjCInterfaceDecl *SID = OID->getSuperClass();
715
716 if (SID)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000717 Out << "@interface " << I << " : " << SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000718 else
719 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000721 // Protocols?
722 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
723 if (!Protocols.empty()) {
724 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
725 E = Protocols.end(); I != E; ++I)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000726 Out << (I == Protocols.begin() ? '<' : ',') << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000727 }
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000729 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000730 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000732 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000733 Out << "{\n";
734 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000735 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
736 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000737 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000738 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000739 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000740 Out << "}\n";
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000743 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000744 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000745 // FIXME: implement the rest...
746}
747
748void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
749 Out << "@protocol ";
Mike Stump1eb44332009-09-09 15:08:12 +0000750 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000751 E = D->protocol_end();
752 I != E; ++I) {
753 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000754 Out << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000755 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000756}
757
758void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000759 Out << "@protocol " << PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000760 VisitDeclContext(PID, false);
761 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000762}
763
764void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000765 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000766
767 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000768 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000769 // FIXME: implement the rest...
770}
771
772void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000773 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000774 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000775 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000777 // FIXME: implement the rest...
778}
779
780void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000781 Out << "@compatibility_alias " << AID
782 << ' ' << AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000783}
784
785/// PrintObjCPropertyDecl - print a property declaration.
786///
787void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
788 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
789 Out << "@required\n";
790 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
791 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000793 Out << "@property";
794 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
795 bool first = true;
796 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000797 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000798 ObjCPropertyDecl::OBJC_PR_readonly) {
799 Out << (first ? ' ' : ',') << "readonly";
800 first = false;
801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000803 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
804 Out << (first ? ' ' : ',') << "getter = "
805 << PDecl->getGetterName().getAsString();
806 first = false;
807 }
808 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
809 Out << (first ? ' ' : ',') << "setter = "
810 << PDecl->getSetterName().getAsString();
811 first = false;
812 }
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000814 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
815 Out << (first ? ' ' : ',') << "assign";
816 first = false;
817 }
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000819 if (PDecl->getPropertyAttributes() &
820 ObjCPropertyDecl::OBJC_PR_readwrite) {
821 Out << (first ? ' ' : ',') << "readwrite";
822 first = false;
823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000825 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
826 Out << (first ? ' ' : ',') << "retain";
827 first = false;
828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000830 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
831 Out << (first ? ' ' : ',') << "copy";
832 first = false;
833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
835 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000836 ObjCPropertyDecl::OBJC_PR_nonatomic) {
837 Out << (first ? ' ' : ',') << "nonatomic";
838 first = false;
839 }
840 Out << " )";
841 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000842 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000843}
844
845void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
846 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000847 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000848 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000849 Out << "@dynamic ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000850 Out << PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000851 if (PID->getPropertyIvarDecl())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000852 Out << '=' << PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000853}
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000854
855void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
856 Out << "using ";
857 D->getTargetNestedNameDecl()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000858 Out << D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000859}
860
John McCall7ba107a2009-11-18 02:36:19 +0000861void
862DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
863 Out << "using typename ";
864 D->getTargetNestedNameSpecifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000865 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +0000866}
867
868void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000869 Out << "using ";
870 D->getTargetNestedNameSpecifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000871 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000872}
John McCall9488ea12009-11-17 05:59:44 +0000873
874void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
875 // ignore
876}