blob: 32ac53d85be8687a5848096ae2165fb2b1fdee5d [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"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000021#include "llvm/Support/raw_ostream.h"
22using namespace clang;
23
24namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000025 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000026 llvm::raw_ostream &Out;
27 ASTContext &Context;
28 PrintingPolicy Policy;
29 unsigned Indentation;
30
Daniel Dunbar512ce602009-11-21 09:12:06 +000031 llvm::raw_ostream& Indent() { return Indent(Indentation); }
32 llvm::raw_ostream& Indent(unsigned Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +000033 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000034
Anders Carlsson0d592922009-08-28 22:39:52 +000035 void Print(AccessSpecifier AS);
Mike Stump1eb44332009-09-09 15:08:12 +000036
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000037 public:
Mike Stump1eb44332009-09-09 15:08:12 +000038 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000039 const PrintingPolicy &Policy,
40 unsigned Indentation = 0)
41 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
42
43 void VisitDeclContext(DeclContext *DC, bool Indent = true);
44
45 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
46 void VisitTypedefDecl(TypedefDecl *D);
47 void VisitEnumDecl(EnumDecl *D);
48 void VisitRecordDecl(RecordDecl *D);
49 void VisitEnumConstantDecl(EnumConstantDecl *D);
50 void VisitFunctionDecl(FunctionDecl *D);
51 void VisitFieldDecl(FieldDecl *D);
52 void VisitVarDecl(VarDecl *D);
53 void VisitParmVarDecl(ParmVarDecl *D);
54 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000055 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000056 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000057 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000058 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000059 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
60 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000061 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000062 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000063 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
64 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
65 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
66 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
67 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
68 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
69 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
70 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
71 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000072 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
73 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000074 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000075 void VisitUsingShadowDecl(UsingShadowDecl *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 Carlsson84834432009-12-14 00:51:04 +0000151void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000152 // Get the translation unit
153 const DeclContext *DC = this;
154 while (!DC->isTranslationUnit())
155 DC = DC->getParent();
156
157 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
158 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
159 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
160}
161
Anders Carlssonf88df862009-09-26 21:58:53 +0000162void Decl::dump() const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000163 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000164}
165
Daniel Dunbar512ce602009-11-21 09:12:06 +0000166llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
167 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000168 Out << " ";
169 return Out;
170}
171
Eli Friedman42f42c02009-05-30 04:20:30 +0000172void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
173 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000174 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000175 Out << ";\n";
176 Decls.clear();
177
178}
179
Anders Carlsson0d592922009-08-28 22:39:52 +0000180void DeclPrinter::Print(AccessSpecifier AS) {
181 switch(AS) {
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000182 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000183 case AS_public: Out << "public"; break;
184 case AS_protected: Out << "protected"; break;
185 case AS_private: Out << " private"; break;
186 }
187}
188
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000189//----------------------------------------------------------------------------
190// Common C declarations
191//----------------------------------------------------------------------------
192
193void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
194 if (Indent)
195 Indentation += Policy.Indentation;
196
Anders Carlsson0d592922009-08-28 22:39:52 +0000197 bool PrintAccess = isa<CXXRecordDecl>(DC);
198 AccessSpecifier CurAS = AS_none;
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Eli Friedman42f42c02009-05-30 04:20:30 +0000200 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000201 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000202 D != DEnd; ++D) {
Eli Friedman48d14a22009-05-30 05:03:24 +0000203 if (!Policy.Dump) {
204 // Skip over implicit declarations in pretty-printing mode.
205 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000206 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
207 // of __builtin_va_list. There should be some other way to check that.
208 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
209 "__builtin_va_list")
210 continue;
Eli Friedman48d14a22009-05-30 05:03:24 +0000211 }
212
Anders Carlsson0d592922009-08-28 22:39:52 +0000213 if (PrintAccess) {
214 AccessSpecifier AS = D->getAccess();
Anders Carlsson35eda442009-08-29 20:47:47 +0000215
John McCalld7eff682009-09-02 00:55:30 +0000216 if (AS != CurAS) {
Daniel Dunbar512ce602009-11-21 09:12:06 +0000217 if (Indent)
218 this->Indent(Indentation - Policy.Indentation);
Anders Carlsson0d592922009-08-28 22:39:52 +0000219 Print(AS);
220 Out << ":\n";
221 CurAS = AS;
222 }
223 }
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Eli Friedman42f42c02009-05-30 04:20:30 +0000225 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
226 // forced to merge the declarations because there's no other way to
227 // refer to the struct in question. This limited merging is safe without
228 // a bunch of other checks because it only merges declarations directly
229 // referring to the tag, not typedefs.
230 //
231 // Check whether the current declaration should be grouped with a previous
232 // unnamed struct.
233 QualType CurDeclType = getDeclType(*D);
234 if (!Decls.empty() && !CurDeclType.isNull()) {
235 QualType BaseType = GetBaseType(CurDeclType);
236 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
237 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
238 Decls.push_back(*D);
239 continue;
240 }
241 }
242
243 // If we have a merged group waiting to be handled, handle it now.
244 if (!Decls.empty())
245 ProcessDeclGroup(Decls);
246
247 // If the current declaration is an unnamed tag type, save it
248 // so we can merge it with the subsequent declaration(s) using it.
249 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
250 Decls.push_back(*D);
251 continue;
252 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000253 this->Indent();
254 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
256 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000257 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000258 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000259 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
260 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000261 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
262 Terminator = 0;
263 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000264 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000265 isa<ObjCInterfaceDecl>(*D) ||
266 isa<ObjCProtocolDecl>(*D) ||
267 isa<ObjCCategoryImplDecl>(*D) ||
268 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000269 Terminator = 0;
270 else if (isa<EnumConstantDecl>(*D)) {
271 DeclContext::decl_iterator Next = D;
272 ++Next;
273 if (Next != DEnd)
274 Terminator = ",";
275 } else
276 Terminator = ";";
277
278 if (Terminator)
279 Out << Terminator;
280 Out << "\n";
281 }
282
Eli Friedman42f42c02009-05-30 04:20:30 +0000283 if (!Decls.empty())
284 ProcessDeclGroup(Decls);
285
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000286 if (Indent)
287 Indentation -= Policy.Indentation;
288}
289
290void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
291 VisitDeclContext(D, false);
292}
293
294void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
295 std::string S = D->getNameAsString();
296 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +0000297 if (!Policy.SuppressSpecifiers)
298 Out << "typedef ";
299 Out << S;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000300}
301
302void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
303 Out << "enum " << D->getNameAsString() << " {\n";
304 VisitDeclContext(D);
305 Indent() << "}";
306}
307
308void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000309 Out << D->getKindName();
Eli Friedman42f42c02009-05-30 04:20:30 +0000310 if (D->getIdentifier()) {
311 Out << " ";
312 Out << D->getNameAsString();
313 }
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000315 if (D->isDefinition()) {
316 Out << " {\n";
317 VisitDeclContext(D);
318 Indent() << "}";
319 }
320}
321
322void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
323 Out << D->getNameAsString();
324 if (Expr *Init = D->getInitExpr()) {
325 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000326 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000327 }
328}
329
Mike Stump1eb44332009-09-09 15:08:12 +0000330void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000331 if (!Policy.SuppressSpecifiers) {
332 switch (D->getStorageClass()) {
333 case FunctionDecl::None: break;
334 case FunctionDecl::Extern: Out << "extern "; break;
335 case FunctionDecl::Static: Out << "static "; break;
336 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
337 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000338
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000339 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000340 if (D->isVirtualAsWritten()) Out << "virtual ";
341 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000342
Douglas Gregor6620a622009-05-30 05:39:39 +0000343 PrintingPolicy SubPolicy(Policy);
344 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000345 std::string Proto = D->getNameAsString();
346 if (isa<FunctionType>(D->getType().getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +0000347 const FunctionType *AFT = D->getType()->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000348
349 const FunctionProtoType *FT = 0;
350 if (D->hasWrittenPrototype())
351 FT = dyn_cast<FunctionProtoType>(AFT);
352
353 Proto += "(";
354 if (FT) {
355 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000356 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000357 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
358 if (i) POut << ", ";
359 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000362 if (FT->isVariadic()) {
363 if (D->getNumParams()) POut << ", ";
364 POut << "...";
365 }
Eli Friedman42f42c02009-05-30 04:20:30 +0000366 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
367 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
368 if (i)
369 Proto += ", ";
370 Proto += D->getParamDecl(i)->getNameAsString();
371 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000372 }
373
374 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000375
376 if (FT && FT->hasExceptionSpec()) {
377 Proto += " throw(";
378 if (FT->hasAnyExceptionSpec())
379 Proto += "...";
380 else
381 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
382 if (I)
383 Proto += ", ";
384
385
386 std::string ExceptionType;
387 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
388 Proto += ExceptionType;
389 }
390 Proto += ")";
391 }
392
Mike Stumpfd350b52009-07-27 21:33:40 +0000393 if (D->hasAttr<NoReturnAttr>())
394 Proto += " __attribute((noreturn))";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000395 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
396 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
397 Proto += " : ";
398 Out << Proto;
399 Proto.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000400 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000401 E = CDecl->init_end();
402 B != E; ++B) {
403 CXXBaseOrMemberInitializer * BMInitializer = (*B);
404 if (B != CDecl->init_begin())
405 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000406 bool hasArguments = (BMInitializer->arg_begin() !=
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000407 BMInitializer->arg_end());
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000408 if (BMInitializer->isMemberInitializer()) {
409 FieldDecl *FD = BMInitializer->getMember();
410 Out << FD->getNameAsString();
411 }
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000412 else // FIXME. skip dependent types for now.
Mike Stump1eb44332009-09-09 15:08:12 +0000413 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000414 BMInitializer->getBaseClass()->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000415 const CXXRecordDecl *BaseDecl =
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000416 cast<CXXRecordDecl>(RT->getDecl());
417 Out << BaseDecl->getNameAsString();
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000418 }
419 if (hasArguments) {
420 Out << "(";
Mike Stump1eb44332009-09-09 15:08:12 +0000421 for (CXXBaseOrMemberInitializer::const_arg_iterator BE =
422 BMInitializer->const_arg_begin(),
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000423 EE = BMInitializer->const_arg_end(); BE != EE; ++BE) {
424 if (BE != BMInitializer->const_arg_begin())
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000425 Out<< ", ";
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000426 const Expr *Exp = (*BE);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000427 Exp->printPretty(Out, Context, 0, Policy, Indentation);
428 }
429 Out << ")";
Fariborz Jahanian939afd82009-07-13 23:31:10 +0000430 } else
431 Out << "()";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000432 }
433 }
434 }
435 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 Gregor59e63572009-05-30 06:58:37 +0000519void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
520 Out << "namespace " << D->getNameAsString() << " {\n";
521 VisitDeclContext(D);
522 Indent() << "}";
523}
524
Douglas Gregor8419fa32009-05-30 06:31:56 +0000525void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
526 Out << "using namespace ";
527 if (D->getQualifier())
528 D->getQualifier()->print(Out, Policy);
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000529 Out << D->getNominatedNamespaceAsWritten()->getNameAsString();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000530}
531
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000532void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
533 Out << "namespace " << D->getNameAsString() << " = ";
534 if (D->getQualifier())
535 D->getQualifier()->print(Out, Policy);
536 Out << D->getAliasedNamespace()->getNameAsString();
537}
538
Douglas Gregor59e63572009-05-30 06:58:37 +0000539void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
540 Out << D->getKindName();
541 if (D->getIdentifier()) {
542 Out << " ";
543 Out << D->getNameAsString();
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Douglas Gregor59e63572009-05-30 06:58:37 +0000546 if (D->isDefinition()) {
547 // Print the base classes
548 if (D->getNumBases()) {
549 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000550 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
551 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000552 if (Base != D->bases_begin())
553 Out << ", ";
554
555 if (Base->isVirtual())
556 Out << "virtual ";
557
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000558 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
559 if (AS != AS_none)
560 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000561 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor59e63572009-05-30 06:58:37 +0000562 }
563 }
564
565 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000566 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000567 Out << " {\n";
568 VisitDeclContext(D);
569 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000570 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000571}
572
573void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
574 const char *l;
575 if (D->getLanguage() == LinkageSpecDecl::lang_c)
576 l = "C";
577 else {
578 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
579 "unknown language in linkage specification");
580 l = "C++";
581 }
582
583 Out << "extern \"" << l << "\" ";
584 if (D->hasBraces()) {
585 Out << "{\n";
586 VisitDeclContext(D);
587 Indent() << "}";
588 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000589 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000590}
591
592void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000593 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Anders Carlsson0487f662009-06-04 05:37:43 +0000595 TemplateParameterList *Params = D->getTemplateParameters();
596 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
597 if (i != 0)
598 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Anders Carlsson0487f662009-06-04 05:37:43 +0000600 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000601 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000602 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000603
604 QualType ParamType =
Anders Carlsson0487f662009-06-04 05:37:43 +0000605 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
606
607 if (TTP->wasDeclaredWithTypename())
608 Out << "typename ";
609 else
610 Out << "class ";
611
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000612 if (TTP->isParameterPack())
613 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Anders Carlsson0487f662009-06-04 05:37:43 +0000615 Out << ParamType.getAsString(Policy);
616
617 if (TTP->hasDefaultArgument()) {
618 Out << " = ";
619 Out << TTP->getDefaultArgument().getAsString(Policy);
620 };
Mike Stump1eb44332009-09-09 15:08:12 +0000621 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000622 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
623 Out << NTTP->getType().getAsString(Policy);
624
625 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
626 Out << ' ';
627 Out << Name->getName();
628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Anders Carlsson0487f662009-06-04 05:37:43 +0000630 if (NTTP->hasDefaultArgument()) {
631 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000632 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000633 Indentation);
634 }
635 }
636 }
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Anders Carlsson0487f662009-06-04 05:37:43 +0000638 Out << "> ";
639
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000640 Visit(D->getTemplatedDecl());
641}
642
643//----------------------------------------------------------------------------
644// Objective-C declarations
645//----------------------------------------------------------------------------
646
647void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
648 Out << "@class ";
649 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
650 I != E; ++I) {
651 if (I != D->begin()) Out << ", ";
Ted Kremenek321c22f2009-11-18 00:28:11 +0000652 Out << I->getInterface()->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000653 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000654}
655
656void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
657 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000658 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000659 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000660 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000661 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000662 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000664 std::string name = OMD->getSelector().getAsString();
665 std::string::size_type pos, lastPos = 0;
666 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
667 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000668 // FIXME: selector is missing here!
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000669 pos = name.find_first_of(":", lastPos);
670 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor59e63572009-05-30 06:58:37 +0000671 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Mike Stump1eb44332009-09-09 15:08:12 +0000672 << (*PI)->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000673 lastPos = pos + 1;
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000676 if (OMD->param_begin() == OMD->param_end())
677 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000679 if (OMD->isVariadic())
680 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000682 if (OMD->getBody()) {
683 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000684 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000685 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000686 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000687}
688
689void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
690 std::string I = OID->getNameAsString();
691 ObjCInterfaceDecl *SID = OID->getSuperClass();
692
693 if (SID)
694 Out << "@implementation " << I << " : " << SID->getNameAsString();
695 else
696 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000697 Out << "\n";
698 VisitDeclContext(OID, false);
699 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000700}
701
702void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
703 std::string I = OID->getNameAsString();
704 ObjCInterfaceDecl *SID = OID->getSuperClass();
705
706 if (SID)
707 Out << "@interface " << I << " : " << SID->getNameAsString();
708 else
709 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000711 // Protocols?
712 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
713 if (!Protocols.empty()) {
714 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
715 E = Protocols.end(); I != E; ++I)
716 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
717 }
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000719 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000720 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000722 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000723 Out << "{\n";
724 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000725 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
726 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000727 Indent() << (*I)->getType().getAsString(Policy)
Mike Stump1eb44332009-09-09 15:08:12 +0000728 << ' ' << (*I)->getNameAsString() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000729 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000730 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000731 Out << "}\n";
732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000734 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000735 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000736 // FIXME: implement the rest...
737}
738
739void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
740 Out << "@protocol ";
Mike Stump1eb44332009-09-09 15:08:12 +0000741 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000742 E = D->protocol_end();
743 I != E; ++I) {
744 if (I != D->protocol_begin()) Out << ", ";
745 Out << (*I)->getNameAsString();
746 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000747}
748
749void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
750 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000751 VisitDeclContext(PID, false);
752 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000753}
754
755void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
756 Out << "@implementation "
757 << PID->getClassInterface()->getNameAsString()
Mike Stump1eb44332009-09-09 15:08:12 +0000758 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000759
760 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000761 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000762 // FIXME: implement the rest...
763}
764
765void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Mike Stump1eb44332009-09-09 15:08:12 +0000766 Out << "@interface "
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000767 << PID->getClassInterface()->getNameAsString()
Douglas Gregor64f65002009-05-30 00:56:08 +0000768 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000769 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000770 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000772 // FIXME: implement the rest...
773}
774
775void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Mike Stump1eb44332009-09-09 15:08:12 +0000776 Out << "@compatibility_alias " << AID->getNameAsString()
777 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000778}
779
780/// PrintObjCPropertyDecl - print a property declaration.
781///
782void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
783 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
784 Out << "@required\n";
785 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
786 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000788 Out << "@property";
789 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
790 bool first = true;
791 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000792 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000793 ObjCPropertyDecl::OBJC_PR_readonly) {
794 Out << (first ? ' ' : ',') << "readonly";
795 first = false;
796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000798 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
799 Out << (first ? ' ' : ',') << "getter = "
800 << PDecl->getGetterName().getAsString();
801 first = false;
802 }
803 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
804 Out << (first ? ' ' : ',') << "setter = "
805 << PDecl->getSetterName().getAsString();
806 first = false;
807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000809 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
810 Out << (first ? ' ' : ',') << "assign";
811 first = false;
812 }
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000814 if (PDecl->getPropertyAttributes() &
815 ObjCPropertyDecl::OBJC_PR_readwrite) {
816 Out << (first ? ' ' : ',') << "readwrite";
817 first = false;
818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000820 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
821 Out << (first ? ' ' : ',') << "retain";
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_copy) {
826 Out << (first ? ' ' : ',') << "copy";
827 first = false;
828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
830 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000831 ObjCPropertyDecl::OBJC_PR_nonatomic) {
832 Out << (first ? ' ' : ',') << "nonatomic";
833 first = false;
834 }
835 Out << " )";
836 }
837 Out << ' ' << PDecl->getType().getAsString(Policy)
838 << ' ' << PDecl->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000839}
840
841void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
842 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000843 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000844 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000845 Out << "@dynamic ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000846 Out << PID->getPropertyDecl()->getNameAsString();
847 if (PID->getPropertyIvarDecl())
848 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000849}
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000850
851void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
852 Out << "using ";
853 D->getTargetNestedNameDecl()->print(Out, Policy);
John McCall9488ea12009-11-17 05:59:44 +0000854 Out << D->getNameAsString();
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000855}
856
John McCall7ba107a2009-11-18 02:36:19 +0000857void
858DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
859 Out << "using typename ";
860 D->getTargetNestedNameSpecifier()->print(Out, Policy);
861 Out << D->getDeclName().getAsString();
862}
863
864void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000865 Out << "using ";
866 D->getTargetNestedNameSpecifier()->print(Out, Policy);
John McCall7ba107a2009-11-18 02:36:19 +0000867 Out << D->getDeclName().getAsString();
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000868}
John McCall9488ea12009-11-17 05:59:44 +0000869
870void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
871 // ignore
872}