blob: 386ad66c9917af7bae122475455e7758ae0b17d2 [file] [log] [blame]
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl::dump method, which pretty print the
11// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclVisitor.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Douglas Gregor9db7dbb2010-01-31 09:12:51 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000021#include "clang/AST/PrettyPrinter.h"
Douglas Gregor15de72c2011-12-02 23:23:56 +000022#include "clang/Basic/Module.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000023#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25
26namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000027 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000028 raw_ostream &Out;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000029 PrintingPolicy Policy;
30 unsigned Indentation;
Richard Trieu5cb3d692011-07-28 00:19:05 +000031 bool PrintInstantiation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000032
Chris Lattner5f9e2722011-07-23 10:55:15 +000033 raw_ostream& Indent() { return Indent(Indentation); }
34 raw_ostream& Indent(unsigned Indentation);
35 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000036
Anders Carlsson0d592922009-08-28 22:39:52 +000037 void Print(AccessSpecifier AS);
Mike Stump1eb44332009-09-09 15:08:12 +000038
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000039 public:
Richard Smithd1420c62012-08-16 03:56:14 +000040 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
41 unsigned Indentation = 0, bool PrintInstantiation = false)
42 : Out(Out), Policy(Policy), Indentation(Indentation),
Richard Trieu5cb3d692011-07-28 00:19:05 +000043 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000044
45 void VisitDeclContext(DeclContext *DC, bool Indent = true);
46
47 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
48 void VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +000049 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000050 void VisitEnumDecl(EnumDecl *D);
51 void VisitRecordDecl(RecordDecl *D);
52 void VisitEnumConstantDecl(EnumConstantDecl *D);
53 void VisitFunctionDecl(FunctionDecl *D);
54 void VisitFieldDecl(FieldDecl *D);
55 void VisitVarDecl(VarDecl *D);
Chris Lattner57ad3782011-02-17 20:34:02 +000056 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000057 void VisitParmVarDecl(ParmVarDecl *D);
58 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor15de72c2011-12-02 23:23:56 +000059 void VisitImportDecl(ImportDecl *D);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +000060 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000061 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000062 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000063 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000064 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000065 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith98a57862011-04-15 13:38:57 +000066 void VisitTemplateDecl(const TemplateDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000067 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
68 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000069 void VisitObjCMethodDecl(ObjCMethodDecl *D);
70 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
71 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000072 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
73 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
74 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
75 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
76 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
77 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000078 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
79 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000080 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000081 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000082
83 void PrintTemplateParameters(const TemplateParameterList *Params,
84 const TemplateArgumentList *Args);
Douglas Gregor1bea8802011-11-19 19:22:57 +000085 void prettyPrintAttributes(Decl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000086 };
87}
88
Richard Trieu5cb3d692011-07-28 00:19:05 +000089void Decl::print(raw_ostream &Out, unsigned Indentation,
90 bool PrintInstantiation) const {
Douglas Gregor30c42402011-09-27 22:38:19 +000091 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000092}
93
Chris Lattner5f9e2722011-07-23 10:55:15 +000094void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +000095 unsigned Indentation, bool PrintInstantiation) const {
Richard Smithd1420c62012-08-16 03:56:14 +000096 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlssonf88df862009-09-26 21:58:53 +000097 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000098}
99
Eli Friedman42f42c02009-05-30 04:20:30 +0000100static QualType GetBaseType(QualType T) {
101 // FIXME: This should be on the Type class!
102 QualType BaseType = T;
103 while (!BaseType->isSpecifierType()) {
104 if (isa<TypedefType>(BaseType))
105 break;
Ted Kremenek6217b802009-07-29 21:53:49 +0000106 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000107 BaseType = PTy->getPointeeType();
Ted Kremenek41c1f212012-10-11 20:58:14 +0000108 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
109 BaseType = BPy->getPointeeType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000110 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
111 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +0000112 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000113 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000114 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000115 BaseType = VTy->getElementType();
Eli Friedman15631b42012-08-08 03:47:15 +0000116 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
117 BaseType = RTy->getPointeeType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000118 else
David Blaikieb219cfc2011-09-23 05:06:16 +0000119 llvm_unreachable("Unknown declarator!");
Eli Friedman42f42c02009-05-30 04:20:30 +0000120 }
121 return BaseType;
122}
123
124static QualType getDeclType(Decl* D) {
Richard Smith162e1c12011-04-15 14:24:37 +0000125 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman42f42c02009-05-30 04:20:30 +0000126 return TDD->getUnderlyingType();
127 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
128 return VD->getType();
129 return QualType();
130}
131
132void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000133 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000134 unsigned Indentation) {
135 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000136 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000137 return;
138 }
139
140 Decl** End = Begin + NumDecls;
141 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
142 if (TD)
143 ++Begin;
144
145 PrintingPolicy SubPolicy(Policy);
John McCall5e1cdac2011-10-07 06:10:15 +0000146 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000147 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000148 Out << " ";
149 SubPolicy.SuppressTag = true;
150 }
151
152 bool isFirst = true;
153 for ( ; Begin != End; ++Begin) {
154 if (isFirst) {
155 SubPolicy.SuppressSpecifiers = false;
156 isFirst = false;
157 } else {
158 if (!isFirst) Out << ", ";
159 SubPolicy.SuppressSpecifiers = true;
160 }
161
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000162 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000163 }
164}
165
Anders Carlsson84834432009-12-14 00:51:04 +0000166void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000167 // Get the translation unit
168 const DeclContext *DC = this;
169 while (!DC->isTranslationUnit())
170 DC = DC->getParent();
171
172 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smithd1420c62012-08-16 03:56:14 +0000173 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000174 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
175}
176
Benjamin Kramer42c72c22012-08-14 14:50:32 +0000177void Decl::dump() const {
178 dump(llvm::errs());
179}
180
Alexander Kornienkoe34a0522012-07-26 16:01:23 +0000181void Decl::dump(raw_ostream &Out) const {
182 PrintingPolicy Policy = getASTContext().getPrintingPolicy();
Richard Smithd1420c62012-08-16 03:56:14 +0000183 Policy.DumpSourceManager = &getASTContext().getSourceManager();
Alexander Kornienkoe34a0522012-07-26 16:01:23 +0000184 print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000185}
186
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar512ce602009-11-21 09:12:06 +0000188 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000189 Out << " ";
190 return Out;
191}
192
Douglas Gregor1bea8802011-11-19 19:22:57 +0000193void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian1bfb00d2012-10-17 21:58:03 +0000194 if (Policy.SuppressAttributes)
195 return;
196
Douglas Gregor1bea8802011-11-19 19:22:57 +0000197 if (D->hasAttrs()) {
198 AttrVec &Attrs = D->getAttrs();
199 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
Richard Smith0dae7292012-08-16 02:43:29 +0000200 Attr *A = *i;
201 A->printPretty(Out, Policy);
Douglas Gregor1bea8802011-11-19 19:22:57 +0000202 }
203 }
204}
205
Chris Lattner5f9e2722011-07-23 10:55:15 +0000206void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000207 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000208 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000209 Out << ";\n";
210 Decls.clear();
211
212}
213
Anders Carlsson0d592922009-08-28 22:39:52 +0000214void DeclPrinter::Print(AccessSpecifier AS) {
215 switch(AS) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000216 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson0d592922009-08-28 22:39:52 +0000217 case AS_public: Out << "public"; break;
218 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000219 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000220 }
221}
222
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000223//----------------------------------------------------------------------------
224// Common C declarations
225//----------------------------------------------------------------------------
226
227void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkod1fc82e2012-08-21 17:36:32 +0000228 if (Policy.TerseOutput)
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000229 return;
230
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000231 if (Indent)
232 Indentation += Policy.Indentation;
233
Chris Lattner5f9e2722011-07-23 10:55:15 +0000234 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000235 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000236 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-07-30 00:47:46 +0000237
238 // Don't print ObjCIvarDecls, as they are printed when visiting the
239 // containing ObjCInterfaceDecl.
240 if (isa<ObjCIvarDecl>(*D))
241 continue;
242
Richard Smithd1420c62012-08-16 03:56:14 +0000243 if (!Policy.DumpSourceManager) {
Eli Friedman48d14a22009-05-30 05:03:24 +0000244 // Skip over implicit declarations in pretty-printing mode.
245 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000246 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidis2574f6f2010-06-17 10:52:11 +0000247 // of __builtin_va_list or __[u]int128_t. There should be some other way
248 // to check that.
249 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
250 if (IdentifierInfo *II = ND->getIdentifier()) {
251 if (II->isStr("__builtin_va_list") ||
252 II->isStr("__int128_t") || II->isStr("__uint128_t"))
253 continue;
254 }
255 }
Eli Friedman48d14a22009-05-30 05:03:24 +0000256 }
257
Eli Friedman42f42c02009-05-30 04:20:30 +0000258 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
259 // forced to merge the declarations because there's no other way to
260 // refer to the struct in question. This limited merging is safe without
261 // a bunch of other checks because it only merges declarations directly
262 // referring to the tag, not typedefs.
263 //
264 // Check whether the current declaration should be grouped with a previous
265 // unnamed struct.
266 QualType CurDeclType = getDeclType(*D);
267 if (!Decls.empty() && !CurDeclType.isNull()) {
268 QualType BaseType = GetBaseType(CurDeclType);
269 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
270 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
271 Decls.push_back(*D);
272 continue;
273 }
274 }
275
276 // If we have a merged group waiting to be handled, handle it now.
277 if (!Decls.empty())
278 ProcessDeclGroup(Decls);
279
280 // If the current declaration is an unnamed tag type, save it
281 // so we can merge it with the subsequent declaration(s) using it.
282 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
283 Decls.push_back(*D);
284 continue;
285 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000286
287 if (isa<AccessSpecDecl>(*D)) {
288 Indentation -= Policy.Indentation;
289 this->Indent();
290 Print(D->getAccess());
291 Out << ":\n";
292 Indentation += Policy.Indentation;
293 continue;
294 }
295
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000296 this->Indent();
297 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000298
299 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000300 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000301 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000302 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
303 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000304 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
305 Terminator = 0;
306 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000307 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000308 isa<ObjCInterfaceDecl>(*D) ||
309 isa<ObjCProtocolDecl>(*D) ||
310 isa<ObjCCategoryImplDecl>(*D) ||
311 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000312 Terminator = 0;
313 else if (isa<EnumConstantDecl>(*D)) {
314 DeclContext::decl_iterator Next = D;
315 ++Next;
316 if (Next != DEnd)
317 Terminator = ",";
318 } else
319 Terminator = ";";
320
321 if (Terminator)
322 Out << Terminator;
323 Out << "\n";
324 }
325
Eli Friedman42f42c02009-05-30 04:20:30 +0000326 if (!Decls.empty())
327 ProcessDeclGroup(Decls);
328
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000329 if (Indent)
330 Indentation -= Policy.Indentation;
331}
332
333void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
334 VisitDeclContext(D, false);
335}
336
337void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000338 if (!Policy.SuppressSpecifiers) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000339 Out << "typedef ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000340
341 if (D->isModulePrivate())
342 Out << "__module_private__ ";
343 }
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000344 D->getUnderlyingType().print(Out, Policy, D->getName());
Douglas Gregor1bea8802011-11-19 19:22:57 +0000345 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000346}
347
Richard Smith162e1c12011-04-15 14:24:37 +0000348void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000349 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy);
Richard Smith162e1c12011-04-15 14:24:37 +0000350}
351
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000352void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000353 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
354 Out << "__module_private__ ";
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000355 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000356 if (D->isScoped()) {
357 if (D->isScopedUsingClassTag())
358 Out << "class ";
359 else
360 Out << "struct ";
361 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000362 Out << *D;
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000363
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000364 if (D->isFixed())
365 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000366
John McCall5e1cdac2011-10-07 06:10:15 +0000367 if (D->isCompleteDefinition()) {
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000368 Out << " {\n";
369 VisitDeclContext(D);
370 Indent() << "}";
371 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000372 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000373}
374
375void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000376 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
377 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000378 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000379 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000380 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000381
John McCall5e1cdac2011-10-07 06:10:15 +0000382 if (D->isCompleteDefinition()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000383 Out << " {\n";
384 VisitDeclContext(D);
385 Indent() << "}";
386 }
387}
388
389void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000390 Out << *D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000391 if (Expr *Init = D->getInitExpr()) {
392 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000393 Init->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000394 }
395}
396
Mike Stump1eb44332009-09-09 15:08:12 +0000397void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000398 if (!Policy.SuppressSpecifiers) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000399 switch (D->getStorageClassAsWritten()) {
John McCalld931b082010-08-26 03:08:43 +0000400 case SC_None: break;
401 case SC_Extern: Out << "extern "; break;
402 case SC_Static: Out << "static "; break;
403 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000404 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
405 llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000406 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000407
Douglas Gregor8d267c52011-09-09 02:06:17 +0000408 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000409 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000410 if (D->isModulePrivate()) Out << "__module_private__ ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000411 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000412
Douglas Gregor6620a622009-05-30 05:39:39 +0000413 PrintingPolicy SubPolicy(Policy);
414 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000415 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000416
Abramo Bagnara723df242010-12-14 22:11:44 +0000417 QualType Ty = D->getType();
John McCallf4c73712011-01-19 06:33:43 +0000418 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000419 Proto = '(' + Proto + ')';
420 Ty = PT->getInnerType();
421 }
422
423 if (isa<FunctionType>(Ty)) {
424 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000425 const FunctionProtoType *FT = 0;
426 if (D->hasWrittenPrototype())
427 FT = dyn_cast<FunctionProtoType>(AFT);
428
429 Proto += "(";
430 if (FT) {
431 llvm::raw_string_ostream POut(Proto);
Richard Smithd1420c62012-08-16 03:56:14 +0000432 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000433 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
434 if (i) POut << ", ";
435 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000438 if (FT->isVariadic()) {
439 if (D->getNumParams()) POut << ", ";
440 POut << "...";
441 }
Sean Hunt10620eb2011-05-06 20:44:56 +0000442 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000443 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
444 if (i)
445 Proto += ", ";
446 Proto += D->getParamDecl(i)->getNameAsString();
447 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000448 }
449
450 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000451
David Blaikie4ef832f2012-08-10 00:55:35 +0000452 if (FT) {
453 if (FT->isConst())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000454 Proto += " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000455 if (FT->isVolatile())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000456 Proto += " volatile";
David Blaikie4ef832f2012-08-10 00:55:35 +0000457 if (FT->isRestrict())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000458 Proto += " restrict";
459 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000460
461 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000462 Proto += " throw(";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000463 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000464 Proto += "...";
465 else
466 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
467 if (I)
468 Proto += ", ";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000469
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000470 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000471 }
472 Proto += ")";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000473 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
474 Proto += " noexcept";
475 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
476 Proto += "(";
477 llvm::raw_string_ostream EOut(Proto);
Richard Smithd1420c62012-08-16 03:56:14 +0000478 FT->getNoexceptExpr()->printPretty(EOut, 0, SubPolicy,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000479 Indentation);
480 EOut.flush();
481 Proto += EOut.str();
482 Proto += ")";
483 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000484 }
485
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000486 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith7a614d82011-06-11 17:19:42 +0000487 bool HasInitializerList = false;
488 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
489 E = CDecl->init_end();
490 B != E; ++B) {
491 CXXCtorInitializer * BMInitializer = (*B);
492 if (BMInitializer->isInClassMemberInitializer())
493 continue;
494
495 if (!HasInitializerList) {
496 Proto += " : ";
497 Out << Proto;
498 Proto.clear();
499 HasInitializerList = true;
500 } else
501 Out << ", ";
502
503 if (BMInitializer->isAnyMemberInitializer()) {
504 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000505 Out << *FD;
Richard Smith7a614d82011-06-11 17:19:42 +0000506 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000507 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith7a614d82011-06-11 17:19:42 +0000508 }
509
510 Out << "(";
511 if (!BMInitializer->getInit()) {
512 // Nothing to print
513 } else {
514 Expr *Init = BMInitializer->getInit();
515 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
516 Init = Tmp->getSubExpr();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000517
Richard Smith7a614d82011-06-11 17:19:42 +0000518 Init = Init->IgnoreParens();
519
520 Expr *SimpleInit = 0;
521 Expr **Args = 0;
522 unsigned NumArgs = 0;
523 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
524 Args = ParenList->getExprs();
525 NumArgs = ParenList->getNumExprs();
526 } else if (CXXConstructExpr *Construct
527 = dyn_cast<CXXConstructExpr>(Init)) {
528 Args = Construct->getArgs();
529 NumArgs = Construct->getNumArgs();
530 } else
531 SimpleInit = Init;
532
533 if (SimpleInit)
Richard Smithd1420c62012-08-16 03:56:14 +0000534 SimpleInit->printPretty(Out, 0, Policy, Indentation);
Richard Smith7a614d82011-06-11 17:19:42 +0000535 else {
536 for (unsigned I = 0; I != NumArgs; ++I) {
537 if (isa<CXXDefaultArgExpr>(Args[I]))
538 break;
539
540 if (I)
541 Out << ", ";
Richard Smithd1420c62012-08-16 03:56:14 +0000542 Args[I]->printPretty(Out, 0, Policy, Indentation);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000543 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000544 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000545 }
Richard Smith7a614d82011-06-11 17:19:42 +0000546 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000547 }
548 }
549 else
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000550 AFT->getResultType().print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000551 } else {
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000552 Ty.print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000553 }
554
Douglas Gregor1bea8802011-11-19 19:22:57 +0000555 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000556
557 if (D->isPure())
558 Out << " = 0";
Sean Hunt10620eb2011-05-06 20:44:56 +0000559 else if (D->isDeletedAsWritten())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000560 Out << " = delete";
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000561 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000562 if (!D->hasPrototype() && D->getNumParams()) {
563 // This is a K&R function definition, so we need to print the
564 // parameters.
565 Out << '\n';
Richard Smithd1420c62012-08-16 03:56:14 +0000566 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000567 Indentation += Policy.Indentation;
568 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
569 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000570 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000571 Out << ";\n";
572 }
573 Indentation -= Policy.Indentation;
574 } else
575 Out << ' ';
576
Richard Smithd1420c62012-08-16 03:56:14 +0000577 D->getBody()->printPretty(Out, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000578 Out << '\n';
579 }
580}
581
582void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000583 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000584 Out << "mutable ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000585 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
586 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000587
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000588 Out << D->getType().stream(Policy, D->getName());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000589
590 if (D->isBitField()) {
591 Out << " : ";
Richard Smithd1420c62012-08-16 03:56:14 +0000592 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000593 }
Richard Smith7a614d82011-06-11 17:19:42 +0000594
595 Expr *Init = D->getInClassInitializer();
596 if (!Policy.SuppressInitializers && Init) {
Richard Smithca523302012-06-10 03:12:00 +0000597 if (D->getInClassInitStyle() == ICIS_ListInit)
598 Out << " ";
599 else
600 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000601 Init->printPretty(Out, 0, Policy, Indentation);
Richard Smith7a614d82011-06-11 17:19:42 +0000602 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000603 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000604}
605
Chris Lattner57ad3782011-02-17 20:34:02 +0000606void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000607 Out << *D << ":";
Chris Lattner57ad3782011-02-17 20:34:02 +0000608}
609
610
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000611void DeclPrinter::VisitVarDecl(VarDecl *D) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000612 StorageClass SCAsWritten = D->getStorageClassAsWritten();
613 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None)
614 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000615
Eli Friedman42f42c02009-05-30 04:20:30 +0000616 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000617 Out << "__thread ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000618 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
619 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000620
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000621 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000622 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000623 T = Parm->getOriginalType();
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000624 T.print(Out, Policy, D->getName());
Richard Smithad762fc2011-04-14 22:09:26 +0000625 Expr *Init = D->getInit();
626 if (!Policy.SuppressInitializers && Init) {
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000627 bool ImplicitInit = false;
628 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
629 ImplicitInit = D->getInitStyle() == VarDecl::CallInit &&
630 Construct->getNumArgs() == 0 && !Construct->isListInitialization();
631 if (!ImplicitInit) {
Eli Friedmane1aebe12012-10-19 20:36:44 +0000632 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000633 Out << "(";
634 else if (D->getInitStyle() == VarDecl::CInit) {
635 Out << " = ";
636 }
Richard Smithd1420c62012-08-16 03:56:14 +0000637 Init->printPretty(Out, 0, Policy, Indentation);
Eli Friedmane1aebe12012-10-19 20:36:44 +0000638 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000639 Out << ")";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000640 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000641 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000642 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000643}
644
645void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
646 VisitVarDecl(D);
647}
648
649void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
650 Out << "__asm (";
Richard Smithd1420c62012-08-16 03:56:14 +0000651 D->getAsmString()->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000652 Out << ")";
653}
654
Douglas Gregor15de72c2011-12-02 23:23:56 +0000655void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Ted Kremenek32ad2ee2012-03-01 22:07:04 +0000656 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName()
Douglas Gregor15de72c2011-12-02 23:23:56 +0000657 << ";\n";
658}
659
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000660void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
661 Out << "static_assert(";
Richard Smithd1420c62012-08-16 03:56:14 +0000662 D->getAssertExpr()->printPretty(Out, 0, Policy, Indentation);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000663 Out << ", ";
Richard Smithd1420c62012-08-16 03:56:14 +0000664 D->getMessage()->printPretty(Out, 0, Policy, Indentation);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000665 Out << ")";
666}
667
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000668//----------------------------------------------------------------------------
669// C++ declarations
670//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000671void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorc30636a2012-05-01 01:43:38 +0000672 if (D->isInline())
673 Out << "inline ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000674 Out << "namespace " << *D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000675 VisitDeclContext(D);
676 Indent() << "}";
677}
678
Douglas Gregor8419fa32009-05-30 06:31:56 +0000679void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
680 Out << "using namespace ";
681 if (D->getQualifier())
682 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000683 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000684}
685
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000686void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000687 Out << "namespace " << *D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000688 if (D->getQualifier())
689 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000690 Out << *D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000691}
692
Douglas Gregor59e63572009-05-30 06:58:37 +0000693void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000694 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
695 Out << "__module_private__ ";
Douglas Gregor59e63572009-05-30 06:58:37 +0000696 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000697 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000698 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000699
John McCall5e1cdac2011-10-07 06:10:15 +0000700 if (D->isCompleteDefinition()) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000701 // Print the base classes
702 if (D->getNumBases()) {
703 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000704 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
705 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000706 if (Base != D->bases_begin())
707 Out << ", ";
708
709 if (Base->isVirtual())
710 Out << "virtual ";
711
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000712 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
713 if (AS != AS_none)
714 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000715 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor34a99e72011-04-27 17:07:55 +0000716
717 if (Base->isPackExpansion())
718 Out << "...";
Douglas Gregor59e63572009-05-30 06:58:37 +0000719 }
720 }
721
722 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000723 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000724 Out << " {\n";
725 VisitDeclContext(D);
726 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000727 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000728}
729
730void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
731 const char *l;
732 if (D->getLanguage() == LinkageSpecDecl::lang_c)
733 l = "C";
734 else {
735 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
736 "unknown language in linkage specification");
737 l = "C++";
738 }
739
740 Out << "extern \"" << l << "\" ";
741 if (D->hasBraces()) {
742 Out << "{\n";
743 VisitDeclContext(D);
744 Indent() << "}";
745 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000746 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000747}
748
Richard Trieu5cb3d692011-07-28 00:19:05 +0000749void DeclPrinter::PrintTemplateParameters(
750 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
751 assert(Params);
752 assert(!Args || Params->size() == Args->size());
753
Anders Carlsson0487f662009-06-04 05:37:43 +0000754 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Anders Carlsson0487f662009-06-04 05:37:43 +0000756 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
757 if (i != 0)
758 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Anders Carlsson0487f662009-06-04 05:37:43 +0000760 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000761 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000762 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Anders Carlsson0487f662009-06-04 05:37:43 +0000764 if (TTP->wasDeclaredWithTypename())
765 Out << "typename ";
766 else
767 Out << "class ";
768
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000769 if (TTP->isParameterPack())
770 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000772 Out << *TTP;
Anders Carlsson0487f662009-06-04 05:37:43 +0000773
Richard Trieu5cb3d692011-07-28 00:19:05 +0000774 if (Args) {
775 Out << " = ";
776 Args->get(i).print(Policy, Out);
777 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000778 Out << " = ";
779 Out << TTP->getDefaultArgument().getAsString(Policy);
780 };
Mike Stump1eb44332009-09-09 15:08:12 +0000781 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000782 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
783 Out << NTTP->getType().getAsString(Policy);
784
Douglas Gregor56bc9832010-12-24 00:15:10 +0000785 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
786 Out << "...";
787
Anders Carlsson0487f662009-06-04 05:37:43 +0000788 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
789 Out << ' ';
790 Out << Name->getName();
791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Richard Trieu5cb3d692011-07-28 00:19:05 +0000793 if (Args) {
794 Out << " = ";
795 Args->get(i).print(Policy, Out);
796 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000797 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000798 NTTP->getDefaultArgument()->printPretty(Out, 0, Policy, Indentation);
Anders Carlsson0487f662009-06-04 05:37:43 +0000799 }
Richard Smith98a57862011-04-15 13:38:57 +0000800 } else if (const TemplateTemplateParmDecl *TTPD =
801 dyn_cast<TemplateTemplateParmDecl>(Param)) {
802 VisitTemplateDecl(TTPD);
803 // FIXME: print the default argument, if present.
Anders Carlsson0487f662009-06-04 05:37:43 +0000804 }
805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Anders Carlsson0487f662009-06-04 05:37:43 +0000807 Out << "> ";
Richard Trieu5cb3d692011-07-28 00:19:05 +0000808}
809
810void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
811 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson0487f662009-06-04 05:37:43 +0000812
Richard Smith98a57862011-04-15 13:38:57 +0000813 if (const TemplateTemplateParmDecl *TTP =
814 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000815 Out << "class ";
816 if (TTP->isParameterPack())
817 Out << "...";
818 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000819 } else {
820 Visit(D->getTemplatedDecl());
821 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000822}
823
Richard Trieu5cb3d692011-07-28 00:19:05 +0000824void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
825 if (PrintInstantiation) {
826 TemplateParameterList *Params = D->getTemplateParameters();
827 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
828 I != E; ++I) {
829 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
830 Visit(*I);
831 }
832 }
833
834 return VisitRedeclarableTemplateDecl(D);
835}
836
837void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
838 if (PrintInstantiation) {
839 TemplateParameterList *Params = D->getTemplateParameters();
840 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
841 I != E; ++I) {
842 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
843 Visit(*I);
844 Out << '\n';
845 }
846 }
847
848 return VisitRedeclarableTemplateDecl(D);
849}
850
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000851//----------------------------------------------------------------------------
852// Objective-C declarations
853//----------------------------------------------------------------------------
854
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000855void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
856 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000857 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000858 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000859 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000860 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000861 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000863 std::string name = OMD->getSelector().getAsString();
864 std::string::size_type pos, lastPos = 0;
865 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
866 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000867 // FIXME: selector is missing here!
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000868 pos = name.find_first_of(':', lastPos);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000869 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000870 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000871 lastPos = pos + 1;
872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000874 if (OMD->param_begin() == OMD->param_end())
875 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000877 if (OMD->isVariadic())
878 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Fariborz Jahanian1bfb00d2012-10-17 21:58:03 +0000880 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000881 Out << ' ';
Richard Smithd1420c62012-08-16 03:56:14 +0000882 OMD->getBody()->printPretty(Out, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000883 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000884 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000885}
886
887void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
888 std::string I = OID->getNameAsString();
889 ObjCInterfaceDecl *SID = OID->getSuperClass();
890
891 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000892 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000893 else
894 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000895 Out << "\n";
896 VisitDeclContext(OID, false);
897 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000898}
899
900void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
901 std::string I = OID->getNameAsString();
902 ObjCInterfaceDecl *SID = OID->getSuperClass();
903
Douglas Gregor7723fec2011-12-15 20:29:51 +0000904 if (!OID->isThisDeclarationADefinition()) {
905 Out << "@class " << I << ";";
906 return;
907 }
908
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000909 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000910 Out << "@interface " << I << " : " << *SID;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000911 else
912 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000914 // Protocols?
915 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
916 if (!Protocols.empty()) {
917 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
918 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000919 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000922 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000923 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000925 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000926 Out << "{\n";
927 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000928 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
929 E = OID->ivar_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +0000930 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000931 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000932 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000933 Out << "}\n";
934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000936 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000937 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000938 // FIXME: implement the rest...
939}
940
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000941void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000942 if (!PID->isThisDeclarationADefinition()) {
943 Out << "@protocol " << PID->getIdentifier() << ";\n";
944 return;
945 }
946
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000947 Out << "@protocol " << *PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000948 VisitDeclContext(PID, false);
949 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000950}
951
952void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000953 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000954
955 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000956 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000957 // FIXME: implement the rest...
958}
959
960void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000961 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000962 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000963 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000965 // FIXME: implement the rest...
966}
967
968void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000969 Out << "@compatibility_alias " << *AID
970 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000971}
972
973/// PrintObjCPropertyDecl - print a property declaration.
974///
975void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
976 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
977 Out << "@required\n";
978 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
979 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000981 Out << "@property";
982 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
983 bool first = true;
984 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000985 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000986 ObjCPropertyDecl::OBJC_PR_readonly) {
987 Out << (first ? ' ' : ',') << "readonly";
988 first = false;
Ted Kremenek07c682a2011-08-17 21:09:35 +0000989 }
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Ted Kremenek07c682a2011-08-17 21:09:35 +0000991 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
992 Out << (first ? ' ' : ',') << "getter = "
993 << PDecl->getGetterName().getAsString();
994 first = false;
995 }
996 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
997 Out << (first ? ' ' : ',') << "setter = "
998 << PDecl->getSetterName().getAsString();
999 first = false;
1000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremenek07c682a2011-08-17 21:09:35 +00001002 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1003 Out << (first ? ' ' : ',') << "assign";
1004 first = false;
1005 }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Ted Kremenek07c682a2011-08-17 21:09:35 +00001007 if (PDecl->getPropertyAttributes() &
1008 ObjCPropertyDecl::OBJC_PR_readwrite) {
1009 Out << (first ? ' ' : ',') << "readwrite";
1010 first = false;
1011 }
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Ted Kremenek07c682a2011-08-17 21:09:35 +00001013 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1014 Out << (first ? ' ' : ',') << "retain";
1015 first = false;
1016 }
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Ted Kremenek07c682a2011-08-17 21:09:35 +00001018 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1019 Out << (first ? ' ' : ',') << "strong";
1020 first = false;
1021 }
John McCallf85e1932011-06-15 23:02:42 +00001022
Ted Kremenek07c682a2011-08-17 21:09:35 +00001023 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1024 Out << (first ? ' ' : ',') << "copy";
1025 first = false;
1026 }
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Ted Kremenek07c682a2011-08-17 21:09:35 +00001028 if (PDecl->getPropertyAttributes() &
1029 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1030 Out << (first ? ' ' : ',') << "nonatomic";
1031 first = false;
1032 }
1033 if (PDecl->getPropertyAttributes() &
1034 ObjCPropertyDecl::OBJC_PR_atomic) {
1035 Out << (first ? ' ' : ',') << "atomic";
1036 first = false;
1037 }
1038
1039 (void) first; // Silence dead store warning due to idiomatic code.
1040 Out << " )";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001041 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001042 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001043}
1044
1045void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1046 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +00001047 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001048 else
Douglas Gregor64f65002009-05-30 00:56:08 +00001049 Out << "@dynamic ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001050 Out << *PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001051 if (PID->getPropertyIvarDecl())
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001052 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001053}
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001054
1055void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1056 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001057 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001058 Out << *D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001059}
1060
John McCall7ba107a2009-11-18 02:36:19 +00001061void
1062DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1063 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +00001064 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001065 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +00001066}
1067
1068void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001069 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001070 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001071 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001072}
John McCall9488ea12009-11-17 05:59:44 +00001073
1074void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1075 // ignore
1076}