blob: fe8f8298bec136557c77aac9c850bfcc496d9d07 [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 ASTContext &Context;
30 PrintingPolicy Policy;
31 unsigned Indentation;
Richard Trieu5cb3d692011-07-28 00:19:05 +000032 bool PrintInstantiation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000033
Chris Lattner5f9e2722011-07-23 10:55:15 +000034 raw_ostream& Indent() { return Indent(Indentation); }
35 raw_ostream& Indent(unsigned Indentation);
36 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000037
Anders Carlsson0d592922009-08-28 22:39:52 +000038 void Print(AccessSpecifier AS);
Mike Stump1eb44332009-09-09 15:08:12 +000039
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000040 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000041 DeclPrinter(raw_ostream &Out, ASTContext &Context,
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000042 const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +000043 unsigned Indentation = 0,
44 bool PrintInstantiation = false)
45 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation),
46 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000047
48 void VisitDeclContext(DeclContext *DC, bool Indent = true);
49
50 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
51 void VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +000052 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000053 void VisitEnumDecl(EnumDecl *D);
54 void VisitRecordDecl(RecordDecl *D);
55 void VisitEnumConstantDecl(EnumConstantDecl *D);
56 void VisitFunctionDecl(FunctionDecl *D);
57 void VisitFieldDecl(FieldDecl *D);
58 void VisitVarDecl(VarDecl *D);
Chris Lattner57ad3782011-02-17 20:34:02 +000059 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000060 void VisitParmVarDecl(ParmVarDecl *D);
61 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor15de72c2011-12-02 23:23:56 +000062 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +000063 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000064 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000065 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000066 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000067 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000068 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith98a57862011-04-15 13:38:57 +000069 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000070 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
71 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000072 void VisitObjCMethodDecl(ObjCMethodDecl *D);
73 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
74 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000075 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 Gregor1bea8802011-11-19 19:22:57 +000088 void prettyPrintAttributes(Decl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000089 };
90}
91
Richard Trieu5cb3d692011-07-28 00:19:05 +000092void Decl::print(raw_ostream &Out, unsigned Indentation,
93 bool PrintInstantiation) const {
Douglas Gregor30c42402011-09-27 22:38:19 +000094 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000095}
96
Chris Lattner5f9e2722011-07-23 10:55:15 +000097void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +000098 unsigned Indentation, bool PrintInstantiation) const {
99 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation, PrintInstantiation);
Anders Carlssonf88df862009-09-26 21:58:53 +0000100 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000101}
102
Eli Friedman42f42c02009-05-30 04:20:30 +0000103static QualType GetBaseType(QualType T) {
104 // FIXME: This should be on the Type class!
105 QualType BaseType = T;
106 while (!BaseType->isSpecifierType()) {
107 if (isa<TypedefType>(BaseType))
108 break;
Ted Kremenek6217b802009-07-29 21:53:49 +0000109 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000110 BaseType = PTy->getPointeeType();
111 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
112 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +0000113 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000114 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000115 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000116 BaseType = VTy->getElementType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000117 else
David Blaikieb219cfc2011-09-23 05:06:16 +0000118 llvm_unreachable("Unknown declarator!");
Eli Friedman42f42c02009-05-30 04:20:30 +0000119 }
120 return BaseType;
121}
122
123static QualType getDeclType(Decl* D) {
Richard Smith162e1c12011-04-15 14:24:37 +0000124 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman42f42c02009-05-30 04:20:30 +0000125 return TDD->getUnderlyingType();
126 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
127 return VD->getType();
128 return QualType();
129}
130
131void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000132 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000133 unsigned Indentation) {
134 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000135 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000136 return;
137 }
138
139 Decl** End = Begin + NumDecls;
140 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
141 if (TD)
142 ++Begin;
143
144 PrintingPolicy SubPolicy(Policy);
John McCall5e1cdac2011-10-07 06:10:15 +0000145 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000146 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000147 Out << " ";
148 SubPolicy.SuppressTag = true;
149 }
150
151 bool isFirst = true;
152 for ( ; Begin != End; ++Begin) {
153 if (isFirst) {
154 SubPolicy.SuppressSpecifiers = false;
155 isFirst = false;
156 } else {
157 if (!isFirst) Out << ", ";
158 SubPolicy.SuppressSpecifiers = true;
159 }
160
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000161 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000162 }
163}
164
Anders Carlsson84834432009-12-14 00:51:04 +0000165void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000166 // Get the translation unit
167 const DeclContext *DC = this;
168 while (!DC->isTranslationUnit())
169 DC = DC->getParent();
170
171 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Douglas Gregor30c42402011-09-27 22:38:19 +0000172 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.getPrintingPolicy(), 0);
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000173 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
174}
175
Alexander Kornienkoe34a0522012-07-26 16:01:23 +0000176void Decl::dump(raw_ostream &Out) const {
177 PrintingPolicy Policy = getASTContext().getPrintingPolicy();
178 Policy.Dump = true;
179 print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000180}
181
Chris Lattner5f9e2722011-07-23 10:55:15 +0000182raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar512ce602009-11-21 09:12:06 +0000183 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000184 Out << " ";
185 return Out;
186}
187
Douglas Gregor1bea8802011-11-19 19:22:57 +0000188void DeclPrinter::prettyPrintAttributes(Decl *D) {
189 if (D->hasAttrs()) {
190 AttrVec &Attrs = D->getAttrs();
191 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
192 Attr *A = *i;
193 A->printPretty(Out, Context);
194 }
195 }
196}
197
Chris Lattner5f9e2722011-07-23 10:55:15 +0000198void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000199 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000200 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000201 Out << ";\n";
202 Decls.clear();
203
204}
205
Anders Carlsson0d592922009-08-28 22:39:52 +0000206void DeclPrinter::Print(AccessSpecifier AS) {
207 switch(AS) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000208 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson0d592922009-08-28 22:39:52 +0000209 case AS_public: Out << "public"; break;
210 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000211 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000212 }
213}
214
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000215//----------------------------------------------------------------------------
216// Common C declarations
217//----------------------------------------------------------------------------
218
219void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
220 if (Indent)
221 Indentation += Policy.Indentation;
222
Chris Lattner5f9e2722011-07-23 10:55:15 +0000223 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000224 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000225 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-07-30 00:47:46 +0000226
227 // Don't print ObjCIvarDecls, as they are printed when visiting the
228 // containing ObjCInterfaceDecl.
229 if (isa<ObjCIvarDecl>(*D))
230 continue;
231
Eli Friedman48d14a22009-05-30 05:03:24 +0000232 if (!Policy.Dump) {
233 // Skip over implicit declarations in pretty-printing mode.
234 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000235 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidis2574f6f2010-06-17 10:52:11 +0000236 // of __builtin_va_list or __[u]int128_t. There should be some other way
237 // to check that.
238 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
239 if (IdentifierInfo *II = ND->getIdentifier()) {
240 if (II->isStr("__builtin_va_list") ||
241 II->isStr("__int128_t") || II->isStr("__uint128_t"))
242 continue;
243 }
244 }
Eli Friedman48d14a22009-05-30 05:03:24 +0000245 }
246
Eli Friedman42f42c02009-05-30 04:20:30 +0000247 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
248 // forced to merge the declarations because there's no other way to
249 // refer to the struct in question. This limited merging is safe without
250 // a bunch of other checks because it only merges declarations directly
251 // referring to the tag, not typedefs.
252 //
253 // Check whether the current declaration should be grouped with a previous
254 // unnamed struct.
255 QualType CurDeclType = getDeclType(*D);
256 if (!Decls.empty() && !CurDeclType.isNull()) {
257 QualType BaseType = GetBaseType(CurDeclType);
258 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
259 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
260 Decls.push_back(*D);
261 continue;
262 }
263 }
264
265 // If we have a merged group waiting to be handled, handle it now.
266 if (!Decls.empty())
267 ProcessDeclGroup(Decls);
268
269 // If the current declaration is an unnamed tag type, save it
270 // so we can merge it with the subsequent declaration(s) using it.
271 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
272 Decls.push_back(*D);
273 continue;
274 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000275
276 if (isa<AccessSpecDecl>(*D)) {
277 Indentation -= Policy.Indentation;
278 this->Indent();
279 Print(D->getAccess());
280 Out << ":\n";
281 Indentation += Policy.Indentation;
282 continue;
283 }
284
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000285 this->Indent();
286 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
288 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000289 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000290 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000291 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
292 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000293 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
294 Terminator = 0;
295 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000296 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000297 isa<ObjCInterfaceDecl>(*D) ||
298 isa<ObjCProtocolDecl>(*D) ||
299 isa<ObjCCategoryImplDecl>(*D) ||
300 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000301 Terminator = 0;
302 else if (isa<EnumConstantDecl>(*D)) {
303 DeclContext::decl_iterator Next = D;
304 ++Next;
305 if (Next != DEnd)
306 Terminator = ",";
307 } else
308 Terminator = ";";
309
310 if (Terminator)
311 Out << Terminator;
312 Out << "\n";
313 }
314
Eli Friedman42f42c02009-05-30 04:20:30 +0000315 if (!Decls.empty())
316 ProcessDeclGroup(Decls);
317
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000318 if (Indent)
319 Indentation -= Policy.Indentation;
320}
321
322void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
323 VisitDeclContext(D, false);
324}
325
326void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000327 if (!Policy.SuppressSpecifiers) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000328 Out << "typedef ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000329
330 if (D->isModulePrivate())
331 Out << "__module_private__ ";
332 }
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000333 D->getUnderlyingType().print(Out, Policy, D->getName());
Douglas Gregor1bea8802011-11-19 19:22:57 +0000334 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000335}
336
Richard Smith162e1c12011-04-15 14:24:37 +0000337void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000338 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy);
Richard Smith162e1c12011-04-15 14:24:37 +0000339}
340
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000341void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000342 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
343 Out << "__module_private__ ";
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000344 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000345 if (D->isScoped()) {
346 if (D->isScopedUsingClassTag())
347 Out << "class ";
348 else
349 Out << "struct ";
350 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000351 Out << *D;
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000352
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000353 if (D->isFixed())
354 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000355
John McCall5e1cdac2011-10-07 06:10:15 +0000356 if (D->isCompleteDefinition()) {
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000357 Out << " {\n";
358 VisitDeclContext(D);
359 Indent() << "}";
360 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000361 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000362}
363
364void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000365 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
366 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000367 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000368 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000369 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000370
John McCall5e1cdac2011-10-07 06:10:15 +0000371 if (D->isCompleteDefinition()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000372 Out << " {\n";
373 VisitDeclContext(D);
374 Indent() << "}";
375 }
376}
377
378void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000379 Out << *D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000380 if (Expr *Init = D->getInitExpr()) {
381 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000382 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000383 }
384}
385
Mike Stump1eb44332009-09-09 15:08:12 +0000386void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000387 if (!Policy.SuppressSpecifiers) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000388 switch (D->getStorageClassAsWritten()) {
John McCalld931b082010-08-26 03:08:43 +0000389 case SC_None: break;
390 case SC_Extern: Out << "extern "; break;
391 case SC_Static: Out << "static "; break;
392 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000393 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
394 llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000395 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000396
Douglas Gregor8d267c52011-09-09 02:06:17 +0000397 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000398 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000399 if (D->isModulePrivate()) Out << "__module_private__ ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000400 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000401
Douglas Gregor6620a622009-05-30 05:39:39 +0000402 PrintingPolicy SubPolicy(Policy);
403 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000404 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000405
Abramo Bagnara723df242010-12-14 22:11:44 +0000406 QualType Ty = D->getType();
John McCallf4c73712011-01-19 06:33:43 +0000407 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000408 Proto = '(' + Proto + ')';
409 Ty = PT->getInnerType();
410 }
411
412 if (isa<FunctionType>(Ty)) {
413 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000414 const FunctionProtoType *FT = 0;
415 if (D->hasWrittenPrototype())
416 FT = dyn_cast<FunctionProtoType>(AFT);
417
418 Proto += "(";
419 if (FT) {
420 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000421 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000422 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
423 if (i) POut << ", ";
424 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000427 if (FT->isVariadic()) {
428 if (D->getNumParams()) POut << ", ";
429 POut << "...";
430 }
Sean Hunt10620eb2011-05-06 20:44:56 +0000431 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000432 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
433 if (i)
434 Proto += ", ";
435 Proto += D->getParamDecl(i)->getNameAsString();
436 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000437 }
438
439 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000440
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000441 if (FT && FT->getTypeQuals()) {
442 unsigned TypeQuals = FT->getTypeQuals();
443 if (TypeQuals & Qualifiers::Const)
444 Proto += " const";
445 if (TypeQuals & Qualifiers::Volatile)
446 Proto += " volatile";
447 if (TypeQuals & Qualifiers::Restrict)
448 Proto += " restrict";
449 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000450
451 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000452 Proto += " throw(";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000453 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000454 Proto += "...";
455 else
456 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
457 if (I)
458 Proto += ", ";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000459
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000460 Proto += FT->getExceptionType(I).getAsString(SubPolicy);;
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000461 }
462 Proto += ")";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000463 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
464 Proto += " noexcept";
465 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
466 Proto += "(";
467 llvm::raw_string_ostream EOut(Proto);
468 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
469 Indentation);
470 EOut.flush();
471 Proto += EOut.str();
472 Proto += ")";
473 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000474 }
475
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000476 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith7a614d82011-06-11 17:19:42 +0000477 bool HasInitializerList = false;
478 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
479 E = CDecl->init_end();
480 B != E; ++B) {
481 CXXCtorInitializer * BMInitializer = (*B);
482 if (BMInitializer->isInClassMemberInitializer())
483 continue;
484
485 if (!HasInitializerList) {
486 Proto += " : ";
487 Out << Proto;
488 Proto.clear();
489 HasInitializerList = true;
490 } else
491 Out << ", ";
492
493 if (BMInitializer->isAnyMemberInitializer()) {
494 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000495 Out << *FD;
Richard Smith7a614d82011-06-11 17:19:42 +0000496 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000497 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith7a614d82011-06-11 17:19:42 +0000498 }
499
500 Out << "(";
501 if (!BMInitializer->getInit()) {
502 // Nothing to print
503 } else {
504 Expr *Init = BMInitializer->getInit();
505 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
506 Init = Tmp->getSubExpr();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000507
Richard Smith7a614d82011-06-11 17:19:42 +0000508 Init = Init->IgnoreParens();
509
510 Expr *SimpleInit = 0;
511 Expr **Args = 0;
512 unsigned NumArgs = 0;
513 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
514 Args = ParenList->getExprs();
515 NumArgs = ParenList->getNumExprs();
516 } else if (CXXConstructExpr *Construct
517 = dyn_cast<CXXConstructExpr>(Init)) {
518 Args = Construct->getArgs();
519 NumArgs = Construct->getNumArgs();
520 } else
521 SimpleInit = Init;
522
523 if (SimpleInit)
524 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
525 else {
526 for (unsigned I = 0; I != NumArgs; ++I) {
527 if (isa<CXXDefaultArgExpr>(Args[I]))
528 break;
529
530 if (I)
531 Out << ", ";
532 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000533 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000534 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000535 }
Richard Smith7a614d82011-06-11 17:19:42 +0000536 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000537 }
538 }
539 else
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000540 AFT->getResultType().print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000541 } else {
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000542 Ty.print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000543 }
544
Douglas Gregor1bea8802011-11-19 19:22:57 +0000545 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000546
547 if (D->isPure())
548 Out << " = 0";
Sean Hunt10620eb2011-05-06 20:44:56 +0000549 else if (D->isDeletedAsWritten())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000550 Out << " = delete";
Sean Hunt10620eb2011-05-06 20:44:56 +0000551 else if (D->doesThisDeclarationHaveABody()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000552 if (!D->hasPrototype() && D->getNumParams()) {
553 // This is a K&R function definition, so we need to print the
554 // parameters.
555 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000556 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000557 Indentation += Policy.Indentation;
558 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
559 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000560 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000561 Out << ";\n";
562 }
563 Indentation -= Policy.Indentation;
564 } else
565 Out << ' ';
566
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000567 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000568 Out << '\n';
569 }
570}
571
572void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000573 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000574 Out << "mutable ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000575 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
576 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000577
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000578 Out << D->getType().stream(Policy, D->getName());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000579
580 if (D->isBitField()) {
581 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000582 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000583 }
Richard Smith7a614d82011-06-11 17:19:42 +0000584
585 Expr *Init = D->getInClassInitializer();
586 if (!Policy.SuppressInitializers && Init) {
Richard Smithca523302012-06-10 03:12:00 +0000587 if (D->getInClassInitStyle() == ICIS_ListInit)
588 Out << " ";
589 else
590 Out << " = ";
Richard Smith7a614d82011-06-11 17:19:42 +0000591 Init->printPretty(Out, Context, 0, Policy, Indentation);
592 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000593 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000594}
595
Chris Lattner57ad3782011-02-17 20:34:02 +0000596void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000597 Out << *D << ":";
Chris Lattner57ad3782011-02-17 20:34:02 +0000598}
599
600
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000601void DeclPrinter::VisitVarDecl(VarDecl *D) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000602 StorageClass SCAsWritten = D->getStorageClassAsWritten();
603 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None)
604 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000605
Eli Friedman42f42c02009-05-30 04:20:30 +0000606 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000607 Out << "__thread ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000608 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
609 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000610
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000611 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000612 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000613 T = Parm->getOriginalType();
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000614 T.print(Out, Policy, D->getName());
Richard Smithad762fc2011-04-14 22:09:26 +0000615 Expr *Init = D->getInit();
616 if (!Policy.SuppressInitializers && Init) {
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000617 bool ImplicitInit = false;
618 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
619 ImplicitInit = D->getInitStyle() == VarDecl::CallInit &&
620 Construct->getNumArgs() == 0 && !Construct->isListInitialization();
621 if (!ImplicitInit) {
622 if (D->getInitStyle() == VarDecl::CallInit)
623 Out << "(";
624 else if (D->getInitStyle() == VarDecl::CInit) {
625 Out << " = ";
626 }
627 Init->printPretty(Out, Context, 0, Policy, Indentation);
628 if (D->getInitStyle() == VarDecl::CallInit)
629 Out << ")";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000630 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000631 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000632 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000633}
634
635void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
636 VisitVarDecl(D);
637}
638
639void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
640 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000641 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000642 Out << ")";
643}
644
Douglas Gregor15de72c2011-12-02 23:23:56 +0000645void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Ted Kremenek32ad2ee2012-03-01 22:07:04 +0000646 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName()
Douglas Gregor15de72c2011-12-02 23:23:56 +0000647 << ";\n";
648}
649
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000650void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
651 Out << "static_assert(";
652 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
653 Out << ", ";
654 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
655 Out << ")";
656}
657
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000658//----------------------------------------------------------------------------
659// C++ declarations
660//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000661void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorc30636a2012-05-01 01:43:38 +0000662 if (D->isInline())
663 Out << "inline ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000664 Out << "namespace " << *D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000665 VisitDeclContext(D);
666 Indent() << "}";
667}
668
Douglas Gregor8419fa32009-05-30 06:31:56 +0000669void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
670 Out << "using namespace ";
671 if (D->getQualifier())
672 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000673 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000674}
675
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000676void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000677 Out << "namespace " << *D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000678 if (D->getQualifier())
679 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000680 Out << *D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000681}
682
Douglas Gregor59e63572009-05-30 06:58:37 +0000683void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000684 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
685 Out << "__module_private__ ";
Douglas Gregor59e63572009-05-30 06:58:37 +0000686 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000687 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000688 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000689
John McCall5e1cdac2011-10-07 06:10:15 +0000690 if (D->isCompleteDefinition()) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000691 // Print the base classes
692 if (D->getNumBases()) {
693 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000694 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
695 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000696 if (Base != D->bases_begin())
697 Out << ", ";
698
699 if (Base->isVirtual())
700 Out << "virtual ";
701
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000702 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
703 if (AS != AS_none)
704 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000705 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor34a99e72011-04-27 17:07:55 +0000706
707 if (Base->isPackExpansion())
708 Out << "...";
Douglas Gregor59e63572009-05-30 06:58:37 +0000709 }
710 }
711
712 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000713 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000714 Out << " {\n";
715 VisitDeclContext(D);
716 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000717 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000718}
719
720void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
721 const char *l;
722 if (D->getLanguage() == LinkageSpecDecl::lang_c)
723 l = "C";
724 else {
725 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
726 "unknown language in linkage specification");
727 l = "C++";
728 }
729
730 Out << "extern \"" << l << "\" ";
731 if (D->hasBraces()) {
732 Out << "{\n";
733 VisitDeclContext(D);
734 Indent() << "}";
735 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000736 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000737}
738
Richard Trieu5cb3d692011-07-28 00:19:05 +0000739void DeclPrinter::PrintTemplateParameters(
740 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
741 assert(Params);
742 assert(!Args || Params->size() == Args->size());
743
Anders Carlsson0487f662009-06-04 05:37:43 +0000744 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Anders Carlsson0487f662009-06-04 05:37:43 +0000746 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
747 if (i != 0)
748 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Anders Carlsson0487f662009-06-04 05:37:43 +0000750 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000751 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000752 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Anders Carlsson0487f662009-06-04 05:37:43 +0000754 if (TTP->wasDeclaredWithTypename())
755 Out << "typename ";
756 else
757 Out << "class ";
758
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000759 if (TTP->isParameterPack())
760 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000762 Out << *TTP;
Anders Carlsson0487f662009-06-04 05:37:43 +0000763
Richard Trieu5cb3d692011-07-28 00:19:05 +0000764 if (Args) {
765 Out << " = ";
766 Args->get(i).print(Policy, Out);
767 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000768 Out << " = ";
769 Out << TTP->getDefaultArgument().getAsString(Policy);
770 };
Mike Stump1eb44332009-09-09 15:08:12 +0000771 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000772 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
773 Out << NTTP->getType().getAsString(Policy);
774
Douglas Gregor56bc9832010-12-24 00:15:10 +0000775 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
776 Out << "...";
777
Anders Carlsson0487f662009-06-04 05:37:43 +0000778 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
779 Out << ' ';
780 Out << Name->getName();
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Richard Trieu5cb3d692011-07-28 00:19:05 +0000783 if (Args) {
784 Out << " = ";
785 Args->get(i).print(Policy, Out);
786 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000787 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000788 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000789 Indentation);
790 }
Richard Smith98a57862011-04-15 13:38:57 +0000791 } else if (const TemplateTemplateParmDecl *TTPD =
792 dyn_cast<TemplateTemplateParmDecl>(Param)) {
793 VisitTemplateDecl(TTPD);
794 // FIXME: print the default argument, if present.
Anders Carlsson0487f662009-06-04 05:37:43 +0000795 }
796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Anders Carlsson0487f662009-06-04 05:37:43 +0000798 Out << "> ";
Richard Trieu5cb3d692011-07-28 00:19:05 +0000799}
800
801void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
802 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson0487f662009-06-04 05:37:43 +0000803
Richard Smith98a57862011-04-15 13:38:57 +0000804 if (const TemplateTemplateParmDecl *TTP =
805 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000806 Out << "class ";
807 if (TTP->isParameterPack())
808 Out << "...";
809 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000810 } else {
811 Visit(D->getTemplatedDecl());
812 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000813}
814
Richard Trieu5cb3d692011-07-28 00:19:05 +0000815void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
816 if (PrintInstantiation) {
817 TemplateParameterList *Params = D->getTemplateParameters();
818 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
819 I != E; ++I) {
820 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
821 Visit(*I);
822 }
823 }
824
825 return VisitRedeclarableTemplateDecl(D);
826}
827
828void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
829 if (PrintInstantiation) {
830 TemplateParameterList *Params = D->getTemplateParameters();
831 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
832 I != E; ++I) {
833 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
834 Visit(*I);
835 Out << '\n';
836 }
837 }
838
839 return VisitRedeclarableTemplateDecl(D);
840}
841
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000842//----------------------------------------------------------------------------
843// Objective-C declarations
844//----------------------------------------------------------------------------
845
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000846void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
847 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000848 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000849 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000850 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000851 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000852 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000854 std::string name = OMD->getSelector().getAsString();
855 std::string::size_type pos, lastPos = 0;
856 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
857 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000858 // FIXME: selector is missing here!
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000859 pos = name.find_first_of(':', lastPos);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000860 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000861 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000862 lastPos = pos + 1;
863 }
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000865 if (OMD->param_begin() == OMD->param_end())
866 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000868 if (OMD->isVariadic())
869 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000871 if (OMD->getBody()) {
872 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000873 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000874 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000875 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000876}
877
878void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
879 std::string I = OID->getNameAsString();
880 ObjCInterfaceDecl *SID = OID->getSuperClass();
881
882 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000883 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000884 else
885 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000886 Out << "\n";
887 VisitDeclContext(OID, false);
888 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000889}
890
891void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
892 std::string I = OID->getNameAsString();
893 ObjCInterfaceDecl *SID = OID->getSuperClass();
894
Douglas Gregor7723fec2011-12-15 20:29:51 +0000895 if (!OID->isThisDeclarationADefinition()) {
896 Out << "@class " << I << ";";
897 return;
898 }
899
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000900 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000901 Out << "@interface " << I << " : " << *SID;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000902 else
903 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000905 // Protocols?
906 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
907 if (!Protocols.empty()) {
908 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
909 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000910 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000911 }
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000913 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000914 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000916 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000917 Out << "{\n";
918 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000919 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
920 E = OID->ivar_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +0000921 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000922 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000923 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000924 Out << "}\n";
925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000927 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000928 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000929 // FIXME: implement the rest...
930}
931
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000932void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000933 if (!PID->isThisDeclarationADefinition()) {
934 Out << "@protocol " << PID->getIdentifier() << ";\n";
935 return;
936 }
937
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000938 Out << "@protocol " << *PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000939 VisitDeclContext(PID, false);
940 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000941}
942
943void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000944 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000945
946 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000947 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000948 // FIXME: implement the rest...
949}
950
951void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000952 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000953 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000954 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000956 // FIXME: implement the rest...
957}
958
959void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000960 Out << "@compatibility_alias " << *AID
961 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000962}
963
964/// PrintObjCPropertyDecl - print a property declaration.
965///
966void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
967 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
968 Out << "@required\n";
969 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
970 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000972 Out << "@property";
973 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
974 bool first = true;
975 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000976 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000977 ObjCPropertyDecl::OBJC_PR_readonly) {
978 Out << (first ? ' ' : ',') << "readonly";
979 first = false;
Ted Kremenek07c682a2011-08-17 21:09:35 +0000980 }
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Ted Kremenek07c682a2011-08-17 21:09:35 +0000982 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
983 Out << (first ? ' ' : ',') << "getter = "
984 << PDecl->getGetterName().getAsString();
985 first = false;
986 }
987 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
988 Out << (first ? ' ' : ',') << "setter = "
989 << PDecl->getSetterName().getAsString();
990 first = false;
991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Ted Kremenek07c682a2011-08-17 21:09:35 +0000993 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
994 Out << (first ? ' ' : ',') << "assign";
995 first = false;
996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Ted Kremenek07c682a2011-08-17 21:09:35 +0000998 if (PDecl->getPropertyAttributes() &
999 ObjCPropertyDecl::OBJC_PR_readwrite) {
1000 Out << (first ? ' ' : ',') << "readwrite";
1001 first = false;
1002 }
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Ted Kremenek07c682a2011-08-17 21:09:35 +00001004 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1005 Out << (first ? ' ' : ',') << "retain";
1006 first = false;
1007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Ted Kremenek07c682a2011-08-17 21:09:35 +00001009 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1010 Out << (first ? ' ' : ',') << "strong";
1011 first = false;
1012 }
John McCallf85e1932011-06-15 23:02:42 +00001013
Ted Kremenek07c682a2011-08-17 21:09:35 +00001014 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1015 Out << (first ? ' ' : ',') << "copy";
1016 first = false;
1017 }
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Ted Kremenek07c682a2011-08-17 21:09:35 +00001019 if (PDecl->getPropertyAttributes() &
1020 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1021 Out << (first ? ' ' : ',') << "nonatomic";
1022 first = false;
1023 }
1024 if (PDecl->getPropertyAttributes() &
1025 ObjCPropertyDecl::OBJC_PR_atomic) {
1026 Out << (first ? ' ' : ',') << "atomic";
1027 first = false;
1028 }
1029
1030 (void) first; // Silence dead store warning due to idiomatic code.
1031 Out << " )";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001032 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001033 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001034}
1035
1036void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1037 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +00001038 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001039 else
Douglas Gregor64f65002009-05-30 00:56:08 +00001040 Out << "@dynamic ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001041 Out << *PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001042 if (PID->getPropertyIvarDecl())
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001043 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001044}
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001045
1046void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1047 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001048 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001049 Out << *D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001050}
1051
John McCall7ba107a2009-11-18 02:36:19 +00001052void
1053DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1054 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +00001055 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001056 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +00001057}
1058
1059void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001060 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001061 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001062 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001063}
John McCall9488ea12009-11-17 05:59:44 +00001064
1065void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1066 // ignore
1067}