blob: 421770ea70fbe0810b8914420acccd9a5881cc3b [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)) {
Richard Smith7a614d82011-06-11 17:19:42 +0000453 bool HasInitializerList = false;
454 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
455 E = CDecl->init_end();
456 B != E; ++B) {
457 CXXCtorInitializer * BMInitializer = (*B);
458 if (BMInitializer->isInClassMemberInitializer())
459 continue;
460
461 if (!HasInitializerList) {
462 Proto += " : ";
463 Out << Proto;
464 Proto.clear();
465 HasInitializerList = true;
466 } else
467 Out << ", ";
468
469 if (BMInitializer->isAnyMemberInitializer()) {
470 FieldDecl *FD = BMInitializer->getAnyMember();
471 Out << FD;
472 } else {
473 Out << QualType(BMInitializer->getBaseClass(),
474 0).getAsString(Policy);
475 }
476
477 Out << "(";
478 if (!BMInitializer->getInit()) {
479 // Nothing to print
480 } else {
481 Expr *Init = BMInitializer->getInit();
482 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
483 Init = Tmp->getSubExpr();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000484
Richard Smith7a614d82011-06-11 17:19:42 +0000485 Init = Init->IgnoreParens();
486
487 Expr *SimpleInit = 0;
488 Expr **Args = 0;
489 unsigned NumArgs = 0;
490 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
491 Args = ParenList->getExprs();
492 NumArgs = ParenList->getNumExprs();
493 } else if (CXXConstructExpr *Construct
494 = dyn_cast<CXXConstructExpr>(Init)) {
495 Args = Construct->getArgs();
496 NumArgs = Construct->getNumArgs();
497 } else
498 SimpleInit = Init;
499
500 if (SimpleInit)
501 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
502 else {
503 for (unsigned I = 0; I != NumArgs; ++I) {
504 if (isa<CXXDefaultArgExpr>(Args[I]))
505 break;
506
507 if (I)
508 Out << ", ";
509 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000510 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000511 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000512 }
Richard Smith7a614d82011-06-11 17:19:42 +0000513 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000514 }
515 }
516 else
517 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000518 } else {
Abramo Bagnara723df242010-12-14 22:11:44 +0000519 Ty.getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000520 }
521
522 Out << Proto;
523
524 if (D->isPure())
525 Out << " = 0";
Sean Hunt10620eb2011-05-06 20:44:56 +0000526 else if (D->isDeletedAsWritten())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000527 Out << " = delete";
Sean Hunt10620eb2011-05-06 20:44:56 +0000528 else if (D->doesThisDeclarationHaveABody()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000529 if (!D->hasPrototype() && D->getNumParams()) {
530 // This is a K&R function definition, so we need to print the
531 // parameters.
532 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000533 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000534 Indentation += Policy.Indentation;
535 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
536 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000537 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000538 Out << ";\n";
539 }
540 Indentation -= Policy.Indentation;
541 } else
542 Out << ' ';
543
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000544 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000545 Out << '\n';
546 }
547}
548
549void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000550 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000551 Out << "mutable ";
552
553 std::string Name = D->getNameAsString();
554 D->getType().getAsStringInternal(Name, Policy);
555 Out << Name;
556
557 if (D->isBitField()) {
558 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000559 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000560 }
Richard Smith7a614d82011-06-11 17:19:42 +0000561
562 Expr *Init = D->getInClassInitializer();
563 if (!Policy.SuppressInitializers && Init) {
564 Out << " = ";
565 Init->printPretty(Out, Context, 0, Policy, Indentation);
566 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000567}
568
Chris Lattner57ad3782011-02-17 20:34:02 +0000569void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
570 Out << D->getNameAsString() << ":";
571}
572
573
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000574void DeclPrinter::VisitVarDecl(VarDecl *D) {
John McCalld931b082010-08-26 03:08:43 +0000575 if (!Policy.SuppressSpecifiers && D->getStorageClass() != SC_None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000576 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
577
Eli Friedman42f42c02009-05-30 04:20:30 +0000578 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000579 Out << "__thread ";
580
581 std::string Name = D->getNameAsString();
582 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000583 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000584 T = Parm->getOriginalType();
585 T.getAsStringInternal(Name, Policy);
586 Out << Name;
Richard Smithad762fc2011-04-14 22:09:26 +0000587 Expr *Init = D->getInit();
588 if (!Policy.SuppressInitializers && Init) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000589 if (D->hasCXXDirectInitializer())
590 Out << "(";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000591 else {
592 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
593 if (!CCE || CCE->getConstructor()->isCopyConstructor())
594 Out << " = ";
595 }
Ted Kremenekbccfd312010-09-07 22:21:59 +0000596 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000597 if (D->hasCXXDirectInitializer())
598 Out << ")";
599 }
600}
601
602void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
603 VisitVarDecl(D);
604}
605
606void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
607 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000608 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000609 Out << ")";
610}
611
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000612void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
613 Out << "static_assert(";
614 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
615 Out << ", ";
616 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
617 Out << ")";
618}
619
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000620//----------------------------------------------------------------------------
621// C++ declarations
622//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000623void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000624 Out << "namespace " << D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000625 VisitDeclContext(D);
626 Indent() << "}";
627}
628
Douglas Gregor8419fa32009-05-30 06:31:56 +0000629void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
630 Out << "using namespace ";
631 if (D->getQualifier())
632 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000633 Out << D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000634}
635
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000636void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000637 Out << "namespace " << D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000638 if (D->getQualifier())
639 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000640 Out << D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000641}
642
Douglas Gregor59e63572009-05-30 06:58:37 +0000643void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
644 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000645 if (D->getIdentifier())
646 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Douglas Gregor59e63572009-05-30 06:58:37 +0000648 if (D->isDefinition()) {
649 // Print the base classes
650 if (D->getNumBases()) {
651 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000652 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
653 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000654 if (Base != D->bases_begin())
655 Out << ", ";
656
657 if (Base->isVirtual())
658 Out << "virtual ";
659
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000660 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
661 if (AS != AS_none)
662 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000663 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor34a99e72011-04-27 17:07:55 +0000664
665 if (Base->isPackExpansion())
666 Out << "...";
Douglas Gregor59e63572009-05-30 06:58:37 +0000667 }
668 }
669
670 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000671 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000672 Out << " {\n";
673 VisitDeclContext(D);
674 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000675 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000676}
677
678void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
679 const char *l;
680 if (D->getLanguage() == LinkageSpecDecl::lang_c)
681 l = "C";
682 else {
683 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
684 "unknown language in linkage specification");
685 l = "C++";
686 }
687
688 Out << "extern \"" << l << "\" ";
689 if (D->hasBraces()) {
690 Out << "{\n";
691 VisitDeclContext(D);
692 Indent() << "}";
693 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000694 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000695}
696
Richard Smith98a57862011-04-15 13:38:57 +0000697void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000698 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Anders Carlsson0487f662009-06-04 05:37:43 +0000700 TemplateParameterList *Params = D->getTemplateParameters();
701 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
702 if (i != 0)
703 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Anders Carlsson0487f662009-06-04 05:37:43 +0000705 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000706 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000707 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Anders Carlsson0487f662009-06-04 05:37:43 +0000709 if (TTP->wasDeclaredWithTypename())
710 Out << "typename ";
711 else
712 Out << "class ";
713
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000714 if (TTP->isParameterPack())
715 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000717 Out << TTP->getNameAsString();
Anders Carlsson0487f662009-06-04 05:37:43 +0000718
719 if (TTP->hasDefaultArgument()) {
720 Out << " = ";
721 Out << TTP->getDefaultArgument().getAsString(Policy);
722 };
Mike Stump1eb44332009-09-09 15:08:12 +0000723 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000724 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
725 Out << NTTP->getType().getAsString(Policy);
726
Douglas Gregor56bc9832010-12-24 00:15:10 +0000727 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
728 Out << "...";
729
Anders Carlsson0487f662009-06-04 05:37:43 +0000730 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
731 Out << ' ';
732 Out << Name->getName();
733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Anders Carlsson0487f662009-06-04 05:37:43 +0000735 if (NTTP->hasDefaultArgument()) {
736 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000737 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000738 Indentation);
739 }
Richard Smith98a57862011-04-15 13:38:57 +0000740 } else if (const TemplateTemplateParmDecl *TTPD =
741 dyn_cast<TemplateTemplateParmDecl>(Param)) {
742 VisitTemplateDecl(TTPD);
743 // FIXME: print the default argument, if present.
Anders Carlsson0487f662009-06-04 05:37:43 +0000744 }
745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Anders Carlsson0487f662009-06-04 05:37:43 +0000747 Out << "> ";
748
Richard Smith98a57862011-04-15 13:38:57 +0000749 if (const TemplateTemplateParmDecl *TTP =
750 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000751 Out << "class ";
752 if (TTP->isParameterPack())
753 Out << "...";
754 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000755 } else {
756 Visit(D->getTemplatedDecl());
757 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000758}
759
760//----------------------------------------------------------------------------
761// Objective-C declarations
762//----------------------------------------------------------------------------
763
764void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
765 Out << "@class ";
766 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
767 I != E; ++I) {
768 if (I != D->begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000769 Out << I->getInterface();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000770 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000771}
772
773void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
774 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000775 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000776 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000777 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000778 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000779 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000781 std::string name = OMD->getSelector().getAsString();
782 std::string::size_type pos, lastPos = 0;
783 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
784 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000785 // FIXME: selector is missing here!
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000786 pos = name.find_first_of(":", lastPos);
787 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000788 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000789 lastPos = pos + 1;
790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000792 if (OMD->param_begin() == OMD->param_end())
793 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000795 if (OMD->isVariadic())
796 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000798 if (OMD->getBody()) {
799 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000800 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000801 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000802 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000803}
804
805void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
806 std::string I = OID->getNameAsString();
807 ObjCInterfaceDecl *SID = OID->getSuperClass();
808
809 if (SID)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000810 Out << "@implementation " << I << " : " << SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000811 else
812 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000813 Out << "\n";
814 VisitDeclContext(OID, false);
815 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000816}
817
818void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
819 std::string I = OID->getNameAsString();
820 ObjCInterfaceDecl *SID = OID->getSuperClass();
821
822 if (SID)
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000823 Out << "@interface " << I << " : " << SID;
824 else
825 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000827 // Protocols?
828 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
829 if (!Protocols.empty()) {
830 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
831 E = Protocols.end(); I != E; ++I)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000832 Out << (I == Protocols.begin() ? '<' : ',') << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000835 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000836 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000838 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000839 Out << "{\n";
840 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000841 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
842 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000843 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000844 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000845 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000846 Out << "}\n";
847 }
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000849 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000850 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000851 // FIXME: implement the rest...
852}
853
854void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
855 Out << "@protocol ";
Mike Stump1eb44332009-09-09 15:08:12 +0000856 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000857 E = D->protocol_end();
858 I != E; ++I) {
859 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000860 Out << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000861 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000862}
863
864void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000865 Out << "@protocol " << PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000866 VisitDeclContext(PID, false);
867 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000868}
869
870void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000871 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000872
873 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000874 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000875 // FIXME: implement the rest...
876}
877
878void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000879 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000880 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000881 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000883 // FIXME: implement the rest...
884}
885
886void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000887 Out << "@compatibility_alias " << AID
888 << ' ' << AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000889}
890
891/// PrintObjCPropertyDecl - print a property declaration.
892///
893void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
894 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
895 Out << "@required\n";
896 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
897 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000899 Out << "@property";
900 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
901 bool first = true;
902 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000903 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000904 ObjCPropertyDecl::OBJC_PR_readonly) {
905 Out << (first ? ' ' : ',') << "readonly";
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_getter) {
910 Out << (first ? ' ' : ',') << "getter = "
911 << PDecl->getGetterName().getAsString();
912 first = false;
913 }
914 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
915 Out << (first ? ' ' : ',') << "setter = "
916 << PDecl->getSetterName().getAsString();
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_assign) {
921 Out << (first ? ' ' : ',') << "assign";
922 first = false;
923 }
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000925 if (PDecl->getPropertyAttributes() &
926 ObjCPropertyDecl::OBJC_PR_readwrite) {
927 Out << (first ? ' ' : ',') << "readwrite";
928 first = false;
929 }
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000931 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
932 Out << (first ? ' ' : ',') << "retain";
933 first = false;
934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000936 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
937 Out << (first ? ' ' : ',') << "copy";
938 first = false;
939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
941 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000942 ObjCPropertyDecl::OBJC_PR_nonatomic) {
943 Out << (first ? ' ' : ',') << "nonatomic";
944 first = false;
945 }
Fariborz Jahanianf33b1e82011-06-11 17:37:19 +0000946 if (PDecl->getPropertyAttributes() &
947 ObjCPropertyDecl::OBJC_PR_atomic) {
948 Out << (first ? ' ' : ',') << "atomic";
949 first = false;
950 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000951 Out << " )";
952 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000953 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000954}
955
956void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
957 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000958 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000959 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000960 Out << "@dynamic ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000961 Out << PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000962 if (PID->getPropertyIvarDecl())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000963 Out << '=' << PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000964}
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000965
966void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
967 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +0000968 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000969 Out << D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000970}
971
John McCall7ba107a2009-11-18 02:36:19 +0000972void
973DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
974 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +0000975 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000976 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +0000977}
978
979void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000980 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +0000981 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000982 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000983}
John McCall9488ea12009-11-17 05:59:44 +0000984
985void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
986 // ignore
987}