blob: 49f27234c0436b65d887c9f78f9acc1f3f9ee44c [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);
Richard Smith162e1c12011-04-15 14:24:37 +000048 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000049 void VisitEnumDecl(EnumDecl *D);
50 void VisitRecordDecl(RecordDecl *D);
51 void VisitEnumConstantDecl(EnumConstantDecl *D);
52 void VisitFunctionDecl(FunctionDecl *D);
53 void VisitFieldDecl(FieldDecl *D);
54 void VisitVarDecl(VarDecl *D);
Chris Lattner57ad3782011-02-17 20:34:02 +000055 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000056 void VisitParmVarDecl(ParmVarDecl *D);
57 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +000058 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000059 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000060 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000061 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000062 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000063 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith98a57862011-04-15 13:38:57 +000064 void VisitTemplateDecl(const TemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000065 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000066 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000067 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
68 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
69 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
70 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
71 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
72 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
73 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
74 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
75 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000076 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
77 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000078 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000079 void VisitUsingShadowDecl(UsingShadowDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000080 };
81}
82
Anders Carlssonf88df862009-09-26 21:58:53 +000083void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000084 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000085}
86
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000087void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Anders Carlssonf88df862009-09-26 21:58:53 +000088 unsigned Indentation) const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000089 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Anders Carlssonf88df862009-09-26 21:58:53 +000090 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000091}
92
Eli Friedman42f42c02009-05-30 04:20:30 +000093static QualType GetBaseType(QualType T) {
94 // FIXME: This should be on the Type class!
95 QualType BaseType = T;
96 while (!BaseType->isSpecifierType()) {
97 if (isa<TypedefType>(BaseType))
98 break;
Ted Kremenek6217b802009-07-29 21:53:49 +000099 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000100 BaseType = PTy->getPointeeType();
101 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
102 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +0000103 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000104 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000105 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000106 BaseType = VTy->getElementType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000107 else
108 assert(0 && "Unknown declarator!");
109 }
110 return BaseType;
111}
112
113static QualType getDeclType(Decl* D) {
Richard Smith162e1c12011-04-15 14:24:37 +0000114 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman42f42c02009-05-30 04:20:30 +0000115 return TDD->getUnderlyingType();
116 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
117 return VD->getType();
118 return QualType();
119}
120
121void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000122 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000123 unsigned Indentation) {
124 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000125 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000126 return;
127 }
128
129 Decl** End = Begin + NumDecls;
130 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
131 if (TD)
132 ++Begin;
133
134 PrintingPolicy SubPolicy(Policy);
135 if (TD && TD->isDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000136 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000137 Out << " ";
138 SubPolicy.SuppressTag = true;
139 }
140
141 bool isFirst = true;
142 for ( ; Begin != End; ++Begin) {
143 if (isFirst) {
144 SubPolicy.SuppressSpecifiers = false;
145 isFirst = false;
146 } else {
147 if (!isFirst) Out << ", ";
148 SubPolicy.SuppressSpecifiers = true;
149 }
150
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000151 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000152 }
153}
154
Anders Carlsson84834432009-12-14 00:51:04 +0000155void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000156 // Get the translation unit
157 const DeclContext *DC = this;
158 while (!DC->isTranslationUnit())
159 DC = DC->getParent();
160
161 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
162 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
163 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
164}
165
Anders Carlssonf88df862009-09-26 21:58:53 +0000166void Decl::dump() const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000167 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000168}
169
Daniel Dunbar512ce602009-11-21 09:12:06 +0000170llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
171 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000172 Out << " ";
173 return Out;
174}
175
Eli Friedman42f42c02009-05-30 04:20:30 +0000176void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
177 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000178 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000179 Out << ";\n";
180 Decls.clear();
181
182}
183
Anders Carlsson0d592922009-08-28 22:39:52 +0000184void DeclPrinter::Print(AccessSpecifier AS) {
185 switch(AS) {
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000186 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000187 case AS_public: Out << "public"; break;
188 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000189 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000190 }
191}
192
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000193//----------------------------------------------------------------------------
194// Common C declarations
195//----------------------------------------------------------------------------
196
197void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
198 if (Indent)
199 Indentation += Policy.Indentation;
200
Eli Friedman42f42c02009-05-30 04:20:30 +0000201 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000202 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000203 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-07-30 00:47:46 +0000204
205 // Don't print ObjCIvarDecls, as they are printed when visiting the
206 // containing ObjCInterfaceDecl.
207 if (isa<ObjCIvarDecl>(*D))
208 continue;
209
Eli Friedman48d14a22009-05-30 05:03:24 +0000210 if (!Policy.Dump) {
211 // Skip over implicit declarations in pretty-printing mode.
212 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000213 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidis2574f6f2010-06-17 10:52:11 +0000214 // of __builtin_va_list or __[u]int128_t. There should be some other way
215 // to check that.
216 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
217 if (IdentifierInfo *II = ND->getIdentifier()) {
218 if (II->isStr("__builtin_va_list") ||
219 II->isStr("__int128_t") || II->isStr("__uint128_t"))
220 continue;
221 }
222 }
Eli Friedman48d14a22009-05-30 05:03:24 +0000223 }
224
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 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000253
254 if (isa<AccessSpecDecl>(*D)) {
255 Indentation -= Policy.Indentation;
256 this->Indent();
257 Print(D->getAccess());
258 Out << ":\n";
259 Indentation += Policy.Indentation;
260 continue;
261 }
262
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000263 this->Indent();
264 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000265
266 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000267 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000268 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000269 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
270 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000271 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
272 Terminator = 0;
273 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000274 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000275 isa<ObjCInterfaceDecl>(*D) ||
276 isa<ObjCProtocolDecl>(*D) ||
277 isa<ObjCCategoryImplDecl>(*D) ||
278 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000279 Terminator = 0;
280 else if (isa<EnumConstantDecl>(*D)) {
281 DeclContext::decl_iterator Next = D;
282 ++Next;
283 if (Next != DEnd)
284 Terminator = ",";
285 } else
286 Terminator = ";";
287
288 if (Terminator)
289 Out << Terminator;
290 Out << "\n";
291 }
292
Eli Friedman42f42c02009-05-30 04:20:30 +0000293 if (!Decls.empty())
294 ProcessDeclGroup(Decls);
295
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000296 if (Indent)
297 Indentation -= Policy.Indentation;
298}
299
300void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
301 VisitDeclContext(D, false);
302}
303
304void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
305 std::string S = D->getNameAsString();
306 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +0000307 if (!Policy.SuppressSpecifiers)
308 Out << "typedef ";
309 Out << S;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000310}
311
Richard Smith162e1c12011-04-15 14:24:37 +0000312void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
313 Out << "using " << D->getNameAsString() << " = "
314 << D->getUnderlyingType().getAsString(Policy);
315}
316
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000317void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000318 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000319 if (D->isScoped()) {
320 if (D->isScopedUsingClassTag())
321 Out << "class ";
322 else
323 Out << "struct ";
324 }
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000325 Out << D;
326
327 if (D->isFixed()) {
328 std::string Underlying;
329 D->getIntegerType().getAsStringInternal(Underlying, Policy);
330 Out << " : " << Underlying;
331 }
332
333 if (D->isDefinition()) {
334 Out << " {\n";
335 VisitDeclContext(D);
336 Indent() << "}";
337 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000338}
339
340void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000341 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000342 if (D->getIdentifier())
343 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000345 if (D->isDefinition()) {
346 Out << " {\n";
347 VisitDeclContext(D);
348 Indent() << "}";
349 }
350}
351
352void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000353 Out << D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000354 if (Expr *Init = D->getInitExpr()) {
355 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000356 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000357 }
358}
359
Mike Stump1eb44332009-09-09 15:08:12 +0000360void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000361 if (!Policy.SuppressSpecifiers) {
362 switch (D->getStorageClass()) {
John McCalld931b082010-08-26 03:08:43 +0000363 case SC_None: break;
364 case SC_Extern: Out << "extern "; break;
365 case SC_Static: Out << "static "; break;
366 case SC_PrivateExtern: Out << "__private_extern__ "; break;
367 case SC_Auto: case SC_Register: llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000368 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000369
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000370 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000371 if (D->isVirtualAsWritten()) Out << "virtual ";
372 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000373
Douglas Gregor6620a622009-05-30 05:39:39 +0000374 PrintingPolicy SubPolicy(Policy);
375 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000376 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000377
Abramo Bagnara723df242010-12-14 22:11:44 +0000378 QualType Ty = D->getType();
John McCallf4c73712011-01-19 06:33:43 +0000379 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000380 Proto = '(' + Proto + ')';
381 Ty = PT->getInnerType();
382 }
383
384 if (isa<FunctionType>(Ty)) {
385 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000386 const FunctionProtoType *FT = 0;
387 if (D->hasWrittenPrototype())
388 FT = dyn_cast<FunctionProtoType>(AFT);
389
390 Proto += "(";
391 if (FT) {
392 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000393 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000394 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
395 if (i) POut << ", ";
396 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000399 if (FT->isVariadic()) {
400 if (D->getNumParams()) POut << ", ";
401 POut << "...";
402 }
Sean Hunt10620eb2011-05-06 20:44:56 +0000403 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000404 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
405 if (i)
406 Proto += ", ";
407 Proto += D->getParamDecl(i)->getNameAsString();
408 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000409 }
410
411 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000412
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000413 if (FT && FT->getTypeQuals()) {
414 unsigned TypeQuals = FT->getTypeQuals();
415 if (TypeQuals & Qualifiers::Const)
416 Proto += " const";
417 if (TypeQuals & Qualifiers::Volatile)
418 Proto += " volatile";
419 if (TypeQuals & Qualifiers::Restrict)
420 Proto += " restrict";
421 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000422
423 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000424 Proto += " throw(";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000425 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000426 Proto += "...";
427 else
428 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
429 if (I)
430 Proto += ", ";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000431
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000432 std::string ExceptionType;
433 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
434 Proto += ExceptionType;
435 }
436 Proto += ")";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000437 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
438 Proto += " noexcept";
439 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
440 Proto += "(";
441 llvm::raw_string_ostream EOut(Proto);
442 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
443 Indentation);
444 EOut.flush();
445 Proto += EOut.str();
446 Proto += ")";
447 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000448 }
449
Mike Stumpfd350b52009-07-27 21:33:40 +0000450 if (D->hasAttr<NoReturnAttr>())
451 Proto += " __attribute((noreturn))";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000452 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Sean Huntcbb67482011-01-08 20:30:50 +0000453 if (CDecl->getNumCtorInitializers() > 0) {
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000454 Proto += " : ";
455 Out << Proto;
456 Proto.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000457 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000458 E = CDecl->init_end();
459 B != E; ++B) {
Sean Huntcbb67482011-01-08 20:30:50 +0000460 CXXCtorInitializer * BMInitializer = (*B);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000461 if (B != CDecl->init_begin())
462 Out << ", ";
Francois Pichet00eb3f92010-12-04 09:14:42 +0000463 if (BMInitializer->isAnyMemberInitializer()) {
464 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000465 Out << FD;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000466 } else {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000467 Out << QualType(BMInitializer->getBaseClass(),
468 0).getAsString(Policy);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000469 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000470
471 Out << "(";
472 if (!BMInitializer->getInit()) {
473 // Nothing to print
474 } else {
475 Expr *Init = BMInitializer->getInit();
John McCall4765fa02010-12-06 08:20:24 +0000476 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000477 Init = Tmp->getSubExpr();
478
479 Init = Init->IgnoreParens();
480
481 Expr *SimpleInit = 0;
482 Expr **Args = 0;
483 unsigned NumArgs = 0;
484 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
485 Args = ParenList->getExprs();
486 NumArgs = ParenList->getNumExprs();
487 } else if (CXXConstructExpr *Construct
488 = dyn_cast<CXXConstructExpr>(Init)) {
489 Args = Construct->getArgs();
490 NumArgs = Construct->getNumArgs();
491 } else
492 SimpleInit = Init;
493
494 if (SimpleInit)
495 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
496 else {
497 for (unsigned I = 0; I != NumArgs; ++I) {
498 if (isa<CXXDefaultArgExpr>(Args[I]))
499 break;
500
501 if (I)
502 Out << ", ";
503 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
504 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000505 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000506 }
507 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000508 }
509 }
510 }
511 else
512 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000513 } else {
Abramo Bagnara723df242010-12-14 22:11:44 +0000514 Ty.getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000515 }
516
517 Out << Proto;
518
519 if (D->isPure())
520 Out << " = 0";
Sean Hunt10620eb2011-05-06 20:44:56 +0000521 else if (D->isDeletedAsWritten())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000522 Out << " = delete";
Sean Hunt10620eb2011-05-06 20:44:56 +0000523 else if (D->doesThisDeclarationHaveABody()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000524 if (!D->hasPrototype() && D->getNumParams()) {
525 // This is a K&R function definition, so we need to print the
526 // parameters.
527 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000528 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000529 Indentation += Policy.Indentation;
530 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
531 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000532 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000533 Out << ";\n";
534 }
535 Indentation -= Policy.Indentation;
536 } else
537 Out << ' ';
538
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000539 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000540 Out << '\n';
541 }
542}
543
544void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000545 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000546 Out << "mutable ";
547
548 std::string Name = D->getNameAsString();
549 D->getType().getAsStringInternal(Name, Policy);
550 Out << Name;
551
552 if (D->isBitField()) {
553 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000554 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000555 }
556}
557
Chris Lattner57ad3782011-02-17 20:34:02 +0000558void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
559 Out << D->getNameAsString() << ":";
560}
561
562
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000563void DeclPrinter::VisitVarDecl(VarDecl *D) {
John McCalld931b082010-08-26 03:08:43 +0000564 if (!Policy.SuppressSpecifiers && D->getStorageClass() != SC_None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000565 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
566
Eli Friedman42f42c02009-05-30 04:20:30 +0000567 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000568 Out << "__thread ";
569
570 std::string Name = D->getNameAsString();
571 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000572 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000573 T = Parm->getOriginalType();
574 T.getAsStringInternal(Name, Policy);
575 Out << Name;
Richard Smithad762fc2011-04-14 22:09:26 +0000576 Expr *Init = D->getInit();
577 if (!Policy.SuppressInitializers && Init) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000578 if (D->hasCXXDirectInitializer())
579 Out << "(";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000580 else {
581 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
582 if (!CCE || CCE->getConstructor()->isCopyConstructor())
583 Out << " = ";
584 }
Ted Kremenekbccfd312010-09-07 22:21:59 +0000585 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000586 if (D->hasCXXDirectInitializer())
587 Out << ")";
588 }
589}
590
591void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
592 VisitVarDecl(D);
593}
594
595void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
596 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000597 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000598 Out << ")";
599}
600
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000601void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
602 Out << "static_assert(";
603 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
604 Out << ", ";
605 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
606 Out << ")";
607}
608
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000609//----------------------------------------------------------------------------
610// C++ declarations
611//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000612void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000613 Out << "namespace " << D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000614 VisitDeclContext(D);
615 Indent() << "}";
616}
617
Douglas Gregor8419fa32009-05-30 06:31:56 +0000618void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
619 Out << "using namespace ";
620 if (D->getQualifier())
621 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000622 Out << D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000623}
624
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000625void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000626 Out << "namespace " << D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000627 if (D->getQualifier())
628 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000629 Out << D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000630}
631
Douglas Gregor59e63572009-05-30 06:58:37 +0000632void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
633 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000634 if (D->getIdentifier())
635 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Douglas Gregor59e63572009-05-30 06:58:37 +0000637 if (D->isDefinition()) {
638 // Print the base classes
639 if (D->getNumBases()) {
640 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000641 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
642 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000643 if (Base != D->bases_begin())
644 Out << ", ";
645
646 if (Base->isVirtual())
647 Out << "virtual ";
648
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000649 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
650 if (AS != AS_none)
651 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000652 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor34a99e72011-04-27 17:07:55 +0000653
654 if (Base->isPackExpansion())
655 Out << "...";
Douglas Gregor59e63572009-05-30 06:58:37 +0000656 }
657 }
658
659 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000660 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000661 Out << " {\n";
662 VisitDeclContext(D);
663 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000664 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000665}
666
667void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
668 const char *l;
669 if (D->getLanguage() == LinkageSpecDecl::lang_c)
670 l = "C";
671 else {
672 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
673 "unknown language in linkage specification");
674 l = "C++";
675 }
676
677 Out << "extern \"" << l << "\" ";
678 if (D->hasBraces()) {
679 Out << "{\n";
680 VisitDeclContext(D);
681 Indent() << "}";
682 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000683 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000684}
685
Richard Smith98a57862011-04-15 13:38:57 +0000686void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000687 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Anders Carlsson0487f662009-06-04 05:37:43 +0000689 TemplateParameterList *Params = D->getTemplateParameters();
690 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
691 if (i != 0)
692 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Anders Carlsson0487f662009-06-04 05:37:43 +0000694 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000695 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000696 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Anders Carlsson0487f662009-06-04 05:37:43 +0000698 if (TTP->wasDeclaredWithTypename())
699 Out << "typename ";
700 else
701 Out << "class ";
702
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000703 if (TTP->isParameterPack())
704 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000706 Out << TTP->getNameAsString();
Anders Carlsson0487f662009-06-04 05:37:43 +0000707
708 if (TTP->hasDefaultArgument()) {
709 Out << " = ";
710 Out << TTP->getDefaultArgument().getAsString(Policy);
711 };
Mike Stump1eb44332009-09-09 15:08:12 +0000712 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000713 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
714 Out << NTTP->getType().getAsString(Policy);
715
Douglas Gregor56bc9832010-12-24 00:15:10 +0000716 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
717 Out << "...";
718
Anders Carlsson0487f662009-06-04 05:37:43 +0000719 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
720 Out << ' ';
721 Out << Name->getName();
722 }
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Anders Carlsson0487f662009-06-04 05:37:43 +0000724 if (NTTP->hasDefaultArgument()) {
725 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000726 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000727 Indentation);
728 }
Richard Smith98a57862011-04-15 13:38:57 +0000729 } else if (const TemplateTemplateParmDecl *TTPD =
730 dyn_cast<TemplateTemplateParmDecl>(Param)) {
731 VisitTemplateDecl(TTPD);
732 // FIXME: print the default argument, if present.
Anders Carlsson0487f662009-06-04 05:37:43 +0000733 }
734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Anders Carlsson0487f662009-06-04 05:37:43 +0000736 Out << "> ";
737
Richard Smith98a57862011-04-15 13:38:57 +0000738 if (const TemplateTemplateParmDecl *TTP =
739 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000740 Out << "class ";
741 if (TTP->isParameterPack())
742 Out << "...";
743 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000744 } else {
745 Visit(D->getTemplatedDecl());
746 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000747}
748
749//----------------------------------------------------------------------------
750// Objective-C declarations
751//----------------------------------------------------------------------------
752
753void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
754 Out << "@class ";
755 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
756 I != E; ++I) {
757 if (I != D->begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000758 Out << I->getInterface();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000759 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000760}
761
762void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
763 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000764 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000765 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000766 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000767 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000768 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000770 std::string name = OMD->getSelector().getAsString();
771 std::string::size_type pos, lastPos = 0;
772 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
773 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000774 // FIXME: selector is missing here!
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000775 pos = name.find_first_of(":", lastPos);
776 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000777 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000778 lastPos = pos + 1;
779 }
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000781 if (OMD->param_begin() == OMD->param_end())
782 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000784 if (OMD->isVariadic())
785 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000787 if (OMD->getBody()) {
788 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000789 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000790 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000791 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000792}
793
794void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
795 std::string I = OID->getNameAsString();
796 ObjCInterfaceDecl *SID = OID->getSuperClass();
797
798 if (SID)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000799 Out << "@implementation " << I << " : " << SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000800 else
801 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000802 Out << "\n";
803 VisitDeclContext(OID, false);
804 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000805}
806
807void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
808 std::string I = OID->getNameAsString();
809 ObjCInterfaceDecl *SID = OID->getSuperClass();
810
811 if (SID)
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000812 Out << "@interface " << I << " : " << SID;
813 else
814 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000816 // Protocols?
817 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
818 if (!Protocols.empty()) {
819 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
820 E = Protocols.end(); I != E; ++I)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000821 Out << (I == Protocols.begin() ? '<' : ',') << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000824 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000825 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000827 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000828 Out << "{\n";
829 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000830 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
831 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000832 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000833 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000834 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000835 Out << "}\n";
836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000838 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000839 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000840 // FIXME: implement the rest...
841}
842
843void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
844 Out << "@protocol ";
Mike Stump1eb44332009-09-09 15:08:12 +0000845 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000846 E = D->protocol_end();
847 I != E; ++I) {
848 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000849 Out << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000850 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000851}
852
853void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000854 Out << "@protocol " << PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000855 VisitDeclContext(PID, false);
856 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000857}
858
859void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000860 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000861
862 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000863 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000864 // FIXME: implement the rest...
865}
866
867void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000868 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000869 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000870 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000872 // FIXME: implement the rest...
873}
874
875void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000876 Out << "@compatibility_alias " << AID
877 << ' ' << AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000878}
879
880/// PrintObjCPropertyDecl - print a property declaration.
881///
882void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
883 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
884 Out << "@required\n";
885 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
886 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000888 Out << "@property";
889 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
890 bool first = true;
891 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000892 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000893 ObjCPropertyDecl::OBJC_PR_readonly) {
894 Out << (first ? ' ' : ',') << "readonly";
895 first = false;
896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000898 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
899 Out << (first ? ' ' : ',') << "getter = "
900 << PDecl->getGetterName().getAsString();
901 first = false;
902 }
903 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
904 Out << (first ? ' ' : ',') << "setter = "
905 << PDecl->getSetterName().getAsString();
906 first = false;
907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000909 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
910 Out << (first ? ' ' : ',') << "assign";
911 first = false;
912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000914 if (PDecl->getPropertyAttributes() &
915 ObjCPropertyDecl::OBJC_PR_readwrite) {
916 Out << (first ? ' ' : ',') << "readwrite";
917 first = false;
918 }
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000920 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
921 Out << (first ? ' ' : ',') << "retain";
922 first = false;
923 }
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000925 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
926 Out << (first ? ' ' : ',') << "copy";
927 first = false;
928 }
Mike Stump1eb44332009-09-09 15:08:12 +0000929
930 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000931 ObjCPropertyDecl::OBJC_PR_nonatomic) {
932 Out << (first ? ' ' : ',') << "nonatomic";
933 first = false;
934 }
935 Out << " )";
936 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000937 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000938}
939
940void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
941 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000942 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000943 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000944 Out << "@dynamic ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000945 Out << PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000946 if (PID->getPropertyIvarDecl())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000947 Out << '=' << PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000948}
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000949
950void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
951 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +0000952 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000953 Out << D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000954}
955
John McCall7ba107a2009-11-18 02:36:19 +0000956void
957DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
958 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +0000959 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000960 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +0000961}
962
963void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000964 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +0000965 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000966 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000967}
John McCall9488ea12009-11-17 05:59:44 +0000968
969void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
970 // ignore
971}