blob: f448144d2821d678be452098aff7678ebc771409 [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);
Douglas Gregor8419fa32009-05-30 06:31:56 +000055 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000056 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000057 void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000058 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000059 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000060 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000061 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000062 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
63 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000064 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000065 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000066 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
67 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
68 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
69 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
70 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
71 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
72 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
73 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
74 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000075 void VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
76 void VisitUsingDecl(UsingDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000077 };
78}
79
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000080void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) {
81 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,
85 unsigned Indentation) {
86 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000087 Printer.Visit(this);
88}
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
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000152void Decl::dump() {
153 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000154}
155
156llvm::raw_ostream& DeclPrinter::Indent() {
157 for (unsigned i = 0; i < Indentation; ++i)
158 Out << " ";
159 return Out;
160}
161
Eli Friedman42f42c02009-05-30 04:20:30 +0000162void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
163 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000164 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000165 Out << ";\n";
166 Decls.clear();
167
168}
169
Anders Carlsson0d592922009-08-28 22:39:52 +0000170void DeclPrinter::Print(AccessSpecifier AS) {
171 switch(AS) {
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000172 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000173 case AS_public: Out << "public"; break;
174 case AS_protected: Out << "protected"; break;
175 case AS_private: Out << " private"; break;
176 }
177}
178
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000179//----------------------------------------------------------------------------
180// Common C declarations
181//----------------------------------------------------------------------------
182
183void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
184 if (Indent)
185 Indentation += Policy.Indentation;
186
Anders Carlsson0d592922009-08-28 22:39:52 +0000187 bool PrintAccess = isa<CXXRecordDecl>(DC);
188 AccessSpecifier CurAS = AS_none;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Eli Friedman42f42c02009-05-30 04:20:30 +0000190 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000191 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000192 D != DEnd; ++D) {
Eli Friedman48d14a22009-05-30 05:03:24 +0000193 if (!Policy.Dump) {
194 // Skip over implicit declarations in pretty-printing mode.
195 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000196 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
197 // of __builtin_va_list. There should be some other way to check that.
198 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
199 "__builtin_va_list")
200 continue;
Eli Friedman48d14a22009-05-30 05:03:24 +0000201 }
202
Anders Carlsson0d592922009-08-28 22:39:52 +0000203 if (PrintAccess) {
204 AccessSpecifier AS = D->getAccess();
Anders Carlsson35eda442009-08-29 20:47:47 +0000205
John McCalld7eff682009-09-02 00:55:30 +0000206 if (AS != CurAS) {
Anders Carlsson0d592922009-08-28 22:39:52 +0000207 Print(AS);
208 Out << ":\n";
209 CurAS = AS;
210 }
211 }
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Eli Friedman42f42c02009-05-30 04:20:30 +0000213 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
214 // forced to merge the declarations because there's no other way to
215 // refer to the struct in question. This limited merging is safe without
216 // a bunch of other checks because it only merges declarations directly
217 // referring to the tag, not typedefs.
218 //
219 // Check whether the current declaration should be grouped with a previous
220 // unnamed struct.
221 QualType CurDeclType = getDeclType(*D);
222 if (!Decls.empty() && !CurDeclType.isNull()) {
223 QualType BaseType = GetBaseType(CurDeclType);
224 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
225 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
226 Decls.push_back(*D);
227 continue;
228 }
229 }
230
231 // If we have a merged group waiting to be handled, handle it now.
232 if (!Decls.empty())
233 ProcessDeclGroup(Decls);
234
235 // If the current declaration is an unnamed tag type, save it
236 // so we can merge it with the subsequent declaration(s) using it.
237 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
238 Decls.push_back(*D);
239 continue;
240 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000241 this->Indent();
242 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
244 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000245 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000246 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000247 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
248 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000249 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
250 Terminator = 0;
251 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000252 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000253 isa<ObjCInterfaceDecl>(*D) ||
254 isa<ObjCProtocolDecl>(*D) ||
255 isa<ObjCCategoryImplDecl>(*D) ||
256 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000257 Terminator = 0;
258 else if (isa<EnumConstantDecl>(*D)) {
259 DeclContext::decl_iterator Next = D;
260 ++Next;
261 if (Next != DEnd)
262 Terminator = ",";
263 } else
264 Terminator = ";";
265
266 if (Terminator)
267 Out << Terminator;
268 Out << "\n";
269 }
270
Eli Friedman42f42c02009-05-30 04:20:30 +0000271 if (!Decls.empty())
272 ProcessDeclGroup(Decls);
273
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000274 if (Indent)
275 Indentation -= Policy.Indentation;
276}
277
278void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
279 VisitDeclContext(D, false);
280}
281
282void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
283 std::string S = D->getNameAsString();
284 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +0000285 if (!Policy.SuppressSpecifiers)
286 Out << "typedef ";
287 Out << S;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000288}
289
290void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
291 Out << "enum " << D->getNameAsString() << " {\n";
292 VisitDeclContext(D);
293 Indent() << "}";
294}
295
296void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000297 Out << D->getKindName();
Eli Friedman42f42c02009-05-30 04:20:30 +0000298 if (D->getIdentifier()) {
299 Out << " ";
300 Out << D->getNameAsString();
301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000303 if (D->isDefinition()) {
304 Out << " {\n";
305 VisitDeclContext(D);
306 Indent() << "}";
307 }
308}
309
310void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
311 Out << D->getNameAsString();
312 if (Expr *Init = D->getInitExpr()) {
313 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000314 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000315 }
316}
317
Mike Stump1eb44332009-09-09 15:08:12 +0000318void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000319 if (!Policy.SuppressSpecifiers) {
320 switch (D->getStorageClass()) {
321 case FunctionDecl::None: break;
322 case FunctionDecl::Extern: Out << "extern "; break;
323 case FunctionDecl::Static: Out << "static "; break;
324 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
325 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000326
Eli Friedman42f42c02009-05-30 04:20:30 +0000327 if (D->isInline()) Out << "inline ";
328 if (D->isVirtualAsWritten()) Out << "virtual ";
329 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000330
Douglas Gregor6620a622009-05-30 05:39:39 +0000331 PrintingPolicy SubPolicy(Policy);
332 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000333 std::string Proto = D->getNameAsString();
334 if (isa<FunctionType>(D->getType().getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +0000335 const FunctionType *AFT = D->getType()->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000336
337 const FunctionProtoType *FT = 0;
338 if (D->hasWrittenPrototype())
339 FT = dyn_cast<FunctionProtoType>(AFT);
340
341 Proto += "(";
342 if (FT) {
343 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000344 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000345 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
346 if (i) POut << ", ";
347 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000350 if (FT->isVariadic()) {
351 if (D->getNumParams()) POut << ", ";
352 POut << "...";
353 }
Eli Friedman42f42c02009-05-30 04:20:30 +0000354 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
355 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
356 if (i)
357 Proto += ", ";
358 Proto += D->getParamDecl(i)->getNameAsString();
359 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000360 }
361
362 Proto += ")";
Mike Stumpfd350b52009-07-27 21:33:40 +0000363 if (D->hasAttr<NoReturnAttr>())
364 Proto += " __attribute((noreturn))";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000365 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
366 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
367 Proto += " : ";
368 Out << Proto;
369 Proto.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000370 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000371 E = CDecl->init_end();
372 B != E; ++B) {
373 CXXBaseOrMemberInitializer * BMInitializer = (*B);
374 if (B != CDecl->init_begin())
375 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000376 bool hasArguments = (BMInitializer->arg_begin() !=
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000377 BMInitializer->arg_end());
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000378 if (BMInitializer->isMemberInitializer()) {
379 FieldDecl *FD = BMInitializer->getMember();
380 Out << FD->getNameAsString();
381 }
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000382 else // FIXME. skip dependent types for now.
Mike Stump1eb44332009-09-09 15:08:12 +0000383 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000384 BMInitializer->getBaseClass()->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000385 const CXXRecordDecl *BaseDecl =
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000386 cast<CXXRecordDecl>(RT->getDecl());
387 Out << BaseDecl->getNameAsString();
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000388 }
389 if (hasArguments) {
390 Out << "(";
Mike Stump1eb44332009-09-09 15:08:12 +0000391 for (CXXBaseOrMemberInitializer::const_arg_iterator BE =
392 BMInitializer->const_arg_begin(),
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000393 EE = BMInitializer->const_arg_end(); BE != EE; ++BE) {
394 if (BE != BMInitializer->const_arg_begin())
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000395 Out<< ", ";
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000396 const Expr *Exp = (*BE);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000397 Exp->printPretty(Out, Context, 0, Policy, Indentation);
398 }
399 Out << ")";
Fariborz Jahanian939afd82009-07-13 23:31:10 +0000400 } else
401 Out << "()";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000402 }
403 }
404 }
Fariborz Jahanian560de452009-07-15 22:34:08 +0000405 else if (CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(D)) {
406 if (DDecl->getNumBaseOrMemberDestructions() > 0) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000407 // List order of base/member destruction for visualization purposes.
Fariborz Jahanian560de452009-07-15 22:34:08 +0000408 assert (D->isThisDeclarationADefinition() && "Destructor with dtor-list");
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000409 Proto += "/* : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000410 for (CXXDestructorDecl::destr_const_iterator *B = DDecl->destr_begin(),
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000411 *E = DDecl->destr_end();
Fariborz Jahanian560de452009-07-15 22:34:08 +0000412 B != E; ++B) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000413 uintptr_t BaseOrMember = (*B);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000414 if (B != DDecl->destr_begin())
415 Proto += ", ";
416
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000417 if (DDecl->isMemberToDestroy(BaseOrMember)) {
418 FieldDecl *FD = DDecl->getMemberToDestroy(BaseOrMember);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000419 Proto += "~";
420 Proto += FD->getNameAsString();
421 }
422 else // FIXME. skip dependent types for now.
Mike Stump1eb44332009-09-09 15:08:12 +0000423 if (const RecordType *RT =
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000424 DDecl->getAnyBaseClassToDestroy(BaseOrMember)
Ted Kremenek6217b802009-07-29 21:53:49 +0000425 ->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000426 const CXXRecordDecl *BaseDecl =
Fariborz Jahanian560de452009-07-15 22:34:08 +0000427 cast<CXXRecordDecl>(RT->getDecl());
428 Proto += "~";
429 Proto += BaseDecl->getNameAsString();
430 }
431 Proto += "()";
432 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000433 Proto += " */";
Fariborz Jahanian560de452009-07-15 22:34:08 +0000434 }
435 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000436 else
437 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000438 } else {
439 D->getType().getAsStringInternal(Proto, Policy);
440 }
441
442 Out << Proto;
443
444 if (D->isPure())
445 Out << " = 0";
446 else if (D->isDeleted())
447 Out << " = delete";
448 else if (D->isThisDeclarationADefinition()) {
449 if (!D->hasPrototype() && D->getNumParams()) {
450 // This is a K&R function definition, so we need to print the
451 // parameters.
452 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000453 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000454 Indentation += Policy.Indentation;
455 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
456 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000457 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000458 Out << ";\n";
459 }
460 Indentation -= Policy.Indentation;
461 } else
462 Out << ' ';
463
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000464 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000465 Out << '\n';
466 }
467}
468
469void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000470 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000471 Out << "mutable ";
472
473 std::string Name = D->getNameAsString();
474 D->getType().getAsStringInternal(Name, Policy);
475 Out << Name;
476
477 if (D->isBitField()) {
478 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000479 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000480 }
481}
482
483void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000484 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000485 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
486
Eli Friedman42f42c02009-05-30 04:20:30 +0000487 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000488 Out << "__thread ";
489
490 std::string Name = D->getNameAsString();
491 QualType T = D->getType();
492 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
493 T = Parm->getOriginalType();
494 T.getAsStringInternal(Name, Policy);
495 Out << Name;
496 if (D->getInit()) {
497 if (D->hasCXXDirectInitializer())
498 Out << "(";
499 else
500 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000501 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000502 if (D->hasCXXDirectInitializer())
503 Out << ")";
504 }
505}
506
507void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
508 VisitVarDecl(D);
509}
510
Douglas Gregor8419fa32009-05-30 06:31:56 +0000511void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
512 VisitVarDecl(D);
513}
514
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000515void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
516 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000517 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000518 Out << ")";
519}
520
521//----------------------------------------------------------------------------
522// C++ declarations
523//----------------------------------------------------------------------------
Douglas Gregor8419fa32009-05-30 06:31:56 +0000524void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000525 assert(false &&
Douglas Gregor8419fa32009-05-30 06:31:56 +0000526 "OverloadedFunctionDecls aren't really decls and are never printed");
527}
528
Douglas Gregor59e63572009-05-30 06:58:37 +0000529void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
530 Out << "namespace " << D->getNameAsString() << " {\n";
531 VisitDeclContext(D);
532 Indent() << "}";
533}
534
Douglas Gregor8419fa32009-05-30 06:31:56 +0000535void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
536 Out << "using namespace ";
537 if (D->getQualifier())
538 D->getQualifier()->print(Out, Policy);
539 Out << D->getNominatedNamespace()->getNameAsString();
540}
541
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000542void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
543 Out << "namespace " << D->getNameAsString() << " = ";
544 if (D->getQualifier())
545 D->getQualifier()->print(Out, Policy);
546 Out << D->getAliasedNamespace()->getNameAsString();
547}
548
Douglas Gregor59e63572009-05-30 06:58:37 +0000549void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
550 Out << D->getKindName();
551 if (D->getIdentifier()) {
552 Out << " ";
553 Out << D->getNameAsString();
554 }
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Douglas Gregor59e63572009-05-30 06:58:37 +0000556 if (D->isDefinition()) {
557 // Print the base classes
558 if (D->getNumBases()) {
559 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000560 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
561 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000562 if (Base != D->bases_begin())
563 Out << ", ";
564
565 if (Base->isVirtual())
566 Out << "virtual ";
567
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000568 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
569 if (AS != AS_none)
570 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000571 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor59e63572009-05-30 06:58:37 +0000572 }
573 }
574
575 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000576 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000577 Out << " {\n";
578 VisitDeclContext(D);
579 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000580 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000581}
582
583void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
584 const char *l;
585 if (D->getLanguage() == LinkageSpecDecl::lang_c)
586 l = "C";
587 else {
588 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
589 "unknown language in linkage specification");
590 l = "C++";
591 }
592
593 Out << "extern \"" << l << "\" ";
594 if (D->hasBraces()) {
595 Out << "{\n";
596 VisitDeclContext(D);
597 Indent() << "}";
598 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000599 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000600}
601
602void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000603 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Anders Carlsson0487f662009-06-04 05:37:43 +0000605 TemplateParameterList *Params = D->getTemplateParameters();
606 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
607 if (i != 0)
608 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Anders Carlsson0487f662009-06-04 05:37:43 +0000610 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000611 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000612 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000613
614 QualType ParamType =
Anders Carlsson0487f662009-06-04 05:37:43 +0000615 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
616
617 if (TTP->wasDeclaredWithTypename())
618 Out << "typename ";
619 else
620 Out << "class ";
621
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000622 if (TTP->isParameterPack())
623 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Anders Carlsson0487f662009-06-04 05:37:43 +0000625 Out << ParamType.getAsString(Policy);
626
627 if (TTP->hasDefaultArgument()) {
628 Out << " = ";
629 Out << TTP->getDefaultArgument().getAsString(Policy);
630 };
Mike Stump1eb44332009-09-09 15:08:12 +0000631 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000632 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
633 Out << NTTP->getType().getAsString(Policy);
634
635 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
636 Out << ' ';
637 Out << Name->getName();
638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Anders Carlsson0487f662009-06-04 05:37:43 +0000640 if (NTTP->hasDefaultArgument()) {
641 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000642 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000643 Indentation);
644 }
645 }
646 }
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Anders Carlsson0487f662009-06-04 05:37:43 +0000648 Out << "> ";
649
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000650 Visit(D->getTemplatedDecl());
651}
652
653//----------------------------------------------------------------------------
654// Objective-C declarations
655//----------------------------------------------------------------------------
656
657void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
658 Out << "@class ";
659 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
660 I != E; ++I) {
661 if (I != D->begin()) Out << ", ";
662 Out << (*I)->getNameAsString();
663 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000664}
665
666void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
667 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000668 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000669 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000670 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000671 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000672 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000674 std::string name = OMD->getSelector().getAsString();
675 std::string::size_type pos, lastPos = 0;
676 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
677 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000678 // FIXME: selector is missing here!
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000679 pos = name.find_first_of(":", lastPos);
680 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor59e63572009-05-30 06:58:37 +0000681 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Mike Stump1eb44332009-09-09 15:08:12 +0000682 << (*PI)->getNameAsString();
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)
704 Out << "@implementation " << I << " : " << SID->getNameAsString();
705 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)
717 Out << "@interface " << I << " : " << SID->getNameAsString();
718 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)
726 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
727 }
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) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000737 Indent() << (*I)->getType().getAsString(Policy)
Mike Stump1eb44332009-09-09 15:08:12 +0000738 << ' ' << (*I)->getNameAsString() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000739 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000740 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000741 Out << "}\n";
742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000744 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000745 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000746 // FIXME: implement the rest...
747}
748
749void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
750 Out << "@protocol ";
Mike Stump1eb44332009-09-09 15:08:12 +0000751 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000752 E = D->protocol_end();
753 I != E; ++I) {
754 if (I != D->protocol_begin()) Out << ", ";
755 Out << (*I)->getNameAsString();
756 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000757}
758
759void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
760 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000761 VisitDeclContext(PID, false);
762 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000763}
764
765void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
766 Out << "@implementation "
767 << PID->getClassInterface()->getNameAsString()
Mike Stump1eb44332009-09-09 15:08:12 +0000768 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000769
770 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000771 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000772 // FIXME: implement the rest...
773}
774
775void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Mike Stump1eb44332009-09-09 15:08:12 +0000776 Out << "@interface "
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000777 << PID->getClassInterface()->getNameAsString()
Douglas Gregor64f65002009-05-30 00:56:08 +0000778 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000779 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000780 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000782 // FIXME: implement the rest...
783}
784
785void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Mike Stump1eb44332009-09-09 15:08:12 +0000786 Out << "@compatibility_alias " << AID->getNameAsString()
787 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000788}
789
790/// PrintObjCPropertyDecl - print a property declaration.
791///
792void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
793 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
794 Out << "@required\n";
795 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
796 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000798 Out << "@property";
799 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
800 bool first = true;
801 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000802 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000803 ObjCPropertyDecl::OBJC_PR_readonly) {
804 Out << (first ? ' ' : ',') << "readonly";
805 first = false;
806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000808 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
809 Out << (first ? ' ' : ',') << "getter = "
810 << PDecl->getGetterName().getAsString();
811 first = false;
812 }
813 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
814 Out << (first ? ' ' : ',') << "setter = "
815 << PDecl->getSetterName().getAsString();
816 first = false;
817 }
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000819 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
820 Out << (first ? ' ' : ',') << "assign";
821 first = false;
822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000824 if (PDecl->getPropertyAttributes() &
825 ObjCPropertyDecl::OBJC_PR_readwrite) {
826 Out << (first ? ' ' : ',') << "readwrite";
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_retain) {
831 Out << (first ? ' ' : ',') << "retain";
832 first = false;
833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000835 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
836 Out << (first ? ' ' : ',') << "copy";
837 first = false;
838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
840 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000841 ObjCPropertyDecl::OBJC_PR_nonatomic) {
842 Out << (first ? ' ' : ',') << "nonatomic";
843 first = false;
844 }
845 Out << " )";
846 }
847 Out << ' ' << PDecl->getType().getAsString(Policy)
848 << ' ' << PDecl->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000849}
850
851void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
852 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000853 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000854 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000855 Out << "@dynamic ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000856 Out << PID->getPropertyDecl()->getNameAsString();
857 if (PID->getPropertyIvarDecl())
858 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000859}
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000860
861void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
862 Out << "using ";
863 D->getTargetNestedNameDecl()->print(Out, Policy);
864 Out << D->getTargetDecl()->getNameAsString();
865}
866
867void DeclPrinter::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
868 Out << "using ";
869 D->getTargetNestedNameSpecifier()->print(Out, Policy);
870 Out << D->getTargetName().getAsString();
871}