blob: 69144735c9721892f5c4186e951a39e7025305f7 [file] [log] [blame]
Douglas Gregor996677c2009-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"
20#include "clang/AST/PrettyPrinter.h"
21#include "llvm/Support/Compiler.h"
Douglas Gregor996677c2009-05-30 00:08:05 +000022#include "llvm/Support/Format.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25
26namespace {
27 class VISIBILITY_HIDDEN DeclPrinter : public DeclVisitor<DeclPrinter> {
28 llvm::raw_ostream &Out;
29 ASTContext &Context;
30 PrintingPolicy Policy;
31 unsigned Indentation;
32
33 llvm::raw_ostream& Indent();
Eli Friedmand73364a2009-05-30 04:20:30 +000034 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor996677c2009-05-30 00:08:05 +000035
36 public:
37 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
38 const PrintingPolicy &Policy,
39 unsigned Indentation = 0)
40 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
41
42 void VisitDeclContext(DeclContext *DC, bool Indent = true);
43
44 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
45 void VisitTypedefDecl(TypedefDecl *D);
46 void VisitEnumDecl(EnumDecl *D);
47 void VisitRecordDecl(RecordDecl *D);
48 void VisitEnumConstantDecl(EnumConstantDecl *D);
49 void VisitFunctionDecl(FunctionDecl *D);
50 void VisitFieldDecl(FieldDecl *D);
51 void VisitVarDecl(VarDecl *D);
52 void VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000053 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000054 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000055 void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D);
Douglas Gregor6185ec02009-05-30 06:58:37 +000056 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000057 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor8d8ddca2009-05-30 06:48:27 +000058 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor6185ec02009-05-30 06:58:37 +000059 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000060 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
61 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000062 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000063 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000064 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
65 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
66 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
67 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
68 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
69 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
70 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
71 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
72 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
73 };
74}
75
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +000076void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) {
77 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +000078}
79
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +000080void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
81 unsigned Indentation) {
82 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +000083 Printer.Visit(this);
84}
85
Eli Friedmand73364a2009-05-30 04:20:30 +000086static QualType GetBaseType(QualType T) {
87 // FIXME: This should be on the Type class!
88 QualType BaseType = T;
89 while (!BaseType->isSpecifierType()) {
90 if (isa<TypedefType>(BaseType))
91 break;
Ted Kremenekd00cd9e2009-07-29 21:53:49 +000092 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedmand73364a2009-05-30 04:20:30 +000093 BaseType = PTy->getPointeeType();
94 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
95 BaseType = ATy->getElementType();
96 else if (const FunctionType* FTy = BaseType->getAsFunctionType())
97 BaseType = FTy->getResultType();
Douglas Gregor6b9f4262009-07-01 23:58:14 +000098 else if (const VectorType *VTy = BaseType->getAsVectorType())
99 BaseType = VTy->getElementType();
Eli Friedmand73364a2009-05-30 04:20:30 +0000100 else
101 assert(0 && "Unknown declarator!");
102 }
103 return BaseType;
104}
105
106static QualType getDeclType(Decl* D) {
107 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
108 return TDD->getUnderlyingType();
109 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
110 return VD->getType();
111 return QualType();
112}
113
114void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000115 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedmand73364a2009-05-30 04:20:30 +0000116 unsigned Indentation) {
117 if (NumDecls == 1) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000118 (*Begin)->print(Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000119 return;
120 }
121
122 Decl** End = Begin + NumDecls;
123 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
124 if (TD)
125 ++Begin;
126
127 PrintingPolicy SubPolicy(Policy);
128 if (TD && TD->isDefinition()) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000129 TD->print(Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000130 Out << " ";
131 SubPolicy.SuppressTag = true;
132 }
133
134 bool isFirst = true;
135 for ( ; Begin != End; ++Begin) {
136 if (isFirst) {
137 SubPolicy.SuppressSpecifiers = false;
138 isFirst = false;
139 } else {
140 if (!isFirst) Out << ", ";
141 SubPolicy.SuppressSpecifiers = true;
142 }
143
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000144 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000145 }
146}
147
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000148void Decl::dump() {
149 print(llvm::errs());
Douglas Gregor996677c2009-05-30 00:08:05 +0000150}
151
152llvm::raw_ostream& DeclPrinter::Indent() {
153 for (unsigned i = 0; i < Indentation; ++i)
154 Out << " ";
155 return Out;
156}
157
Eli Friedmand73364a2009-05-30 04:20:30 +0000158void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
159 this->Indent();
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000160 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000161 Out << ";\n";
162 Decls.clear();
163
164}
165
Douglas Gregor996677c2009-05-30 00:08:05 +0000166//----------------------------------------------------------------------------
167// Common C declarations
168//----------------------------------------------------------------------------
169
170void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
171 if (Indent)
172 Indentation += Policy.Indentation;
173
Eli Friedmand73364a2009-05-30 04:20:30 +0000174 llvm::SmallVector<Decl*, 2> Decls;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000175 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor996677c2009-05-30 00:08:05 +0000176 D != DEnd; ++D) {
Eli Friedman4bdae722009-05-30 05:03:24 +0000177 if (!Policy.Dump) {
178 // Skip over implicit declarations in pretty-printing mode.
179 if (D->isImplicit()) continue;
Eli Friedman68ff4682009-05-30 06:35:22 +0000180 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
181 // of __builtin_va_list. There should be some other way to check that.
182 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
183 "__builtin_va_list")
184 continue;
Eli Friedman4bdae722009-05-30 05:03:24 +0000185 }
186
Eli Friedmand73364a2009-05-30 04:20:30 +0000187 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
188 // forced to merge the declarations because there's no other way to
189 // refer to the struct in question. This limited merging is safe without
190 // a bunch of other checks because it only merges declarations directly
191 // referring to the tag, not typedefs.
192 //
193 // Check whether the current declaration should be grouped with a previous
194 // unnamed struct.
195 QualType CurDeclType = getDeclType(*D);
196 if (!Decls.empty() && !CurDeclType.isNull()) {
197 QualType BaseType = GetBaseType(CurDeclType);
198 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
199 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
200 Decls.push_back(*D);
201 continue;
202 }
203 }
204
205 // If we have a merged group waiting to be handled, handle it now.
206 if (!Decls.empty())
207 ProcessDeclGroup(Decls);
208
209 // If the current declaration is an unnamed tag type, save it
210 // so we can merge it with the subsequent declaration(s) using it.
211 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
212 Decls.push_back(*D);
213 continue;
214 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000215 this->Indent();
216 Visit(*D);
217
218 // FIXME: Need to be able to tell the DeclPrinter when
219 const char *Terminator = 0;
220 if (isa<FunctionDecl>(*D) &&
221 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
222 Terminator = 0;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000223 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
224 Terminator = 0;
225 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
226 isa<ObjCImplementationDecl>(*D) ||
227 isa<ObjCInterfaceDecl>(*D) ||
228 isa<ObjCProtocolDecl>(*D) ||
229 isa<ObjCCategoryImplDecl>(*D) ||
230 isa<ObjCCategoryDecl>(*D))
Douglas Gregor996677c2009-05-30 00:08:05 +0000231 Terminator = 0;
232 else if (isa<EnumConstantDecl>(*D)) {
233 DeclContext::decl_iterator Next = D;
234 ++Next;
235 if (Next != DEnd)
236 Terminator = ",";
237 } else
238 Terminator = ";";
239
240 if (Terminator)
241 Out << Terminator;
242 Out << "\n";
243 }
244
Eli Friedmand73364a2009-05-30 04:20:30 +0000245 if (!Decls.empty())
246 ProcessDeclGroup(Decls);
247
Douglas Gregor996677c2009-05-30 00:08:05 +0000248 if (Indent)
249 Indentation -= Policy.Indentation;
250}
251
252void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
253 VisitDeclContext(D, false);
254}
255
256void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
257 std::string S = D->getNameAsString();
258 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedmand73364a2009-05-30 04:20:30 +0000259 if (!Policy.SuppressSpecifiers)
260 Out << "typedef ";
261 Out << S;
Douglas Gregor996677c2009-05-30 00:08:05 +0000262}
263
264void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
265 Out << "enum " << D->getNameAsString() << " {\n";
266 VisitDeclContext(D);
267 Indent() << "}";
268}
269
270void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor996677c2009-05-30 00:08:05 +0000271 Out << D->getKindName();
Eli Friedmand73364a2009-05-30 04:20:30 +0000272 if (D->getIdentifier()) {
273 Out << " ";
274 Out << D->getNameAsString();
275 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000276
277 if (D->isDefinition()) {
278 Out << " {\n";
279 VisitDeclContext(D);
280 Indent() << "}";
281 }
282}
283
284void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
285 Out << D->getNameAsString();
286 if (Expr *Init = D->getInitExpr()) {
287 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000288 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000289 }
290}
291
292void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000293 if (!Policy.SuppressSpecifiers) {
294 switch (D->getStorageClass()) {
295 case FunctionDecl::None: break;
296 case FunctionDecl::Extern: Out << "extern "; break;
297 case FunctionDecl::Static: Out << "static "; break;
298 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
299 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000300
Eli Friedmand73364a2009-05-30 04:20:30 +0000301 if (D->isInline()) Out << "inline ";
302 if (D->isVirtualAsWritten()) Out << "virtual ";
303 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000304
Douglas Gregor25133312009-05-30 05:39:39 +0000305 PrintingPolicy SubPolicy(Policy);
306 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor996677c2009-05-30 00:08:05 +0000307 std::string Proto = D->getNameAsString();
308 if (isa<FunctionType>(D->getType().getTypePtr())) {
309 const FunctionType *AFT = D->getType()->getAsFunctionType();
310
311 const FunctionProtoType *FT = 0;
312 if (D->hasWrittenPrototype())
313 FT = dyn_cast<FunctionProtoType>(AFT);
314
315 Proto += "(";
316 if (FT) {
317 llvm::raw_string_ostream POut(Proto);
Douglas Gregor25133312009-05-30 05:39:39 +0000318 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000319 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
320 if (i) POut << ", ";
321 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
322 }
323
324 if (FT->isVariadic()) {
325 if (D->getNumParams()) POut << ", ";
326 POut << "...";
327 }
Eli Friedmand73364a2009-05-30 04:20:30 +0000328 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
329 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
330 if (i)
331 Proto += ", ";
332 Proto += D->getParamDecl(i)->getNameAsString();
333 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000334 }
335
336 Proto += ")";
Mike Stump5963a2d2009-07-27 21:33:40 +0000337 if (D->hasAttr<NoReturnAttr>())
338 Proto += " __attribute((noreturn))";
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000339 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
340 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
341 Proto += " : ";
342 Out << Proto;
343 Proto.clear();
344 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
345 E = CDecl->init_end();
346 B != E; ++B) {
347 CXXBaseOrMemberInitializer * BMInitializer = (*B);
348 if (B != CDecl->init_begin())
349 Out << ", ";
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000350 bool hasArguments = (BMInitializer->arg_begin() !=
351 BMInitializer->arg_end());
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000352 if (BMInitializer->isMemberInitializer()) {
353 FieldDecl *FD = BMInitializer->getMember();
354 Out << FD->getNameAsString();
355 }
Fariborz Jahanian0cdeff12009-07-14 23:41:35 +0000356 else // FIXME. skip dependent types for now.
357 if (const RecordType *RT =
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000358 BMInitializer->getBaseClass()->getAs<RecordType>()) {
Fariborz Jahanian0cdeff12009-07-14 23:41:35 +0000359 const CXXRecordDecl *BaseDecl =
360 cast<CXXRecordDecl>(RT->getDecl());
361 Out << BaseDecl->getNameAsString();
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000362 }
363 if (hasArguments) {
364 Out << "(";
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000365 for (CXXBaseOrMemberInitializer::const_arg_iterator BE =
366 BMInitializer->const_arg_begin(),
367 EE = BMInitializer->const_arg_end(); BE != EE; ++BE) {
368 if (BE != BMInitializer->const_arg_begin())
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000369 Out<< ", ";
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000370 const Expr *Exp = (*BE);
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000371 Exp->printPretty(Out, Context, 0, Policy, Indentation);
372 }
373 Out << ")";
Fariborz Jahanianc0562732009-07-13 23:31:10 +0000374 } else
375 Out << "()";
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000376 }
377 }
378 }
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000379 else if (CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(D)) {
380 if (DDecl->getNumBaseOrMemberDestructions() > 0) {
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000381 // List order of base/member destruction for visualization purposes.
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000382 assert (D->isThisDeclarationADefinition() && "Destructor with dtor-list");
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000383 Proto += "/* : ";
384 for (CXXDestructorDecl::destr_const_iterator *B = DDecl->destr_begin(),
385 *E = DDecl->destr_end();
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000386 B != E; ++B) {
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000387 uintptr_t BaseOrMember = (*B);
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000388 if (B != DDecl->destr_begin())
389 Proto += ", ";
390
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000391 if (DDecl->isMemberToDestroy(BaseOrMember)) {
392 FieldDecl *FD = DDecl->getMemberToDestroy(BaseOrMember);
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000393 Proto += "~";
394 Proto += FD->getNameAsString();
395 }
396 else // FIXME. skip dependent types for now.
397 if (const RecordType *RT =
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000398 DDecl->getAnyBaseClassToDestroy(BaseOrMember)
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000399 ->getAs<RecordType>()) {
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000400 const CXXRecordDecl *BaseDecl =
401 cast<CXXRecordDecl>(RT->getDecl());
402 Proto += "~";
403 Proto += BaseDecl->getNameAsString();
404 }
405 Proto += "()";
406 }
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000407 Proto += " */";
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000408 }
409 }
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000410 else
411 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000412 } else {
413 D->getType().getAsStringInternal(Proto, Policy);
414 }
415
416 Out << Proto;
417
418 if (D->isPure())
419 Out << " = 0";
420 else if (D->isDeleted())
421 Out << " = delete";
422 else if (D->isThisDeclarationADefinition()) {
423 if (!D->hasPrototype() && D->getNumParams()) {
424 // This is a K&R function definition, so we need to print the
425 // parameters.
426 Out << '\n';
Douglas Gregor25133312009-05-30 05:39:39 +0000427 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000428 Indentation += Policy.Indentation;
429 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
430 Indent();
Douglas Gregor25133312009-05-30 05:39:39 +0000431 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor996677c2009-05-30 00:08:05 +0000432 Out << ";\n";
433 }
434 Indentation -= Policy.Indentation;
435 } else
436 Out << ' ';
437
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000438 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000439 Out << '\n';
440 }
441}
442
443void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000444 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000445 Out << "mutable ";
446
447 std::string Name = D->getNameAsString();
448 D->getType().getAsStringInternal(Name, Policy);
449 Out << Name;
450
451 if (D->isBitField()) {
452 Out << " : ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000453 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000454 }
455}
456
457void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000458 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000459 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
460
Eli Friedmand73364a2009-05-30 04:20:30 +0000461 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000462 Out << "__thread ";
463
464 std::string Name = D->getNameAsString();
465 QualType T = D->getType();
466 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
467 T = Parm->getOriginalType();
468 T.getAsStringInternal(Name, Policy);
469 Out << Name;
470 if (D->getInit()) {
471 if (D->hasCXXDirectInitializer())
472 Out << "(";
473 else
474 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000475 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000476 if (D->hasCXXDirectInitializer())
477 Out << ")";
478 }
479}
480
481void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
482 VisitVarDecl(D);
483}
484
Douglas Gregor1d27d692009-05-30 06:31:56 +0000485void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
486 VisitVarDecl(D);
487}
488
Douglas Gregor996677c2009-05-30 00:08:05 +0000489void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
490 Out << "__asm (";
Eli Friedman4bdae722009-05-30 05:03:24 +0000491 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000492 Out << ")";
493}
494
495//----------------------------------------------------------------------------
496// C++ declarations
497//----------------------------------------------------------------------------
Douglas Gregor1d27d692009-05-30 06:31:56 +0000498void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
499 assert(false &&
500 "OverloadedFunctionDecls aren't really decls and are never printed");
501}
502
Douglas Gregor6185ec02009-05-30 06:58:37 +0000503void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
504 Out << "namespace " << D->getNameAsString() << " {\n";
505 VisitDeclContext(D);
506 Indent() << "}";
507}
508
Douglas Gregor1d27d692009-05-30 06:31:56 +0000509void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
510 Out << "using namespace ";
511 if (D->getQualifier())
512 D->getQualifier()->print(Out, Policy);
513 Out << D->getNominatedNamespace()->getNameAsString();
514}
515
Douglas Gregor8d8ddca2009-05-30 06:48:27 +0000516void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
517 Out << "namespace " << D->getNameAsString() << " = ";
518 if (D->getQualifier())
519 D->getQualifier()->print(Out, Policy);
520 Out << D->getAliasedNamespace()->getNameAsString();
521}
522
Douglas Gregor6185ec02009-05-30 06:58:37 +0000523void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
524 Out << D->getKindName();
525 if (D->getIdentifier()) {
526 Out << " ";
527 Out << D->getNameAsString();
528 }
529
530 if (D->isDefinition()) {
531 // Print the base classes
532 if (D->getNumBases()) {
533 Out << " : ";
534 for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
535 BaseEnd = D->bases_end();
536 Base != BaseEnd; ++Base) {
537 if (Base != D->bases_begin())
538 Out << ", ";
539
540 if (Base->isVirtual())
541 Out << "virtual ";
542
543 switch(Base->getAccessSpecifierAsWritten()) {
544 case AS_none: break;
545 case AS_public: Out << "public "; break;
546 case AS_protected: Out << "protected "; break;
547 case AS_private: Out << " private "; break;
548 }
549
550 Out << Base->getType().getAsString(Policy);
551 }
552 }
553
554 // Print the class definition
Douglas Gregor0af57572009-05-31 07:13:39 +0000555 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor6185ec02009-05-30 06:58:37 +0000556 Out << " {\n";
557 VisitDeclContext(D);
558 Indent() << "}";
559 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000560}
561
562void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
563 const char *l;
564 if (D->getLanguage() == LinkageSpecDecl::lang_c)
565 l = "C";
566 else {
567 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
568 "unknown language in linkage specification");
569 l = "C++";
570 }
571
572 Out << "extern \"" << l << "\" ";
573 if (D->hasBraces()) {
574 Out << "{\n";
575 VisitDeclContext(D);
576 Indent() << "}";
577 } else
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000578 Visit(*D->decls_begin());
Douglas Gregor996677c2009-05-30 00:08:05 +0000579}
580
581void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson56b309a2009-06-04 05:37:43 +0000582 Out << "template <";
583
584 TemplateParameterList *Params = D->getTemplateParameters();
585 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
586 if (i != 0)
587 Out << ", ";
588
589 const Decl *Param = Params->getParam(i);
590 if (const TemplateTypeParmDecl *TTP =
591 dyn_cast<TemplateTypeParmDecl>(Param)) {
592
593 QualType ParamType =
594 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
595
596 if (TTP->wasDeclaredWithTypename())
597 Out << "typename ";
598 else
599 Out << "class ";
600
Anders Carlssoneebbf9a2009-06-12 22:23:22 +0000601 if (TTP->isParameterPack())
602 Out << "... ";
603
Anders Carlsson56b309a2009-06-04 05:37:43 +0000604 Out << ParamType.getAsString(Policy);
605
606 if (TTP->hasDefaultArgument()) {
607 Out << " = ";
608 Out << TTP->getDefaultArgument().getAsString(Policy);
609 };
610 } else if (const NonTypeTemplateParmDecl *NTTP =
611 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
612 Out << NTTP->getType().getAsString(Policy);
613
614 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
615 Out << ' ';
616 Out << Name->getName();
617 }
618
619 if (NTTP->hasDefaultArgument()) {
620 Out << " = ";
621 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
622 Indentation);
623 }
624 }
625 }
626
627 Out << "> ";
628
Douglas Gregor996677c2009-05-30 00:08:05 +0000629 Visit(D->getTemplatedDecl());
630}
631
632//----------------------------------------------------------------------------
633// Objective-C declarations
634//----------------------------------------------------------------------------
635
636void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
637 Out << "@class ";
638 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
639 I != E; ++I) {
640 if (I != D->begin()) Out << ", ";
641 Out << (*I)->getNameAsString();
642 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000643}
644
645void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
646 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000647 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000648 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000649 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000650 if (!OMD->getResultType().isNull())
Douglas Gregor6185ec02009-05-30 06:58:37 +0000651 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Douglas Gregor996677c2009-05-30 00:08:05 +0000652
653 std::string name = OMD->getSelector().getAsString();
654 std::string::size_type pos, lastPos = 0;
655 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
656 E = OMD->param_end(); PI != E; ++PI) {
657 // FIXME: selector is missing here!
658 pos = name.find_first_of(":", lastPos);
659 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor6185ec02009-05-30 06:58:37 +0000660 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Douglas Gregor996677c2009-05-30 00:08:05 +0000661 << (*PI)->getNameAsString();
662 lastPos = pos + 1;
663 }
664
665 if (OMD->param_begin() == OMD->param_end())
666 Out << " " << name;
667
668 if (OMD->isVariadic())
669 Out << ", ...";
670
671 if (OMD->getBody()) {
672 Out << ' ';
Eli Friedman4bdae722009-05-30 05:03:24 +0000673 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000674 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000675 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000676}
677
678void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
679 std::string I = OID->getNameAsString();
680 ObjCInterfaceDecl *SID = OID->getSuperClass();
681
682 if (SID)
683 Out << "@implementation " << I << " : " << SID->getNameAsString();
684 else
685 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000686 Out << "\n";
687 VisitDeclContext(OID, false);
688 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000689}
690
691void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
692 std::string I = OID->getNameAsString();
693 ObjCInterfaceDecl *SID = OID->getSuperClass();
694
695 if (SID)
696 Out << "@interface " << I << " : " << SID->getNameAsString();
697 else
698 Out << "@interface " << I;
699
700 // Protocols?
701 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
702 if (!Protocols.empty()) {
703 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
704 E = Protocols.end(); I != E; ++I)
705 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
706 }
707
708 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000709 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000710
711 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000712 Out << "{\n";
713 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000714 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
715 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000716 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000717 << ' ' << (*I)->getNameAsString() << ";\n";
718 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000719 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000720 Out << "}\n";
721 }
722
723 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000724 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000725 // FIXME: implement the rest...
726}
727
728void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
729 Out << "@protocol ";
730 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
731 E = D->protocol_end();
732 I != E; ++I) {
733 if (I != D->protocol_begin()) Out << ", ";
734 Out << (*I)->getNameAsString();
735 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000736}
737
738void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
739 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000740 VisitDeclContext(PID, false);
741 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000742}
743
744void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
745 Out << "@implementation "
746 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000747 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000748
749 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000750 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000751 // FIXME: implement the rest...
752}
753
754void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
755 Out << "@interface "
756 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000757 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000758 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000759 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000760
761 // FIXME: implement the rest...
762}
763
764void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
765 Out << "@compatibility_alias " << AID->getNameAsString()
766 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
767}
768
769/// PrintObjCPropertyDecl - print a property declaration.
770///
771void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
772 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
773 Out << "@required\n";
774 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
775 Out << "@optional\n";
776
777 Out << "@property";
778 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
779 bool first = true;
780 Out << " (";
781 if (PDecl->getPropertyAttributes() &
782 ObjCPropertyDecl::OBJC_PR_readonly) {
783 Out << (first ? ' ' : ',') << "readonly";
784 first = false;
785 }
786
787 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
788 Out << (first ? ' ' : ',') << "getter = "
789 << PDecl->getGetterName().getAsString();
790 first = false;
791 }
792 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
793 Out << (first ? ' ' : ',') << "setter = "
794 << PDecl->getSetterName().getAsString();
795 first = false;
796 }
797
798 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
799 Out << (first ? ' ' : ',') << "assign";
800 first = false;
801 }
802
803 if (PDecl->getPropertyAttributes() &
804 ObjCPropertyDecl::OBJC_PR_readwrite) {
805 Out << (first ? ' ' : ',') << "readwrite";
806 first = false;
807 }
808
809 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
810 Out << (first ? ' ' : ',') << "retain";
811 first = false;
812 }
813
814 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
815 Out << (first ? ' ' : ',') << "copy";
816 first = false;
817 }
818
819 if (PDecl->getPropertyAttributes() &
820 ObjCPropertyDecl::OBJC_PR_nonatomic) {
821 Out << (first ? ' ' : ',') << "nonatomic";
822 first = false;
823 }
824 Out << " )";
825 }
826 Out << ' ' << PDecl->getType().getAsString(Policy)
827 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000828}
829
830void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
831 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000832 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000833 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000834 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000835 Out << PID->getPropertyDecl()->getNameAsString();
836 if (PID->getPropertyIvarDecl())
837 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000838}