blob: 83f38badd1455ec42cf19a61ae68be1b9865ebbe [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"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000020#include "clang/AST/Expr.h"
Douglas Gregor9db7dbb2010-01-31 09:12:51 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000022#include "clang/AST/PrettyPrinter.h"
Douglas Gregor15de72c2011-12-02 23:23:56 +000023#include "clang/Basic/Module.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000024#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26
27namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000028 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000029 raw_ostream &Out;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000030 PrintingPolicy Policy;
31 unsigned Indentation;
Richard Trieu5cb3d692011-07-28 00:19:05 +000032 bool PrintInstantiation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000033
Chris Lattner5f9e2722011-07-23 10:55:15 +000034 raw_ostream& Indent() { return Indent(Indentation); }
35 raw_ostream& Indent(unsigned Indentation);
36 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000037
Anders Carlsson0d592922009-08-28 22:39:52 +000038 void Print(AccessSpecifier AS);
Mike Stump1eb44332009-09-09 15:08:12 +000039
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000040 public:
Richard Smithd1420c62012-08-16 03:56:14 +000041 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
42 unsigned Indentation = 0, bool PrintInstantiation = false)
43 : Out(Out), Policy(Policy), Indentation(Indentation),
Richard Trieu5cb3d692011-07-28 00:19:05 +000044 PrintInstantiation(PrintInstantiation) { }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000045
46 void VisitDeclContext(DeclContext *DC, bool Indent = true);
47
48 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
49 void VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +000050 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000051 void VisitEnumDecl(EnumDecl *D);
52 void VisitRecordDecl(RecordDecl *D);
53 void VisitEnumConstantDecl(EnumConstantDecl *D);
54 void VisitFunctionDecl(FunctionDecl *D);
55 void VisitFieldDecl(FieldDecl *D);
56 void VisitVarDecl(VarDecl *D);
Chris Lattner57ad3782011-02-17 20:34:02 +000057 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000058 void VisitParmVarDecl(ParmVarDecl *D);
59 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor15de72c2011-12-02 23:23:56 +000060 void VisitImportDecl(ImportDecl *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);
71 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
72 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000073 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
74 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
75 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
76 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
77 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
78 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000079 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
80 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000081 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000082 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Trieu5cb3d692011-07-28 00:19:05 +000083
84 void PrintTemplateParameters(const TemplateParameterList *Params,
85 const TemplateArgumentList *Args);
Douglas Gregor1bea8802011-11-19 19:22:57 +000086 void prettyPrintAttributes(Decl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000087 };
88}
89
Richard Trieu5cb3d692011-07-28 00:19:05 +000090void Decl::print(raw_ostream &Out, unsigned Indentation,
91 bool PrintInstantiation) const {
Douglas Gregor30c42402011-09-27 22:38:19 +000092 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000093}
94
Chris Lattner5f9e2722011-07-23 10:55:15 +000095void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +000096 unsigned Indentation, bool PrintInstantiation) const {
Richard Smithd1420c62012-08-16 03:56:14 +000097 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
Anders Carlssonf88df862009-09-26 21:58:53 +000098 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000099}
100
Eli Friedman42f42c02009-05-30 04:20:30 +0000101static QualType GetBaseType(QualType T) {
102 // FIXME: This should be on the Type class!
103 QualType BaseType = T;
104 while (!BaseType->isSpecifierType()) {
105 if (isa<TypedefType>(BaseType))
106 break;
Ted Kremenek6217b802009-07-29 21:53:49 +0000107 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000108 BaseType = PTy->getPointeeType();
Ted Kremenek41c1f212012-10-11 20:58:14 +0000109 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
110 BaseType = BPy->getPointeeType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000111 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
112 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +0000113 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000114 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000115 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000116 BaseType = VTy->getElementType();
Eli Friedman15631b42012-08-08 03:47:15 +0000117 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
118 BaseType = RTy->getPointeeType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000119 else
David Blaikieb219cfc2011-09-23 05:06:16 +0000120 llvm_unreachable("Unknown declarator!");
Eli Friedman42f42c02009-05-30 04:20:30 +0000121 }
122 return BaseType;
123}
124
125static QualType getDeclType(Decl* D) {
Richard Smith162e1c12011-04-15 14:24:37 +0000126 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman42f42c02009-05-30 04:20:30 +0000127 return TDD->getUnderlyingType();
128 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
129 return VD->getType();
130 return QualType();
131}
132
133void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000135 unsigned Indentation) {
136 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000137 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000138 return;
139 }
140
141 Decl** End = Begin + NumDecls;
142 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
143 if (TD)
144 ++Begin;
145
146 PrintingPolicy SubPolicy(Policy);
John McCall5e1cdac2011-10-07 06:10:15 +0000147 if (TD && TD->isCompleteDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000148 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000149 Out << " ";
150 SubPolicy.SuppressTag = true;
151 }
152
153 bool isFirst = true;
154 for ( ; Begin != End; ++Begin) {
155 if (isFirst) {
156 SubPolicy.SuppressSpecifiers = false;
157 isFirst = false;
158 } else {
159 if (!isFirst) Out << ", ";
160 SubPolicy.SuppressSpecifiers = true;
161 }
162
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000163 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000164 }
165}
166
Anders Carlsson84834432009-12-14 00:51:04 +0000167void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000168 // Get the translation unit
169 const DeclContext *DC = this;
170 while (!DC->isTranslationUnit())
171 DC = DC->getParent();
172
173 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smithd1420c62012-08-16 03:56:14 +0000174 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000175 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
176}
177
Benjamin Kramer42c72c22012-08-14 14:50:32 +0000178void Decl::dump() const {
179 dump(llvm::errs());
180}
181
Alexander Kornienkoe34a0522012-07-26 16:01:23 +0000182void Decl::dump(raw_ostream &Out) const {
183 PrintingPolicy Policy = getASTContext().getPrintingPolicy();
Richard Smithd1420c62012-08-16 03:56:14 +0000184 Policy.DumpSourceManager = &getASTContext().getSourceManager();
Alexander Kornienkoe34a0522012-07-26 16:01:23 +0000185 print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000186}
187
Chris Lattner5f9e2722011-07-23 10:55:15 +0000188raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
Daniel Dunbar512ce602009-11-21 09:12:06 +0000189 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000190 Out << " ";
191 return Out;
192}
193
Douglas Gregor1bea8802011-11-19 19:22:57 +0000194void DeclPrinter::prettyPrintAttributes(Decl *D) {
Fariborz Jahanian1bfb00d2012-10-17 21:58:03 +0000195 if (Policy.SuppressAttributes)
196 return;
197
Douglas Gregor1bea8802011-11-19 19:22:57 +0000198 if (D->hasAttrs()) {
199 AttrVec &Attrs = D->getAttrs();
200 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
Richard Smith0dae7292012-08-16 02:43:29 +0000201 Attr *A = *i;
202 A->printPretty(Out, Policy);
Douglas Gregor1bea8802011-11-19 19:22:57 +0000203 }
204 }
205}
206
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000208 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000209 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000210 Out << ";\n";
211 Decls.clear();
212
213}
214
Anders Carlsson0d592922009-08-28 22:39:52 +0000215void DeclPrinter::Print(AccessSpecifier AS) {
216 switch(AS) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000217 case AS_none: llvm_unreachable("No access specifier!");
Anders Carlsson0d592922009-08-28 22:39:52 +0000218 case AS_public: Out << "public"; break;
219 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000220 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000221 }
222}
223
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000224//----------------------------------------------------------------------------
225// Common C declarations
226//----------------------------------------------------------------------------
227
228void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
Dmitri Gribenkod1fc82e2012-08-21 17:36:32 +0000229 if (Policy.TerseOutput)
Dmitri Gribenko49795ae2012-08-20 23:39:06 +0000230 return;
231
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000232 if (Indent)
233 Indentation += Policy.Indentation;
234
Chris Lattner5f9e2722011-07-23 10:55:15 +0000235 SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000236 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000237 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-07-30 00:47:46 +0000238
239 // Don't print ObjCIvarDecls, as they are printed when visiting the
240 // containing ObjCInterfaceDecl.
241 if (isa<ObjCIvarDecl>(*D))
242 continue;
243
Richard Smithd1420c62012-08-16 03:56:14 +0000244 if (!Policy.DumpSourceManager) {
Eli Friedman48d14a22009-05-30 05:03:24 +0000245 // Skip over implicit declarations in pretty-printing mode.
246 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000247 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidis2574f6f2010-06-17 10:52:11 +0000248 // of __builtin_va_list or __[u]int128_t. There should be some other way
249 // to check that.
250 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
251 if (IdentifierInfo *II = ND->getIdentifier()) {
252 if (II->isStr("__builtin_va_list") ||
253 II->isStr("__int128_t") || II->isStr("__uint128_t"))
254 continue;
255 }
256 }
Eli Friedman48d14a22009-05-30 05:03:24 +0000257 }
258
Eli Friedman42f42c02009-05-30 04:20:30 +0000259 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
260 // forced to merge the declarations because there's no other way to
261 // refer to the struct in question. This limited merging is safe without
262 // a bunch of other checks because it only merges declarations directly
263 // referring to the tag, not typedefs.
264 //
265 // Check whether the current declaration should be grouped with a previous
266 // unnamed struct.
267 QualType CurDeclType = getDeclType(*D);
268 if (!Decls.empty() && !CurDeclType.isNull()) {
269 QualType BaseType = GetBaseType(CurDeclType);
270 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
271 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
272 Decls.push_back(*D);
273 continue;
274 }
275 }
276
277 // If we have a merged group waiting to be handled, handle it now.
278 if (!Decls.empty())
279 ProcessDeclGroup(Decls);
280
281 // If the current declaration is an unnamed tag type, save it
282 // so we can merge it with the subsequent declaration(s) using it.
283 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
284 Decls.push_back(*D);
285 continue;
286 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000287
288 if (isa<AccessSpecDecl>(*D)) {
289 Indentation -= Policy.Indentation;
290 this->Indent();
291 Print(D->getAccess());
292 Out << ":\n";
293 Indentation += Policy.Indentation;
294 continue;
295 }
296
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000297 this->Indent();
298 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
300 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000301 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000302 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000303 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
304 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000305 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
306 Terminator = 0;
307 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000308 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000309 isa<ObjCInterfaceDecl>(*D) ||
310 isa<ObjCProtocolDecl>(*D) ||
311 isa<ObjCCategoryImplDecl>(*D) ||
312 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000313 Terminator = 0;
314 else if (isa<EnumConstantDecl>(*D)) {
315 DeclContext::decl_iterator Next = D;
316 ++Next;
317 if (Next != DEnd)
318 Terminator = ",";
319 } else
320 Terminator = ";";
321
322 if (Terminator)
323 Out << Terminator;
324 Out << "\n";
325 }
326
Eli Friedman42f42c02009-05-30 04:20:30 +0000327 if (!Decls.empty())
328 ProcessDeclGroup(Decls);
329
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000330 if (Indent)
331 Indentation -= Policy.Indentation;
332}
333
334void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
335 VisitDeclContext(D, false);
336}
337
338void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000339 if (!Policy.SuppressSpecifiers) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000340 Out << "typedef ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000341
342 if (D->isModulePrivate())
343 Out << "__module_private__ ";
344 }
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000345 D->getUnderlyingType().print(Out, Policy, D->getName());
Douglas Gregor1bea8802011-11-19 19:22:57 +0000346 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000347}
348
Richard Smith162e1c12011-04-15 14:24:37 +0000349void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000350 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy);
Richard Smith162e1c12011-04-15 14:24:37 +0000351}
352
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000353void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000354 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
355 Out << "__module_private__ ";
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000356 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000357 if (D->isScoped()) {
358 if (D->isScopedUsingClassTag())
359 Out << "class ";
360 else
361 Out << "struct ";
362 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000363 Out << *D;
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000364
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000365 if (D->isFixed())
366 Out << " : " << D->getIntegerType().stream(Policy);
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000367
John McCall5e1cdac2011-10-07 06:10:15 +0000368 if (D->isCompleteDefinition()) {
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000369 Out << " {\n";
370 VisitDeclContext(D);
371 Indent() << "}";
372 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000373 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000374}
375
376void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000377 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
378 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000379 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000380 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000381 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000382
John McCall5e1cdac2011-10-07 06:10:15 +0000383 if (D->isCompleteDefinition()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000384 Out << " {\n";
385 VisitDeclContext(D);
386 Indent() << "}";
387 }
388}
389
390void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000391 Out << *D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000392 if (Expr *Init = D->getInitExpr()) {
393 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000394 Init->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000395 }
396}
397
Mike Stump1eb44332009-09-09 15:08:12 +0000398void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000399 if (!Policy.SuppressSpecifiers) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000400 switch (D->getStorageClassAsWritten()) {
John McCalld931b082010-08-26 03:08:43 +0000401 case SC_None: break;
402 case SC_Extern: Out << "extern "; break;
403 case SC_Static: Out << "static "; break;
404 case SC_PrivateExtern: Out << "__private_extern__ "; break;
Peter Collingbourne8c25fc52011-09-19 21:14:35 +0000405 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
406 llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000407 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000408
Douglas Gregor8d267c52011-09-09 02:06:17 +0000409 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000410 if (D->isVirtualAsWritten()) Out << "virtual ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000411 if (D->isModulePrivate()) Out << "__module_private__ ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000412 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000413
Douglas Gregor6620a622009-05-30 05:39:39 +0000414 PrintingPolicy SubPolicy(Policy);
415 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000416 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000417
Abramo Bagnara723df242010-12-14 22:11:44 +0000418 QualType Ty = D->getType();
John McCallf4c73712011-01-19 06:33:43 +0000419 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000420 Proto = '(' + Proto + ')';
421 Ty = PT->getInnerType();
422 }
423
424 if (isa<FunctionType>(Ty)) {
425 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000426 const FunctionProtoType *FT = 0;
427 if (D->hasWrittenPrototype())
428 FT = dyn_cast<FunctionProtoType>(AFT);
429
430 Proto += "(";
431 if (FT) {
432 llvm::raw_string_ostream POut(Proto);
Richard Smithd1420c62012-08-16 03:56:14 +0000433 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000434 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
435 if (i) POut << ", ";
436 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
437 }
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000439 if (FT->isVariadic()) {
440 if (D->getNumParams()) POut << ", ";
441 POut << "...";
442 }
Sean Hunt10620eb2011-05-06 20:44:56 +0000443 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000444 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
445 if (i)
446 Proto += ", ";
447 Proto += D->getParamDecl(i)->getNameAsString();
448 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000449 }
450
451 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000452
David Blaikie4ef832f2012-08-10 00:55:35 +0000453 if (FT) {
454 if (FT->isConst())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000455 Proto += " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000456 if (FT->isVolatile())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000457 Proto += " volatile";
David Blaikie4ef832f2012-08-10 00:55:35 +0000458 if (FT->isRestrict())
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000459 Proto += " restrict";
460 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000461
462 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000463 Proto += " throw(";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000464 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000465 Proto += "...";
466 else
467 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
468 if (I)
469 Proto += ", ";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000470
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000471 Proto += FT->getExceptionType(I).getAsString(SubPolicy);
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000472 }
473 Proto += ")";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000474 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
475 Proto += " noexcept";
476 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
477 Proto += "(";
478 llvm::raw_string_ostream EOut(Proto);
Richard Smithd1420c62012-08-16 03:56:14 +0000479 FT->getNoexceptExpr()->printPretty(EOut, 0, SubPolicy,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000480 Indentation);
481 EOut.flush();
482 Proto += EOut.str();
483 Proto += ")";
484 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000485 }
486
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000487 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith7a614d82011-06-11 17:19:42 +0000488 bool HasInitializerList = false;
489 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
490 E = CDecl->init_end();
491 B != E; ++B) {
492 CXXCtorInitializer * BMInitializer = (*B);
493 if (BMInitializer->isInClassMemberInitializer())
494 continue;
495
496 if (!HasInitializerList) {
497 Proto += " : ";
498 Out << Proto;
499 Proto.clear();
500 HasInitializerList = true;
501 } else
502 Out << ", ";
503
504 if (BMInitializer->isAnyMemberInitializer()) {
505 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000506 Out << *FD;
Richard Smith7a614d82011-06-11 17:19:42 +0000507 } else {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000508 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
Richard Smith7a614d82011-06-11 17:19:42 +0000509 }
510
511 Out << "(";
512 if (!BMInitializer->getInit()) {
513 // Nothing to print
514 } else {
515 Expr *Init = BMInitializer->getInit();
516 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
517 Init = Tmp->getSubExpr();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000518
Richard Smith7a614d82011-06-11 17:19:42 +0000519 Init = Init->IgnoreParens();
520
521 Expr *SimpleInit = 0;
522 Expr **Args = 0;
523 unsigned NumArgs = 0;
524 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
525 Args = ParenList->getExprs();
526 NumArgs = ParenList->getNumExprs();
527 } else if (CXXConstructExpr *Construct
528 = dyn_cast<CXXConstructExpr>(Init)) {
529 Args = Construct->getArgs();
530 NumArgs = Construct->getNumArgs();
531 } else
532 SimpleInit = Init;
533
534 if (SimpleInit)
Richard Smithd1420c62012-08-16 03:56:14 +0000535 SimpleInit->printPretty(Out, 0, Policy, Indentation);
Richard Smith7a614d82011-06-11 17:19:42 +0000536 else {
537 for (unsigned I = 0; I != NumArgs; ++I) {
538 if (isa<CXXDefaultArgExpr>(Args[I]))
539 break;
540
541 if (I)
542 Out << ", ";
Richard Smithd1420c62012-08-16 03:56:14 +0000543 Args[I]->printPretty(Out, 0, Policy, Indentation);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000544 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000545 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000546 }
Richard Smith7a614d82011-06-11 17:19:42 +0000547 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000548 }
549 }
550 else
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000551 AFT->getResultType().print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000552 } else {
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000553 Ty.print(Out, Policy, Proto);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000554 }
555
Douglas Gregor1bea8802011-11-19 19:22:57 +0000556 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000557
558 if (D->isPure())
559 Out << " = 0";
Sean Hunt10620eb2011-05-06 20:44:56 +0000560 else if (D->isDeletedAsWritten())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000561 Out << " = delete";
Dmitri Gribenko2e0b8d92012-08-21 17:47:24 +0000562 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000563 if (!D->hasPrototype() && D->getNumParams()) {
564 // This is a K&R function definition, so we need to print the
565 // parameters.
566 Out << '\n';
Richard Smithd1420c62012-08-16 03:56:14 +0000567 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000568 Indentation += Policy.Indentation;
569 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
570 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000571 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000572 Out << ";\n";
573 }
574 Indentation -= Policy.Indentation;
575 } else
576 Out << ' ';
577
Richard Smithd1420c62012-08-16 03:56:14 +0000578 D->getBody()->printPretty(Out, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000579 Out << '\n';
580 }
581}
582
583void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000584 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000585 Out << "mutable ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000586 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
587 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000588
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000589 Out << D->getType().stream(Policy, D->getName());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000590
591 if (D->isBitField()) {
592 Out << " : ";
Richard Smithd1420c62012-08-16 03:56:14 +0000593 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000594 }
Richard Smith7a614d82011-06-11 17:19:42 +0000595
596 Expr *Init = D->getInClassInitializer();
597 if (!Policy.SuppressInitializers && Init) {
Richard Smithca523302012-06-10 03:12:00 +0000598 if (D->getInClassInitStyle() == ICIS_ListInit)
599 Out << " ";
600 else
601 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000602 Init->printPretty(Out, 0, Policy, Indentation);
Richard Smith7a614d82011-06-11 17:19:42 +0000603 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000604 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000605}
606
Chris Lattner57ad3782011-02-17 20:34:02 +0000607void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000608 Out << *D << ":";
Chris Lattner57ad3782011-02-17 20:34:02 +0000609}
610
611
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000612void DeclPrinter::VisitVarDecl(VarDecl *D) {
Peter Collingbourne13330c42011-11-08 02:52:52 +0000613 StorageClass SCAsWritten = D->getStorageClassAsWritten();
614 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None)
615 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000616
Eli Friedman42f42c02009-05-30 04:20:30 +0000617 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000618 Out << "__thread ";
Douglas Gregor8d267c52011-09-09 02:06:17 +0000619 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
620 Out << "__module_private__ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000621
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000622 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000623 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000624 T = Parm->getOriginalType();
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000625 T.print(Out, Policy, D->getName());
Richard Smithad762fc2011-04-14 22:09:26 +0000626 Expr *Init = D->getInit();
627 if (!Policy.SuppressInitializers && Init) {
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000628 bool ImplicitInit = false;
629 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
630 ImplicitInit = D->getInitStyle() == VarDecl::CallInit &&
631 Construct->getNumArgs() == 0 && !Construct->isListInitialization();
632 if (!ImplicitInit) {
Eli Friedmane1aebe12012-10-19 20:36:44 +0000633 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000634 Out << "(";
635 else if (D->getInitStyle() == VarDecl::CInit) {
636 Out << " = ";
637 }
Richard Smithd1420c62012-08-16 03:56:14 +0000638 Init->printPretty(Out, 0, Policy, Indentation);
Eli Friedmane1aebe12012-10-19 20:36:44 +0000639 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000640 Out << ")";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000641 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000642 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000643 prettyPrintAttributes(D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000644}
645
646void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
647 VisitVarDecl(D);
648}
649
650void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
651 Out << "__asm (";
Richard Smithd1420c62012-08-16 03:56:14 +0000652 D->getAsmString()->printPretty(Out, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000653 Out << ")";
654}
655
Douglas Gregor15de72c2011-12-02 23:23:56 +0000656void DeclPrinter::VisitImportDecl(ImportDecl *D) {
Ted Kremenek32ad2ee2012-03-01 22:07:04 +0000657 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName()
Douglas Gregor15de72c2011-12-02 23:23:56 +0000658 << ";\n";
659}
660
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000661void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
662 Out << "static_assert(";
Richard Smithd1420c62012-08-16 03:56:14 +0000663 D->getAssertExpr()->printPretty(Out, 0, Policy, Indentation);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000664 Out << ", ";
Richard Smithd1420c62012-08-16 03:56:14 +0000665 D->getMessage()->printPretty(Out, 0, Policy, Indentation);
Peter Collingbourne28ac87e2011-03-16 18:37:27 +0000666 Out << ")";
667}
668
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000669//----------------------------------------------------------------------------
670// C++ declarations
671//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000672void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorc30636a2012-05-01 01:43:38 +0000673 if (D->isInline())
674 Out << "inline ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000675 Out << "namespace " << *D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000676 VisitDeclContext(D);
677 Indent() << "}";
678}
679
Douglas Gregor8419fa32009-05-30 06:31:56 +0000680void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
681 Out << "using namespace ";
682 if (D->getQualifier())
683 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000684 Out << *D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000685}
686
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000687void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000688 Out << "namespace " << *D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000689 if (D->getQualifier())
690 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000691 Out << *D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000692}
693
Douglas Gregor59e63572009-05-30 06:58:37 +0000694void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Douglas Gregor8d267c52011-09-09 02:06:17 +0000695 if (!Policy.SuppressSpecifiers && D->isModulePrivate())
696 Out << "__module_private__ ";
Douglas Gregor59e63572009-05-30 06:58:37 +0000697 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000698 if (D->getIdentifier())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000699 Out << ' ' << *D;
Mike Stump1eb44332009-09-09 15:08:12 +0000700
John McCall5e1cdac2011-10-07 06:10:15 +0000701 if (D->isCompleteDefinition()) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000702 // Print the base classes
703 if (D->getNumBases()) {
704 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000705 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
706 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000707 if (Base != D->bases_begin())
708 Out << ", ";
709
710 if (Base->isVirtual())
711 Out << "virtual ";
712
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000713 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
714 if (AS != AS_none)
715 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000716 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor34a99e72011-04-27 17:07:55 +0000717
718 if (Base->isPackExpansion())
719 Out << "...";
Douglas Gregor59e63572009-05-30 06:58:37 +0000720 }
721 }
722
723 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000724 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000725 Out << " {\n";
726 VisitDeclContext(D);
727 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000728 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000729}
730
731void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
732 const char *l;
733 if (D->getLanguage() == LinkageSpecDecl::lang_c)
734 l = "C";
735 else {
736 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
737 "unknown language in linkage specification");
738 l = "C++";
739 }
740
741 Out << "extern \"" << l << "\" ";
742 if (D->hasBraces()) {
743 Out << "{\n";
744 VisitDeclContext(D);
745 Indent() << "}";
746 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000747 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000748}
749
Richard Trieu5cb3d692011-07-28 00:19:05 +0000750void DeclPrinter::PrintTemplateParameters(
751 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
752 assert(Params);
753 assert(!Args || Params->size() == Args->size());
754
Anders Carlsson0487f662009-06-04 05:37:43 +0000755 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Anders Carlsson0487f662009-06-04 05:37:43 +0000757 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
758 if (i != 0)
759 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Anders Carlsson0487f662009-06-04 05:37:43 +0000761 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000762 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000763 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Anders Carlsson0487f662009-06-04 05:37:43 +0000765 if (TTP->wasDeclaredWithTypename())
766 Out << "typename ";
767 else
768 Out << "class ";
769
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000770 if (TTP->isParameterPack())
771 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Benjamin Kramera59d20b2012-02-07 11:57:57 +0000773 Out << *TTP;
Anders Carlsson0487f662009-06-04 05:37:43 +0000774
Richard Trieu5cb3d692011-07-28 00:19:05 +0000775 if (Args) {
776 Out << " = ";
777 Args->get(i).print(Policy, Out);
778 } else if (TTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000779 Out << " = ";
780 Out << TTP->getDefaultArgument().getAsString(Policy);
781 };
Mike Stump1eb44332009-09-09 15:08:12 +0000782 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000783 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
784 Out << NTTP->getType().getAsString(Policy);
785
Douglas Gregor56bc9832010-12-24 00:15:10 +0000786 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
787 Out << "...";
788
Anders Carlsson0487f662009-06-04 05:37:43 +0000789 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
790 Out << ' ';
791 Out << Name->getName();
792 }
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Richard Trieu5cb3d692011-07-28 00:19:05 +0000794 if (Args) {
795 Out << " = ";
796 Args->get(i).print(Policy, Out);
797 } else if (NTTP->hasDefaultArgument()) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000798 Out << " = ";
Richard Smithd1420c62012-08-16 03:56:14 +0000799 NTTP->getDefaultArgument()->printPretty(Out, 0, Policy, Indentation);
Anders Carlsson0487f662009-06-04 05:37:43 +0000800 }
Richard Smith98a57862011-04-15 13:38:57 +0000801 } else if (const TemplateTemplateParmDecl *TTPD =
802 dyn_cast<TemplateTemplateParmDecl>(Param)) {
803 VisitTemplateDecl(TTPD);
804 // FIXME: print the default argument, if present.
Anders Carlsson0487f662009-06-04 05:37:43 +0000805 }
806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Anders Carlsson0487f662009-06-04 05:37:43 +0000808 Out << "> ";
Richard Trieu5cb3d692011-07-28 00:19:05 +0000809}
810
811void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
812 PrintTemplateParameters(D->getTemplateParameters());
Anders Carlsson0487f662009-06-04 05:37:43 +0000813
Richard Smith98a57862011-04-15 13:38:57 +0000814 if (const TemplateTemplateParmDecl *TTP =
815 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000816 Out << "class ";
817 if (TTP->isParameterPack())
818 Out << "...";
819 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000820 } else {
821 Visit(D->getTemplatedDecl());
822 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000823}
824
Richard Trieu5cb3d692011-07-28 00:19:05 +0000825void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
826 if (PrintInstantiation) {
827 TemplateParameterList *Params = D->getTemplateParameters();
828 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
829 I != E; ++I) {
830 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
831 Visit(*I);
832 }
833 }
834
835 return VisitRedeclarableTemplateDecl(D);
836}
837
838void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
839 if (PrintInstantiation) {
840 TemplateParameterList *Params = D->getTemplateParameters();
841 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
842 I != E; ++I) {
843 PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
844 Visit(*I);
845 Out << '\n';
846 }
847 }
848
849 return VisitRedeclarableTemplateDecl(D);
850}
851
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000852//----------------------------------------------------------------------------
853// Objective-C declarations
854//----------------------------------------------------------------------------
855
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000856void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
857 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000858 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000859 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000860 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000861 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000862 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000864 std::string name = OMD->getSelector().getAsString();
865 std::string::size_type pos, lastPos = 0;
866 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
867 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000868 // FIXME: selector is missing here!
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000869 pos = name.find_first_of(':', lastPos);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000870 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000871 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000872 lastPos = pos + 1;
873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000875 if (OMD->param_begin() == OMD->param_end())
876 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000878 if (OMD->isVariadic())
879 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Fariborz Jahanian1bfb00d2012-10-17 21:58:03 +0000881 if (OMD->getBody() && !Policy.TerseOutput) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000882 Out << ' ';
Richard Smithd1420c62012-08-16 03:56:14 +0000883 OMD->getBody()->printPretty(Out, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000884 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000885 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000886}
887
888void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
889 std::string I = OID->getNameAsString();
890 ObjCInterfaceDecl *SID = OID->getSuperClass();
891
892 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000893 Out << "@implementation " << I << " : " << *SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000894 else
895 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000896 Out << "\n";
897 VisitDeclContext(OID, false);
898 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000899}
900
901void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
902 std::string I = OID->getNameAsString();
903 ObjCInterfaceDecl *SID = OID->getSuperClass();
904
Douglas Gregor7723fec2011-12-15 20:29:51 +0000905 if (!OID->isThisDeclarationADefinition()) {
906 Out << "@class " << I << ";";
907 return;
908 }
909
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000910 if (SID)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000911 Out << "@interface " << I << " : " << *SID;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000912 else
913 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000915 // Protocols?
916 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
917 if (!Protocols.empty()) {
918 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
919 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000920 Out << (I == Protocols.begin() ? '<' : ',') << **I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000921 }
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000923 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000924 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000926 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000927 Out << "{\n";
928 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000929 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
930 E = OID->ivar_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +0000931 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000932 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000933 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000934 Out << "}\n";
935 }
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000937 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000938 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000939 // FIXME: implement the rest...
940}
941
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000942void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000943 if (!PID->isThisDeclarationADefinition()) {
944 Out << "@protocol " << PID->getIdentifier() << ";\n";
945 return;
946 }
947
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000948 Out << "@protocol " << *PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000949 VisitDeclContext(PID, false);
950 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000951}
952
953void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000954 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000955
956 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000957 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000958 // FIXME: implement the rest...
959}
960
961void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000962 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000963 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000964 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000966 // FIXME: implement the rest...
967}
968
969void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000970 Out << "@compatibility_alias " << *AID
971 << ' ' << *AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000972}
973
974/// PrintObjCPropertyDecl - print a property declaration.
975///
976void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
977 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
978 Out << "@required\n";
979 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
980 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000982 Out << "@property";
983 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
984 bool first = true;
985 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000986 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000987 ObjCPropertyDecl::OBJC_PR_readonly) {
988 Out << (first ? ' ' : ',') << "readonly";
989 first = false;
Ted Kremenek07c682a2011-08-17 21:09:35 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenek07c682a2011-08-17 21:09:35 +0000992 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
993 Out << (first ? ' ' : ',') << "getter = "
994 << PDecl->getGetterName().getAsString();
995 first = false;
996 }
997 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
998 Out << (first ? ' ' : ',') << "setter = "
999 << PDecl->getSetterName().getAsString();
1000 first = false;
1001 }
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Ted Kremenek07c682a2011-08-17 21:09:35 +00001003 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
1004 Out << (first ? ' ' : ',') << "assign";
1005 first = false;
1006 }
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Ted Kremenek07c682a2011-08-17 21:09:35 +00001008 if (PDecl->getPropertyAttributes() &
1009 ObjCPropertyDecl::OBJC_PR_readwrite) {
1010 Out << (first ? ' ' : ',') << "readwrite";
1011 first = false;
1012 }
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Ted Kremenek07c682a2011-08-17 21:09:35 +00001014 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1015 Out << (first ? ' ' : ',') << "retain";
1016 first = false;
1017 }
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Ted Kremenek07c682a2011-08-17 21:09:35 +00001019 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1020 Out << (first ? ' ' : ',') << "strong";
1021 first = false;
1022 }
John McCallf85e1932011-06-15 23:02:42 +00001023
Ted Kremenek07c682a2011-08-17 21:09:35 +00001024 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1025 Out << (first ? ' ' : ',') << "copy";
1026 first = false;
1027 }
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Ted Kremenek07c682a2011-08-17 21:09:35 +00001029 if (PDecl->getPropertyAttributes() &
1030 ObjCPropertyDecl::OBJC_PR_nonatomic) {
1031 Out << (first ? ' ' : ',') << "nonatomic";
1032 first = false;
1033 }
1034 if (PDecl->getPropertyAttributes() &
1035 ObjCPropertyDecl::OBJC_PR_atomic) {
1036 Out << (first ? ' ' : ',') << "atomic";
1037 first = false;
1038 }
1039
1040 (void) first; // Silence dead store warning due to idiomatic code.
1041 Out << " )";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001042 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001043 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001044}
1045
1046void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1047 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +00001048 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001049 else
Douglas Gregor64f65002009-05-30 00:56:08 +00001050 Out << "@dynamic ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001051 Out << *PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001052 if (PID->getPropertyIvarDecl())
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001053 Out << '=' << *PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001054}
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001055
1056void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1057 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001058 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001059 Out << *D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001060}
1061
John McCall7ba107a2009-11-18 02:36:19 +00001062void
1063DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1064 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +00001065 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001066 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +00001067}
1068
1069void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001070 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +00001071 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001072 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +00001073}
John McCall9488ea12009-11-17 05:59:44 +00001074
1075void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1076 // ignore
1077}