blob: e5b4b0431b1ff0cb6e61b04eda9b52639131ec81 [file] [log] [blame]
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl::dump method, which pretty print the
11// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclVisitor.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Douglas Gregor9db7dbb2010-01-31 09:12:51 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000021#include "clang/AST/PrettyPrinter.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000022#include "llvm/Support/raw_ostream.h"
23using namespace clang;
24
25namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000026 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000027 raw_ostream &Out;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000028 ASTContext &Context;
29 PrintingPolicy Policy;
30 unsigned Indentation;
Richard Trieu5cb3d692011-07-28 00:19:05 +000031 bool PrintInstantiation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000032
Chris Lattner5f9e2722011-07-23 10:55:15 +000033 raw_ostream& Indent() { return Indent(Indentation); }
34 raw_ostream& Indent(unsigned Indentation);
35 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000036
Anders Carlsson0d592922009-08-28 22:39:52 +000037 void Print(AccessSpecifier AS);
Mike Stump1eb44332009-09-09 15:08:12 +000038
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000039 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000040 DeclPrinter(raw_ostream &Out, ASTContext &Context,
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000041 const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +000042 unsigned Indentation = 0,
43 bool PrintInstantiation = false)
44 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation),
45 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000046
47 void VisitDeclContext(DeclContext *DC, bool Indent = true);
48
49 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
50 void VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +000051 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000052 void VisitEnumDecl(EnumDecl *D);
53 void VisitRecordDecl(RecordDecl *D);
54 void VisitEnumConstantDecl(EnumConstantDecl *D);
55 void VisitFunctionDecl(FunctionDecl *D);
56 void VisitFieldDecl(FieldDecl *D);
57 void VisitVarDecl(VarDecl *D);
Chris Lattner57ad3782011-02-17 20:34:02 +000058 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000059 void VisitParmVarDecl(ParmVarDecl *D);
60 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +000061 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000062 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000063 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000064 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000065 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000066 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith98a57862011-04-15 13:38:57 +000067 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000068 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
69 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000070 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000071 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000072 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
73 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
74 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
75 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
76 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
77 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
78 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
79 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
80 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000081 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
82 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000083 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000084 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000085
86 void PrintTemplateParameters(const TemplateParameterList *Params,
87 const TemplateArgumentList *Args);
Douglas 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
Anders Carlssonf88df862009-09-26 21:58:53 +0000176void Decl::dump() const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000177 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000178}
179
Chris Lattner5f9e2722011-07-23 10:55:15 +0000180raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar512ce602009-11-21 09:12:06 +0000181 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000182 Out << " ";
183 return Out;
184}
185
Douglas Gregor1bea8802011-11-19 19:22:57 +0000186void DeclPrinter::prettyPrintAttributes(Decl *D) {
187 if (D->hasAttrs()) {
188 AttrVec &Attrs = D->getAttrs();
189 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
190 Attr *A = *i;
191 A->printPretty(Out, Context);
192 }
193 }
194}
195
Chris Lattner5f9e2722011-07-23 10:55:15 +0000196void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000197 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000198 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000199 Out << ";\n";
200 Decls.clear();
201
202}
203
Anders Carlsson0d592922009-08-28 22:39:52 +0000204void DeclPrinter::Print(AccessSpecifier AS) {
205 switch(AS) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000206 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson0d592922009-08-28 22:39:52 +0000207 case AS_public: Out << "public"; break;
208 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000209 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000210 }
211}
212
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000213//----------------------------------------------------------------------------
214// Common C declarations
215//----------------------------------------------------------------------------
216
217void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
218 if (Indent)
219 Indentation += Policy.Indentation;
220
Chris Lattner5f9e2722011-07-23 10:55:15 +0000221 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000222 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000223 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-07-30 00:47:46 +0000224
225 // Don't print ObjCIvarDecls, as they are printed when visiting the
226 // containing ObjCInterfaceDecl.
227 if (isa<ObjCIvarDecl>(*D))
228 continue;
229
Eli Friedman48d14a22009-05-30 05:03:24 +0000230 if (!Policy.Dump) {
231 // Skip over implicit declarations in pretty-printing mode.
232 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000233 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidis2574f6f2010-06-17 10:52:11 +0000234 // of __builtin_va_list or __[u]int128_t. There should be some other way
235 // to check that.
236 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
237 if (IdentifierInfo *II = ND->getIdentifier()) {
238 if (II->isStr("__builtin_va_list") ||
239 II->isStr("__int128_t") || II->isStr("__uint128_t"))
240 continue;
241 }
242 }
Eli Friedman48d14a22009-05-30 05:03:24 +0000243 }
244
Eli Friedman42f42c02009-05-30 04:20:30 +0000245 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
246 // forced to merge the declarations because there's no other way to
247 // refer to the struct in question. This limited merging is safe without
248 // a bunch of other checks because it only merges declarations directly
249 // referring to the tag, not typedefs.
250 //
251 // Check whether the current declaration should be grouped with a previous
252 // unnamed struct.
253 QualType CurDeclType = getDeclType(*D);
254 if (!Decls.empty() && !CurDeclType.isNull()) {
255 QualType BaseType = GetBaseType(CurDeclType);
256 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
257 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
258 Decls.push_back(*D);
259 continue;
260 }
261 }
262
263 // If we have a merged group waiting to be handled, handle it now.
264 if (!Decls.empty())
265 ProcessDeclGroup(Decls);
266
267 // If the current declaration is an unnamed tag type, save it
268 // so we can merge it with the subsequent declaration(s) using it.
269 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
270 Decls.push_back(*D);
271 continue;
272 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000273
274 if (isa<AccessSpecDecl>(*D)) {
275 Indentation -= Policy.Indentation;
276 this->Indent();
277 Print(D->getAccess());
278 Out << ":\n";
279 Indentation += Policy.Indentation;
280 continue;
281 }
282
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000283 this->Indent();
284 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
286 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000287 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000288 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000289 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
290 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000291 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
292 Terminator = 0;
293 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000294 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000295 isa<ObjCInterfaceDecl>(*D) ||
296 isa<ObjCProtocolDecl>(*D) ||
297 isa<ObjCCategoryImplDecl>(*D) ||
298 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000299 Terminator = 0;
300 else if (isa<EnumConstantDecl>(*D)) {
301 DeclContext::decl_iterator Next = D;
302 ++Next;
303 if (Next != DEnd)
304 Terminator = ",";
305 } else
306 Terminator = ";";
307
308 if (Terminator)
309 Out << Terminator;
310 Out << "\n";
311 }
312
Eli Friedman42f42c02009-05-30 04:20:30 +0000313 if (!Decls.empty())
314 ProcessDeclGroup(Decls);
315
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000316 if (Indent)
317 Indentation -= Policy.Indentation;
318}
319
320void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
321 VisitDeclContext(D, false);
322}
323
324void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
325 std::string S = D->getNameAsString();
326 D->getUnderlyingType().getAsStringInternal(S, Policy);
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 }
Eli Friedman42f42c02009-05-30 04:20:30 +0000333 Out << S;
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) {
338 Out << "using " << D->getNameAsString() << " = "
339 << D->getUnderlyingType().getAsString(Policy);
340}
341
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000342void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000343 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
344 Out << "__module_private__ ";
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000345 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000346 if (D->isScoped()) {
347 if (D->isScopedUsingClassTag())
348 Out << "class ";
349 else
350 Out << "struct ";
351 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000352 Out << *D;
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000353
354 if (D->isFixed()) {
355 std::string Underlying;
356 D->getIntegerType().getAsStringInternal(Underlying, Policy);
357 Out << " : " << Underlying;
358 }
359
John McCall5e1cdac2011-10-07 06:10:15 +0000360 if (D->isCompleteDefinition()) {
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000361 Out << " {\n";
362 VisitDeclContext(D);
363 Indent() << "}";
364 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000365 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000366}
367
368void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000369 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
370 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000371 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000372 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000373 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
John McCall5e1cdac2011-10-07 06:10:15 +0000375 if (D->isCompleteDefinition()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000376 Out << " {\n";
377 VisitDeclContext(D);
378 Indent() << "}";
379 }
380}
381
382void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000383 Out << *D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000384 if (Expr *Init = D->getInitExpr()) {
385 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000386 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000387 }
388}
389
Mike Stump1eb44332009-09-09 15:08:12 +0000390void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000391 if (!Policy.SuppressSpecifiers) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000392 switch (D->getStorageClassAsWritten()) {
John McCalld931b082010-08-26 03:08:43 +0000393 case SC_None: break;
394 case SC_Extern: Out << "extern "; break;
395 case SC_Static: Out << "static "; break;
396 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000397 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
398 llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000399 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000400
Douglas Gregor8d267c52011-09-09 02:06:17 +0000401 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000402 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000403 if (D->isModulePrivate()) Out << "__module_private__ ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000404 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000405
Douglas Gregor6620a622009-05-30 05:39:39 +0000406 PrintingPolicy SubPolicy(Policy);
407 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000408 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000409
Abramo Bagnara723df242010-12-14 22:11:44 +0000410 QualType Ty = D->getType();
John McCallf4c73712011-01-19 06:33:43 +0000411 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000412 Proto = '(' + Proto + ')';
413 Ty = PT->getInnerType();
414 }
415
416 if (isa<FunctionType>(Ty)) {
417 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000418 const FunctionProtoType *FT = 0;
419 if (D->hasWrittenPrototype())
420 FT = dyn_cast<FunctionProtoType>(AFT);
421
422 Proto += "(";
423 if (FT) {
424 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000425 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000426 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
427 if (i) POut << ", ";
428 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000431 if (FT->isVariadic()) {
432 if (D->getNumParams()) POut << ", ";
433 POut << "...";
434 }
Sean Hunt10620eb2011-05-06 20:44:56 +0000435 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000436 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
437 if (i)
438 Proto += ", ";
439 Proto += D->getParamDecl(i)->getNameAsString();
440 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000441 }
442
443 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000444
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000445 if (FT && FT->getTypeQuals()) {
446 unsigned TypeQuals = FT->getTypeQuals();
447 if (TypeQuals & Qualifiers::Const)
448 Proto += " const";
449 if (TypeQuals & Qualifiers::Volatile)
450 Proto += " volatile";
451 if (TypeQuals & Qualifiers::Restrict)
452 Proto += " restrict";
453 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000454
455 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000456 Proto += " throw(";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000457 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000458 Proto += "...";
459 else
460 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
461 if (I)
462 Proto += ", ";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000463
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000464 std::string ExceptionType;
465 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
466 Proto += ExceptionType;
467 }
468 Proto += ")";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000469 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
470 Proto += " noexcept";
471 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
472 Proto += "(";
473 llvm::raw_string_ostream EOut(Proto);
474 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
475 Indentation);
476 EOut.flush();
477 Proto += EOut.str();
478 Proto += ")";
479 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000480 }
481
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000482 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith7a614d82011-06-11 17:19:42 +0000483 bool HasInitializerList = false;
484 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
485 E = CDecl->init_end();
486 B != E; ++B) {
487 CXXCtorInitializer * BMInitializer = (*B);
488 if (BMInitializer->isInClassMemberInitializer())
489 continue;
490
491 if (!HasInitializerList) {
492 Proto += " : ";
493 Out << Proto;
494 Proto.clear();
495 HasInitializerList = true;
496 } else
497 Out << ", ";
498
499 if (BMInitializer->isAnyMemberInitializer()) {
500 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000501 Out << *FD;
Richard Smith7a614d82011-06-11 17:19:42 +0000502 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000503 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith7a614d82011-06-11 17:19:42 +0000504 }
505
506 Out << "(";
507 if (!BMInitializer->getInit()) {
508 // Nothing to print
509 } else {
510 Expr *Init = BMInitializer->getInit();
511 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
512 Init = Tmp->getSubExpr();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000513
Richard Smith7a614d82011-06-11 17:19:42 +0000514 Init = Init->IgnoreParens();
515
516 Expr *SimpleInit = 0;
517 Expr **Args = 0;
518 unsigned NumArgs = 0;
519 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
520 Args = ParenList->getExprs();
521 NumArgs = ParenList->getNumExprs();
522 } else if (CXXConstructExpr *Construct
523 = dyn_cast<CXXConstructExpr>(Init)) {
524 Args = Construct->getArgs();
525 NumArgs = Construct->getNumArgs();
526 } else
527 SimpleInit = Init;
528
529 if (SimpleInit)
530 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
531 else {
532 for (unsigned I = 0; I != NumArgs; ++I) {
533 if (isa<CXXDefaultArgExpr>(Args[I]))
534 break;
535
536 if (I)
537 Out << ", ";
538 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000539 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000540 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000541 }
Richard Smith7a614d82011-06-11 17:19:42 +0000542 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000543 }
544 }
545 else
546 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000547 } else {
Abramo Bagnara723df242010-12-14 22:11:44 +0000548 Ty.getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000549 }
550
551 Out << Proto;
Douglas Gregor1bea8802011-11-19 19:22:57 +0000552 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000553
554 if (D->isPure())
555 Out << " = 0";
Sean Hunt10620eb2011-05-06 20:44:56 +0000556 else if (D->isDeletedAsWritten())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000557 Out << " = delete";
Sean Hunt10620eb2011-05-06 20:44:56 +0000558 else if (D->doesThisDeclarationHaveABody()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000559 if (!D->hasPrototype() && D->getNumParams()) {
560 // This is a K&R function definition, so we need to print the
561 // parameters.
562 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000563 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000564 Indentation += Policy.Indentation;
565 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
566 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000567 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000568 Out << ";\n";
569 }
570 Indentation -= Policy.Indentation;
571 } else
572 Out << ' ';
573
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000574 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000575 Out << '\n';
576 }
577}
578
579void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000580 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000581 Out << "mutable ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000582 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
583 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000584
585 std::string Name = D->getNameAsString();
586 D->getType().getAsStringInternal(Name, Policy);
587 Out << Name;
588
589 if (D->isBitField()) {
590 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000591 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000592 }
Richard Smith7a614d82011-06-11 17:19:42 +0000593
594 Expr *Init = D->getInClassInitializer();
595 if (!Policy.SuppressInitializers && Init) {
596 Out << " = ";
597 Init->printPretty(Out, Context, 0, Policy, Indentation);
598 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000599 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000600}
601
Chris Lattner57ad3782011-02-17 20:34:02 +0000602void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
603 Out << D->getNameAsString() << ":";
604}
605
606
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000607void DeclPrinter::VisitVarDecl(VarDecl *D) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000608 StorageClass SCAsWritten = D->getStorageClassAsWritten();
609 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None)
610 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000611
Eli Friedman42f42c02009-05-30 04:20:30 +0000612 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000613 Out << "__thread ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000614 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
615 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000616
617 std::string Name = D->getNameAsString();
618 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000619 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000620 T = Parm->getOriginalType();
621 T.getAsStringInternal(Name, Policy);
622 Out << Name;
Richard Smithad762fc2011-04-14 22:09:26 +0000623 Expr *Init = D->getInit();
624 if (!Policy.SuppressInitializers && Init) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000625 if (D->hasCXXDirectInitializer())
626 Out << "(";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000627 else {
628 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
Jim Goodnow II5eca37c2011-10-30 11:17:39 +0000629 if (!CCE || CCE->getConstructor()->isCopyOrMoveConstructor())
Ted Kremenekc57d6552010-09-17 23:04:38 +0000630 Out << " = ";
631 }
Ted Kremenekbccfd312010-09-07 22:21:59 +0000632 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000633 if (D->hasCXXDirectInitializer())
634 Out << ")";
635 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000636 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000637}
638
639void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
640 VisitVarDecl(D);
641}
642
643void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
644 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000645 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000646 Out << ")";
647}
648
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000649void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
650 Out << "static_assert(";
651 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
652 Out << ", ";
653 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
654 Out << ")";
655}
656
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000657//----------------------------------------------------------------------------
658// C++ declarations
659//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000660void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000661 Out << "namespace " << *D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000662 VisitDeclContext(D);
663 Indent() << "}";
664}
665
Douglas Gregor8419fa32009-05-30 06:31:56 +0000666void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
667 Out << "using namespace ";
668 if (D->getQualifier())
669 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000670 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000671}
672
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000673void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000674 Out << "namespace " << *D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000675 if (D->getQualifier())
676 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000677 Out << *D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000678}
679
Douglas Gregor59e63572009-05-30 06:58:37 +0000680void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000681 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
682 Out << "__module_private__ ";
Douglas Gregor59e63572009-05-30 06:58:37 +0000683 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000684 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000685 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000686
John McCall5e1cdac2011-10-07 06:10:15 +0000687 if (D->isCompleteDefinition()) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000688 // Print the base classes
689 if (D->getNumBases()) {
690 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000691 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
692 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000693 if (Base != D->bases_begin())
694 Out << ", ";
695
696 if (Base->isVirtual())
697 Out << "virtual ";
698
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000699 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
700 if (AS != AS_none)
701 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000702 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor34a99e72011-04-27 17:07:55 +0000703
704 if (Base->isPackExpansion())
705 Out << "...";
Douglas Gregor59e63572009-05-30 06:58:37 +0000706 }
707 }
708
709 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000710 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000711 Out << " {\n";
712 VisitDeclContext(D);
713 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000714 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000715}
716
717void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
718 const char *l;
719 if (D->getLanguage() == LinkageSpecDecl::lang_c)
720 l = "C";
721 else {
722 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
723 "unknown language in linkage specification");
724 l = "C++";
725 }
726
727 Out << "extern \"" << l << "\" ";
728 if (D->hasBraces()) {
729 Out << "{\n";
730 VisitDeclContext(D);
731 Indent() << "}";
732 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000733 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000734}
735
Richard Trieu5cb3d692011-07-28 00:19:05 +0000736void DeclPrinter::PrintTemplateParameters(
737 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
738 assert(Params);
739 assert(!Args || Params->size() == Args->size());
740
Anders Carlsson0487f662009-06-04 05:37:43 +0000741 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Anders Carlsson0487f662009-06-04 05:37:43 +0000743 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
744 if (i != 0)
745 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Anders Carlsson0487f662009-06-04 05:37:43 +0000747 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000748 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000749 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Anders Carlsson0487f662009-06-04 05:37:43 +0000751 if (TTP->wasDeclaredWithTypename())
752 Out << "typename ";
753 else
754 Out << "class ";
755
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000756 if (TTP->isParameterPack())
757 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000759 Out << TTP->getNameAsString();
Anders Carlsson0487f662009-06-04 05:37:43 +0000760
Richard Trieu5cb3d692011-07-28 00:19:05 +0000761 if (Args) {
762 Out << " = ";
763 Args->get(i).print(Policy, Out);
764 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000765 Out << " = ";
766 Out << TTP->getDefaultArgument().getAsString(Policy);
767 };
Mike Stump1eb44332009-09-09 15:08:12 +0000768 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000769 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
770 Out << NTTP->getType().getAsString(Policy);
771
Douglas Gregor56bc9832010-12-24 00:15:10 +0000772 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
773 Out << "...";
774
Anders Carlsson0487f662009-06-04 05:37:43 +0000775 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
776 Out << ' ';
777 Out << Name->getName();
778 }
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Richard Trieu5cb3d692011-07-28 00:19:05 +0000780 if (Args) {
781 Out << " = ";
782 Args->get(i).print(Policy, Out);
783 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000784 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000785 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000786 Indentation);
787 }
Richard Smith98a57862011-04-15 13:38:57 +0000788 } else if (const TemplateTemplateParmDecl *TTPD =
789 dyn_cast<TemplateTemplateParmDecl>(Param)) {
790 VisitTemplateDecl(TTPD);
791 // FIXME: print the default argument, if present.
Anders Carlsson0487f662009-06-04 05:37:43 +0000792 }
793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Anders Carlsson0487f662009-06-04 05:37:43 +0000795 Out << "> ";
Richard Trieu5cb3d692011-07-28 00:19:05 +0000796}
797
798void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
799 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson0487f662009-06-04 05:37:43 +0000800
Richard Smith98a57862011-04-15 13:38:57 +0000801 if (const TemplateTemplateParmDecl *TTP =
802 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000803 Out << "class ";
804 if (TTP->isParameterPack())
805 Out << "...";
806 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000807 } else {
808 Visit(D->getTemplatedDecl());
809 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000810}
811
Richard Trieu5cb3d692011-07-28 00:19:05 +0000812void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
813 if (PrintInstantiation) {
814 TemplateParameterList *Params = D->getTemplateParameters();
815 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
816 I != E; ++I) {
817 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
818 Visit(*I);
819 }
820 }
821
822 return VisitRedeclarableTemplateDecl(D);
823}
824
825void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
826 if (PrintInstantiation) {
827 TemplateParameterList *Params = D->getTemplateParameters();
828 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
829 I != E; ++I) {
830 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
831 Visit(*I);
832 Out << '\n';
833 }
834 }
835
836 return VisitRedeclarableTemplateDecl(D);
837}
838
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000839//----------------------------------------------------------------------------
840// Objective-C declarations
841//----------------------------------------------------------------------------
842
843void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000844 Out << "@class " << *D->getForwardInterfaceDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000845}
846
847void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
848 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000849 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000850 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000851 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000852 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000853 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000855 std::string name = OMD->getSelector().getAsString();
856 std::string::size_type pos, lastPos = 0;
857 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
858 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000859 // FIXME: selector is missing here!
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000860 pos = name.find_first_of(':', lastPos);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000861 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000862 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000863 lastPos = pos + 1;
864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000866 if (OMD->param_begin() == OMD->param_end())
867 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000869 if (OMD->isVariadic())
870 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000872 if (OMD->getBody()) {
873 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000874 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000875 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000876 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000877}
878
879void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
880 std::string I = OID->getNameAsString();
881 ObjCInterfaceDecl *SID = OID->getSuperClass();
882
883 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000884 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000885 else
886 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000887 Out << "\n";
888 VisitDeclContext(OID, false);
889 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000890}
891
892void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
893 std::string I = OID->getNameAsString();
894 ObjCInterfaceDecl *SID = OID->getSuperClass();
895
896 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000897 Out << "@interface " << I << " : " << *SID;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000898 else
899 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000901 // Protocols?
902 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
903 if (!Protocols.empty()) {
904 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
905 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000906 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000909 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000910 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000912 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000913 Out << "{\n";
914 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000915 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
916 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000917 Indent() << (*I)->getType().getAsString(Policy) << ' ' << **I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000918 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000919 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000920 Out << "}\n";
921 }
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000923 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000924 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000925 // FIXME: implement the rest...
926}
927
928void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
929 Out << "@protocol ";
Mike Stump1eb44332009-09-09 15:08:12 +0000930 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000931 E = D->protocol_end();
932 I != E; ++I) {
933 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000934 Out << **I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000935 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000936}
937
938void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000939 Out << "@protocol " << *PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000940 VisitDeclContext(PID, false);
941 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000942}
943
944void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000945 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000946
947 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000948 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000949 // FIXME: implement the rest...
950}
951
952void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000953 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000954 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000955 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000957 // FIXME: implement the rest...
958}
959
960void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000961 Out << "@compatibility_alias " << *AID
962 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000963}
964
965/// PrintObjCPropertyDecl - print a property declaration.
966///
967void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
968 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
969 Out << "@required\n";
970 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
971 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000973 Out << "@property";
974 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
975 bool first = true;
976 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000977 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000978 ObjCPropertyDecl::OBJC_PR_readonly) {
979 Out << (first ? ' ' : ',') << "readonly";
980 first = false;
Ted Kremenek07c682a2011-08-17 21:09:35 +0000981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenek07c682a2011-08-17 21:09:35 +0000983 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
984 Out << (first ? ' ' : ',') << "getter = "
985 << PDecl->getGetterName().getAsString();
986 first = false;
987 }
988 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
989 Out << (first ? ' ' : ',') << "setter = "
990 << PDecl->getSetterName().getAsString();
991 first = false;
992 }
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Ted Kremenek07c682a2011-08-17 21:09:35 +0000994 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
995 Out << (first ? ' ' : ',') << "assign";
996 first = false;
997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Ted Kremenek07c682a2011-08-17 21:09:35 +0000999 if (PDecl->getPropertyAttributes() &
1000 ObjCPropertyDecl::OBJC_PR_readwrite) {
1001 Out << (first ? ' ' : ',') << "readwrite";
1002 first = false;
1003 }
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Ted Kremenek07c682a2011-08-17 21:09:35 +00001005 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1006 Out << (first ? ' ' : ',') << "retain";
1007 first = false;
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Ted Kremenek07c682a2011-08-17 21:09:35 +00001010 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1011 Out << (first ? ' ' : ',') << "strong";
1012 first = false;
1013 }
John McCallf85e1932011-06-15 23:02:42 +00001014
Ted Kremenek07c682a2011-08-17 21:09:35 +00001015 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1016 Out << (first ? ' ' : ',') << "copy";
1017 first = false;
1018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremenek07c682a2011-08-17 21:09:35 +00001020 if (PDecl->getPropertyAttributes() &
1021 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1022 Out << (first ? ' ' : ',') << "nonatomic";
1023 first = false;
1024 }
1025 if (PDecl->getPropertyAttributes() &
1026 ObjCPropertyDecl::OBJC_PR_atomic) {
1027 Out << (first ? ' ' : ',') << "atomic";
1028 first = false;
1029 }
1030
1031 (void) first; // Silence dead store warning due to idiomatic code.
1032 Out << " )";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001033 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001034 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001035}
1036
1037void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1038 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +00001039 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001040 else
Douglas Gregor64f65002009-05-30 00:56:08 +00001041 Out << "@dynamic ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001042 Out << *PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001043 if (PID->getPropertyIvarDecl())
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001044 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001045}
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001046
1047void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1048 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001049 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001050 Out << *D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001051}
1052
John McCall7ba107a2009-11-18 02:36:19 +00001053void
1054DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1055 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +00001056 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001057 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +00001058}
1059
1060void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001061 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001062 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001063 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001064}
John McCall9488ea12009-11-17 05:59:44 +00001065
1066void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1067 // ignore
1068}