blob: 5a3144049488c172db11425a70d640d487d9c5d2 [file] [log] [blame]
Douglas Gregor278f52e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl::dump method, which pretty print the
11// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclVisitor.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Douglas Gregor7ae2d772010-01-31 09:12:51 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000021#include "clang/AST/PrettyPrinter.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000022#include "llvm/Support/raw_ostream.h"
23using namespace clang;
24
25namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000026 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Douglas Gregor278f52e2009-05-30 00:08:05 +000027 llvm::raw_ostream &Out;
28 ASTContext &Context;
29 PrintingPolicy Policy;
30 unsigned Indentation;
31
Daniel Dunbar671f45e2009-11-21 09:12:06 +000032 llvm::raw_ostream& Indent() { return Indent(Indentation); }
33 llvm::raw_ostream& Indent(unsigned Indentation);
Eli Friedman79635842009-05-30 04:20:30 +000034 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor278f52e2009-05-30 00:08:05 +000035
Anders Carlsson601d6e42009-08-28 22:39:52 +000036 void Print(AccessSpecifier AS);
Mike Stump11289f42009-09-09 15:08:12 +000037
Douglas Gregor278f52e2009-05-30 00:08:05 +000038 public:
Mike Stump11289f42009-09-09 15:08:12 +000039 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
Douglas Gregor278f52e2009-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 Gregor5f478b72009-05-30 06:58:37 +000056 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000057 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000058 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000059 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000060 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
61 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000062 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000063 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000064 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
65 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
66 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
67 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
68 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
69 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
70 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
71 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
72 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000073 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
74 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +000075 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +000076 void VisitUsingShadowDecl(UsingShadowDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000077 };
78}
79
Anders Carlsson46f87dc2009-09-26 21:58:53 +000080void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000081 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +000082}
83
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000084void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Anders Carlsson46f87dc2009-09-26 21:58:53 +000085 unsigned Indentation) const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000086 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +000087 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +000088}
89
Eli Friedman79635842009-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 Kremenekc23c7e62009-07-29 21:53:49 +000096 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +000097 BaseType = PTy->getPointeeType();
98 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
99 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000100 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman79635842009-05-30 04:20:30 +0000101 BaseType = FTy->getResultType();
John McCall9dd450b2009-09-21 23:43:11 +0000102 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000103 BaseType = VTy->getElementType();
Eli Friedman79635842009-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 Kyrtzidis8a803cc2009-06-30 02:35:04 +0000119 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000120 unsigned Indentation) {
121 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000122 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-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 Kyrtzidis8a803cc2009-06-30 02:35:04 +0000133 TD->print(Out, Policy, Indentation);
Eli Friedman79635842009-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 Kyrtzidis8a803cc2009-06-30 02:35:04 +0000148 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000149 }
150}
151
Anders Carlsson89112292009-12-14 00:51:04 +0000152void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000153 // Get the translation unit
154 const DeclContext *DC = this;
155 while (!DC->isTranslationUnit())
156 DC = DC->getParent();
157
158 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
159 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
160 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
161}
162
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000163void Decl::dump() const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000164 print(llvm::errs());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000165}
166
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000167llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
168 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000169 Out << " ";
170 return Out;
171}
172
Eli Friedman79635842009-05-30 04:20:30 +0000173void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
174 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000175 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000176 Out << ";\n";
177 Decls.clear();
178
179}
180
Anders Carlsson601d6e42009-08-28 22:39:52 +0000181void DeclPrinter::Print(AccessSpecifier AS) {
182 switch(AS) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000183 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000184 case AS_public: Out << "public"; break;
185 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000186 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000187 }
188}
189
Douglas Gregor278f52e2009-05-30 00:08:05 +0000190//----------------------------------------------------------------------------
191// Common C declarations
192//----------------------------------------------------------------------------
193
194void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
195 if (Indent)
196 Indentation += Policy.Indentation;
197
Eli Friedman79635842009-05-30 04:20:30 +0000198 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000199 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000200 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000201
202 // Don't print ObjCIvarDecls, as they are printed when visiting the
203 // containing ObjCInterfaceDecl.
204 if (isa<ObjCIvarDecl>(*D))
205 continue;
206
Eli Friedmanef334fd2009-05-30 05:03:24 +0000207 if (!Policy.Dump) {
208 // Skip over implicit declarations in pretty-printing mode.
209 if (D->isImplicit()) continue;
Eli Friedman17304242009-05-30 06:35:22 +0000210 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidisfa533e72010-06-17 10:52:11 +0000211 // of __builtin_va_list or __[u]int128_t. There should be some other way
212 // to check that.
213 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
214 if (IdentifierInfo *II = ND->getIdentifier()) {
215 if (II->isStr("__builtin_va_list") ||
216 II->isStr("__int128_t") || II->isStr("__uint128_t"))
217 continue;
218 }
219 }
Eli Friedmanef334fd2009-05-30 05:03:24 +0000220 }
221
Eli Friedman79635842009-05-30 04:20:30 +0000222 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
223 // forced to merge the declarations because there's no other way to
224 // refer to the struct in question. This limited merging is safe without
225 // a bunch of other checks because it only merges declarations directly
226 // referring to the tag, not typedefs.
227 //
228 // Check whether the current declaration should be grouped with a previous
229 // unnamed struct.
230 QualType CurDeclType = getDeclType(*D);
231 if (!Decls.empty() && !CurDeclType.isNull()) {
232 QualType BaseType = GetBaseType(CurDeclType);
233 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
234 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
235 Decls.push_back(*D);
236 continue;
237 }
238 }
239
240 // If we have a merged group waiting to be handled, handle it now.
241 if (!Decls.empty())
242 ProcessDeclGroup(Decls);
243
244 // If the current declaration is an unnamed tag type, save it
245 // so we can merge it with the subsequent declaration(s) using it.
246 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
247 Decls.push_back(*D);
248 continue;
249 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000250
251 if (isa<AccessSpecDecl>(*D)) {
252 Indentation -= Policy.Indentation;
253 this->Indent();
254 Print(D->getAccess());
255 Out << ":\n";
256 Indentation += Policy.Indentation;
257 continue;
258 }
259
Douglas Gregor278f52e2009-05-30 00:08:05 +0000260 this->Indent();
261 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000262
263 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor278f52e2009-05-30 00:08:05 +0000264 const char *Terminator = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000265 if (isa<FunctionDecl>(*D) &&
Douglas Gregor278f52e2009-05-30 00:08:05 +0000266 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
267 Terminator = 0;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000268 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
269 Terminator = 0;
270 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000271 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000272 isa<ObjCInterfaceDecl>(*D) ||
273 isa<ObjCProtocolDecl>(*D) ||
274 isa<ObjCCategoryImplDecl>(*D) ||
275 isa<ObjCCategoryDecl>(*D))
Douglas Gregor278f52e2009-05-30 00:08:05 +0000276 Terminator = 0;
277 else if (isa<EnumConstantDecl>(*D)) {
278 DeclContext::decl_iterator Next = D;
279 ++Next;
280 if (Next != DEnd)
281 Terminator = ",";
282 } else
283 Terminator = ";";
284
285 if (Terminator)
286 Out << Terminator;
287 Out << "\n";
288 }
289
Eli Friedman79635842009-05-30 04:20:30 +0000290 if (!Decls.empty())
291 ProcessDeclGroup(Decls);
292
Douglas Gregor278f52e2009-05-30 00:08:05 +0000293 if (Indent)
294 Indentation -= Policy.Indentation;
295}
296
297void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
298 VisitDeclContext(D, false);
299}
300
301void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
302 std::string S = D->getNameAsString();
303 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman79635842009-05-30 04:20:30 +0000304 if (!Policy.SuppressSpecifiers)
305 Out << "typedef ";
306 Out << S;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000307}
308
309void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000310 Out << "enum ";
311 if (D->isScoped())
312 Out << "class ";
313 Out << D;
314
315 if (D->isFixed()) {
316 std::string Underlying;
317 D->getIntegerType().getAsStringInternal(Underlying, Policy);
318 Out << " : " << Underlying;
319 }
320
321 if (D->isDefinition()) {
322 Out << " {\n";
323 VisitDeclContext(D);
324 Indent() << "}";
325 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000326}
327
328void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000329 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000330 if (D->getIdentifier())
331 Out << ' ' << D;
Mike Stump11289f42009-09-09 15:08:12 +0000332
Douglas Gregor278f52e2009-05-30 00:08:05 +0000333 if (D->isDefinition()) {
334 Out << " {\n";
335 VisitDeclContext(D);
336 Indent() << "}";
337 }
338}
339
340void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000341 Out << D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000342 if (Expr *Init = D->getInitExpr()) {
343 Out << " = ";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000344 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000345 }
346}
347
Mike Stump11289f42009-09-09 15:08:12 +0000348void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000349 if (!Policy.SuppressSpecifiers) {
350 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000351 case SC_None: break;
352 case SC_Extern: Out << "extern "; break;
353 case SC_Static: Out << "static "; break;
354 case SC_PrivateExtern: Out << "__private_extern__ "; break;
355 case SC_Auto: case SC_Register: llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000356 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000357
Douglas Gregor35b57532009-10-27 21:01:01 +0000358 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000359 if (D->isVirtualAsWritten()) Out << "virtual ";
360 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000361
Douglas Gregor2d042f12009-05-30 05:39:39 +0000362 PrintingPolicy SubPolicy(Policy);
363 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000364 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000365 if (isa<FunctionType>(D->getType().getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +0000366 const FunctionType *AFT = D->getType()->getAs<FunctionType>();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000367
368 const FunctionProtoType *FT = 0;
369 if (D->hasWrittenPrototype())
370 FT = dyn_cast<FunctionProtoType>(AFT);
371
372 Proto += "(";
373 if (FT) {
374 llvm::raw_string_ostream POut(Proto);
Douglas Gregor2d042f12009-05-30 05:39:39 +0000375 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000376 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
377 if (i) POut << ", ";
378 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
379 }
Mike Stump11289f42009-09-09 15:08:12 +0000380
Douglas Gregor278f52e2009-05-30 00:08:05 +0000381 if (FT->isVariadic()) {
382 if (D->getNumParams()) POut << ", ";
383 POut << "...";
384 }
Eli Friedman79635842009-05-30 04:20:30 +0000385 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
386 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
387 if (i)
388 Proto += ", ";
389 Proto += D->getParamDecl(i)->getNameAsString();
390 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000391 }
392
393 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000394
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000395 if (FT && FT->getTypeQuals()) {
396 unsigned TypeQuals = FT->getTypeQuals();
397 if (TypeQuals & Qualifiers::Const)
398 Proto += " const";
399 if (TypeQuals & Qualifiers::Volatile)
400 Proto += " volatile";
401 if (TypeQuals & Qualifiers::Restrict)
402 Proto += " restrict";
403 }
404
Douglas Gregor049bdca2009-12-08 17:45:32 +0000405 if (FT && FT->hasExceptionSpec()) {
406 Proto += " throw(";
407 if (FT->hasAnyExceptionSpec())
408 Proto += "...";
409 else
410 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
411 if (I)
412 Proto += ", ";
413
414
415 std::string ExceptionType;
416 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
417 Proto += ExceptionType;
418 }
419 Proto += ")";
420 }
421
Mike Stump9a9e0c22009-07-27 21:33:40 +0000422 if (D->hasAttr<NoReturnAttr>())
423 Proto += " __attribute((noreturn))";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000424 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
425 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
426 Proto += " : ";
427 Out << Proto;
428 Proto.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000429 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000430 E = CDecl->init_end();
431 B != E; ++B) {
432 CXXBaseOrMemberInitializer * BMInitializer = (*B);
433 if (B != CDecl->init_begin())
434 Out << ", ";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000435 if (BMInitializer->isMemberInitializer()) {
436 FieldDecl *FD = BMInitializer->getMember();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000437 Out << FD;
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000438 } else {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000439 Out << QualType(BMInitializer->getBaseClass(),
440 0).getAsString(Policy);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000441 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000442
443 Out << "(";
444 if (!BMInitializer->getInit()) {
445 // Nothing to print
446 } else {
447 Expr *Init = BMInitializer->getInit();
448 if (CXXExprWithTemporaries *Tmp
449 = dyn_cast<CXXExprWithTemporaries>(Init))
450 Init = Tmp->getSubExpr();
451
452 Init = Init->IgnoreParens();
453
454 Expr *SimpleInit = 0;
455 Expr **Args = 0;
456 unsigned NumArgs = 0;
457 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
458 Args = ParenList->getExprs();
459 NumArgs = ParenList->getNumExprs();
460 } else if (CXXConstructExpr *Construct
461 = dyn_cast<CXXConstructExpr>(Init)) {
462 Args = Construct->getArgs();
463 NumArgs = Construct->getNumArgs();
464 } else
465 SimpleInit = Init;
466
467 if (SimpleInit)
468 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
469 else {
470 for (unsigned I = 0; I != NumArgs; ++I) {
471 if (isa<CXXDefaultArgExpr>(Args[I]))
472 break;
473
474 if (I)
475 Out << ", ";
476 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
477 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000478 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000479 }
480 Out << ")";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000481 }
482 }
483 }
484 else
485 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000486 } else {
487 D->getType().getAsStringInternal(Proto, Policy);
488 }
489
490 Out << Proto;
491
492 if (D->isPure())
493 Out << " = 0";
494 else if (D->isDeleted())
495 Out << " = delete";
496 else if (D->isThisDeclarationADefinition()) {
497 if (!D->hasPrototype() && D->getNumParams()) {
498 // This is a K&R function definition, so we need to print the
499 // parameters.
500 Out << '\n';
Douglas Gregor2d042f12009-05-30 05:39:39 +0000501 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000502 Indentation += Policy.Indentation;
503 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
504 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000505 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000506 Out << ";\n";
507 }
508 Indentation -= Policy.Indentation;
509 } else
510 Out << ' ';
511
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000512 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000513 Out << '\n';
514 }
515}
516
517void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000518 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000519 Out << "mutable ";
520
521 std::string Name = D->getNameAsString();
522 D->getType().getAsStringInternal(Name, Policy);
523 Out << Name;
524
525 if (D->isBitField()) {
526 Out << " : ";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000527 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000528 }
529}
530
531void DeclPrinter::VisitVarDecl(VarDecl *D) {
John McCall8e7d6562010-08-26 03:08:43 +0000532 if (!Policy.SuppressSpecifiers && D->getStorageClass() != SC_None)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000533 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
534
Eli Friedman79635842009-05-30 04:20:30 +0000535 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000536 Out << "__thread ";
537
538 std::string Name = D->getNameAsString();
539 QualType T = D->getType();
John McCall856bbea2009-10-23 21:48:59 +0000540 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor278f52e2009-05-30 00:08:05 +0000541 T = Parm->getOriginalType();
542 T.getAsStringInternal(Name, Policy);
543 Out << Name;
Ted Kremenek41994fd2010-09-07 22:21:59 +0000544 if (Expr *Init = D->getInit()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000545 if (D->hasCXXDirectInitializer())
546 Out << "(";
Ted Kremenek35869382010-09-17 23:04:38 +0000547 else {
548 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
549 if (!CCE || CCE->getConstructor()->isCopyConstructor())
550 Out << " = ";
551 }
Ted Kremenek41994fd2010-09-07 22:21:59 +0000552 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000553 if (D->hasCXXDirectInitializer())
554 Out << ")";
555 }
556}
557
558void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
559 VisitVarDecl(D);
560}
561
562void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
563 Out << "__asm (";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000564 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000565 Out << ")";
566}
567
568//----------------------------------------------------------------------------
569// C++ declarations
570//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000571void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000572 Out << "namespace " << D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000573 VisitDeclContext(D);
574 Indent() << "}";
575}
576
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000577void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
578 Out << "using namespace ";
579 if (D->getQualifier())
580 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000581 Out << D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000582}
583
Douglas Gregor18231932009-05-30 06:48:27 +0000584void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000585 Out << "namespace " << D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000586 if (D->getQualifier())
587 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000588 Out << D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000589}
590
Douglas Gregor5f478b72009-05-30 06:58:37 +0000591void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
592 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000593 if (D->getIdentifier())
594 Out << ' ' << D;
Mike Stump11289f42009-09-09 15:08:12 +0000595
Douglas Gregor5f478b72009-05-30 06:58:37 +0000596 if (D->isDefinition()) {
597 // Print the base classes
598 if (D->getNumBases()) {
599 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000600 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
601 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000602 if (Base != D->bases_begin())
603 Out << ", ";
604
605 if (Base->isVirtual())
606 Out << "virtual ";
607
Anders Carlsson6df9e072009-08-29 20:36:12 +0000608 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
609 if (AS != AS_none)
610 Print(AS);
Anders Carlsson601d6e42009-08-28 22:39:52 +0000611 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor5f478b72009-05-30 06:58:37 +0000612 }
613 }
614
615 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000616 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000617 Out << " {\n";
618 VisitDeclContext(D);
619 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000620 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000621}
622
623void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
624 const char *l;
625 if (D->getLanguage() == LinkageSpecDecl::lang_c)
626 l = "C";
627 else {
628 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
629 "unknown language in linkage specification");
630 l = "C++";
631 }
632
633 Out << "extern \"" << l << "\" ";
634 if (D->hasBraces()) {
635 Out << "{\n";
636 VisitDeclContext(D);
637 Indent() << "}";
638 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000639 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000640}
641
642void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000643 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000644
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000645 TemplateParameterList *Params = D->getTemplateParameters();
646 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
647 if (i != 0)
648 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000649
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000650 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000651 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000652 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000653
654 QualType ParamType =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000655 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
656
657 if (TTP->wasDeclaredWithTypename())
658 Out << "typename ";
659 else
660 Out << "class ";
661
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000662 if (TTP->isParameterPack())
663 Out << "... ";
Mike Stump11289f42009-09-09 15:08:12 +0000664
Douglas Gregor2ebcae12010-06-16 15:23:05 +0000665 Out << ParamType.getAsString(Policy);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000666
667 if (TTP->hasDefaultArgument()) {
668 Out << " = ";
669 Out << TTP->getDefaultArgument().getAsString(Policy);
670 };
Mike Stump11289f42009-09-09 15:08:12 +0000671 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000672 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
673 Out << NTTP->getType().getAsString(Policy);
674
675 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
676 Out << ' ';
677 Out << Name->getName();
678 }
Mike Stump11289f42009-09-09 15:08:12 +0000679
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000680 if (NTTP->hasDefaultArgument()) {
681 Out << " = ";
Mike Stump11289f42009-09-09 15:08:12 +0000682 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000683 Indentation);
684 }
685 }
686 }
Mike Stump11289f42009-09-09 15:08:12 +0000687
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000688 Out << "> ";
689
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000690 if (isa<TemplateTemplateParmDecl>(D)) {
691 Out << "class " << D->getName();
692 } else {
693 Visit(D->getTemplatedDecl());
694 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000695}
696
697//----------------------------------------------------------------------------
698// Objective-C declarations
699//----------------------------------------------------------------------------
700
701void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
702 Out << "@class ";
703 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
704 I != E; ++I) {
705 if (I != D->begin()) Out << ", ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000706 Out << I->getInterface();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000707 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000708}
709
710void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
711 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000712 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +0000713 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000714 Out << "+ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000715 if (!OMD->getResultType().isNull())
Douglas Gregor5f478b72009-05-30 06:58:37 +0000716 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000717
Douglas Gregor278f52e2009-05-30 00:08:05 +0000718 std::string name = OMD->getSelector().getAsString();
719 std::string::size_type pos, lastPos = 0;
720 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
721 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump11289f42009-09-09 15:08:12 +0000722 // FIXME: selector is missing here!
Douglas Gregor278f52e2009-05-30 00:08:05 +0000723 pos = name.find_first_of(":", lastPos);
724 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000725 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000726 lastPos = pos + 1;
727 }
Mike Stump11289f42009-09-09 15:08:12 +0000728
Douglas Gregor278f52e2009-05-30 00:08:05 +0000729 if (OMD->param_begin() == OMD->param_end())
730 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +0000731
Douglas Gregor278f52e2009-05-30 00:08:05 +0000732 if (OMD->isVariadic())
733 Out << ", ...";
Mike Stump11289f42009-09-09 15:08:12 +0000734
Douglas Gregor278f52e2009-05-30 00:08:05 +0000735 if (OMD->getBody()) {
736 Out << ' ';
Eli Friedmanef334fd2009-05-30 05:03:24 +0000737 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000738 Out << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000739 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000740}
741
742void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
743 std::string I = OID->getNameAsString();
744 ObjCInterfaceDecl *SID = OID->getSuperClass();
745
746 if (SID)
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000747 Out << "@implementation " << I << " : " << SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000748 else
749 Out << "@implementation " << I;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000750 Out << "\n";
751 VisitDeclContext(OID, false);
752 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000753}
754
755void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
756 std::string I = OID->getNameAsString();
757 ObjCInterfaceDecl *SID = OID->getSuperClass();
758
759 if (SID)
Douglas Gregor1c283312010-08-11 12:19:30 +0000760 Out << "@interface " << I << " : " << SID;
761 else
762 Out << "@interface " << I;
Mike Stump11289f42009-09-09 15:08:12 +0000763
Douglas Gregor278f52e2009-05-30 00:08:05 +0000764 // Protocols?
765 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
766 if (!Protocols.empty()) {
767 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
768 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000769 Out << (I == Protocols.begin() ? '<' : ',') << *I;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000770 }
Mike Stump11289f42009-09-09 15:08:12 +0000771
Douglas Gregor278f52e2009-05-30 00:08:05 +0000772 if (!Protocols.empty())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000773 Out << "> ";
Mike Stump11289f42009-09-09 15:08:12 +0000774
Douglas Gregor278f52e2009-05-30 00:08:05 +0000775 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000776 Out << "{\n";
777 Indentation += Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000778 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
779 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000780 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000781 }
Douglas Gregor36098ff2009-05-30 00:56:08 +0000782 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000783 Out << "}\n";
784 }
Mike Stump11289f42009-09-09 15:08:12 +0000785
Douglas Gregor278f52e2009-05-30 00:08:05 +0000786 VisitDeclContext(OID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000787 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000788 // FIXME: implement the rest...
789}
790
791void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
792 Out << "@protocol ";
Mike Stump11289f42009-09-09 15:08:12 +0000793 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor278f52e2009-05-30 00:08:05 +0000794 E = D->protocol_end();
795 I != E; ++I) {
796 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000797 Out << *I;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000798 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000799}
800
801void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000802 Out << "@protocol " << PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000803 VisitDeclContext(PID, false);
804 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000805}
806
807void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000808 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000809
810 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000811 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000812 // FIXME: implement the rest...
813}
814
815void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000816 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000817 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000818 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +0000819
Douglas Gregor278f52e2009-05-30 00:08:05 +0000820 // FIXME: implement the rest...
821}
822
823void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000824 Out << "@compatibility_alias " << AID
825 << ' ' << AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000826}
827
828/// PrintObjCPropertyDecl - print a property declaration.
829///
830void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
831 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
832 Out << "@required\n";
833 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
834 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +0000835
Douglas Gregor278f52e2009-05-30 00:08:05 +0000836 Out << "@property";
837 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
838 bool first = true;
839 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +0000840 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +0000841 ObjCPropertyDecl::OBJC_PR_readonly) {
842 Out << (first ? ' ' : ',') << "readonly";
843 first = false;
844 }
Mike Stump11289f42009-09-09 15:08:12 +0000845
Douglas Gregor278f52e2009-05-30 00:08:05 +0000846 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
847 Out << (first ? ' ' : ',') << "getter = "
848 << PDecl->getGetterName().getAsString();
849 first = false;
850 }
851 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
852 Out << (first ? ' ' : ',') << "setter = "
853 << PDecl->getSetterName().getAsString();
854 first = false;
855 }
Mike Stump11289f42009-09-09 15:08:12 +0000856
Douglas Gregor278f52e2009-05-30 00:08:05 +0000857 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
858 Out << (first ? ' ' : ',') << "assign";
859 first = false;
860 }
Mike Stump11289f42009-09-09 15:08:12 +0000861
Douglas Gregor278f52e2009-05-30 00:08:05 +0000862 if (PDecl->getPropertyAttributes() &
863 ObjCPropertyDecl::OBJC_PR_readwrite) {
864 Out << (first ? ' ' : ',') << "readwrite";
865 first = false;
866 }
Mike Stump11289f42009-09-09 15:08:12 +0000867
Douglas Gregor278f52e2009-05-30 00:08:05 +0000868 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
869 Out << (first ? ' ' : ',') << "retain";
870 first = false;
871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
Douglas Gregor278f52e2009-05-30 00:08:05 +0000873 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
874 Out << (first ? ' ' : ',') << "copy";
875 first = false;
876 }
Mike Stump11289f42009-09-09 15:08:12 +0000877
878 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +0000879 ObjCPropertyDecl::OBJC_PR_nonatomic) {
880 Out << (first ? ' ' : ',') << "nonatomic";
881 first = false;
882 }
883 Out << " )";
884 }
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000885 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000886}
887
888void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
889 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +0000890 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000891 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000892 Out << "@dynamic ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000893 Out << PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000894 if (PID->getPropertyIvarDecl())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000895 Out << '=' << PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000896}
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000897
898void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
899 Out << "using ";
900 D->getTargetNestedNameDecl()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000901 Out << D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000902}
903
John McCalle61f2ba2009-11-18 02:36:19 +0000904void
905DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
906 Out << "using typename ";
907 D->getTargetNestedNameSpecifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000908 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +0000909}
910
911void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000912 Out << "using ";
913 D->getTargetNestedNameSpecifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000914 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000915}
John McCall3f746822009-11-17 05:59:44 +0000916
917void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
918 // ignore
919}