blob: 275f2db82b52f84969612a64a24139f98be0c11f [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
Anders Carlsson3f3a85a2009-08-28 22:39:52 +000036 void Print(AccessSpecifier AS);
37
Douglas Gregor996677c2009-05-30 00:08:05 +000038 public:
39 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
40 const PrintingPolicy &Policy,
41 unsigned Indentation = 0)
42 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
43
44 void VisitDeclContext(DeclContext *DC, bool Indent = true);
45
46 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
47 void VisitTypedefDecl(TypedefDecl *D);
48 void VisitEnumDecl(EnumDecl *D);
49 void VisitRecordDecl(RecordDecl *D);
50 void VisitEnumConstantDecl(EnumConstantDecl *D);
51 void VisitFunctionDecl(FunctionDecl *D);
52 void VisitFieldDecl(FieldDecl *D);
53 void VisitVarDecl(VarDecl *D);
54 void VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000055 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000056 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000057 void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D);
Douglas Gregor6185ec02009-05-30 06:58:37 +000058 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000059 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor8d8ddca2009-05-30 06:48:27 +000060 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor6185ec02009-05-30 06:58:37 +000061 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000062 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
63 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000064 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000065 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000066 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
67 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
68 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
69 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
70 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
71 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
72 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
73 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
74 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Anders Carlssoncd2f2f12009-08-28 19:16:39 +000075 void VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
76 void VisitUsingDecl(UsingDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000077 };
78}
79
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +000080void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) {
81 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +000082}
83
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +000084void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
85 unsigned Indentation) {
86 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +000087 Printer.Visit(this);
88}
89
Eli Friedmand73364a2009-05-30 04:20:30 +000090static QualType GetBaseType(QualType T) {
91 // FIXME: This should be on the Type class!
92 QualType BaseType = T;
93 while (!BaseType->isSpecifierType()) {
94 if (isa<TypedefType>(BaseType))
95 break;
Ted Kremenekd00cd9e2009-07-29 21:53:49 +000096 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedmand73364a2009-05-30 04:20:30 +000097 BaseType = PTy->getPointeeType();
98 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
99 BaseType = ATy->getElementType();
100 else if (const FunctionType* FTy = BaseType->getAsFunctionType())
101 BaseType = FTy->getResultType();
Douglas Gregor6b9f4262009-07-01 23:58:14 +0000102 else if (const VectorType *VTy = BaseType->getAsVectorType())
103 BaseType = VTy->getElementType();
Eli Friedmand73364a2009-05-30 04:20:30 +0000104 else
105 assert(0 && "Unknown declarator!");
106 }
107 return BaseType;
108}
109
110static QualType getDeclType(Decl* D) {
111 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
112 return TDD->getUnderlyingType();
113 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
114 return VD->getType();
115 return QualType();
116}
117
118void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000119 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedmand73364a2009-05-30 04:20:30 +0000120 unsigned Indentation) {
121 if (NumDecls == 1) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000122 (*Begin)->print(Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000123 return;
124 }
125
126 Decl** End = Begin + NumDecls;
127 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
128 if (TD)
129 ++Begin;
130
131 PrintingPolicy SubPolicy(Policy);
132 if (TD && TD->isDefinition()) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000133 TD->print(Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000134 Out << " ";
135 SubPolicy.SuppressTag = true;
136 }
137
138 bool isFirst = true;
139 for ( ; Begin != End; ++Begin) {
140 if (isFirst) {
141 SubPolicy.SuppressSpecifiers = false;
142 isFirst = false;
143 } else {
144 if (!isFirst) Out << ", ";
145 SubPolicy.SuppressSpecifiers = true;
146 }
147
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000148 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000149 }
150}
151
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000152void Decl::dump() {
153 print(llvm::errs());
Douglas Gregor996677c2009-05-30 00:08:05 +0000154}
155
156llvm::raw_ostream& DeclPrinter::Indent() {
157 for (unsigned i = 0; i < Indentation; ++i)
158 Out << " ";
159 return Out;
160}
161
Eli Friedmand73364a2009-05-30 04:20:30 +0000162void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
163 this->Indent();
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000164 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000165 Out << ";\n";
166 Decls.clear();
167
168}
169
Anders Carlsson3f3a85a2009-08-28 22:39:52 +0000170void DeclPrinter::Print(AccessSpecifier AS) {
171 switch(AS) {
Anders Carlssonf736ea02009-08-29 20:36:12 +0000172 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson3f3a85a2009-08-28 22:39:52 +0000173 case AS_public: Out << "public"; break;
174 case AS_protected: Out << "protected"; break;
175 case AS_private: Out << " private"; break;
176 }
177}
178
Douglas Gregor996677c2009-05-30 00:08:05 +0000179//----------------------------------------------------------------------------
180// Common C declarations
181//----------------------------------------------------------------------------
182
183void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
184 if (Indent)
185 Indentation += Policy.Indentation;
186
Anders Carlsson3f3a85a2009-08-28 22:39:52 +0000187 bool PrintAccess = isa<CXXRecordDecl>(DC);
188 AccessSpecifier CurAS = AS_none;
189
Eli Friedmand73364a2009-05-30 04:20:30 +0000190 llvm::SmallVector<Decl*, 2> Decls;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000191 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor996677c2009-05-30 00:08:05 +0000192 D != DEnd; ++D) {
Eli Friedman4bdae722009-05-30 05:03:24 +0000193 if (!Policy.Dump) {
194 // Skip over implicit declarations in pretty-printing mode.
195 if (D->isImplicit()) continue;
Eli Friedman68ff4682009-05-30 06:35:22 +0000196 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
197 // of __builtin_va_list. There should be some other way to check that.
198 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
199 "__builtin_va_list")
200 continue;
Eli Friedman4bdae722009-05-30 05:03:24 +0000201 }
202
Anders Carlsson3f3a85a2009-08-28 22:39:52 +0000203 if (PrintAccess) {
204 AccessSpecifier AS = D->getAccess();
Anders Carlsson65f016b2009-08-29 20:47:47 +0000205
206 if (AS != CurAS &&
207 // FIXME: This check shouldn't be necessary.
208 D->getFriendObjectKind() == Decl::FOK_Undeclared) {
Anders Carlsson3f3a85a2009-08-28 22:39:52 +0000209 Print(AS);
210 Out << ":\n";
211 CurAS = AS;
212 }
213 }
214
Eli Friedmand73364a2009-05-30 04:20:30 +0000215 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
216 // forced to merge the declarations because there's no other way to
217 // refer to the struct in question. This limited merging is safe without
218 // a bunch of other checks because it only merges declarations directly
219 // referring to the tag, not typedefs.
220 //
221 // Check whether the current declaration should be grouped with a previous
222 // unnamed struct.
223 QualType CurDeclType = getDeclType(*D);
224 if (!Decls.empty() && !CurDeclType.isNull()) {
225 QualType BaseType = GetBaseType(CurDeclType);
226 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
227 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
228 Decls.push_back(*D);
229 continue;
230 }
231 }
232
233 // If we have a merged group waiting to be handled, handle it now.
234 if (!Decls.empty())
235 ProcessDeclGroup(Decls);
236
237 // If the current declaration is an unnamed tag type, save it
238 // so we can merge it with the subsequent declaration(s) using it.
239 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
240 Decls.push_back(*D);
241 continue;
242 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000243 this->Indent();
244 Visit(*D);
245
246 // FIXME: Need to be able to tell the DeclPrinter when
247 const char *Terminator = 0;
248 if (isa<FunctionDecl>(*D) &&
249 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
250 Terminator = 0;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000251 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
252 Terminator = 0;
253 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
254 isa<ObjCImplementationDecl>(*D) ||
255 isa<ObjCInterfaceDecl>(*D) ||
256 isa<ObjCProtocolDecl>(*D) ||
257 isa<ObjCCategoryImplDecl>(*D) ||
258 isa<ObjCCategoryDecl>(*D))
Douglas Gregor996677c2009-05-30 00:08:05 +0000259 Terminator = 0;
260 else if (isa<EnumConstantDecl>(*D)) {
261 DeclContext::decl_iterator Next = D;
262 ++Next;
263 if (Next != DEnd)
264 Terminator = ",";
265 } else
266 Terminator = ";";
267
268 if (Terminator)
269 Out << Terminator;
270 Out << "\n";
271 }
272
Eli Friedmand73364a2009-05-30 04:20:30 +0000273 if (!Decls.empty())
274 ProcessDeclGroup(Decls);
275
Douglas Gregor996677c2009-05-30 00:08:05 +0000276 if (Indent)
277 Indentation -= Policy.Indentation;
278}
279
280void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
281 VisitDeclContext(D, false);
282}
283
284void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
285 std::string S = D->getNameAsString();
286 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedmand73364a2009-05-30 04:20:30 +0000287 if (!Policy.SuppressSpecifiers)
288 Out << "typedef ";
289 Out << S;
Douglas Gregor996677c2009-05-30 00:08:05 +0000290}
291
292void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
293 Out << "enum " << D->getNameAsString() << " {\n";
294 VisitDeclContext(D);
295 Indent() << "}";
296}
297
298void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor996677c2009-05-30 00:08:05 +0000299 Out << D->getKindName();
Eli Friedmand73364a2009-05-30 04:20:30 +0000300 if (D->getIdentifier()) {
301 Out << " ";
302 Out << D->getNameAsString();
303 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000304
305 if (D->isDefinition()) {
306 Out << " {\n";
307 VisitDeclContext(D);
308 Indent() << "}";
309 }
310}
311
312void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
313 Out << D->getNameAsString();
314 if (Expr *Init = D->getInitExpr()) {
315 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000316 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000317 }
318}
319
320void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000321 if (!Policy.SuppressSpecifiers) {
322 switch (D->getStorageClass()) {
323 case FunctionDecl::None: break;
324 case FunctionDecl::Extern: Out << "extern "; break;
325 case FunctionDecl::Static: Out << "static "; break;
326 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
327 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000328
Eli Friedmand73364a2009-05-30 04:20:30 +0000329 if (D->isInline()) Out << "inline ";
330 if (D->isVirtualAsWritten()) Out << "virtual ";
331 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000332
Douglas Gregor25133312009-05-30 05:39:39 +0000333 PrintingPolicy SubPolicy(Policy);
334 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor996677c2009-05-30 00:08:05 +0000335 std::string Proto = D->getNameAsString();
336 if (isa<FunctionType>(D->getType().getTypePtr())) {
337 const FunctionType *AFT = D->getType()->getAsFunctionType();
338
339 const FunctionProtoType *FT = 0;
340 if (D->hasWrittenPrototype())
341 FT = dyn_cast<FunctionProtoType>(AFT);
342
343 Proto += "(";
344 if (FT) {
345 llvm::raw_string_ostream POut(Proto);
Douglas Gregor25133312009-05-30 05:39:39 +0000346 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000347 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
348 if (i) POut << ", ";
349 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
350 }
351
352 if (FT->isVariadic()) {
353 if (D->getNumParams()) POut << ", ";
354 POut << "...";
355 }
Eli Friedmand73364a2009-05-30 04:20:30 +0000356 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
357 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
358 if (i)
359 Proto += ", ";
360 Proto += D->getParamDecl(i)->getNameAsString();
361 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000362 }
363
364 Proto += ")";
Mike Stump5963a2d2009-07-27 21:33:40 +0000365 if (D->hasAttr<NoReturnAttr>())
366 Proto += " __attribute((noreturn))";
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000367 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
368 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
369 Proto += " : ";
370 Out << Proto;
371 Proto.clear();
372 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
373 E = CDecl->init_end();
374 B != E; ++B) {
375 CXXBaseOrMemberInitializer * BMInitializer = (*B);
376 if (B != CDecl->init_begin())
377 Out << ", ";
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000378 bool hasArguments = (BMInitializer->arg_begin() !=
379 BMInitializer->arg_end());
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000380 if (BMInitializer->isMemberInitializer()) {
381 FieldDecl *FD = BMInitializer->getMember();
382 Out << FD->getNameAsString();
383 }
Fariborz Jahanian0cdeff12009-07-14 23:41:35 +0000384 else // FIXME. skip dependent types for now.
385 if (const RecordType *RT =
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000386 BMInitializer->getBaseClass()->getAs<RecordType>()) {
Fariborz Jahanian0cdeff12009-07-14 23:41:35 +0000387 const CXXRecordDecl *BaseDecl =
388 cast<CXXRecordDecl>(RT->getDecl());
389 Out << BaseDecl->getNameAsString();
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000390 }
391 if (hasArguments) {
392 Out << "(";
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000393 for (CXXBaseOrMemberInitializer::const_arg_iterator BE =
394 BMInitializer->const_arg_begin(),
395 EE = BMInitializer->const_arg_end(); BE != EE; ++BE) {
396 if (BE != BMInitializer->const_arg_begin())
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000397 Out<< ", ";
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000398 const Expr *Exp = (*BE);
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000399 Exp->printPretty(Out, Context, 0, Policy, Indentation);
400 }
401 Out << ")";
Fariborz Jahanianc0562732009-07-13 23:31:10 +0000402 } else
403 Out << "()";
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000404 }
405 }
406 }
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000407 else if (CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(D)) {
408 if (DDecl->getNumBaseOrMemberDestructions() > 0) {
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000409 // List order of base/member destruction for visualization purposes.
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000410 assert (D->isThisDeclarationADefinition() && "Destructor with dtor-list");
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000411 Proto += "/* : ";
412 for (CXXDestructorDecl::destr_const_iterator *B = DDecl->destr_begin(),
413 *E = DDecl->destr_end();
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000414 B != E; ++B) {
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000415 uintptr_t BaseOrMember = (*B);
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000416 if (B != DDecl->destr_begin())
417 Proto += ", ";
418
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000419 if (DDecl->isMemberToDestroy(BaseOrMember)) {
420 FieldDecl *FD = DDecl->getMemberToDestroy(BaseOrMember);
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000421 Proto += "~";
422 Proto += FD->getNameAsString();
423 }
424 else // FIXME. skip dependent types for now.
425 if (const RecordType *RT =
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000426 DDecl->getAnyBaseClassToDestroy(BaseOrMember)
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000427 ->getAs<RecordType>()) {
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000428 const CXXRecordDecl *BaseDecl =
429 cast<CXXRecordDecl>(RT->getDecl());
430 Proto += "~";
431 Proto += BaseDecl->getNameAsString();
432 }
433 Proto += "()";
434 }
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000435 Proto += " */";
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000436 }
437 }
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000438 else
439 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000440 } else {
441 D->getType().getAsStringInternal(Proto, Policy);
442 }
443
444 Out << Proto;
445
446 if (D->isPure())
447 Out << " = 0";
448 else if (D->isDeleted())
449 Out << " = delete";
450 else if (D->isThisDeclarationADefinition()) {
451 if (!D->hasPrototype() && D->getNumParams()) {
452 // This is a K&R function definition, so we need to print the
453 // parameters.
454 Out << '\n';
Douglas Gregor25133312009-05-30 05:39:39 +0000455 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000456 Indentation += Policy.Indentation;
457 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
458 Indent();
Douglas Gregor25133312009-05-30 05:39:39 +0000459 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor996677c2009-05-30 00:08:05 +0000460 Out << ";\n";
461 }
462 Indentation -= Policy.Indentation;
463 } else
464 Out << ' ';
465
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000466 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000467 Out << '\n';
468 }
469}
470
471void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000472 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000473 Out << "mutable ";
474
475 std::string Name = D->getNameAsString();
476 D->getType().getAsStringInternal(Name, Policy);
477 Out << Name;
478
479 if (D->isBitField()) {
480 Out << " : ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000481 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000482 }
483}
484
485void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000486 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000487 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
488
Eli Friedmand73364a2009-05-30 04:20:30 +0000489 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000490 Out << "__thread ";
491
492 std::string Name = D->getNameAsString();
493 QualType T = D->getType();
494 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
495 T = Parm->getOriginalType();
496 T.getAsStringInternal(Name, Policy);
497 Out << Name;
498 if (D->getInit()) {
499 if (D->hasCXXDirectInitializer())
500 Out << "(";
501 else
502 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000503 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000504 if (D->hasCXXDirectInitializer())
505 Out << ")";
506 }
507}
508
509void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
510 VisitVarDecl(D);
511}
512
Douglas Gregor1d27d692009-05-30 06:31:56 +0000513void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
514 VisitVarDecl(D);
515}
516
Douglas Gregor996677c2009-05-30 00:08:05 +0000517void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
518 Out << "__asm (";
Eli Friedman4bdae722009-05-30 05:03:24 +0000519 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000520 Out << ")";
521}
522
523//----------------------------------------------------------------------------
524// C++ declarations
525//----------------------------------------------------------------------------
Douglas Gregor1d27d692009-05-30 06:31:56 +0000526void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
527 assert(false &&
528 "OverloadedFunctionDecls aren't really decls and are never printed");
529}
530
Douglas Gregor6185ec02009-05-30 06:58:37 +0000531void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
532 Out << "namespace " << D->getNameAsString() << " {\n";
533 VisitDeclContext(D);
534 Indent() << "}";
535}
536
Douglas Gregor1d27d692009-05-30 06:31:56 +0000537void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
538 Out << "using namespace ";
539 if (D->getQualifier())
540 D->getQualifier()->print(Out, Policy);
541 Out << D->getNominatedNamespace()->getNameAsString();
542}
543
Douglas Gregor8d8ddca2009-05-30 06:48:27 +0000544void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
545 Out << "namespace " << D->getNameAsString() << " = ";
546 if (D->getQualifier())
547 D->getQualifier()->print(Out, Policy);
548 Out << D->getAliasedNamespace()->getNameAsString();
549}
550
Douglas Gregor6185ec02009-05-30 06:58:37 +0000551void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
552 Out << D->getKindName();
553 if (D->getIdentifier()) {
554 Out << " ";
555 Out << D->getNameAsString();
556 }
557
558 if (D->isDefinition()) {
559 // Print the base classes
560 if (D->getNumBases()) {
561 Out << " : ";
562 for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
563 BaseEnd = D->bases_end();
564 Base != BaseEnd; ++Base) {
565 if (Base != D->bases_begin())
566 Out << ", ";
567
568 if (Base->isVirtual())
569 Out << "virtual ";
570
Anders Carlssonf736ea02009-08-29 20:36:12 +0000571 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
572 if (AS != AS_none)
573 Print(AS);
Anders Carlsson3f3a85a2009-08-28 22:39:52 +0000574 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor6185ec02009-05-30 06:58:37 +0000575 }
576 }
577
578 // Print the class definition
Douglas Gregor0af57572009-05-31 07:13:39 +0000579 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor6185ec02009-05-30 06:58:37 +0000580 Out << " {\n";
581 VisitDeclContext(D);
582 Indent() << "}";
583 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000584}
585
586void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
587 const char *l;
588 if (D->getLanguage() == LinkageSpecDecl::lang_c)
589 l = "C";
590 else {
591 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
592 "unknown language in linkage specification");
593 l = "C++";
594 }
595
596 Out << "extern \"" << l << "\" ";
597 if (D->hasBraces()) {
598 Out << "{\n";
599 VisitDeclContext(D);
600 Indent() << "}";
601 } else
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000602 Visit(*D->decls_begin());
Douglas Gregor996677c2009-05-30 00:08:05 +0000603}
604
605void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson56b309a2009-06-04 05:37:43 +0000606 Out << "template <";
607
608 TemplateParameterList *Params = D->getTemplateParameters();
609 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
610 if (i != 0)
611 Out << ", ";
612
613 const Decl *Param = Params->getParam(i);
614 if (const TemplateTypeParmDecl *TTP =
615 dyn_cast<TemplateTypeParmDecl>(Param)) {
616
617 QualType ParamType =
618 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
619
620 if (TTP->wasDeclaredWithTypename())
621 Out << "typename ";
622 else
623 Out << "class ";
624
Anders Carlssoneebbf9a2009-06-12 22:23:22 +0000625 if (TTP->isParameterPack())
626 Out << "... ";
627
Anders Carlsson56b309a2009-06-04 05:37:43 +0000628 Out << ParamType.getAsString(Policy);
629
630 if (TTP->hasDefaultArgument()) {
631 Out << " = ";
632 Out << TTP->getDefaultArgument().getAsString(Policy);
633 };
634 } else if (const NonTypeTemplateParmDecl *NTTP =
635 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
636 Out << NTTP->getType().getAsString(Policy);
637
638 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
639 Out << ' ';
640 Out << Name->getName();
641 }
642
643 if (NTTP->hasDefaultArgument()) {
644 Out << " = ";
645 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
646 Indentation);
647 }
648 }
649 }
650
651 Out << "> ";
652
Douglas Gregor996677c2009-05-30 00:08:05 +0000653 Visit(D->getTemplatedDecl());
654}
655
656//----------------------------------------------------------------------------
657// Objective-C declarations
658//----------------------------------------------------------------------------
659
660void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
661 Out << "@class ";
662 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
663 I != E; ++I) {
664 if (I != D->begin()) Out << ", ";
665 Out << (*I)->getNameAsString();
666 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000667}
668
669void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
670 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000671 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000672 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000673 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000674 if (!OMD->getResultType().isNull())
Douglas Gregor6185ec02009-05-30 06:58:37 +0000675 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Douglas Gregor996677c2009-05-30 00:08:05 +0000676
677 std::string name = OMD->getSelector().getAsString();
678 std::string::size_type pos, lastPos = 0;
679 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
680 E = OMD->param_end(); PI != E; ++PI) {
681 // FIXME: selector is missing here!
682 pos = name.find_first_of(":", lastPos);
683 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor6185ec02009-05-30 06:58:37 +0000684 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Douglas Gregor996677c2009-05-30 00:08:05 +0000685 << (*PI)->getNameAsString();
686 lastPos = pos + 1;
687 }
688
689 if (OMD->param_begin() == OMD->param_end())
690 Out << " " << name;
691
692 if (OMD->isVariadic())
693 Out << ", ...";
694
695 if (OMD->getBody()) {
696 Out << ' ';
Eli Friedman4bdae722009-05-30 05:03:24 +0000697 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000698 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000699 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000700}
701
702void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
703 std::string I = OID->getNameAsString();
704 ObjCInterfaceDecl *SID = OID->getSuperClass();
705
706 if (SID)
707 Out << "@implementation " << I << " : " << SID->getNameAsString();
708 else
709 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000710 Out << "\n";
711 VisitDeclContext(OID, false);
712 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000713}
714
715void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
716 std::string I = OID->getNameAsString();
717 ObjCInterfaceDecl *SID = OID->getSuperClass();
718
719 if (SID)
720 Out << "@interface " << I << " : " << SID->getNameAsString();
721 else
722 Out << "@interface " << I;
723
724 // Protocols?
725 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
726 if (!Protocols.empty()) {
727 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
728 E = Protocols.end(); I != E; ++I)
729 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
730 }
731
732 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000733 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000734
735 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000736 Out << "{\n";
737 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000738 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
739 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000740 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000741 << ' ' << (*I)->getNameAsString() << ";\n";
742 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000743 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000744 Out << "}\n";
745 }
746
747 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000748 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000749 // FIXME: implement the rest...
750}
751
752void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
753 Out << "@protocol ";
754 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
755 E = D->protocol_end();
756 I != E; ++I) {
757 if (I != D->protocol_begin()) Out << ", ";
758 Out << (*I)->getNameAsString();
759 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000760}
761
762void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
763 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000764 VisitDeclContext(PID, false);
765 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000766}
767
768void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
769 Out << "@implementation "
770 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000771 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000772
773 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000774 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000775 // FIXME: implement the rest...
776}
777
778void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
779 Out << "@interface "
780 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000781 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000782 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000783 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000784
785 // FIXME: implement the rest...
786}
787
788void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
789 Out << "@compatibility_alias " << AID->getNameAsString()
790 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
791}
792
793/// PrintObjCPropertyDecl - print a property declaration.
794///
795void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
796 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
797 Out << "@required\n";
798 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
799 Out << "@optional\n";
800
801 Out << "@property";
802 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
803 bool first = true;
804 Out << " (";
805 if (PDecl->getPropertyAttributes() &
806 ObjCPropertyDecl::OBJC_PR_readonly) {
807 Out << (first ? ' ' : ',') << "readonly";
808 first = false;
809 }
810
811 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
812 Out << (first ? ' ' : ',') << "getter = "
813 << PDecl->getGetterName().getAsString();
814 first = false;
815 }
816 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
817 Out << (first ? ' ' : ',') << "setter = "
818 << PDecl->getSetterName().getAsString();
819 first = false;
820 }
821
822 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
823 Out << (first ? ' ' : ',') << "assign";
824 first = false;
825 }
826
827 if (PDecl->getPropertyAttributes() &
828 ObjCPropertyDecl::OBJC_PR_readwrite) {
829 Out << (first ? ' ' : ',') << "readwrite";
830 first = false;
831 }
832
833 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
834 Out << (first ? ' ' : ',') << "retain";
835 first = false;
836 }
837
838 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
839 Out << (first ? ' ' : ',') << "copy";
840 first = false;
841 }
842
843 if (PDecl->getPropertyAttributes() &
844 ObjCPropertyDecl::OBJC_PR_nonatomic) {
845 Out << (first ? ' ' : ',') << "nonatomic";
846 first = false;
847 }
848 Out << " )";
849 }
850 Out << ' ' << PDecl->getType().getAsString(Policy)
851 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000852}
853
854void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
855 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000856 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000857 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000858 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000859 Out << PID->getPropertyDecl()->getNameAsString();
860 if (PID->getPropertyIvarDecl())
861 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000862}
Anders Carlssoncd2f2f12009-08-28 19:16:39 +0000863
864void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
865 Out << "using ";
866 D->getTargetNestedNameDecl()->print(Out, Policy);
867 Out << D->getTargetDecl()->getNameAsString();
868}
869
870void DeclPrinter::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
871 Out << "using ";
872 D->getTargetNestedNameSpecifier()->print(Out, Policy);
873 Out << D->getTargetName().getAsString();
874}