blob: 3d87fa99b0ca1cabd13495878d88e69c9da5f1fe [file] [log] [blame]
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl::dump method, which pretty print the
11// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclVisitor.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Douglas Gregor9db7dbb2010-01-31 09:12:51 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000021#include "clang/AST/PrettyPrinter.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000022#include "llvm/Support/raw_ostream.h"
23using namespace clang;
24
25namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000026 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000027 llvm::raw_ostream &Out;
28 ASTContext &Context;
29 PrintingPolicy Policy;
30 unsigned Indentation;
31
Daniel Dunbar512ce602009-11-21 09:12:06 +000032 llvm::raw_ostream& Indent() { return Indent(Indentation); }
33 llvm::raw_ostream& Indent(unsigned Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +000034 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000035
Anders Carlsson0d592922009-08-28 22:39:52 +000036 void Print(AccessSpecifier AS);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000038 public:
Mike Stump1eb44332009-09-09 15:08:12 +000039 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000040 const PrintingPolicy &Policy,
41 unsigned Indentation = 0)
42 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
43
44 void VisitDeclContext(DeclContext *DC, bool Indent = true);
45
46 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
47 void VisitTypedefDecl(TypedefDecl *D);
48 void VisitEnumDecl(EnumDecl *D);
49 void VisitRecordDecl(RecordDecl *D);
50 void VisitEnumConstantDecl(EnumConstantDecl *D);
51 void VisitFunctionDecl(FunctionDecl *D);
52 void VisitFieldDecl(FieldDecl *D);
53 void VisitVarDecl(VarDecl *D);
54 void VisitParmVarDecl(ParmVarDecl *D);
55 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000056 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000057 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000058 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000059 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000060 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
61 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000062 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000063 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000064 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
65 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
66 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
67 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
68 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
69 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
70 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
71 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
72 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000073 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
74 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000075 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000076 void VisitUsingShadowDecl(UsingShadowDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000077 };
78}
79
Anders Carlssonf88df862009-09-26 21:58:53 +000080void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000081 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000082}
83
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000084void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Anders Carlssonf88df862009-09-26 21:58:53 +000085 unsigned Indentation) const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000086 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Anders Carlssonf88df862009-09-26 21:58:53 +000087 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000088}
89
Eli Friedman42f42c02009-05-30 04:20:30 +000090static QualType GetBaseType(QualType T) {
91 // FIXME: This should be on the Type class!
92 QualType BaseType = T;
93 while (!BaseType->isSpecifierType()) {
94 if (isa<TypedefType>(BaseType))
95 break;
Ted Kremenek6217b802009-07-29 21:53:49 +000096 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +000097 BaseType = PTy->getPointeeType();
98 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
99 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +0000100 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000101 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000102 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000103 BaseType = VTy->getElementType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000104 else
105 assert(0 && "Unknown declarator!");
106 }
107 return BaseType;
108}
109
110static QualType getDeclType(Decl* D) {
111 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
112 return TDD->getUnderlyingType();
113 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
114 return VD->getType();
115 return QualType();
116}
117
118void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000119 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000120 unsigned Indentation) {
121 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000122 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000123 return;
124 }
125
126 Decl** End = Begin + NumDecls;
127 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
128 if (TD)
129 ++Begin;
130
131 PrintingPolicy SubPolicy(Policy);
132 if (TD && TD->isDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000133 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000134 Out << " ";
135 SubPolicy.SuppressTag = true;
136 }
137
138 bool isFirst = true;
139 for ( ; Begin != End; ++Begin) {
140 if (isFirst) {
141 SubPolicy.SuppressSpecifiers = false;
142 isFirst = false;
143 } else {
144 if (!isFirst) Out << ", ";
145 SubPolicy.SuppressSpecifiers = true;
146 }
147
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000148 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000149 }
150}
151
Anders Carlsson84834432009-12-14 00:51:04 +0000152void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000153 // Get the translation unit
154 const DeclContext *DC = this;
155 while (!DC->isTranslationUnit())
156 DC = DC->getParent();
157
158 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
159 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
160 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
161}
162
Anders Carlssonf88df862009-09-26 21:58:53 +0000163void Decl::dump() const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000164 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000165}
166
Daniel Dunbar512ce602009-11-21 09:12:06 +0000167llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
168 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000169 Out << " ";
170 return Out;
171}
172
Eli Friedman42f42c02009-05-30 04:20:30 +0000173void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
174 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000175 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000176 Out << ";\n";
177 Decls.clear();
178
179}
180
Anders Carlsson0d592922009-08-28 22:39:52 +0000181void DeclPrinter::Print(AccessSpecifier AS) {
182 switch(AS) {
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000183 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000184 case AS_public: Out << "public"; break;
185 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000186 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000187 }
188}
189
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000190//----------------------------------------------------------------------------
191// Common C declarations
192//----------------------------------------------------------------------------
193
194void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
195 if (Indent)
196 Indentation += Policy.Indentation;
197
Eli Friedman42f42c02009-05-30 04:20:30 +0000198 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000199 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000200 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-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 Friedman48d14a22009-05-30 05:03:24 +0000207 if (!Policy.Dump) {
208 // Skip over implicit declarations in pretty-printing mode.
209 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000210 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidis2574f6f2010-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 Friedman48d14a22009-05-30 05:03:24 +0000220 }
221
Eli Friedman42f42c02009-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 Bagnara6206d532010-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 Gregor4fe0c8e2009-05-30 00:08:05 +0000260 this->Indent();
261 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
263 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000264 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000265 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000266 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
267 Terminator = 0;
Douglas Gregor64f65002009-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 Stump1eb44332009-09-09 15:08:12 +0000271 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000272 isa<ObjCInterfaceDecl>(*D) ||
273 isa<ObjCProtocolDecl>(*D) ||
274 isa<ObjCCategoryImplDecl>(*D) ||
275 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-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 Friedman42f42c02009-05-30 04:20:30 +0000290 if (!Decls.empty())
291 ProcessDeclGroup(Decls);
292
Douglas Gregor4fe0c8e2009-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 Friedman42f42c02009-05-30 04:20:30 +0000304 if (!Policy.SuppressSpecifiers)
305 Out << "typedef ";
306 Out << S;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000307}
308
309void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000310 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000311 if (D->isScoped()) {
312 if (D->isScopedUsingClassTag())
313 Out << "class ";
314 else
315 Out << "struct ";
316 }
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000317 Out << D;
318
319 if (D->isFixed()) {
320 std::string Underlying;
321 D->getIntegerType().getAsStringInternal(Underlying, Policy);
322 Out << " : " << Underlying;
323 }
324
325 if (D->isDefinition()) {
326 Out << " {\n";
327 VisitDeclContext(D);
328 Indent() << "}";
329 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000330}
331
332void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000333 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000334 if (D->getIdentifier())
335 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000337 if (D->isDefinition()) {
338 Out << " {\n";
339 VisitDeclContext(D);
340 Indent() << "}";
341 }
342}
343
344void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000345 Out << D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000346 if (Expr *Init = D->getInitExpr()) {
347 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000348 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000349 }
350}
351
Mike Stump1eb44332009-09-09 15:08:12 +0000352void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000353 if (!Policy.SuppressSpecifiers) {
354 switch (D->getStorageClass()) {
John McCalld931b082010-08-26 03:08:43 +0000355 case SC_None: break;
356 case SC_Extern: Out << "extern "; break;
357 case SC_Static: Out << "static "; break;
358 case SC_PrivateExtern: Out << "__private_extern__ "; break;
359 case SC_Auto: case SC_Register: llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000360 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000361
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000362 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000363 if (D->isVirtualAsWritten()) Out << "virtual ";
364 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000365
Douglas Gregor6620a622009-05-30 05:39:39 +0000366 PrintingPolicy SubPolicy(Policy);
367 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000369
Abramo Bagnara723df242010-12-14 22:11:44 +0000370 QualType Ty = D->getType();
371 while (ParenType* PT = dyn_cast<ParenType>(Ty)) {
372 Proto = '(' + Proto + ')';
373 Ty = PT->getInnerType();
374 }
375
376 if (isa<FunctionType>(Ty)) {
377 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000378 const FunctionProtoType *FT = 0;
379 if (D->hasWrittenPrototype())
380 FT = dyn_cast<FunctionProtoType>(AFT);
381
382 Proto += "(";
383 if (FT) {
384 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000385 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000386 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
387 if (i) POut << ", ";
388 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000391 if (FT->isVariadic()) {
392 if (D->getNumParams()) POut << ", ";
393 POut << "...";
394 }
Eli Friedman42f42c02009-05-30 04:20:30 +0000395 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
396 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
397 if (i)
398 Proto += ", ";
399 Proto += D->getParamDecl(i)->getNameAsString();
400 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000401 }
402
403 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000404
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000405 if (FT && FT->getTypeQuals()) {
406 unsigned TypeQuals = FT->getTypeQuals();
407 if (TypeQuals & Qualifiers::Const)
408 Proto += " const";
409 if (TypeQuals & Qualifiers::Volatile)
410 Proto += " volatile";
411 if (TypeQuals & Qualifiers::Restrict)
412 Proto += " restrict";
413 }
414
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000415 if (FT && FT->hasExceptionSpec()) {
416 Proto += " throw(";
417 if (FT->hasAnyExceptionSpec())
418 Proto += "...";
419 else
420 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
421 if (I)
422 Proto += ", ";
423
424
425 std::string ExceptionType;
426 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
427 Proto += ExceptionType;
428 }
429 Proto += ")";
430 }
431
Mike Stumpfd350b52009-07-27 21:33:40 +0000432 if (D->hasAttr<NoReturnAttr>())
433 Proto += " __attribute((noreturn))";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000434 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Sean Huntcbb67482011-01-08 20:30:50 +0000435 if (CDecl->getNumCtorInitializers() > 0) {
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000436 Proto += " : ";
437 Out << Proto;
438 Proto.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000439 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000440 E = CDecl->init_end();
441 B != E; ++B) {
Sean Huntcbb67482011-01-08 20:30:50 +0000442 CXXCtorInitializer * BMInitializer = (*B);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000443 if (B != CDecl->init_begin())
444 Out << ", ";
Francois Pichet00eb3f92010-12-04 09:14:42 +0000445 if (BMInitializer->isAnyMemberInitializer()) {
446 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000447 Out << FD;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000448 } else {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000449 Out << QualType(BMInitializer->getBaseClass(),
450 0).getAsString(Policy);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000451 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000452
453 Out << "(";
454 if (!BMInitializer->getInit()) {
455 // Nothing to print
456 } else {
457 Expr *Init = BMInitializer->getInit();
John McCall4765fa02010-12-06 08:20:24 +0000458 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000459 Init = Tmp->getSubExpr();
460
461 Init = Init->IgnoreParens();
462
463 Expr *SimpleInit = 0;
464 Expr **Args = 0;
465 unsigned NumArgs = 0;
466 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
467 Args = ParenList->getExprs();
468 NumArgs = ParenList->getNumExprs();
469 } else if (CXXConstructExpr *Construct
470 = dyn_cast<CXXConstructExpr>(Init)) {
471 Args = Construct->getArgs();
472 NumArgs = Construct->getNumArgs();
473 } else
474 SimpleInit = Init;
475
476 if (SimpleInit)
477 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
478 else {
479 for (unsigned I = 0; I != NumArgs; ++I) {
480 if (isa<CXXDefaultArgExpr>(Args[I]))
481 break;
482
483 if (I)
484 Out << ", ";
485 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
486 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000487 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000488 }
489 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000490 }
491 }
492 }
493 else
494 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000495 } else {
Abramo Bagnara723df242010-12-14 22:11:44 +0000496 Ty.getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000497 }
498
499 Out << Proto;
500
501 if (D->isPure())
502 Out << " = 0";
503 else if (D->isDeleted())
504 Out << " = delete";
505 else if (D->isThisDeclarationADefinition()) {
506 if (!D->hasPrototype() && D->getNumParams()) {
507 // This is a K&R function definition, so we need to print the
508 // parameters.
509 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000510 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000511 Indentation += Policy.Indentation;
512 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
513 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000514 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000515 Out << ";\n";
516 }
517 Indentation -= Policy.Indentation;
518 } else
519 Out << ' ';
520
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000521 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000522 Out << '\n';
523 }
524}
525
526void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000527 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000528 Out << "mutable ";
529
530 std::string Name = D->getNameAsString();
531 D->getType().getAsStringInternal(Name, Policy);
532 Out << Name;
533
534 if (D->isBitField()) {
535 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000536 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000537 }
538}
539
540void DeclPrinter::VisitVarDecl(VarDecl *D) {
John McCalld931b082010-08-26 03:08:43 +0000541 if (!Policy.SuppressSpecifiers && D->getStorageClass() != SC_None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000542 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
543
Eli Friedman42f42c02009-05-30 04:20:30 +0000544 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000545 Out << "__thread ";
546
547 std::string Name = D->getNameAsString();
548 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000549 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000550 T = Parm->getOriginalType();
551 T.getAsStringInternal(Name, Policy);
552 Out << Name;
Ted Kremenekbccfd312010-09-07 22:21:59 +0000553 if (Expr *Init = D->getInit()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000554 if (D->hasCXXDirectInitializer())
555 Out << "(";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000556 else {
557 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
558 if (!CCE || CCE->getConstructor()->isCopyConstructor())
559 Out << " = ";
560 }
Ted Kremenekbccfd312010-09-07 22:21:59 +0000561 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000562 if (D->hasCXXDirectInitializer())
563 Out << ")";
564 }
565}
566
567void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
568 VisitVarDecl(D);
569}
570
571void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
572 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000573 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000574 Out << ")";
575}
576
577//----------------------------------------------------------------------------
578// C++ declarations
579//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000580void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000581 Out << "namespace " << D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000582 VisitDeclContext(D);
583 Indent() << "}";
584}
585
Douglas Gregor8419fa32009-05-30 06:31:56 +0000586void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
587 Out << "using namespace ";
588 if (D->getQualifier())
589 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000590 Out << D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000591}
592
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000593void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000594 Out << "namespace " << D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000595 if (D->getQualifier())
596 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000597 Out << D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000598}
599
Douglas Gregor59e63572009-05-30 06:58:37 +0000600void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
601 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000602 if (D->getIdentifier())
603 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Douglas Gregor59e63572009-05-30 06:58:37 +0000605 if (D->isDefinition()) {
606 // Print the base classes
607 if (D->getNumBases()) {
608 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000609 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
610 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000611 if (Base != D->bases_begin())
612 Out << ", ";
613
614 if (Base->isVirtual())
615 Out << "virtual ";
616
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000617 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
618 if (AS != AS_none)
619 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000620 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor59e63572009-05-30 06:58:37 +0000621 }
622 }
623
624 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000625 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000626 Out << " {\n";
627 VisitDeclContext(D);
628 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000629 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000630}
631
632void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
633 const char *l;
634 if (D->getLanguage() == LinkageSpecDecl::lang_c)
635 l = "C";
636 else {
637 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
638 "unknown language in linkage specification");
639 l = "C++";
640 }
641
642 Out << "extern \"" << l << "\" ";
643 if (D->hasBraces()) {
644 Out << "{\n";
645 VisitDeclContext(D);
646 Indent() << "}";
647 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000648 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000649}
650
651void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000652 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Anders Carlsson0487f662009-06-04 05:37:43 +0000654 TemplateParameterList *Params = D->getTemplateParameters();
655 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
656 if (i != 0)
657 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Anders Carlsson0487f662009-06-04 05:37:43 +0000659 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000660 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000661 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000662
663 QualType ParamType =
Anders Carlsson0487f662009-06-04 05:37:43 +0000664 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
665
666 if (TTP->wasDeclaredWithTypename())
667 Out << "typename ";
668 else
669 Out << "class ";
670
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000671 if (TTP->isParameterPack())
672 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Douglas Gregorefed5c82010-06-16 15:23:05 +0000674 Out << ParamType.getAsString(Policy);
Anders Carlsson0487f662009-06-04 05:37:43 +0000675
676 if (TTP->hasDefaultArgument()) {
677 Out << " = ";
678 Out << TTP->getDefaultArgument().getAsString(Policy);
679 };
Mike Stump1eb44332009-09-09 15:08:12 +0000680 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000681 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
682 Out << NTTP->getType().getAsString(Policy);
683
Douglas Gregor56bc9832010-12-24 00:15:10 +0000684 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
685 Out << "...";
686
Anders Carlsson0487f662009-06-04 05:37:43 +0000687 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
688 Out << ' ';
689 Out << Name->getName();
690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Anders Carlsson0487f662009-06-04 05:37:43 +0000692 if (NTTP->hasDefaultArgument()) {
693 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000694 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000695 Indentation);
696 }
697 }
698 }
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Anders Carlsson0487f662009-06-04 05:37:43 +0000700 Out << "> ";
701
Douglas Gregor61c4d282011-01-05 15:48:55 +0000702 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
703 Out << "class ";
704 if (TTP->isParameterPack())
705 Out << "...";
706 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000707 } else {
708 Visit(D->getTemplatedDecl());
709 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000710}
711
712//----------------------------------------------------------------------------
713// Objective-C declarations
714//----------------------------------------------------------------------------
715
716void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
717 Out << "@class ";
718 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
719 I != E; ++I) {
720 if (I != D->begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000721 Out << I->getInterface();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000722 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000723}
724
725void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
726 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000727 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000728 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000729 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000730 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000731 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000733 std::string name = OMD->getSelector().getAsString();
734 std::string::size_type pos, lastPos = 0;
735 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
736 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000737 // FIXME: selector is missing here!
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000738 pos = name.find_first_of(":", lastPos);
739 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000740 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000741 lastPos = pos + 1;
742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000744 if (OMD->param_begin() == OMD->param_end())
745 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000747 if (OMD->isVariadic())
748 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000750 if (OMD->getBody()) {
751 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000752 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000753 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000754 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000755}
756
757void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
758 std::string I = OID->getNameAsString();
759 ObjCInterfaceDecl *SID = OID->getSuperClass();
760
761 if (SID)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000762 Out << "@implementation " << I << " : " << SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000763 else
764 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000765 Out << "\n";
766 VisitDeclContext(OID, false);
767 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000768}
769
770void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
771 std::string I = OID->getNameAsString();
772 ObjCInterfaceDecl *SID = OID->getSuperClass();
773
774 if (SID)
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000775 Out << "@interface " << I << " : " << SID;
776 else
777 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000779 // Protocols?
780 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
781 if (!Protocols.empty()) {
782 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
783 E = Protocols.end(); I != E; ++I)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000784 Out << (I == Protocols.begin() ? '<' : ',') << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000787 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000788 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000790 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000791 Out << "{\n";
792 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000793 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
794 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000795 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000796 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000797 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000798 Out << "}\n";
799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000801 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000802 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000803 // FIXME: implement the rest...
804}
805
806void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
807 Out << "@protocol ";
Mike Stump1eb44332009-09-09 15:08:12 +0000808 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000809 E = D->protocol_end();
810 I != E; ++I) {
811 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000812 Out << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000813 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000814}
815
816void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000817 Out << "@protocol " << PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000818 VisitDeclContext(PID, false);
819 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000820}
821
822void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000823 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000824
825 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000826 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000827 // FIXME: implement the rest...
828}
829
830void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000831 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000832 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000833 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000835 // FIXME: implement the rest...
836}
837
838void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000839 Out << "@compatibility_alias " << AID
840 << ' ' << AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000841}
842
843/// PrintObjCPropertyDecl - print a property declaration.
844///
845void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
846 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
847 Out << "@required\n";
848 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
849 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000851 Out << "@property";
852 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
853 bool first = true;
854 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000855 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000856 ObjCPropertyDecl::OBJC_PR_readonly) {
857 Out << (first ? ' ' : ',') << "readonly";
858 first = false;
859 }
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000861 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
862 Out << (first ? ' ' : ',') << "getter = "
863 << PDecl->getGetterName().getAsString();
864 first = false;
865 }
866 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
867 Out << (first ? ' ' : ',') << "setter = "
868 << PDecl->getSetterName().getAsString();
869 first = false;
870 }
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000872 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
873 Out << (first ? ' ' : ',') << "assign";
874 first = false;
875 }
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000877 if (PDecl->getPropertyAttributes() &
878 ObjCPropertyDecl::OBJC_PR_readwrite) {
879 Out << (first ? ' ' : ',') << "readwrite";
880 first = false;
881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000883 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
884 Out << (first ? ' ' : ',') << "retain";
885 first = false;
886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000888 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
889 Out << (first ? ' ' : ',') << "copy";
890 first = false;
891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
893 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000894 ObjCPropertyDecl::OBJC_PR_nonatomic) {
895 Out << (first ? ' ' : ',') << "nonatomic";
896 first = false;
897 }
898 Out << " )";
899 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000900 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000901}
902
903void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
904 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000905 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000906 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000907 Out << "@dynamic ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000908 Out << PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000909 if (PID->getPropertyIvarDecl())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000910 Out << '=' << PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000911}
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000912
913void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
914 Out << "using ";
915 D->getTargetNestedNameDecl()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000916 Out << D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000917}
918
John McCall7ba107a2009-11-18 02:36:19 +0000919void
920DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
921 Out << "using typename ";
922 D->getTargetNestedNameSpecifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000923 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +0000924}
925
926void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000927 Out << "using ";
928 D->getTargetNestedNameSpecifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000929 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000930}
John McCall9488ea12009-11-17 05:59:44 +0000931
932void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
933 // ignore
934}