blob: a17e14053cbf34b8c6083236b2c29684c595a8c1 [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> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000027 raw_ostream &Out;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000028 ASTContext &Context;
29 PrintingPolicy Policy;
30 unsigned Indentation;
Richard Trieu5cb3d692011-07-28 00:19:05 +000031 bool PrintInstantiation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000032
Chris Lattner5f9e2722011-07-23 10:55:15 +000033 raw_ostream& Indent() { return Indent(Indentation); }
34 raw_ostream& Indent(unsigned Indentation);
35 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000036
Anders Carlsson0d592922009-08-28 22:39:52 +000037 void Print(AccessSpecifier AS);
Mike Stump1eb44332009-09-09 15:08:12 +000038
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000039 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000040 DeclPrinter(raw_ostream &Out, ASTContext &Context,
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000041 const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +000042 unsigned Indentation = 0,
43 bool PrintInstantiation = false)
44 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation),
45 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000046
47 void VisitDeclContext(DeclContext *DC, bool Indent = true);
48
49 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
50 void VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +000051 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000052 void VisitEnumDecl(EnumDecl *D);
53 void VisitRecordDecl(RecordDecl *D);
54 void VisitEnumConstantDecl(EnumConstantDecl *D);
55 void VisitFunctionDecl(FunctionDecl *D);
56 void VisitFieldDecl(FieldDecl *D);
57 void VisitVarDecl(VarDecl *D);
Chris Lattner57ad3782011-02-17 20:34:02 +000058 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000059 void VisitParmVarDecl(ParmVarDecl *D);
60 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +000061 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000062 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000063 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000064 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000065 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000066 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith98a57862011-04-15 13:38:57 +000067 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000068 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
69 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000070 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000071 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000072 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
73 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
74 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
75 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
76 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
77 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
78 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
79 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
80 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000081 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
82 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000083 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000084 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000085
86 void PrintTemplateParameters(const TemplateParameterList *Params,
87 const TemplateArgumentList *Args);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000088 };
89}
90
Richard Trieu5cb3d692011-07-28 00:19:05 +000091void Decl::print(raw_ostream &Out, unsigned Indentation,
92 bool PrintInstantiation) const {
93 print(Out, getASTContext().PrintingPolicy, Indentation, PrintInstantiation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000094}
95
Chris Lattner5f9e2722011-07-23 10:55:15 +000096void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +000097 unsigned Indentation, bool PrintInstantiation) const {
98 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation, PrintInstantiation);
Anders Carlssonf88df862009-09-26 21:58:53 +000099 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000100}
101
Eli Friedman42f42c02009-05-30 04:20:30 +0000102static QualType GetBaseType(QualType T) {
103 // FIXME: This should be on the Type class!
104 QualType BaseType = T;
105 while (!BaseType->isSpecifierType()) {
106 if (isa<TypedefType>(BaseType))
107 break;
Ted Kremenek6217b802009-07-29 21:53:49 +0000108 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000109 BaseType = PTy->getPointeeType();
110 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
111 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +0000112 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000113 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000114 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000115 BaseType = VTy->getElementType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000116 else
David Blaikieb219cfc2011-09-23 05:06:16 +0000117 llvm_unreachable("Unknown declarator!");
Eli Friedman42f42c02009-05-30 04:20:30 +0000118 }
119 return BaseType;
120}
121
122static QualType getDeclType(Decl* D) {
Richard Smith162e1c12011-04-15 14:24:37 +0000123 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman42f42c02009-05-30 04:20:30 +0000124 return TDD->getUnderlyingType();
125 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
126 return VD->getType();
127 return QualType();
128}
129
130void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000131 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000132 unsigned Indentation) {
133 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000134 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000135 return;
136 }
137
138 Decl** End = Begin + NumDecls;
139 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
140 if (TD)
141 ++Begin;
142
143 PrintingPolicy SubPolicy(Policy);
144 if (TD && TD->isDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000145 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000146 Out << " ";
147 SubPolicy.SuppressTag = true;
148 }
149
150 bool isFirst = true;
151 for ( ; Begin != End; ++Begin) {
152 if (isFirst) {
153 SubPolicy.SuppressSpecifiers = false;
154 isFirst = false;
155 } else {
156 if (!isFirst) Out << ", ";
157 SubPolicy.SuppressSpecifiers = true;
158 }
159
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000160 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000161 }
162}
163
Anders Carlsson84834432009-12-14 00:51:04 +0000164void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000165 // Get the translation unit
166 const DeclContext *DC = this;
167 while (!DC->isTranslationUnit())
168 DC = DC->getParent();
169
170 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
171 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
172 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
173}
174
Anders Carlssonf88df862009-09-26 21:58:53 +0000175void Decl::dump() const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000176 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000177}
178
Chris Lattner5f9e2722011-07-23 10:55:15 +0000179raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar512ce602009-11-21 09:12:06 +0000180 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000181 Out << " ";
182 return Out;
183}
184
Chris Lattner5f9e2722011-07-23 10:55:15 +0000185void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000186 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000187 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000188 Out << ";\n";
189 Decls.clear();
190
191}
192
Anders Carlsson0d592922009-08-28 22:39:52 +0000193void DeclPrinter::Print(AccessSpecifier AS) {
194 switch(AS) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000195 case AS_none: llvm_unreachable("No access specifier!"); break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000196 case AS_public: Out << "public"; break;
197 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000198 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000199 }
200}
201
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000202//----------------------------------------------------------------------------
203// Common C declarations
204//----------------------------------------------------------------------------
205
206void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
207 if (Indent)
208 Indentation += Policy.Indentation;
209
Chris Lattner5f9e2722011-07-23 10:55:15 +0000210 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000211 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000212 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-07-30 00:47:46 +0000213
214 // Don't print ObjCIvarDecls, as they are printed when visiting the
215 // containing ObjCInterfaceDecl.
216 if (isa<ObjCIvarDecl>(*D))
217 continue;
218
Eli Friedman48d14a22009-05-30 05:03:24 +0000219 if (!Policy.Dump) {
220 // Skip over implicit declarations in pretty-printing mode.
221 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000222 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidis2574f6f2010-06-17 10:52:11 +0000223 // of __builtin_va_list or __[u]int128_t. There should be some other way
224 // to check that.
225 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
226 if (IdentifierInfo *II = ND->getIdentifier()) {
227 if (II->isStr("__builtin_va_list") ||
228 II->isStr("__int128_t") || II->isStr("__uint128_t"))
229 continue;
230 }
231 }
Eli Friedman48d14a22009-05-30 05:03:24 +0000232 }
233
Eli Friedman42f42c02009-05-30 04:20:30 +0000234 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
235 // forced to merge the declarations because there's no other way to
236 // refer to the struct in question. This limited merging is safe without
237 // a bunch of other checks because it only merges declarations directly
238 // referring to the tag, not typedefs.
239 //
240 // Check whether the current declaration should be grouped with a previous
241 // unnamed struct.
242 QualType CurDeclType = getDeclType(*D);
243 if (!Decls.empty() && !CurDeclType.isNull()) {
244 QualType BaseType = GetBaseType(CurDeclType);
245 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
246 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
247 Decls.push_back(*D);
248 continue;
249 }
250 }
251
252 // If we have a merged group waiting to be handled, handle it now.
253 if (!Decls.empty())
254 ProcessDeclGroup(Decls);
255
256 // If the current declaration is an unnamed tag type, save it
257 // so we can merge it with the subsequent declaration(s) using it.
258 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
259 Decls.push_back(*D);
260 continue;
261 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000262
263 if (isa<AccessSpecDecl>(*D)) {
264 Indentation -= Policy.Indentation;
265 this->Indent();
266 Print(D->getAccess());
267 Out << ":\n";
268 Indentation += Policy.Indentation;
269 continue;
270 }
271
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000272 this->Indent();
273 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
275 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000276 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000277 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000278 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
279 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000280 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
281 Terminator = 0;
282 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000283 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000284 isa<ObjCInterfaceDecl>(*D) ||
285 isa<ObjCProtocolDecl>(*D) ||
286 isa<ObjCCategoryImplDecl>(*D) ||
287 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000288 Terminator = 0;
289 else if (isa<EnumConstantDecl>(*D)) {
290 DeclContext::decl_iterator Next = D;
291 ++Next;
292 if (Next != DEnd)
293 Terminator = ",";
294 } else
295 Terminator = ";";
296
297 if (Terminator)
298 Out << Terminator;
299 Out << "\n";
300 }
301
Eli Friedman42f42c02009-05-30 04:20:30 +0000302 if (!Decls.empty())
303 ProcessDeclGroup(Decls);
304
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000305 if (Indent)
306 Indentation -= Policy.Indentation;
307}
308
309void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
310 VisitDeclContext(D, false);
311}
312
313void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
314 std::string S = D->getNameAsString();
315 D->getUnderlyingType().getAsStringInternal(S, Policy);
Douglas Gregor8d267c52011-09-09 02:06:17 +0000316 if (!Policy.SuppressSpecifiers) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000317 Out << "typedef ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000318
319 if (D->isModulePrivate())
320 Out << "__module_private__ ";
321 }
Eli Friedman42f42c02009-05-30 04:20:30 +0000322 Out << S;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000323}
324
Richard Smith162e1c12011-04-15 14:24:37 +0000325void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
326 Out << "using " << D->getNameAsString() << " = "
327 << D->getUnderlyingType().getAsString(Policy);
328}
329
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000330void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000331 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
332 Out << "__module_private__ ";
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000333 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000334 if (D->isScoped()) {
335 if (D->isScopedUsingClassTag())
336 Out << "class ";
337 else
338 Out << "struct ";
339 }
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000340 Out << D;
341
342 if (D->isFixed()) {
343 std::string Underlying;
344 D->getIntegerType().getAsStringInternal(Underlying, Policy);
345 Out << " : " << Underlying;
346 }
347
348 if (D->isDefinition()) {
349 Out << " {\n";
350 VisitDeclContext(D);
351 Indent() << "}";
352 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000353}
354
355void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000356 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
357 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000358 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000359 if (D->getIdentifier())
360 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000362 if (D->isDefinition()) {
363 Out << " {\n";
364 VisitDeclContext(D);
365 Indent() << "}";
366 }
367}
368
369void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000370 Out << D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000371 if (Expr *Init = D->getInitExpr()) {
372 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000373 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000374 }
375}
376
Mike Stump1eb44332009-09-09 15:08:12 +0000377void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000378 if (!Policy.SuppressSpecifiers) {
379 switch (D->getStorageClass()) {
John McCalld931b082010-08-26 03:08:43 +0000380 case SC_None: break;
381 case SC_Extern: Out << "extern "; break;
382 case SC_Static: Out << "static "; break;
383 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000384 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
385 llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000386 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000387
Douglas Gregor8d267c52011-09-09 02:06:17 +0000388 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000389 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000390 if (D->isModulePrivate()) Out << "__module_private__ ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000391 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000392
Douglas Gregor6620a622009-05-30 05:39:39 +0000393 PrintingPolicy SubPolicy(Policy);
394 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000395 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000396
Abramo Bagnara723df242010-12-14 22:11:44 +0000397 QualType Ty = D->getType();
John McCallf4c73712011-01-19 06:33:43 +0000398 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000399 Proto = '(' + Proto + ')';
400 Ty = PT->getInnerType();
401 }
402
403 if (isa<FunctionType>(Ty)) {
404 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000405 const FunctionProtoType *FT = 0;
406 if (D->hasWrittenPrototype())
407 FT = dyn_cast<FunctionProtoType>(AFT);
408
409 Proto += "(";
410 if (FT) {
411 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000412 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000413 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
414 if (i) POut << ", ";
415 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
416 }
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000418 if (FT->isVariadic()) {
419 if (D->getNumParams()) POut << ", ";
420 POut << "...";
421 }
Sean Hunt10620eb2011-05-06 20:44:56 +0000422 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000423 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
424 if (i)
425 Proto += ", ";
426 Proto += D->getParamDecl(i)->getNameAsString();
427 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000428 }
429
430 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000431
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000432 if (FT && FT->getTypeQuals()) {
433 unsigned TypeQuals = FT->getTypeQuals();
434 if (TypeQuals & Qualifiers::Const)
435 Proto += " const";
436 if (TypeQuals & Qualifiers::Volatile)
437 Proto += " volatile";
438 if (TypeQuals & Qualifiers::Restrict)
439 Proto += " restrict";
440 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000441
442 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000443 Proto += " throw(";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000444 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000445 Proto += "...";
446 else
447 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
448 if (I)
449 Proto += ", ";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000450
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000451 std::string ExceptionType;
452 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
453 Proto += ExceptionType;
454 }
455 Proto += ")";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000456 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
457 Proto += " noexcept";
458 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
459 Proto += "(";
460 llvm::raw_string_ostream EOut(Proto);
461 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
462 Indentation);
463 EOut.flush();
464 Proto += EOut.str();
465 Proto += ")";
466 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000467 }
468
Mike Stumpfd350b52009-07-27 21:33:40 +0000469 if (D->hasAttr<NoReturnAttr>())
470 Proto += " __attribute((noreturn))";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000471 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith7a614d82011-06-11 17:19:42 +0000472 bool HasInitializerList = false;
473 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
474 E = CDecl->init_end();
475 B != E; ++B) {
476 CXXCtorInitializer * BMInitializer = (*B);
477 if (BMInitializer->isInClassMemberInitializer())
478 continue;
479
480 if (!HasInitializerList) {
481 Proto += " : ";
482 Out << Proto;
483 Proto.clear();
484 HasInitializerList = true;
485 } else
486 Out << ", ";
487
488 if (BMInitializer->isAnyMemberInitializer()) {
489 FieldDecl *FD = BMInitializer->getAnyMember();
490 Out << FD;
491 } else {
492 Out << QualType(BMInitializer->getBaseClass(),
493 0).getAsString(Policy);
494 }
495
496 Out << "(";
497 if (!BMInitializer->getInit()) {
498 // Nothing to print
499 } else {
500 Expr *Init = BMInitializer->getInit();
501 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
502 Init = Tmp->getSubExpr();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000503
Richard Smith7a614d82011-06-11 17:19:42 +0000504 Init = Init->IgnoreParens();
505
506 Expr *SimpleInit = 0;
507 Expr **Args = 0;
508 unsigned NumArgs = 0;
509 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
510 Args = ParenList->getExprs();
511 NumArgs = ParenList->getNumExprs();
512 } else if (CXXConstructExpr *Construct
513 = dyn_cast<CXXConstructExpr>(Init)) {
514 Args = Construct->getArgs();
515 NumArgs = Construct->getNumArgs();
516 } else
517 SimpleInit = Init;
518
519 if (SimpleInit)
520 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
521 else {
522 for (unsigned I = 0; I != NumArgs; ++I) {
523 if (isa<CXXDefaultArgExpr>(Args[I]))
524 break;
525
526 if (I)
527 Out << ", ";
528 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000529 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000530 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000531 }
Richard Smith7a614d82011-06-11 17:19:42 +0000532 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000533 }
534 }
535 else
536 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000537 } else {
Abramo Bagnara723df242010-12-14 22:11:44 +0000538 Ty.getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000539 }
540
541 Out << Proto;
542
543 if (D->isPure())
544 Out << " = 0";
Sean Hunt10620eb2011-05-06 20:44:56 +0000545 else if (D->isDeletedAsWritten())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000546 Out << " = delete";
Sean Hunt10620eb2011-05-06 20:44:56 +0000547 else if (D->doesThisDeclarationHaveABody()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000548 if (!D->hasPrototype() && D->getNumParams()) {
549 // This is a K&R function definition, so we need to print the
550 // parameters.
551 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000552 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000553 Indentation += Policy.Indentation;
554 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
555 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000556 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000557 Out << ";\n";
558 }
559 Indentation -= Policy.Indentation;
560 } else
561 Out << ' ';
562
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000563 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000564 Out << '\n';
565 }
566}
567
568void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000569 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000570 Out << "mutable ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000571 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
572 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000573
574 std::string Name = D->getNameAsString();
575 D->getType().getAsStringInternal(Name, Policy);
576 Out << Name;
577
578 if (D->isBitField()) {
579 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000580 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000581 }
Richard Smith7a614d82011-06-11 17:19:42 +0000582
583 Expr *Init = D->getInClassInitializer();
584 if (!Policy.SuppressInitializers && Init) {
585 Out << " = ";
586 Init->printPretty(Out, Context, 0, Policy, Indentation);
587 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000588}
589
Chris Lattner57ad3782011-02-17 20:34:02 +0000590void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
591 Out << D->getNameAsString() << ":";
592}
593
594
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000595void DeclPrinter::VisitVarDecl(VarDecl *D) {
John McCalld931b082010-08-26 03:08:43 +0000596 if (!Policy.SuppressSpecifiers && D->getStorageClass() != SC_None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000597 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
598
Eli Friedman42f42c02009-05-30 04:20:30 +0000599 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000600 Out << "__thread ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000601 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
602 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000603
604 std::string Name = D->getNameAsString();
605 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000606 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000607 T = Parm->getOriginalType();
608 T.getAsStringInternal(Name, Policy);
609 Out << Name;
Richard Smithad762fc2011-04-14 22:09:26 +0000610 Expr *Init = D->getInit();
611 if (!Policy.SuppressInitializers && Init) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000612 if (D->hasCXXDirectInitializer())
613 Out << "(";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000614 else {
615 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
616 if (!CCE || CCE->getConstructor()->isCopyConstructor())
617 Out << " = ";
618 }
Ted Kremenekbccfd312010-09-07 22:21:59 +0000619 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000620 if (D->hasCXXDirectInitializer())
621 Out << ")";
622 }
623}
624
625void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
626 VisitVarDecl(D);
627}
628
629void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
630 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000631 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000632 Out << ")";
633}
634
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000635void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
636 Out << "static_assert(";
637 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
638 Out << ", ";
639 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
640 Out << ")";
641}
642
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000643//----------------------------------------------------------------------------
644// C++ declarations
645//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000646void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000647 Out << "namespace " << D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000648 VisitDeclContext(D);
649 Indent() << "}";
650}
651
Douglas Gregor8419fa32009-05-30 06:31:56 +0000652void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
653 Out << "using namespace ";
654 if (D->getQualifier())
655 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000656 Out << D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000657}
658
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000659void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000660 Out << "namespace " << D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000661 if (D->getQualifier())
662 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000663 Out << D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000664}
665
Douglas Gregor59e63572009-05-30 06:58:37 +0000666void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000667 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
668 Out << "__module_private__ ";
Douglas Gregor59e63572009-05-30 06:58:37 +0000669 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000670 if (D->getIdentifier())
671 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Douglas Gregor59e63572009-05-30 06:58:37 +0000673 if (D->isDefinition()) {
674 // Print the base classes
675 if (D->getNumBases()) {
676 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000677 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
678 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000679 if (Base != D->bases_begin())
680 Out << ", ";
681
682 if (Base->isVirtual())
683 Out << "virtual ";
684
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000685 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
686 if (AS != AS_none)
687 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000688 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor34a99e72011-04-27 17:07:55 +0000689
690 if (Base->isPackExpansion())
691 Out << "...";
Douglas Gregor59e63572009-05-30 06:58:37 +0000692 }
693 }
694
695 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000696 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000697 Out << " {\n";
698 VisitDeclContext(D);
699 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000700 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000701}
702
703void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
704 const char *l;
705 if (D->getLanguage() == LinkageSpecDecl::lang_c)
706 l = "C";
707 else {
708 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
709 "unknown language in linkage specification");
710 l = "C++";
711 }
712
713 Out << "extern \"" << l << "\" ";
714 if (D->hasBraces()) {
715 Out << "{\n";
716 VisitDeclContext(D);
717 Indent() << "}";
718 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000719 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000720}
721
Richard Trieu5cb3d692011-07-28 00:19:05 +0000722void DeclPrinter::PrintTemplateParameters(
723 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
724 assert(Params);
725 assert(!Args || Params->size() == Args->size());
726
Anders Carlsson0487f662009-06-04 05:37:43 +0000727 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Anders Carlsson0487f662009-06-04 05:37:43 +0000729 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
730 if (i != 0)
731 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Anders Carlsson0487f662009-06-04 05:37:43 +0000733 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000734 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000735 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Anders Carlsson0487f662009-06-04 05:37:43 +0000737 if (TTP->wasDeclaredWithTypename())
738 Out << "typename ";
739 else
740 Out << "class ";
741
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000742 if (TTP->isParameterPack())
743 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000745 Out << TTP->getNameAsString();
Anders Carlsson0487f662009-06-04 05:37:43 +0000746
Richard Trieu5cb3d692011-07-28 00:19:05 +0000747 if (Args) {
748 Out << " = ";
749 Args->get(i).print(Policy, Out);
750 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000751 Out << " = ";
752 Out << TTP->getDefaultArgument().getAsString(Policy);
753 };
Mike Stump1eb44332009-09-09 15:08:12 +0000754 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000755 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
756 Out << NTTP->getType().getAsString(Policy);
757
Douglas Gregor56bc9832010-12-24 00:15:10 +0000758 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
759 Out << "...";
760
Anders Carlsson0487f662009-06-04 05:37:43 +0000761 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
762 Out << ' ';
763 Out << Name->getName();
764 }
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Richard Trieu5cb3d692011-07-28 00:19:05 +0000766 if (Args) {
767 Out << " = ";
768 Args->get(i).print(Policy, Out);
769 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000770 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000771 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000772 Indentation);
773 }
Richard Smith98a57862011-04-15 13:38:57 +0000774 } else if (const TemplateTemplateParmDecl *TTPD =
775 dyn_cast<TemplateTemplateParmDecl>(Param)) {
776 VisitTemplateDecl(TTPD);
777 // FIXME: print the default argument, if present.
Anders Carlsson0487f662009-06-04 05:37:43 +0000778 }
779 }
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Anders Carlsson0487f662009-06-04 05:37:43 +0000781 Out << "> ";
Richard Trieu5cb3d692011-07-28 00:19:05 +0000782}
783
784void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
785 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson0487f662009-06-04 05:37:43 +0000786
Richard Smith98a57862011-04-15 13:38:57 +0000787 if (const TemplateTemplateParmDecl *TTP =
788 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000789 Out << "class ";
790 if (TTP->isParameterPack())
791 Out << "...";
792 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000793 } else {
794 Visit(D->getTemplatedDecl());
795 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000796}
797
Richard Trieu5cb3d692011-07-28 00:19:05 +0000798void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
799 if (PrintInstantiation) {
800 TemplateParameterList *Params = D->getTemplateParameters();
801 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
802 I != E; ++I) {
803 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
804 Visit(*I);
805 }
806 }
807
808 return VisitRedeclarableTemplateDecl(D);
809}
810
811void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
812 if (PrintInstantiation) {
813 TemplateParameterList *Params = D->getTemplateParameters();
814 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
815 I != E; ++I) {
816 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
817 Visit(*I);
818 Out << '\n';
819 }
820 }
821
822 return VisitRedeclarableTemplateDecl(D);
823}
824
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000825//----------------------------------------------------------------------------
826// Objective-C declarations
827//----------------------------------------------------------------------------
828
829void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
830 Out << "@class ";
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000831 Out << D->getForwardInterfaceDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000832}
833
834void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
835 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000836 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000837 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000838 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000839 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000840 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000842 std::string name = OMD->getSelector().getAsString();
843 std::string::size_type pos, lastPos = 0;
844 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
845 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000846 // FIXME: selector is missing here!
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000847 pos = name.find_first_of(":", lastPos);
848 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000849 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000850 lastPos = pos + 1;
851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000853 if (OMD->param_begin() == OMD->param_end())
854 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000856 if (OMD->isVariadic())
857 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000859 if (OMD->getBody()) {
860 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000861 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000862 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000863 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000864}
865
866void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
867 std::string I = OID->getNameAsString();
868 ObjCInterfaceDecl *SID = OID->getSuperClass();
869
870 if (SID)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000871 Out << "@implementation " << I << " : " << SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000872 else
873 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000874 Out << "\n";
875 VisitDeclContext(OID, false);
876 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000877}
878
879void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
880 std::string I = OID->getNameAsString();
881 ObjCInterfaceDecl *SID = OID->getSuperClass();
882
883 if (SID)
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000884 Out << "@interface " << I << " : " << SID;
885 else
886 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000888 // Protocols?
889 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
890 if (!Protocols.empty()) {
891 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
892 E = Protocols.end(); I != E; ++I)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000893 Out << (I == Protocols.begin() ? '<' : ',') << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000896 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000897 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000899 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000900 Out << "{\n";
901 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000902 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
903 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000904 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000905 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000906 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000907 Out << "}\n";
908 }
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000910 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000911 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000912 // FIXME: implement the rest...
913}
914
915void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
916 Out << "@protocol ";
Mike Stump1eb44332009-09-09 15:08:12 +0000917 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000918 E = D->protocol_end();
919 I != E; ++I) {
920 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000921 Out << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000922 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000923}
924
925void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000926 Out << "@protocol " << PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000927 VisitDeclContext(PID, false);
928 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000929}
930
931void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000932 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000933
934 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000935 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000936 // FIXME: implement the rest...
937}
938
939void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000940 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000941 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000942 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000944 // FIXME: implement the rest...
945}
946
947void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000948 Out << "@compatibility_alias " << AID
949 << ' ' << AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000950}
951
952/// PrintObjCPropertyDecl - print a property declaration.
953///
954void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
955 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
956 Out << "@required\n";
957 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
958 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000960 Out << "@property";
961 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
962 bool first = true;
963 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000964 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000965 ObjCPropertyDecl::OBJC_PR_readonly) {
966 Out << (first ? ' ' : ',') << "readonly";
967 first = false;
Ted Kremenek07c682a2011-08-17 21:09:35 +0000968 }
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Ted Kremenek07c682a2011-08-17 21:09:35 +0000970 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
971 Out << (first ? ' ' : ',') << "getter = "
972 << PDecl->getGetterName().getAsString();
973 first = false;
974 }
975 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
976 Out << (first ? ' ' : ',') << "setter = "
977 << PDecl->getSetterName().getAsString();
978 first = false;
979 }
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek07c682a2011-08-17 21:09:35 +0000981 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
982 Out << (first ? ' ' : ',') << "assign";
983 first = false;
984 }
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Ted Kremenek07c682a2011-08-17 21:09:35 +0000986 if (PDecl->getPropertyAttributes() &
987 ObjCPropertyDecl::OBJC_PR_readwrite) {
988 Out << (first ? ' ' : ',') << "readwrite";
989 first = false;
990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenek07c682a2011-08-17 21:09:35 +0000992 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
993 Out << (first ? ' ' : ',') << "retain";
994 first = false;
995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Ted Kremenek07c682a2011-08-17 21:09:35 +0000997 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
998 Out << (first ? ' ' : ',') << "strong";
999 first = false;
1000 }
John McCallf85e1932011-06-15 23:02:42 +00001001
Ted Kremenek07c682a2011-08-17 21:09:35 +00001002 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1003 Out << (first ? ' ' : ',') << "copy";
1004 first = false;
1005 }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Ted Kremenek07c682a2011-08-17 21:09:35 +00001007 if (PDecl->getPropertyAttributes() &
1008 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1009 Out << (first ? ' ' : ',') << "nonatomic";
1010 first = false;
1011 }
1012 if (PDecl->getPropertyAttributes() &
1013 ObjCPropertyDecl::OBJC_PR_atomic) {
1014 Out << (first ? ' ' : ',') << "atomic";
1015 first = false;
1016 }
1017
1018 (void) first; // Silence dead store warning due to idiomatic code.
1019 Out << " )";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001020 }
Benjamin Kramer900fc632010-04-17 09:33:03 +00001021 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001022}
1023
1024void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1025 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +00001026 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001027 else
Douglas Gregor64f65002009-05-30 00:56:08 +00001028 Out << "@dynamic ";
Benjamin Kramer900fc632010-04-17 09:33:03 +00001029 Out << PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001030 if (PID->getPropertyIvarDecl())
Benjamin Kramer900fc632010-04-17 09:33:03 +00001031 Out << '=' << PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001032}
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001033
1034void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1035 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001036 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001037 Out << D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001038}
1039
John McCall7ba107a2009-11-18 02:36:19 +00001040void
1041DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1042 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +00001043 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001044 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +00001045}
1046
1047void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001048 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001049 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001050 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001051}
John McCall9488ea12009-11-17 05:59:44 +00001052
1053void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1054 // ignore
1055}