blob: 7ef30c3bb34fd8eaec09ae9571dc2d0485d2072f [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"
20#include "clang/AST/PrettyPrinter.h"
21#include "llvm/Support/Compiler.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000022#include "llvm/Support/Format.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25
26namespace {
27 class VISIBILITY_HIDDEN DeclPrinter : public DeclVisitor<DeclPrinter> {
28 llvm::raw_ostream &Out;
29 ASTContext &Context;
30 PrintingPolicy Policy;
31 unsigned Indentation;
32
33 llvm::raw_ostream& Indent();
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 Gregor8419fa32009-05-30 06:31:56 +000056 void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000057 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000058 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000059 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000060 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000061 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
62 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000063 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000064 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000065 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
66 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
67 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
68 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
69 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
70 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
71 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
72 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
73 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000074 void VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
75 void VisitUsingDecl(UsingDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000076 };
77}
78
Anders Carlssonf88df862009-09-26 21:58:53 +000079void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000080 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000081}
82
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000083void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Anders Carlssonf88df862009-09-26 21:58:53 +000084 unsigned Indentation) const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000085 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Anders Carlssonf88df862009-09-26 21:58:53 +000086 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000087}
88
Eli Friedman42f42c02009-05-30 04:20:30 +000089static QualType GetBaseType(QualType T) {
90 // FIXME: This should be on the Type class!
91 QualType BaseType = T;
92 while (!BaseType->isSpecifierType()) {
93 if (isa<TypedefType>(BaseType))
94 break;
Ted Kremenek6217b802009-07-29 21:53:49 +000095 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +000096 BaseType = PTy->getPointeeType();
97 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
98 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +000099 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000100 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000101 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000102 BaseType = VTy->getElementType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000103 else
104 assert(0 && "Unknown declarator!");
105 }
106 return BaseType;
107}
108
109static QualType getDeclType(Decl* D) {
110 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
111 return TDD->getUnderlyingType();
112 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
113 return VD->getType();
114 return QualType();
115}
116
117void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000118 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000119 unsigned Indentation) {
120 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000121 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000122 return;
123 }
124
125 Decl** End = Begin + NumDecls;
126 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
127 if (TD)
128 ++Begin;
129
130 PrintingPolicy SubPolicy(Policy);
131 if (TD && TD->isDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000132 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000133 Out << " ";
134 SubPolicy.SuppressTag = true;
135 }
136
137 bool isFirst = true;
138 for ( ; Begin != End; ++Begin) {
139 if (isFirst) {
140 SubPolicy.SuppressSpecifiers = false;
141 isFirst = false;
142 } else {
143 if (!isFirst) Out << ", ";
144 SubPolicy.SuppressSpecifiers = true;
145 }
146
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000147 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000148 }
149}
150
Anders Carlssonf88df862009-09-26 21:58:53 +0000151void Decl::dump() const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000152 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000153}
154
155llvm::raw_ostream& DeclPrinter::Indent() {
156 for (unsigned i = 0; i < Indentation; ++i)
157 Out << " ";
158 return Out;
159}
160
Eli Friedman42f42c02009-05-30 04:20:30 +0000161void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
162 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000163 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000164 Out << ";\n";
165 Decls.clear();
166
167}
168
Anders Carlsson0d592922009-08-28 22:39:52 +0000169void DeclPrinter::Print(AccessSpecifier AS) {
170 switch(AS) {
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000171 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000172 case AS_public: Out << "public"; break;
173 case AS_protected: Out << "protected"; break;
174 case AS_private: Out << " private"; break;
175 }
176}
177
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000178//----------------------------------------------------------------------------
179// Common C declarations
180//----------------------------------------------------------------------------
181
182void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
183 if (Indent)
184 Indentation += Policy.Indentation;
185
Anders Carlsson0d592922009-08-28 22:39:52 +0000186 bool PrintAccess = isa<CXXRecordDecl>(DC);
187 AccessSpecifier CurAS = AS_none;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Eli Friedman42f42c02009-05-30 04:20:30 +0000189 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000190 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000191 D != DEnd; ++D) {
Eli Friedman48d14a22009-05-30 05:03:24 +0000192 if (!Policy.Dump) {
193 // Skip over implicit declarations in pretty-printing mode.
194 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000195 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
196 // of __builtin_va_list. There should be some other way to check that.
197 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
198 "__builtin_va_list")
199 continue;
Eli Friedman48d14a22009-05-30 05:03:24 +0000200 }
201
Anders Carlsson0d592922009-08-28 22:39:52 +0000202 if (PrintAccess) {
203 AccessSpecifier AS = D->getAccess();
Anders Carlsson35eda442009-08-29 20:47:47 +0000204
John McCalld7eff682009-09-02 00:55:30 +0000205 if (AS != CurAS) {
Anders Carlsson0d592922009-08-28 22:39:52 +0000206 Print(AS);
207 Out << ":\n";
208 CurAS = AS;
209 }
210 }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Eli Friedman42f42c02009-05-30 04:20:30 +0000212 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
213 // forced to merge the declarations because there's no other way to
214 // refer to the struct in question. This limited merging is safe without
215 // a bunch of other checks because it only merges declarations directly
216 // referring to the tag, not typedefs.
217 //
218 // Check whether the current declaration should be grouped with a previous
219 // unnamed struct.
220 QualType CurDeclType = getDeclType(*D);
221 if (!Decls.empty() && !CurDeclType.isNull()) {
222 QualType BaseType = GetBaseType(CurDeclType);
223 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
224 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
225 Decls.push_back(*D);
226 continue;
227 }
228 }
229
230 // If we have a merged group waiting to be handled, handle it now.
231 if (!Decls.empty())
232 ProcessDeclGroup(Decls);
233
234 // If the current declaration is an unnamed tag type, save it
235 // so we can merge it with the subsequent declaration(s) using it.
236 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
237 Decls.push_back(*D);
238 continue;
239 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000240 this->Indent();
241 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000242
243 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000244 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000245 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000246 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
247 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000248 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
249 Terminator = 0;
250 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000251 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000252 isa<ObjCInterfaceDecl>(*D) ||
253 isa<ObjCProtocolDecl>(*D) ||
254 isa<ObjCCategoryImplDecl>(*D) ||
255 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000256 Terminator = 0;
257 else if (isa<EnumConstantDecl>(*D)) {
258 DeclContext::decl_iterator Next = D;
259 ++Next;
260 if (Next != DEnd)
261 Terminator = ",";
262 } else
263 Terminator = ";";
264
265 if (Terminator)
266 Out << Terminator;
267 Out << "\n";
268 }
269
Eli Friedman42f42c02009-05-30 04:20:30 +0000270 if (!Decls.empty())
271 ProcessDeclGroup(Decls);
272
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000273 if (Indent)
274 Indentation -= Policy.Indentation;
275}
276
277void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
278 VisitDeclContext(D, false);
279}
280
281void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
282 std::string S = D->getNameAsString();
283 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +0000284 if (!Policy.SuppressSpecifiers)
285 Out << "typedef ";
286 Out << S;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000287}
288
289void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
290 Out << "enum " << D->getNameAsString() << " {\n";
291 VisitDeclContext(D);
292 Indent() << "}";
293}
294
295void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000296 Out << D->getKindName();
Eli Friedman42f42c02009-05-30 04:20:30 +0000297 if (D->getIdentifier()) {
298 Out << " ";
299 Out << D->getNameAsString();
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000302 if (D->isDefinition()) {
303 Out << " {\n";
304 VisitDeclContext(D);
305 Indent() << "}";
306 }
307}
308
309void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
310 Out << D->getNameAsString();
311 if (Expr *Init = D->getInitExpr()) {
312 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000313 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000314 }
315}
316
Mike Stump1eb44332009-09-09 15:08:12 +0000317void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000318 if (!Policy.SuppressSpecifiers) {
319 switch (D->getStorageClass()) {
320 case FunctionDecl::None: break;
321 case FunctionDecl::Extern: Out << "extern "; break;
322 case FunctionDecl::Static: Out << "static "; break;
323 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
324 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000325
Eli Friedman42f42c02009-05-30 04:20:30 +0000326 if (D->isInline()) Out << "inline ";
327 if (D->isVirtualAsWritten()) Out << "virtual ";
328 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000329
Douglas Gregor6620a622009-05-30 05:39:39 +0000330 PrintingPolicy SubPolicy(Policy);
331 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000332 std::string Proto = D->getNameAsString();
333 if (isa<FunctionType>(D->getType().getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +0000334 const FunctionType *AFT = D->getType()->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000335
336 const FunctionProtoType *FT = 0;
337 if (D->hasWrittenPrototype())
338 FT = dyn_cast<FunctionProtoType>(AFT);
339
340 Proto += "(";
341 if (FT) {
342 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000343 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000344 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
345 if (i) POut << ", ";
346 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000349 if (FT->isVariadic()) {
350 if (D->getNumParams()) POut << ", ";
351 POut << "...";
352 }
Eli Friedman42f42c02009-05-30 04:20:30 +0000353 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
354 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
355 if (i)
356 Proto += ", ";
357 Proto += D->getParamDecl(i)->getNameAsString();
358 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000359 }
360
361 Proto += ")";
Mike Stumpfd350b52009-07-27 21:33:40 +0000362 if (D->hasAttr<NoReturnAttr>())
363 Proto += " __attribute((noreturn))";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000364 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
365 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
366 Proto += " : ";
367 Out << Proto;
368 Proto.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000369 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000370 E = CDecl->init_end();
371 B != E; ++B) {
372 CXXBaseOrMemberInitializer * BMInitializer = (*B);
373 if (B != CDecl->init_begin())
374 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000375 bool hasArguments = (BMInitializer->arg_begin() !=
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000376 BMInitializer->arg_end());
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000377 if (BMInitializer->isMemberInitializer()) {
378 FieldDecl *FD = BMInitializer->getMember();
379 Out << FD->getNameAsString();
380 }
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000381 else // FIXME. skip dependent types for now.
Mike Stump1eb44332009-09-09 15:08:12 +0000382 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000383 BMInitializer->getBaseClass()->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000384 const CXXRecordDecl *BaseDecl =
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000385 cast<CXXRecordDecl>(RT->getDecl());
386 Out << BaseDecl->getNameAsString();
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000387 }
388 if (hasArguments) {
389 Out << "(";
Mike Stump1eb44332009-09-09 15:08:12 +0000390 for (CXXBaseOrMemberInitializer::const_arg_iterator BE =
391 BMInitializer->const_arg_begin(),
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000392 EE = BMInitializer->const_arg_end(); BE != EE; ++BE) {
393 if (BE != BMInitializer->const_arg_begin())
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000394 Out<< ", ";
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000395 const Expr *Exp = (*BE);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000396 Exp->printPretty(Out, Context, 0, Policy, Indentation);
397 }
398 Out << ")";
Fariborz Jahanian939afd82009-07-13 23:31:10 +0000399 } else
400 Out << "()";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000401 }
402 }
403 }
Fariborz Jahanian560de452009-07-15 22:34:08 +0000404 else if (CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(D)) {
405 if (DDecl->getNumBaseOrMemberDestructions() > 0) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000406 // List order of base/member destruction for visualization purposes.
Fariborz Jahanian560de452009-07-15 22:34:08 +0000407 assert (D->isThisDeclarationADefinition() && "Destructor with dtor-list");
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000408 Proto += "/* : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000409 for (CXXDestructorDecl::destr_const_iterator *B = DDecl->destr_begin(),
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000410 *E = DDecl->destr_end();
Fariborz Jahanian560de452009-07-15 22:34:08 +0000411 B != E; ++B) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000412 uintptr_t BaseOrMember = (*B);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000413 if (B != DDecl->destr_begin())
414 Proto += ", ";
415
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000416 if (DDecl->isMemberToDestroy(BaseOrMember)) {
417 FieldDecl *FD = DDecl->getMemberToDestroy(BaseOrMember);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000418 Proto += "~";
419 Proto += FD->getNameAsString();
420 }
421 else // FIXME. skip dependent types for now.
Mike Stump1eb44332009-09-09 15:08:12 +0000422 if (const RecordType *RT =
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000423 DDecl->getAnyBaseClassToDestroy(BaseOrMember)
Ted Kremenek6217b802009-07-29 21:53:49 +0000424 ->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000425 const CXXRecordDecl *BaseDecl =
Fariborz Jahanian560de452009-07-15 22:34:08 +0000426 cast<CXXRecordDecl>(RT->getDecl());
427 Proto += "~";
428 Proto += BaseDecl->getNameAsString();
429 }
430 Proto += "()";
431 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000432 Proto += " */";
Fariborz Jahanian560de452009-07-15 22:34:08 +0000433 }
434 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000435 else
436 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000437 } else {
438 D->getType().getAsStringInternal(Proto, Policy);
439 }
440
441 Out << Proto;
442
443 if (D->isPure())
444 Out << " = 0";
445 else if (D->isDeleted())
446 Out << " = delete";
447 else if (D->isThisDeclarationADefinition()) {
448 if (!D->hasPrototype() && D->getNumParams()) {
449 // This is a K&R function definition, so we need to print the
450 // parameters.
451 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000452 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000453 Indentation += Policy.Indentation;
454 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
455 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000456 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000457 Out << ";\n";
458 }
459 Indentation -= Policy.Indentation;
460 } else
461 Out << ' ';
462
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000463 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000464 Out << '\n';
465 }
466}
467
468void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000469 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000470 Out << "mutable ";
471
472 std::string Name = D->getNameAsString();
473 D->getType().getAsStringInternal(Name, Policy);
474 Out << Name;
475
476 if (D->isBitField()) {
477 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000478 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000479 }
480}
481
482void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000483 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000484 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
485
Eli Friedman42f42c02009-05-30 04:20:30 +0000486 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000487 Out << "__thread ";
488
489 std::string Name = D->getNameAsString();
490 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000491 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000492 T = Parm->getOriginalType();
493 T.getAsStringInternal(Name, Policy);
494 Out << Name;
495 if (D->getInit()) {
496 if (D->hasCXXDirectInitializer())
497 Out << "(";
498 else
499 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000500 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000501 if (D->hasCXXDirectInitializer())
502 Out << ")";
503 }
504}
505
506void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
507 VisitVarDecl(D);
508}
509
510void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
511 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000512 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000513 Out << ")";
514}
515
516//----------------------------------------------------------------------------
517// C++ declarations
518//----------------------------------------------------------------------------
Douglas Gregor8419fa32009-05-30 06:31:56 +0000519void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000520 assert(false &&
Douglas Gregor8419fa32009-05-30 06:31:56 +0000521 "OverloadedFunctionDecls aren't really decls and are never printed");
522}
523
Douglas Gregor59e63572009-05-30 06:58:37 +0000524void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
525 Out << "namespace " << D->getNameAsString() << " {\n";
526 VisitDeclContext(D);
527 Indent() << "}";
528}
529
Douglas Gregor8419fa32009-05-30 06:31:56 +0000530void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
531 Out << "using namespace ";
532 if (D->getQualifier())
533 D->getQualifier()->print(Out, Policy);
534 Out << D->getNominatedNamespace()->getNameAsString();
535}
536
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000537void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
538 Out << "namespace " << D->getNameAsString() << " = ";
539 if (D->getQualifier())
540 D->getQualifier()->print(Out, Policy);
541 Out << D->getAliasedNamespace()->getNameAsString();
542}
543
Douglas Gregor59e63572009-05-30 06:58:37 +0000544void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
545 Out << D->getKindName();
546 if (D->getIdentifier()) {
547 Out << " ";
548 Out << D->getNameAsString();
549 }
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Douglas Gregor59e63572009-05-30 06:58:37 +0000551 if (D->isDefinition()) {
552 // Print the base classes
553 if (D->getNumBases()) {
554 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000555 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
556 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000557 if (Base != D->bases_begin())
558 Out << ", ";
559
560 if (Base->isVirtual())
561 Out << "virtual ";
562
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000563 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
564 if (AS != AS_none)
565 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000566 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor59e63572009-05-30 06:58:37 +0000567 }
568 }
569
570 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000571 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000572 Out << " {\n";
573 VisitDeclContext(D);
574 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000575 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000576}
577
578void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
579 const char *l;
580 if (D->getLanguage() == LinkageSpecDecl::lang_c)
581 l = "C";
582 else {
583 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
584 "unknown language in linkage specification");
585 l = "C++";
586 }
587
588 Out << "extern \"" << l << "\" ";
589 if (D->hasBraces()) {
590 Out << "{\n";
591 VisitDeclContext(D);
592 Indent() << "}";
593 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000594 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000595}
596
597void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000598 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Anders Carlsson0487f662009-06-04 05:37:43 +0000600 TemplateParameterList *Params = D->getTemplateParameters();
601 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
602 if (i != 0)
603 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Anders Carlsson0487f662009-06-04 05:37:43 +0000605 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000606 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000607 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000608
609 QualType ParamType =
Anders Carlsson0487f662009-06-04 05:37:43 +0000610 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
611
612 if (TTP->wasDeclaredWithTypename())
613 Out << "typename ";
614 else
615 Out << "class ";
616
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000617 if (TTP->isParameterPack())
618 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Anders Carlsson0487f662009-06-04 05:37:43 +0000620 Out << ParamType.getAsString(Policy);
621
622 if (TTP->hasDefaultArgument()) {
623 Out << " = ";
624 Out << TTP->getDefaultArgument().getAsString(Policy);
625 };
Mike Stump1eb44332009-09-09 15:08:12 +0000626 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000627 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
628 Out << NTTP->getType().getAsString(Policy);
629
630 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
631 Out << ' ';
632 Out << Name->getName();
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Anders Carlsson0487f662009-06-04 05:37:43 +0000635 if (NTTP->hasDefaultArgument()) {
636 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000637 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000638 Indentation);
639 }
640 }
641 }
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Anders Carlsson0487f662009-06-04 05:37:43 +0000643 Out << "> ";
644
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000645 Visit(D->getTemplatedDecl());
646}
647
648//----------------------------------------------------------------------------
649// Objective-C declarations
650//----------------------------------------------------------------------------
651
652void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
653 Out << "@class ";
654 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
655 I != E; ++I) {
656 if (I != D->begin()) Out << ", ";
657 Out << (*I)->getNameAsString();
658 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000659}
660
661void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
662 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000663 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000664 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000665 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000666 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000667 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000669 std::string name = OMD->getSelector().getAsString();
670 std::string::size_type pos, lastPos = 0;
671 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
672 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000673 // FIXME: selector is missing here!
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000674 pos = name.find_first_of(":", lastPos);
675 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor59e63572009-05-30 06:58:37 +0000676 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Mike Stump1eb44332009-09-09 15:08:12 +0000677 << (*PI)->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000678 lastPos = pos + 1;
679 }
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000681 if (OMD->param_begin() == OMD->param_end())
682 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000684 if (OMD->isVariadic())
685 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000687 if (OMD->getBody()) {
688 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000689 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000690 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000691 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000692}
693
694void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
695 std::string I = OID->getNameAsString();
696 ObjCInterfaceDecl *SID = OID->getSuperClass();
697
698 if (SID)
699 Out << "@implementation " << I << " : " << SID->getNameAsString();
700 else
701 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000702 Out << "\n";
703 VisitDeclContext(OID, false);
704 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000705}
706
707void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
708 std::string I = OID->getNameAsString();
709 ObjCInterfaceDecl *SID = OID->getSuperClass();
710
711 if (SID)
712 Out << "@interface " << I << " : " << SID->getNameAsString();
713 else
714 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000716 // Protocols?
717 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
718 if (!Protocols.empty()) {
719 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
720 E = Protocols.end(); I != E; ++I)
721 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
722 }
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000724 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000725 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000727 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000728 Out << "{\n";
729 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000730 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
731 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000732 Indent() << (*I)->getType().getAsString(Policy)
Mike Stump1eb44332009-09-09 15:08:12 +0000733 << ' ' << (*I)->getNameAsString() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000734 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000735 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000736 Out << "}\n";
737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000739 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000740 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000741 // FIXME: implement the rest...
742}
743
744void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
745 Out << "@protocol ";
Mike Stump1eb44332009-09-09 15:08:12 +0000746 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000747 E = D->protocol_end();
748 I != E; ++I) {
749 if (I != D->protocol_begin()) Out << ", ";
750 Out << (*I)->getNameAsString();
751 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000752}
753
754void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
755 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000756 VisitDeclContext(PID, false);
757 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000758}
759
760void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
761 Out << "@implementation "
762 << PID->getClassInterface()->getNameAsString()
Mike Stump1eb44332009-09-09 15:08:12 +0000763 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000764
765 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000766 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000767 // FIXME: implement the rest...
768}
769
770void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Mike Stump1eb44332009-09-09 15:08:12 +0000771 Out << "@interface "
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000772 << PID->getClassInterface()->getNameAsString()
Douglas Gregor64f65002009-05-30 00:56:08 +0000773 << '(' << PID->getNameAsString() << ")\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) {
Mike Stump1eb44332009-09-09 15:08:12 +0000781 Out << "@compatibility_alias " << AID->getNameAsString()
782 << ' ' << AID->getClassInterface()->getNameAsString() << ";\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 }
842 Out << ' ' << PDecl->getType().getAsString(Policy)
843 << ' ' << PDecl->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000844}
845
846void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
847 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000848 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000849 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000850 Out << "@dynamic ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000851 Out << PID->getPropertyDecl()->getNameAsString();
852 if (PID->getPropertyIvarDecl())
853 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000854}
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000855
856void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
857 Out << "using ";
858 D->getTargetNestedNameDecl()->print(Out, Policy);
859 Out << D->getTargetDecl()->getNameAsString();
860}
861
862void DeclPrinter::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
863 Out << "using ";
864 D->getTargetNestedNameSpecifier()->print(Out, Policy);
865 Out << D->getTargetName().getAsString();
866}