blob: 7f47604dece35e92a59f507fec297fd4e1866cc5 [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 Gregor15de72c2011-12-02 23:23:56 +000022#include "clang/Basic/Module.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000023#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25
26namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000027 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000028 raw_ostream &Out;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000029 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:
Richard Smithd1420c62012-08-16 03:56:14 +000040 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
41 unsigned Indentation = 0, bool PrintInstantiation = false)
42 : Out(Out), Policy(Policy), Indentation(Indentation),
Richard Trieu5cb3d692011-07-28 00:19:05 +000043 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000044
45 void VisitDeclContext(DeclContext *DC, bool Indent = true);
46
47 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
48 void VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +000049 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000050 void VisitEnumDecl(EnumDecl *D);
51 void VisitRecordDecl(RecordDecl *D);
52 void VisitEnumConstantDecl(EnumConstantDecl *D);
53 void VisitFunctionDecl(FunctionDecl *D);
54 void VisitFieldDecl(FieldDecl *D);
55 void VisitVarDecl(VarDecl *D);
Chris Lattner57ad3782011-02-17 20:34:02 +000056 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000057 void VisitParmVarDecl(ParmVarDecl *D);
58 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor15de72c2011-12-02 23:23:56 +000059 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +000060 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000061 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000062 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000063 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000064 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000065 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith98a57862011-04-15 13:38:57 +000066 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000067 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
68 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000069 void VisitObjCMethodDecl(ObjCMethodDecl *D);
70 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
71 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000072 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
73 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
74 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
75 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
76 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
77 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000078 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
79 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000080 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000081 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000082
83 void PrintTemplateParameters(const TemplateParameterList *Params,
84 const TemplateArgumentList *Args);
Douglas Gregor1bea8802011-11-19 19:22:57 +000085 void prettyPrintAttributes(Decl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000086 };
87}
88
Richard Trieu5cb3d692011-07-28 00:19:05 +000089void Decl::print(raw_ostream &Out, unsigned Indentation,
90 bool PrintInstantiation) const {
Douglas Gregor30c42402011-09-27 22:38:19 +000091 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000092}
93
Chris Lattner5f9e2722011-07-23 10:55:15 +000094void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +000095 unsigned Indentation, bool PrintInstantiation) const {
Richard Smithd1420c62012-08-16 03:56:14 +000096 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlssonf88df862009-09-26 21:58:53 +000097 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000098}
99
Eli Friedman42f42c02009-05-30 04:20:30 +0000100static QualType GetBaseType(QualType T) {
101 // FIXME: This should be on the Type class!
102 QualType BaseType = T;
103 while (!BaseType->isSpecifierType()) {
104 if (isa<TypedefType>(BaseType))
105 break;
Ted Kremenek6217b802009-07-29 21:53:49 +0000106 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000107 BaseType = PTy->getPointeeType();
108 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
109 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +0000110 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000111 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000112 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000113 BaseType = VTy->getElementType();
Eli Friedman15631b42012-08-08 03:47:15 +0000114 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
115 BaseType = RTy->getPointeeType();
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);
John McCall5e1cdac2011-10-07 06:10:15 +0000144 if (TD && TD->isCompleteDefinition()) {
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();
Richard Smithd1420c62012-08-16 03:56:14 +0000171 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000172 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
173}
174
Benjamin Kramer42c72c22012-08-14 14:50:32 +0000175void Decl::dump() const {
176 dump(llvm::errs());
177}
178
Alexander Kornienkoe34a0522012-07-26 16:01:23 +0000179void Decl::dump(raw_ostream &Out) const {
180 PrintingPolicy Policy = getASTContext().getPrintingPolicy();
Richard Smithd1420c62012-08-16 03:56:14 +0000181 Policy.DumpSourceManager = &getASTContext().getSourceManager();
Alexander Kornienkoe34a0522012-07-26 16:01:23 +0000182 print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000183}
184
Chris Lattner5f9e2722011-07-23 10:55:15 +0000185raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar512ce602009-11-21 09:12:06 +0000186 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000187 Out << " ";
188 return Out;
189}
190
Douglas Gregor1bea8802011-11-19 19:22:57 +0000191void DeclPrinter::prettyPrintAttributes(Decl *D) {
192 if (D->hasAttrs()) {
193 AttrVec &Attrs = D->getAttrs();
194 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
Richard Smith0dae7292012-08-16 02:43:29 +0000195 Attr *A = *i;
196 A->printPretty(Out, Policy);
Douglas Gregor1bea8802011-11-19 19:22:57 +0000197 }
198 }
199}
200
Chris Lattner5f9e2722011-07-23 10:55:15 +0000201void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000202 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000203 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000204 Out << ";\n";
205 Decls.clear();
206
207}
208
Anders Carlsson0d592922009-08-28 22:39:52 +0000209void DeclPrinter::Print(AccessSpecifier AS) {
210 switch(AS) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000211 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson0d592922009-08-28 22:39:52 +0000212 case AS_public: Out << "public"; break;
213 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000214 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000215 }
216}
217
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000218//----------------------------------------------------------------------------
219// Common C declarations
220//----------------------------------------------------------------------------
221
222void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
223 if (Indent)
224 Indentation += Policy.Indentation;
225
Chris Lattner5f9e2722011-07-23 10:55:15 +0000226 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000227 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000228 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-07-30 00:47:46 +0000229
230 // Don't print ObjCIvarDecls, as they are printed when visiting the
231 // containing ObjCInterfaceDecl.
232 if (isa<ObjCIvarDecl>(*D))
233 continue;
234
Richard Smithd1420c62012-08-16 03:56:14 +0000235 if (!Policy.DumpSourceManager) {
Eli Friedman48d14a22009-05-30 05:03:24 +0000236 // Skip over implicit declarations in pretty-printing mode.
237 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000238 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidis2574f6f2010-06-17 10:52:11 +0000239 // of __builtin_va_list or __[u]int128_t. There should be some other way
240 // to check that.
241 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
242 if (IdentifierInfo *II = ND->getIdentifier()) {
243 if (II->isStr("__builtin_va_list") ||
244 II->isStr("__int128_t") || II->isStr("__uint128_t"))
245 continue;
246 }
247 }
Eli Friedman48d14a22009-05-30 05:03:24 +0000248 }
249
Eli Friedman42f42c02009-05-30 04:20:30 +0000250 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
251 // forced to merge the declarations because there's no other way to
252 // refer to the struct in question. This limited merging is safe without
253 // a bunch of other checks because it only merges declarations directly
254 // referring to the tag, not typedefs.
255 //
256 // Check whether the current declaration should be grouped with a previous
257 // unnamed struct.
258 QualType CurDeclType = getDeclType(*D);
259 if (!Decls.empty() && !CurDeclType.isNull()) {
260 QualType BaseType = GetBaseType(CurDeclType);
261 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
262 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
263 Decls.push_back(*D);
264 continue;
265 }
266 }
267
268 // If we have a merged group waiting to be handled, handle it now.
269 if (!Decls.empty())
270 ProcessDeclGroup(Decls);
271
272 // If the current declaration is an unnamed tag type, save it
273 // so we can merge it with the subsequent declaration(s) using it.
274 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
275 Decls.push_back(*D);
276 continue;
277 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000278
279 if (isa<AccessSpecDecl>(*D)) {
280 Indentation -= Policy.Indentation;
281 this->Indent();
282 Print(D->getAccess());
283 Out << ":\n";
284 Indentation += Policy.Indentation;
285 continue;
286 }
287
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000288 this->Indent();
289 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
291 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000292 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000293 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000294 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
295 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000296 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
297 Terminator = 0;
298 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000299 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000300 isa<ObjCInterfaceDecl>(*D) ||
301 isa<ObjCProtocolDecl>(*D) ||
302 isa<ObjCCategoryImplDecl>(*D) ||
303 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000304 Terminator = 0;
305 else if (isa<EnumConstantDecl>(*D)) {
306 DeclContext::decl_iterator Next = D;
307 ++Next;
308 if (Next != DEnd)
309 Terminator = ",";
310 } else
311 Terminator = ";";
312
313 if (Terminator)
314 Out << Terminator;
315 Out << "\n";
316 }
317
Eli Friedman42f42c02009-05-30 04:20:30 +0000318 if (!Decls.empty())
319 ProcessDeclGroup(Decls);
320
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000321 if (Indent)
322 Indentation -= Policy.Indentation;
323}
324
325void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
326 VisitDeclContext(D, false);
327}
328
329void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000330 if (!Policy.SuppressSpecifiers) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000331 Out << "typedef ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000332
333 if (D->isModulePrivate())
334 Out << "__module_private__ ";
335 }
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000336 D->getUnderlyingType().print(Out, Policy, D->getName());
Douglas Gregor1bea8802011-11-19 19:22:57 +0000337 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000338}
339
Richard Smith162e1c12011-04-15 14:24:37 +0000340void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000341 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy);
Richard Smith162e1c12011-04-15 14:24:37 +0000342}
343
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000344void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000345 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
346 Out << "__module_private__ ";
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000347 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000348 if (D->isScoped()) {
349 if (D->isScopedUsingClassTag())
350 Out << "class ";
351 else
352 Out << "struct ";
353 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000354 Out << *D;
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000355
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000356 if (D->isFixed())
357 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000358
John McCall5e1cdac2011-10-07 06:10:15 +0000359 if (D->isCompleteDefinition()) {
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000360 Out << " {\n";
361 VisitDeclContext(D);
362 Indent() << "}";
363 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000364 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000365}
366
367void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000368 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
369 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000370 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000371 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000372 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000373
John McCall5e1cdac2011-10-07 06:10:15 +0000374 if (D->isCompleteDefinition()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000375 Out << " {\n";
376 VisitDeclContext(D);
377 Indent() << "}";
378 }
379}
380
381void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000382 Out << *D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000383 if (Expr *Init = D->getInitExpr()) {
384 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000385 Init->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000386 }
387}
388
Mike Stump1eb44332009-09-09 15:08:12 +0000389void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000390 if (!Policy.SuppressSpecifiers) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000391 switch (D->getStorageClassAsWritten()) {
John McCalld931b082010-08-26 03:08:43 +0000392 case SC_None: break;
393 case SC_Extern: Out << "extern "; break;
394 case SC_Static: Out << "static "; break;
395 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000396 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
397 llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000398 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000399
Douglas Gregor8d267c52011-09-09 02:06:17 +0000400 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000401 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000402 if (D->isModulePrivate()) Out << "__module_private__ ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000403 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000404
Douglas Gregor6620a622009-05-30 05:39:39 +0000405 PrintingPolicy SubPolicy(Policy);
406 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000407 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000408
Abramo Bagnara723df242010-12-14 22:11:44 +0000409 QualType Ty = D->getType();
John McCallf4c73712011-01-19 06:33:43 +0000410 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000411 Proto = '(' + Proto + ')';
412 Ty = PT->getInnerType();
413 }
414
415 if (isa<FunctionType>(Ty)) {
416 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000417 const FunctionProtoType *FT = 0;
418 if (D->hasWrittenPrototype())
419 FT = dyn_cast<FunctionProtoType>(AFT);
420
421 Proto += "(";
422 if (FT) {
423 llvm::raw_string_ostream POut(Proto);
Richard Smithd1420c62012-08-16 03:56:14 +0000424 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000425 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
426 if (i) POut << ", ";
427 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
428 }
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000430 if (FT->isVariadic()) {
431 if (D->getNumParams()) POut << ", ";
432 POut << "...";
433 }
Sean Hunt10620eb2011-05-06 20:44:56 +0000434 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000435 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
436 if (i)
437 Proto += ", ";
438 Proto += D->getParamDecl(i)->getNameAsString();
439 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000440 }
441
442 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000443
David Blaikie4ef832f2012-08-10 00:55:35 +0000444 if (FT) {
445 if (FT->isConst())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000446 Proto += " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000447 if (FT->isVolatile())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000448 Proto += " volatile";
David Blaikie4ef832f2012-08-10 00:55:35 +0000449 if (FT->isRestrict())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000450 Proto += " restrict";
451 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000452
453 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000454 Proto += " throw(";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000455 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000456 Proto += "...";
457 else
458 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
459 if (I)
460 Proto += ", ";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000461
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000462 Proto += FT->getExceptionType(I).getAsString(SubPolicy);;
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000463 }
464 Proto += ")";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000465 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
466 Proto += " noexcept";
467 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
468 Proto += "(";
469 llvm::raw_string_ostream EOut(Proto);
Richard Smithd1420c62012-08-16 03:56:14 +0000470 FT->getNoexceptExpr()->printPretty(EOut, 0, SubPolicy,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000471 Indentation);
472 EOut.flush();
473 Proto += EOut.str();
474 Proto += ")";
475 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000476 }
477
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000478 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith7a614d82011-06-11 17:19:42 +0000479 bool HasInitializerList = false;
480 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
481 E = CDecl->init_end();
482 B != E; ++B) {
483 CXXCtorInitializer * BMInitializer = (*B);
484 if (BMInitializer->isInClassMemberInitializer())
485 continue;
486
487 if (!HasInitializerList) {
488 Proto += " : ";
489 Out << Proto;
490 Proto.clear();
491 HasInitializerList = true;
492 } else
493 Out << ", ";
494
495 if (BMInitializer->isAnyMemberInitializer()) {
496 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000497 Out << *FD;
Richard Smith7a614d82011-06-11 17:19:42 +0000498 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000499 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith7a614d82011-06-11 17:19:42 +0000500 }
501
502 Out << "(";
503 if (!BMInitializer->getInit()) {
504 // Nothing to print
505 } else {
506 Expr *Init = BMInitializer->getInit();
507 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
508 Init = Tmp->getSubExpr();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000509
Richard Smith7a614d82011-06-11 17:19:42 +0000510 Init = Init->IgnoreParens();
511
512 Expr *SimpleInit = 0;
513 Expr **Args = 0;
514 unsigned NumArgs = 0;
515 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
516 Args = ParenList->getExprs();
517 NumArgs = ParenList->getNumExprs();
518 } else if (CXXConstructExpr *Construct
519 = dyn_cast<CXXConstructExpr>(Init)) {
520 Args = Construct->getArgs();
521 NumArgs = Construct->getNumArgs();
522 } else
523 SimpleInit = Init;
524
525 if (SimpleInit)
Richard Smithd1420c62012-08-16 03:56:14 +0000526 SimpleInit->printPretty(Out, 0, Policy, Indentation);
Richard Smith7a614d82011-06-11 17:19:42 +0000527 else {
528 for (unsigned I = 0; I != NumArgs; ++I) {
529 if (isa<CXXDefaultArgExpr>(Args[I]))
530 break;
531
532 if (I)
533 Out << ", ";
Richard Smithd1420c62012-08-16 03:56:14 +0000534 Args[I]->printPretty(Out, 0, Policy, Indentation);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000535 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000536 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000537 }
Richard Smith7a614d82011-06-11 17:19:42 +0000538 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000539 }
540 }
541 else
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000542 AFT->getResultType().print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000543 } else {
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000544 Ty.print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000545 }
546
Douglas Gregor1bea8802011-11-19 19:22:57 +0000547 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000548
549 if (D->isPure())
550 Out << " = 0";
Sean Hunt10620eb2011-05-06 20:44:56 +0000551 else if (D->isDeletedAsWritten())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000552 Out << " = delete";
Sean Hunt10620eb2011-05-06 20:44:56 +0000553 else if (D->doesThisDeclarationHaveABody()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000554 if (!D->hasPrototype() && D->getNumParams()) {
555 // This is a K&R function definition, so we need to print the
556 // parameters.
557 Out << '\n';
Richard Smithd1420c62012-08-16 03:56:14 +0000558 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000559 Indentation += Policy.Indentation;
560 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
561 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000562 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000563 Out << ";\n";
564 }
565 Indentation -= Policy.Indentation;
566 } else
567 Out << ' ';
568
Richard Smithd1420c62012-08-16 03:56:14 +0000569 D->getBody()->printPretty(Out, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000570 Out << '\n';
571 }
572}
573
574void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000575 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000576 Out << "mutable ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000577 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
578 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000579
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000580 Out << D->getType().stream(Policy, D->getName());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000581
582 if (D->isBitField()) {
583 Out << " : ";
Richard Smithd1420c62012-08-16 03:56:14 +0000584 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000585 }
Richard Smith7a614d82011-06-11 17:19:42 +0000586
587 Expr *Init = D->getInClassInitializer();
588 if (!Policy.SuppressInitializers && Init) {
Richard Smithca523302012-06-10 03:12:00 +0000589 if (D->getInClassInitStyle() == ICIS_ListInit)
590 Out << " ";
591 else
592 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000593 Init->printPretty(Out, 0, Policy, Indentation);
Richard Smith7a614d82011-06-11 17:19:42 +0000594 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000595 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000596}
597
Chris Lattner57ad3782011-02-17 20:34:02 +0000598void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000599 Out << *D << ":";
Chris Lattner57ad3782011-02-17 20:34:02 +0000600}
601
602
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000603void DeclPrinter::VisitVarDecl(VarDecl *D) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000604 StorageClass SCAsWritten = D->getStorageClassAsWritten();
605 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None)
606 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000607
Eli Friedman42f42c02009-05-30 04:20:30 +0000608 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000609 Out << "__thread ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000610 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
611 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000612
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000613 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000614 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000615 T = Parm->getOriginalType();
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000616 T.print(Out, Policy, D->getName());
Richard Smithad762fc2011-04-14 22:09:26 +0000617 Expr *Init = D->getInit();
618 if (!Policy.SuppressInitializers && Init) {
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000619 bool ImplicitInit = false;
620 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
621 ImplicitInit = D->getInitStyle() == VarDecl::CallInit &&
622 Construct->getNumArgs() == 0 && !Construct->isListInitialization();
623 if (!ImplicitInit) {
624 if (D->getInitStyle() == VarDecl::CallInit)
625 Out << "(";
626 else if (D->getInitStyle() == VarDecl::CInit) {
627 Out << " = ";
628 }
Richard Smithd1420c62012-08-16 03:56:14 +0000629 Init->printPretty(Out, 0, Policy, Indentation);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000630 if (D->getInitStyle() == VarDecl::CallInit)
631 Out << ")";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000632 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000633 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000634 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000635}
636
637void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
638 VisitVarDecl(D);
639}
640
641void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
642 Out << "__asm (";
Richard Smithd1420c62012-08-16 03:56:14 +0000643 D->getAsmString()->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000644 Out << ")";
645}
646
Douglas Gregor15de72c2011-12-02 23:23:56 +0000647void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Ted Kremenek32ad2ee2012-03-01 22:07:04 +0000648 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName()
Douglas Gregor15de72c2011-12-02 23:23:56 +0000649 << ";\n";
650}
651
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000652void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
653 Out << "static_assert(";
Richard Smithd1420c62012-08-16 03:56:14 +0000654 D->getAssertExpr()->printPretty(Out, 0, Policy, Indentation);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000655 Out << ", ";
Richard Smithd1420c62012-08-16 03:56:14 +0000656 D->getMessage()->printPretty(Out, 0, Policy, Indentation);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000657 Out << ")";
658}
659
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000660//----------------------------------------------------------------------------
661// C++ declarations
662//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000663void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorc30636a2012-05-01 01:43:38 +0000664 if (D->isInline())
665 Out << "inline ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000666 Out << "namespace " << *D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000667 VisitDeclContext(D);
668 Indent() << "}";
669}
670
Douglas Gregor8419fa32009-05-30 06:31:56 +0000671void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
672 Out << "using namespace ";
673 if (D->getQualifier())
674 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000675 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000676}
677
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000678void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000679 Out << "namespace " << *D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000680 if (D->getQualifier())
681 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000682 Out << *D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000683}
684
Douglas Gregor59e63572009-05-30 06:58:37 +0000685void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000686 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
687 Out << "__module_private__ ";
Douglas Gregor59e63572009-05-30 06:58:37 +0000688 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000689 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000690 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000691
John McCall5e1cdac2011-10-07 06:10:15 +0000692 if (D->isCompleteDefinition()) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000693 // Print the base classes
694 if (D->getNumBases()) {
695 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000696 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
697 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000698 if (Base != D->bases_begin())
699 Out << ", ";
700
701 if (Base->isVirtual())
702 Out << "virtual ";
703
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000704 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
705 if (AS != AS_none)
706 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000707 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor34a99e72011-04-27 17:07:55 +0000708
709 if (Base->isPackExpansion())
710 Out << "...";
Douglas Gregor59e63572009-05-30 06:58:37 +0000711 }
712 }
713
714 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000715 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000716 Out << " {\n";
717 VisitDeclContext(D);
718 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000719 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000720}
721
722void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
723 const char *l;
724 if (D->getLanguage() == LinkageSpecDecl::lang_c)
725 l = "C";
726 else {
727 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
728 "unknown language in linkage specification");
729 l = "C++";
730 }
731
732 Out << "extern \"" << l << "\" ";
733 if (D->hasBraces()) {
734 Out << "{\n";
735 VisitDeclContext(D);
736 Indent() << "}";
737 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000738 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000739}
740
Richard Trieu5cb3d692011-07-28 00:19:05 +0000741void DeclPrinter::PrintTemplateParameters(
742 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
743 assert(Params);
744 assert(!Args || Params->size() == Args->size());
745
Anders Carlsson0487f662009-06-04 05:37:43 +0000746 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Anders Carlsson0487f662009-06-04 05:37:43 +0000748 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
749 if (i != 0)
750 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Anders Carlsson0487f662009-06-04 05:37:43 +0000752 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000753 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000754 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Anders Carlsson0487f662009-06-04 05:37:43 +0000756 if (TTP->wasDeclaredWithTypename())
757 Out << "typename ";
758 else
759 Out << "class ";
760
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000761 if (TTP->isParameterPack())
762 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000764 Out << *TTP;
Anders Carlsson0487f662009-06-04 05:37:43 +0000765
Richard Trieu5cb3d692011-07-28 00:19:05 +0000766 if (Args) {
767 Out << " = ";
768 Args->get(i).print(Policy, Out);
769 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000770 Out << " = ";
771 Out << TTP->getDefaultArgument().getAsString(Policy);
772 };
Mike Stump1eb44332009-09-09 15:08:12 +0000773 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000774 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
775 Out << NTTP->getType().getAsString(Policy);
776
Douglas Gregor56bc9832010-12-24 00:15:10 +0000777 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
778 Out << "...";
779
Anders Carlsson0487f662009-06-04 05:37:43 +0000780 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
781 Out << ' ';
782 Out << Name->getName();
783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Richard Trieu5cb3d692011-07-28 00:19:05 +0000785 if (Args) {
786 Out << " = ";
787 Args->get(i).print(Policy, Out);
788 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000789 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000790 NTTP->getDefaultArgument()->printPretty(Out, 0, Policy, Indentation);
Anders Carlsson0487f662009-06-04 05:37:43 +0000791 }
Richard Smith98a57862011-04-15 13:38:57 +0000792 } else if (const TemplateTemplateParmDecl *TTPD =
793 dyn_cast<TemplateTemplateParmDecl>(Param)) {
794 VisitTemplateDecl(TTPD);
795 // FIXME: print the default argument, if present.
Anders Carlsson0487f662009-06-04 05:37:43 +0000796 }
797 }
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Anders Carlsson0487f662009-06-04 05:37:43 +0000799 Out << "> ";
Richard Trieu5cb3d692011-07-28 00:19:05 +0000800}
801
802void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
803 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson0487f662009-06-04 05:37:43 +0000804
Richard Smith98a57862011-04-15 13:38:57 +0000805 if (const TemplateTemplateParmDecl *TTP =
806 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000807 Out << "class ";
808 if (TTP->isParameterPack())
809 Out << "...";
810 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000811 } else {
812 Visit(D->getTemplatedDecl());
813 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000814}
815
Richard Trieu5cb3d692011-07-28 00:19:05 +0000816void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
817 if (PrintInstantiation) {
818 TemplateParameterList *Params = D->getTemplateParameters();
819 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
820 I != E; ++I) {
821 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
822 Visit(*I);
823 }
824 }
825
826 return VisitRedeclarableTemplateDecl(D);
827}
828
829void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
830 if (PrintInstantiation) {
831 TemplateParameterList *Params = D->getTemplateParameters();
832 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
833 I != E; ++I) {
834 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
835 Visit(*I);
836 Out << '\n';
837 }
838 }
839
840 return VisitRedeclarableTemplateDecl(D);
841}
842
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000843//----------------------------------------------------------------------------
844// Objective-C declarations
845//----------------------------------------------------------------------------
846
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000847void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
848 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000849 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000850 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000851 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000852 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000853 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000855 std::string name = OMD->getSelector().getAsString();
856 std::string::size_type pos, lastPos = 0;
857 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
858 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000859 // FIXME: selector is missing here!
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000860 pos = name.find_first_of(':', lastPos);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000861 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000862 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000863 lastPos = pos + 1;
864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000866 if (OMD->param_begin() == OMD->param_end())
867 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000869 if (OMD->isVariadic())
870 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000872 if (OMD->getBody()) {
873 Out << ' ';
Richard Smithd1420c62012-08-16 03:56:14 +0000874 OMD->getBody()->printPretty(Out, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000875 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000876 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000877}
878
879void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
880 std::string I = OID->getNameAsString();
881 ObjCInterfaceDecl *SID = OID->getSuperClass();
882
883 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000884 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000885 else
886 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000887 Out << "\n";
888 VisitDeclContext(OID, false);
889 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000890}
891
892void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
893 std::string I = OID->getNameAsString();
894 ObjCInterfaceDecl *SID = OID->getSuperClass();
895
Douglas Gregor7723fec2011-12-15 20:29:51 +0000896 if (!OID->isThisDeclarationADefinition()) {
897 Out << "@class " << I << ";";
898 return;
899 }
900
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000901 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000902 Out << "@interface " << I << " : " << *SID;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000903 else
904 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000906 // Protocols?
907 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
908 if (!Protocols.empty()) {
909 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
910 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000911 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000914 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000915 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000917 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000918 Out << "{\n";
919 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000920 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
921 E = OID->ivar_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +0000922 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000923 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000924 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000925 Out << "}\n";
926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000928 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000929 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000930 // FIXME: implement the rest...
931}
932
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000933void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000934 if (!PID->isThisDeclarationADefinition()) {
935 Out << "@protocol " << PID->getIdentifier() << ";\n";
936 return;
937 }
938
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000939 Out << "@protocol " << *PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000940 VisitDeclContext(PID, false);
941 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000942}
943
944void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000945 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000946
947 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000948 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000949 // FIXME: implement the rest...
950}
951
952void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000953 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000954 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000955 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000957 // FIXME: implement the rest...
958}
959
960void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000961 Out << "@compatibility_alias " << *AID
962 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000963}
964
965/// PrintObjCPropertyDecl - print a property declaration.
966///
967void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
968 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
969 Out << "@required\n";
970 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
971 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000973 Out << "@property";
974 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
975 bool first = true;
976 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000977 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000978 ObjCPropertyDecl::OBJC_PR_readonly) {
979 Out << (first ? ' ' : ',') << "readonly";
980 first = false;
Ted Kremenek07c682a2011-08-17 21:09:35 +0000981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenek07c682a2011-08-17 21:09:35 +0000983 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
984 Out << (first ? ' ' : ',') << "getter = "
985 << PDecl->getGetterName().getAsString();
986 first = false;
987 }
988 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
989 Out << (first ? ' ' : ',') << "setter = "
990 << PDecl->getSetterName().getAsString();
991 first = false;
992 }
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Ted Kremenek07c682a2011-08-17 21:09:35 +0000994 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
995 Out << (first ? ' ' : ',') << "assign";
996 first = false;
997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Ted Kremenek07c682a2011-08-17 21:09:35 +0000999 if (PDecl->getPropertyAttributes() &
1000 ObjCPropertyDecl::OBJC_PR_readwrite) {
1001 Out << (first ? ' ' : ',') << "readwrite";
1002 first = false;
1003 }
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Ted Kremenek07c682a2011-08-17 21:09:35 +00001005 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1006 Out << (first ? ' ' : ',') << "retain";
1007 first = false;
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Ted Kremenek07c682a2011-08-17 21:09:35 +00001010 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1011 Out << (first ? ' ' : ',') << "strong";
1012 first = false;
1013 }
John McCallf85e1932011-06-15 23:02:42 +00001014
Ted Kremenek07c682a2011-08-17 21:09:35 +00001015 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1016 Out << (first ? ' ' : ',') << "copy";
1017 first = false;
1018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremenek07c682a2011-08-17 21:09:35 +00001020 if (PDecl->getPropertyAttributes() &
1021 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1022 Out << (first ? ' ' : ',') << "nonatomic";
1023 first = false;
1024 }
1025 if (PDecl->getPropertyAttributes() &
1026 ObjCPropertyDecl::OBJC_PR_atomic) {
1027 Out << (first ? ' ' : ',') << "atomic";
1028 first = false;
1029 }
1030
1031 (void) first; // Silence dead store warning due to idiomatic code.
1032 Out << " )";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001033 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001034 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001035}
1036
1037void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1038 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +00001039 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001040 else
Douglas Gregor64f65002009-05-30 00:56:08 +00001041 Out << "@dynamic ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001042 Out << *PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001043 if (PID->getPropertyIvarDecl())
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001044 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001045}
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001046
1047void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1048 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001049 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001050 Out << *D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001051}
1052
John McCall7ba107a2009-11-18 02:36:19 +00001053void
1054DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1055 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +00001056 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001057 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +00001058}
1059
1060void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001061 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001062 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001063 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001064}
John McCall9488ea12009-11-17 05:59:44 +00001065
1066void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1067 // ignore
1068}