blob: 8a53900d5d0d0b3563fe8f394f8660d4764a8bef [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 Friedman15631b42012-08-08 03:47:15 +0000117 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
118 BaseType = RTy->getPointeeType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000119 else
David Blaikieb219cfc2011-09-23 05:06:16 +0000120 llvm_unreachable("Unknown declarator!");
Eli Friedman42f42c02009-05-30 04:20:30 +0000121 }
122 return BaseType;
123}
124
125static QualType getDeclType(Decl* D) {
Richard Smith162e1c12011-04-15 14:24:37 +0000126 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman42f42c02009-05-30 04:20:30 +0000127 return TDD->getUnderlyingType();
128 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
129 return VD->getType();
130 return QualType();
131}
132
133void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000135 unsigned Indentation) {
136 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000137 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000138 return;
139 }
140
141 Decl** End = Begin + NumDecls;
142 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
143 if (TD)
144 ++Begin;
145
146 PrintingPolicy SubPolicy(Policy);
John McCall5e1cdac2011-10-07 06:10:15 +0000147 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000148 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000149 Out << " ";
150 SubPolicy.SuppressTag = true;
151 }
152
153 bool isFirst = true;
154 for ( ; Begin != End; ++Begin) {
155 if (isFirst) {
156 SubPolicy.SuppressSpecifiers = false;
157 isFirst = false;
158 } else {
159 if (!isFirst) Out << ", ";
160 SubPolicy.SuppressSpecifiers = true;
161 }
162
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000163 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000164 }
165}
166
Anders Carlsson84834432009-12-14 00:51:04 +0000167void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000168 // Get the translation unit
169 const DeclContext *DC = this;
170 while (!DC->isTranslationUnit())
171 DC = DC->getParent();
172
173 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Douglas Gregor30c42402011-09-27 22:38:19 +0000174 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.getPrintingPolicy(), 0);
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000175 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
176}
177
Benjamin Kramer42c72c22012-08-14 14:50:32 +0000178void Decl::dump() const {
179 dump(llvm::errs());
180}
181
Alexander Kornienkoe34a0522012-07-26 16:01:23 +0000182void Decl::dump(raw_ostream &Out) const {
183 PrintingPolicy Policy = getASTContext().getPrintingPolicy();
184 Policy.Dump = true;
185 print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000186}
187
Chris Lattner5f9e2722011-07-23 10:55:15 +0000188raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar512ce602009-11-21 09:12:06 +0000189 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000190 Out << " ";
191 return Out;
192}
193
Douglas Gregor1bea8802011-11-19 19:22:57 +0000194void DeclPrinter::prettyPrintAttributes(Decl *D) {
195 if (D->hasAttrs()) {
196 AttrVec &Attrs = D->getAttrs();
197 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
198 Attr *A = *i;
199 A->printPretty(Out, Context);
200 }
201 }
202}
203
Chris Lattner5f9e2722011-07-23 10:55:15 +0000204void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000205 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000206 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000207 Out << ";\n";
208 Decls.clear();
209
210}
211
Anders Carlsson0d592922009-08-28 22:39:52 +0000212void DeclPrinter::Print(AccessSpecifier AS) {
213 switch(AS) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000214 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson0d592922009-08-28 22:39:52 +0000215 case AS_public: Out << "public"; break;
216 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000217 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000218 }
219}
220
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000221//----------------------------------------------------------------------------
222// Common C declarations
223//----------------------------------------------------------------------------
224
225void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
226 if (Indent)
227 Indentation += Policy.Indentation;
228
Chris Lattner5f9e2722011-07-23 10:55:15 +0000229 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000230 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000231 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-07-30 00:47:46 +0000232
233 // Don't print ObjCIvarDecls, as they are printed when visiting the
234 // containing ObjCInterfaceDecl.
235 if (isa<ObjCIvarDecl>(*D))
236 continue;
237
Eli Friedman48d14a22009-05-30 05:03:24 +0000238 if (!Policy.Dump) {
239 // Skip over implicit declarations in pretty-printing mode.
240 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000241 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidis2574f6f2010-06-17 10:52:11 +0000242 // of __builtin_va_list or __[u]int128_t. There should be some other way
243 // to check that.
244 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
245 if (IdentifierInfo *II = ND->getIdentifier()) {
246 if (II->isStr("__builtin_va_list") ||
247 II->isStr("__int128_t") || II->isStr("__uint128_t"))
248 continue;
249 }
250 }
Eli Friedman48d14a22009-05-30 05:03:24 +0000251 }
252
Eli Friedman42f42c02009-05-30 04:20:30 +0000253 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
254 // forced to merge the declarations because there's no other way to
255 // refer to the struct in question. This limited merging is safe without
256 // a bunch of other checks because it only merges declarations directly
257 // referring to the tag, not typedefs.
258 //
259 // Check whether the current declaration should be grouped with a previous
260 // unnamed struct.
261 QualType CurDeclType = getDeclType(*D);
262 if (!Decls.empty() && !CurDeclType.isNull()) {
263 QualType BaseType = GetBaseType(CurDeclType);
264 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
265 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
266 Decls.push_back(*D);
267 continue;
268 }
269 }
270
271 // If we have a merged group waiting to be handled, handle it now.
272 if (!Decls.empty())
273 ProcessDeclGroup(Decls);
274
275 // If the current declaration is an unnamed tag type, save it
276 // so we can merge it with the subsequent declaration(s) using it.
277 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
278 Decls.push_back(*D);
279 continue;
280 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000281
282 if (isa<AccessSpecDecl>(*D)) {
283 Indentation -= Policy.Indentation;
284 this->Indent();
285 Print(D->getAccess());
286 Out << ":\n";
287 Indentation += Policy.Indentation;
288 continue;
289 }
290
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000291 this->Indent();
292 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000293
294 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000295 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000296 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000297 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
298 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000299 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
300 Terminator = 0;
301 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000302 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000303 isa<ObjCInterfaceDecl>(*D) ||
304 isa<ObjCProtocolDecl>(*D) ||
305 isa<ObjCCategoryImplDecl>(*D) ||
306 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000307 Terminator = 0;
308 else if (isa<EnumConstantDecl>(*D)) {
309 DeclContext::decl_iterator Next = D;
310 ++Next;
311 if (Next != DEnd)
312 Terminator = ",";
313 } else
314 Terminator = ";";
315
316 if (Terminator)
317 Out << Terminator;
318 Out << "\n";
319 }
320
Eli Friedman42f42c02009-05-30 04:20:30 +0000321 if (!Decls.empty())
322 ProcessDeclGroup(Decls);
323
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000324 if (Indent)
325 Indentation -= Policy.Indentation;
326}
327
328void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
329 VisitDeclContext(D, false);
330}
331
332void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000333 if (!Policy.SuppressSpecifiers) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000334 Out << "typedef ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000335
336 if (D->isModulePrivate())
337 Out << "__module_private__ ";
338 }
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000339 D->getUnderlyingType().print(Out, Policy, D->getName());
Douglas Gregor1bea8802011-11-19 19:22:57 +0000340 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000341}
342
Richard Smith162e1c12011-04-15 14:24:37 +0000343void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000344 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy);
Richard Smith162e1c12011-04-15 14:24:37 +0000345}
346
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000347void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000348 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
349 Out << "__module_private__ ";
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000350 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000351 if (D->isScoped()) {
352 if (D->isScopedUsingClassTag())
353 Out << "class ";
354 else
355 Out << "struct ";
356 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000357 Out << *D;
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000358
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000359 if (D->isFixed())
360 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000361
John McCall5e1cdac2011-10-07 06:10:15 +0000362 if (D->isCompleteDefinition()) {
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000363 Out << " {\n";
364 VisitDeclContext(D);
365 Indent() << "}";
366 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000367 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000368}
369
370void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000371 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
372 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000373 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000374 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000375 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000376
John McCall5e1cdac2011-10-07 06:10:15 +0000377 if (D->isCompleteDefinition()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000378 Out << " {\n";
379 VisitDeclContext(D);
380 Indent() << "}";
381 }
382}
383
384void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000385 Out << *D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000386 if (Expr *Init = D->getInitExpr()) {
387 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000388 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000389 }
390}
391
Mike Stump1eb44332009-09-09 15:08:12 +0000392void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000393 if (!Policy.SuppressSpecifiers) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000394 switch (D->getStorageClassAsWritten()) {
John McCalld931b082010-08-26 03:08:43 +0000395 case SC_None: break;
396 case SC_Extern: Out << "extern "; break;
397 case SC_Static: Out << "static "; break;
398 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000399 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
400 llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000401 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000402
Douglas Gregor8d267c52011-09-09 02:06:17 +0000403 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000404 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000405 if (D->isModulePrivate()) Out << "__module_private__ ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000406 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000407
Douglas Gregor6620a622009-05-30 05:39:39 +0000408 PrintingPolicy SubPolicy(Policy);
409 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000410 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000411
Abramo Bagnara723df242010-12-14 22:11:44 +0000412 QualType Ty = D->getType();
John McCallf4c73712011-01-19 06:33:43 +0000413 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000414 Proto = '(' + Proto + ')';
415 Ty = PT->getInnerType();
416 }
417
418 if (isa<FunctionType>(Ty)) {
419 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000420 const FunctionProtoType *FT = 0;
421 if (D->hasWrittenPrototype())
422 FT = dyn_cast<FunctionProtoType>(AFT);
423
424 Proto += "(";
425 if (FT) {
426 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000427 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000428 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
429 if (i) POut << ", ";
430 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000433 if (FT->isVariadic()) {
434 if (D->getNumParams()) POut << ", ";
435 POut << "...";
436 }
Sean Hunt10620eb2011-05-06 20:44:56 +0000437 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000438 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
439 if (i)
440 Proto += ", ";
441 Proto += D->getParamDecl(i)->getNameAsString();
442 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000443 }
444
445 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000446
David Blaikie4ef832f2012-08-10 00:55:35 +0000447 if (FT) {
448 if (FT->isConst())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000449 Proto += " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000450 if (FT->isVolatile())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000451 Proto += " volatile";
David Blaikie4ef832f2012-08-10 00:55:35 +0000452 if (FT->isRestrict())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000453 Proto += " restrict";
454 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000455
456 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000457 Proto += " throw(";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000458 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000459 Proto += "...";
460 else
461 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
462 if (I)
463 Proto += ", ";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000464
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000465 Proto += FT->getExceptionType(I).getAsString(SubPolicy);;
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000466 }
467 Proto += ")";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000468 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
469 Proto += " noexcept";
470 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
471 Proto += "(";
472 llvm::raw_string_ostream EOut(Proto);
473 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
474 Indentation);
475 EOut.flush();
476 Proto += EOut.str();
477 Proto += ")";
478 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000479 }
480
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000481 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith7a614d82011-06-11 17:19:42 +0000482 bool HasInitializerList = false;
483 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
484 E = CDecl->init_end();
485 B != E; ++B) {
486 CXXCtorInitializer * BMInitializer = (*B);
487 if (BMInitializer->isInClassMemberInitializer())
488 continue;
489
490 if (!HasInitializerList) {
491 Proto += " : ";
492 Out << Proto;
493 Proto.clear();
494 HasInitializerList = true;
495 } else
496 Out << ", ";
497
498 if (BMInitializer->isAnyMemberInitializer()) {
499 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000500 Out << *FD;
Richard Smith7a614d82011-06-11 17:19:42 +0000501 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000502 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith7a614d82011-06-11 17:19:42 +0000503 }
504
505 Out << "(";
506 if (!BMInitializer->getInit()) {
507 // Nothing to print
508 } else {
509 Expr *Init = BMInitializer->getInit();
510 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
511 Init = Tmp->getSubExpr();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000512
Richard Smith7a614d82011-06-11 17:19:42 +0000513 Init = Init->IgnoreParens();
514
515 Expr *SimpleInit = 0;
516 Expr **Args = 0;
517 unsigned NumArgs = 0;
518 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
519 Args = ParenList->getExprs();
520 NumArgs = ParenList->getNumExprs();
521 } else if (CXXConstructExpr *Construct
522 = dyn_cast<CXXConstructExpr>(Init)) {
523 Args = Construct->getArgs();
524 NumArgs = Construct->getNumArgs();
525 } else
526 SimpleInit = Init;
527
528 if (SimpleInit)
529 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
530 else {
531 for (unsigned I = 0; I != NumArgs; ++I) {
532 if (isa<CXXDefaultArgExpr>(Args[I]))
533 break;
534
535 if (I)
536 Out << ", ";
537 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000538 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000539 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000540 }
Richard Smith7a614d82011-06-11 17:19:42 +0000541 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000542 }
543 }
544 else
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000545 AFT->getResultType().print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000546 } else {
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000547 Ty.print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000548 }
549
Douglas Gregor1bea8802011-11-19 19:22:57 +0000550 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000551
552 if (D->isPure())
553 Out << " = 0";
Sean Hunt10620eb2011-05-06 20:44:56 +0000554 else if (D->isDeletedAsWritten())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000555 Out << " = delete";
Sean Hunt10620eb2011-05-06 20:44:56 +0000556 else if (D->doesThisDeclarationHaveABody()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000557 if (!D->hasPrototype() && D->getNumParams()) {
558 // This is a K&R function definition, so we need to print the
559 // parameters.
560 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000561 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000562 Indentation += Policy.Indentation;
563 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
564 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000565 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000566 Out << ";\n";
567 }
568 Indentation -= Policy.Indentation;
569 } else
570 Out << ' ';
571
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000572 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000573 Out << '\n';
574 }
575}
576
577void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000578 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000579 Out << "mutable ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000580 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
581 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000582
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000583 Out << D->getType().stream(Policy, D->getName());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000584
585 if (D->isBitField()) {
586 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000587 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000588 }
Richard Smith7a614d82011-06-11 17:19:42 +0000589
590 Expr *Init = D->getInClassInitializer();
591 if (!Policy.SuppressInitializers && Init) {
Richard Smithca523302012-06-10 03:12:00 +0000592 if (D->getInClassInitStyle() == ICIS_ListInit)
593 Out << " ";
594 else
595 Out << " = ";
Richard Smith7a614d82011-06-11 17:19:42 +0000596 Init->printPretty(Out, Context, 0, Policy, Indentation);
597 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000598 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000599}
600
Chris Lattner57ad3782011-02-17 20:34:02 +0000601void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000602 Out << *D << ":";
Chris Lattner57ad3782011-02-17 20:34:02 +0000603}
604
605
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000606void DeclPrinter::VisitVarDecl(VarDecl *D) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000607 StorageClass SCAsWritten = D->getStorageClassAsWritten();
608 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None)
609 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000610
Eli Friedman42f42c02009-05-30 04:20:30 +0000611 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000612 Out << "__thread ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000613 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
614 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000615
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000616 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000617 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000618 T = Parm->getOriginalType();
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000619 T.print(Out, Policy, D->getName());
Richard Smithad762fc2011-04-14 22:09:26 +0000620 Expr *Init = D->getInit();
621 if (!Policy.SuppressInitializers && Init) {
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000622 bool ImplicitInit = false;
623 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
624 ImplicitInit = D->getInitStyle() == VarDecl::CallInit &&
625 Construct->getNumArgs() == 0 && !Construct->isListInitialization();
626 if (!ImplicitInit) {
627 if (D->getInitStyle() == VarDecl::CallInit)
628 Out << "(";
629 else if (D->getInitStyle() == VarDecl::CInit) {
630 Out << " = ";
631 }
632 Init->printPretty(Out, Context, 0, Policy, Indentation);
633 if (D->getInitStyle() == VarDecl::CallInit)
634 Out << ")";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000635 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000636 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000637 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000638}
639
640void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
641 VisitVarDecl(D);
642}
643
644void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
645 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000646 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000647 Out << ")";
648}
649
Douglas Gregor15de72c2011-12-02 23:23:56 +0000650void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Ted Kremenek32ad2ee2012-03-01 22:07:04 +0000651 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName()
Douglas Gregor15de72c2011-12-02 23:23:56 +0000652 << ";\n";
653}
654
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000655void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
656 Out << "static_assert(";
657 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
658 Out << ", ";
659 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
660 Out << ")";
661}
662
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000663//----------------------------------------------------------------------------
664// C++ declarations
665//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000666void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorc30636a2012-05-01 01:43:38 +0000667 if (D->isInline())
668 Out << "inline ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000669 Out << "namespace " << *D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000670 VisitDeclContext(D);
671 Indent() << "}";
672}
673
Douglas Gregor8419fa32009-05-30 06:31:56 +0000674void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
675 Out << "using namespace ";
676 if (D->getQualifier())
677 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000678 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000679}
680
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000681void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000682 Out << "namespace " << *D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000683 if (D->getQualifier())
684 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000685 Out << *D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000686}
687
Douglas Gregor59e63572009-05-30 06:58:37 +0000688void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000689 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
690 Out << "__module_private__ ";
Douglas Gregor59e63572009-05-30 06:58:37 +0000691 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000692 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000693 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000694
John McCall5e1cdac2011-10-07 06:10:15 +0000695 if (D->isCompleteDefinition()) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000696 // Print the base classes
697 if (D->getNumBases()) {
698 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000699 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
700 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000701 if (Base != D->bases_begin())
702 Out << ", ";
703
704 if (Base->isVirtual())
705 Out << "virtual ";
706
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000707 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
708 if (AS != AS_none)
709 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000710 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor34a99e72011-04-27 17:07:55 +0000711
712 if (Base->isPackExpansion())
713 Out << "...";
Douglas Gregor59e63572009-05-30 06:58:37 +0000714 }
715 }
716
717 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000718 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000719 Out << " {\n";
720 VisitDeclContext(D);
721 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000722 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000723}
724
725void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
726 const char *l;
727 if (D->getLanguage() == LinkageSpecDecl::lang_c)
728 l = "C";
729 else {
730 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
731 "unknown language in linkage specification");
732 l = "C++";
733 }
734
735 Out << "extern \"" << l << "\" ";
736 if (D->hasBraces()) {
737 Out << "{\n";
738 VisitDeclContext(D);
739 Indent() << "}";
740 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000741 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000742}
743
Richard Trieu5cb3d692011-07-28 00:19:05 +0000744void DeclPrinter::PrintTemplateParameters(
745 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
746 assert(Params);
747 assert(!Args || Params->size() == Args->size());
748
Anders Carlsson0487f662009-06-04 05:37:43 +0000749 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Anders Carlsson0487f662009-06-04 05:37:43 +0000751 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
752 if (i != 0)
753 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Anders Carlsson0487f662009-06-04 05:37:43 +0000755 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000756 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000757 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Anders Carlsson0487f662009-06-04 05:37:43 +0000759 if (TTP->wasDeclaredWithTypename())
760 Out << "typename ";
761 else
762 Out << "class ";
763
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000764 if (TTP->isParameterPack())
765 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000767 Out << *TTP;
Anders Carlsson0487f662009-06-04 05:37:43 +0000768
Richard Trieu5cb3d692011-07-28 00:19:05 +0000769 if (Args) {
770 Out << " = ";
771 Args->get(i).print(Policy, Out);
772 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000773 Out << " = ";
774 Out << TTP->getDefaultArgument().getAsString(Policy);
775 };
Mike Stump1eb44332009-09-09 15:08:12 +0000776 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000777 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
778 Out << NTTP->getType().getAsString(Policy);
779
Douglas Gregor56bc9832010-12-24 00:15:10 +0000780 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
781 Out << "...";
782
Anders Carlsson0487f662009-06-04 05:37:43 +0000783 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
784 Out << ' ';
785 Out << Name->getName();
786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Richard Trieu5cb3d692011-07-28 00:19:05 +0000788 if (Args) {
789 Out << " = ";
790 Args->get(i).print(Policy, Out);
791 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000792 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000793 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000794 Indentation);
795 }
Richard Smith98a57862011-04-15 13:38:57 +0000796 } else if (const TemplateTemplateParmDecl *TTPD =
797 dyn_cast<TemplateTemplateParmDecl>(Param)) {
798 VisitTemplateDecl(TTPD);
799 // FIXME: print the default argument, if present.
Anders Carlsson0487f662009-06-04 05:37:43 +0000800 }
801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Anders Carlsson0487f662009-06-04 05:37:43 +0000803 Out << "> ";
Richard Trieu5cb3d692011-07-28 00:19:05 +0000804}
805
806void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
807 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson0487f662009-06-04 05:37:43 +0000808
Richard Smith98a57862011-04-15 13:38:57 +0000809 if (const TemplateTemplateParmDecl *TTP =
810 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000811 Out << "class ";
812 if (TTP->isParameterPack())
813 Out << "...";
814 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000815 } else {
816 Visit(D->getTemplatedDecl());
817 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000818}
819
Richard Trieu5cb3d692011-07-28 00:19:05 +0000820void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
821 if (PrintInstantiation) {
822 TemplateParameterList *Params = D->getTemplateParameters();
823 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
824 I != E; ++I) {
825 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
826 Visit(*I);
827 }
828 }
829
830 return VisitRedeclarableTemplateDecl(D);
831}
832
833void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
834 if (PrintInstantiation) {
835 TemplateParameterList *Params = D->getTemplateParameters();
836 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
837 I != E; ++I) {
838 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
839 Visit(*I);
840 Out << '\n';
841 }
842 }
843
844 return VisitRedeclarableTemplateDecl(D);
845}
846
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000847//----------------------------------------------------------------------------
848// Objective-C declarations
849//----------------------------------------------------------------------------
850
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000851void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
852 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000853 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000854 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000855 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000856 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000857 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000859 std::string name = OMD->getSelector().getAsString();
860 std::string::size_type pos, lastPos = 0;
861 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
862 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000863 // FIXME: selector is missing here!
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000864 pos = name.find_first_of(':', lastPos);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000865 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000866 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000867 lastPos = pos + 1;
868 }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000870 if (OMD->param_begin() == OMD->param_end())
871 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000873 if (OMD->isVariadic())
874 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000876 if (OMD->getBody()) {
877 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000878 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000879 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000880 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000881}
882
883void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
884 std::string I = OID->getNameAsString();
885 ObjCInterfaceDecl *SID = OID->getSuperClass();
886
887 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000888 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000889 else
890 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000891 Out << "\n";
892 VisitDeclContext(OID, false);
893 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000894}
895
896void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
897 std::string I = OID->getNameAsString();
898 ObjCInterfaceDecl *SID = OID->getSuperClass();
899
Douglas Gregor7723fec2011-12-15 20:29:51 +0000900 if (!OID->isThisDeclarationADefinition()) {
901 Out << "@class " << I << ";";
902 return;
903 }
904
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000905 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000906 Out << "@interface " << I << " : " << *SID;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000907 else
908 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000910 // Protocols?
911 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
912 if (!Protocols.empty()) {
913 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
914 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000915 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000916 }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000918 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000919 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000921 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000922 Out << "{\n";
923 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000924 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
925 E = OID->ivar_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +0000926 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000927 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000928 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000929 Out << "}\n";
930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000932 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000933 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000934 // FIXME: implement the rest...
935}
936
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000937void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000938 if (!PID->isThisDeclarationADefinition()) {
939 Out << "@protocol " << PID->getIdentifier() << ";\n";
940 return;
941 }
942
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000943 Out << "@protocol " << *PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000944 VisitDeclContext(PID, false);
945 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000946}
947
948void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000949 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000950
951 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000952 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000953 // FIXME: implement the rest...
954}
955
956void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000957 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000958 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000959 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000961 // FIXME: implement the rest...
962}
963
964void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000965 Out << "@compatibility_alias " << *AID
966 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000967}
968
969/// PrintObjCPropertyDecl - print a property declaration.
970///
971void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
972 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
973 Out << "@required\n";
974 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
975 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000977 Out << "@property";
978 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
979 bool first = true;
980 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000981 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000982 ObjCPropertyDecl::OBJC_PR_readonly) {
983 Out << (first ? ' ' : ',') << "readonly";
984 first = false;
Ted Kremenek07c682a2011-08-17 21:09:35 +0000985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Ted Kremenek07c682a2011-08-17 21:09:35 +0000987 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
988 Out << (first ? ' ' : ',') << "getter = "
989 << PDecl->getGetterName().getAsString();
990 first = false;
991 }
992 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
993 Out << (first ? ' ' : ',') << "setter = "
994 << PDecl->getSetterName().getAsString();
995 first = false;
996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Ted Kremenek07c682a2011-08-17 21:09:35 +0000998 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
999 Out << (first ? ' ' : ',') << "assign";
1000 first = false;
1001 }
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Ted Kremenek07c682a2011-08-17 21:09:35 +00001003 if (PDecl->getPropertyAttributes() &
1004 ObjCPropertyDecl::OBJC_PR_readwrite) {
1005 Out << (first ? ' ' : ',') << "readwrite";
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_retain) {
1010 Out << (first ? ' ' : ',') << "retain";
1011 first = false;
1012 }
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Ted Kremenek07c682a2011-08-17 21:09:35 +00001014 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1015 Out << (first ? ' ' : ',') << "strong";
1016 first = false;
1017 }
John McCallf85e1932011-06-15 23:02:42 +00001018
Ted Kremenek07c682a2011-08-17 21:09:35 +00001019 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1020 Out << (first ? ' ' : ',') << "copy";
1021 first = false;
1022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Ted Kremenek07c682a2011-08-17 21:09:35 +00001024 if (PDecl->getPropertyAttributes() &
1025 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1026 Out << (first ? ' ' : ',') << "nonatomic";
1027 first = false;
1028 }
1029 if (PDecl->getPropertyAttributes() &
1030 ObjCPropertyDecl::OBJC_PR_atomic) {
1031 Out << (first ? ' ' : ',') << "atomic";
1032 first = false;
1033 }
1034
1035 (void) first; // Silence dead store warning due to idiomatic code.
1036 Out << " )";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001037 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001038 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001039}
1040
1041void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1042 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +00001043 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001044 else
Douglas Gregor64f65002009-05-30 00:56:08 +00001045 Out << "@dynamic ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001046 Out << *PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001047 if (PID->getPropertyIvarDecl())
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001048 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001049}
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001050
1051void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1052 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001053 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001054 Out << *D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001055}
1056
John McCall7ba107a2009-11-18 02:36:19 +00001057void
1058DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1059 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +00001060 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001061 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +00001062}
1063
1064void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001065 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001066 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001067 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001068}
John McCall9488ea12009-11-17 05:59:44 +00001069
1070void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1071 // ignore
1072}