blob: 1dea2238bc31d6b224aeca7d12865652b2a63500 [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);
Anders Carlssoncd2f2f12009-08-28 19:16:39 +000073 void VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
74 void VisitUsingDecl(UsingDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000075 };
76}
77
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +000078void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) {
79 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +000080}
81
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +000082void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
83 unsigned Indentation) {
84 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +000085 Printer.Visit(this);
86}
87
Eli Friedmand73364a2009-05-30 04:20:30 +000088static QualType GetBaseType(QualType T) {
89 // FIXME: This should be on the Type class!
90 QualType BaseType = T;
91 while (!BaseType->isSpecifierType()) {
92 if (isa<TypedefType>(BaseType))
93 break;
Ted Kremenekd00cd9e2009-07-29 21:53:49 +000094 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedmand73364a2009-05-30 04:20:30 +000095 BaseType = PTy->getPointeeType();
96 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
97 BaseType = ATy->getElementType();
98 else if (const FunctionType* FTy = BaseType->getAsFunctionType())
99 BaseType = FTy->getResultType();
Douglas Gregor6b9f4262009-07-01 23:58:14 +0000100 else if (const VectorType *VTy = BaseType->getAsVectorType())
101 BaseType = VTy->getElementType();
Eli Friedmand73364a2009-05-30 04:20:30 +0000102 else
103 assert(0 && "Unknown declarator!");
104 }
105 return BaseType;
106}
107
108static QualType getDeclType(Decl* D) {
109 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
110 return TDD->getUnderlyingType();
111 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
112 return VD->getType();
113 return QualType();
114}
115
116void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000117 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedmand73364a2009-05-30 04:20:30 +0000118 unsigned Indentation) {
119 if (NumDecls == 1) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000120 (*Begin)->print(Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000121 return;
122 }
123
124 Decl** End = Begin + NumDecls;
125 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
126 if (TD)
127 ++Begin;
128
129 PrintingPolicy SubPolicy(Policy);
130 if (TD && TD->isDefinition()) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000131 TD->print(Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000132 Out << " ";
133 SubPolicy.SuppressTag = true;
134 }
135
136 bool isFirst = true;
137 for ( ; Begin != End; ++Begin) {
138 if (isFirst) {
139 SubPolicy.SuppressSpecifiers = false;
140 isFirst = false;
141 } else {
142 if (!isFirst) Out << ", ";
143 SubPolicy.SuppressSpecifiers = true;
144 }
145
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000146 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000147 }
148}
149
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000150void Decl::dump() {
151 print(llvm::errs());
Douglas Gregor996677c2009-05-30 00:08:05 +0000152}
153
154llvm::raw_ostream& DeclPrinter::Indent() {
155 for (unsigned i = 0; i < Indentation; ++i)
156 Out << " ";
157 return Out;
158}
159
Eli Friedmand73364a2009-05-30 04:20:30 +0000160void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
161 this->Indent();
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000162 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000163 Out << ";\n";
164 Decls.clear();
165
166}
167
Douglas Gregor996677c2009-05-30 00:08:05 +0000168//----------------------------------------------------------------------------
169// Common C declarations
170//----------------------------------------------------------------------------
171
172void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
173 if (Indent)
174 Indentation += Policy.Indentation;
175
Eli Friedmand73364a2009-05-30 04:20:30 +0000176 llvm::SmallVector<Decl*, 2> Decls;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000177 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor996677c2009-05-30 00:08:05 +0000178 D != DEnd; ++D) {
Eli Friedman4bdae722009-05-30 05:03:24 +0000179 if (!Policy.Dump) {
180 // Skip over implicit declarations in pretty-printing mode.
181 if (D->isImplicit()) continue;
Eli Friedman68ff4682009-05-30 06:35:22 +0000182 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
183 // of __builtin_va_list. There should be some other way to check that.
184 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
185 "__builtin_va_list")
186 continue;
Eli Friedman4bdae722009-05-30 05:03:24 +0000187 }
188
Eli Friedmand73364a2009-05-30 04:20:30 +0000189 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
190 // forced to merge the declarations because there's no other way to
191 // refer to the struct in question. This limited merging is safe without
192 // a bunch of other checks because it only merges declarations directly
193 // referring to the tag, not typedefs.
194 //
195 // Check whether the current declaration should be grouped with a previous
196 // unnamed struct.
197 QualType CurDeclType = getDeclType(*D);
198 if (!Decls.empty() && !CurDeclType.isNull()) {
199 QualType BaseType = GetBaseType(CurDeclType);
200 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
201 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
202 Decls.push_back(*D);
203 continue;
204 }
205 }
206
207 // If we have a merged group waiting to be handled, handle it now.
208 if (!Decls.empty())
209 ProcessDeclGroup(Decls);
210
211 // If the current declaration is an unnamed tag type, save it
212 // so we can merge it with the subsequent declaration(s) using it.
213 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
214 Decls.push_back(*D);
215 continue;
216 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000217 this->Indent();
218 Visit(*D);
219
220 // FIXME: Need to be able to tell the DeclPrinter when
221 const char *Terminator = 0;
222 if (isa<FunctionDecl>(*D) &&
223 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
224 Terminator = 0;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000225 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
226 Terminator = 0;
227 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
228 isa<ObjCImplementationDecl>(*D) ||
229 isa<ObjCInterfaceDecl>(*D) ||
230 isa<ObjCProtocolDecl>(*D) ||
231 isa<ObjCCategoryImplDecl>(*D) ||
232 isa<ObjCCategoryDecl>(*D))
Douglas Gregor996677c2009-05-30 00:08:05 +0000233 Terminator = 0;
234 else if (isa<EnumConstantDecl>(*D)) {
235 DeclContext::decl_iterator Next = D;
236 ++Next;
237 if (Next != DEnd)
238 Terminator = ",";
239 } else
240 Terminator = ";";
241
242 if (Terminator)
243 Out << Terminator;
244 Out << "\n";
245 }
246
Eli Friedmand73364a2009-05-30 04:20:30 +0000247 if (!Decls.empty())
248 ProcessDeclGroup(Decls);
249
Douglas Gregor996677c2009-05-30 00:08:05 +0000250 if (Indent)
251 Indentation -= Policy.Indentation;
252}
253
254void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
255 VisitDeclContext(D, false);
256}
257
258void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
259 std::string S = D->getNameAsString();
260 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedmand73364a2009-05-30 04:20:30 +0000261 if (!Policy.SuppressSpecifiers)
262 Out << "typedef ";
263 Out << S;
Douglas Gregor996677c2009-05-30 00:08:05 +0000264}
265
266void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
267 Out << "enum " << D->getNameAsString() << " {\n";
268 VisitDeclContext(D);
269 Indent() << "}";
270}
271
272void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor996677c2009-05-30 00:08:05 +0000273 Out << D->getKindName();
Eli Friedmand73364a2009-05-30 04:20:30 +0000274 if (D->getIdentifier()) {
275 Out << " ";
276 Out << D->getNameAsString();
277 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000278
279 if (D->isDefinition()) {
280 Out << " {\n";
281 VisitDeclContext(D);
282 Indent() << "}";
283 }
284}
285
286void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
287 Out << D->getNameAsString();
288 if (Expr *Init = D->getInitExpr()) {
289 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000290 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000291 }
292}
293
294void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000295 if (!Policy.SuppressSpecifiers) {
296 switch (D->getStorageClass()) {
297 case FunctionDecl::None: break;
298 case FunctionDecl::Extern: Out << "extern "; break;
299 case FunctionDecl::Static: Out << "static "; break;
300 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
301 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000302
Eli Friedmand73364a2009-05-30 04:20:30 +0000303 if (D->isInline()) Out << "inline ";
304 if (D->isVirtualAsWritten()) Out << "virtual ";
305 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000306
Douglas Gregor25133312009-05-30 05:39:39 +0000307 PrintingPolicy SubPolicy(Policy);
308 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor996677c2009-05-30 00:08:05 +0000309 std::string Proto = D->getNameAsString();
310 if (isa<FunctionType>(D->getType().getTypePtr())) {
311 const FunctionType *AFT = D->getType()->getAsFunctionType();
312
313 const FunctionProtoType *FT = 0;
314 if (D->hasWrittenPrototype())
315 FT = dyn_cast<FunctionProtoType>(AFT);
316
317 Proto += "(";
318 if (FT) {
319 llvm::raw_string_ostream POut(Proto);
Douglas Gregor25133312009-05-30 05:39:39 +0000320 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000321 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
322 if (i) POut << ", ";
323 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
324 }
325
326 if (FT->isVariadic()) {
327 if (D->getNumParams()) POut << ", ";
328 POut << "...";
329 }
Eli Friedmand73364a2009-05-30 04:20:30 +0000330 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
331 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
332 if (i)
333 Proto += ", ";
334 Proto += D->getParamDecl(i)->getNameAsString();
335 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000336 }
337
338 Proto += ")";
Mike Stump5963a2d2009-07-27 21:33:40 +0000339 if (D->hasAttr<NoReturnAttr>())
340 Proto += " __attribute((noreturn))";
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000341 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
342 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
343 Proto += " : ";
344 Out << Proto;
345 Proto.clear();
346 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
347 E = CDecl->init_end();
348 B != E; ++B) {
349 CXXBaseOrMemberInitializer * BMInitializer = (*B);
350 if (B != CDecl->init_begin())
351 Out << ", ";
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000352 bool hasArguments = (BMInitializer->arg_begin() !=
353 BMInitializer->arg_end());
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000354 if (BMInitializer->isMemberInitializer()) {
355 FieldDecl *FD = BMInitializer->getMember();
356 Out << FD->getNameAsString();
357 }
Fariborz Jahanian0cdeff12009-07-14 23:41:35 +0000358 else // FIXME. skip dependent types for now.
359 if (const RecordType *RT =
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000360 BMInitializer->getBaseClass()->getAs<RecordType>()) {
Fariborz Jahanian0cdeff12009-07-14 23:41:35 +0000361 const CXXRecordDecl *BaseDecl =
362 cast<CXXRecordDecl>(RT->getDecl());
363 Out << BaseDecl->getNameAsString();
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000364 }
365 if (hasArguments) {
366 Out << "(";
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000367 for (CXXBaseOrMemberInitializer::const_arg_iterator BE =
368 BMInitializer->const_arg_begin(),
369 EE = BMInitializer->const_arg_end(); BE != EE; ++BE) {
370 if (BE != BMInitializer->const_arg_begin())
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000371 Out<< ", ";
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000372 const Expr *Exp = (*BE);
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000373 Exp->printPretty(Out, Context, 0, Policy, Indentation);
374 }
375 Out << ")";
Fariborz Jahanianc0562732009-07-13 23:31:10 +0000376 } else
377 Out << "()";
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000378 }
379 }
380 }
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000381 else if (CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(D)) {
382 if (DDecl->getNumBaseOrMemberDestructions() > 0) {
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000383 // List order of base/member destruction for visualization purposes.
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000384 assert (D->isThisDeclarationADefinition() && "Destructor with dtor-list");
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000385 Proto += "/* : ";
386 for (CXXDestructorDecl::destr_const_iterator *B = DDecl->destr_begin(),
387 *E = DDecl->destr_end();
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000388 B != E; ++B) {
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000389 uintptr_t BaseOrMember = (*B);
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000390 if (B != DDecl->destr_begin())
391 Proto += ", ";
392
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000393 if (DDecl->isMemberToDestroy(BaseOrMember)) {
394 FieldDecl *FD = DDecl->getMemberToDestroy(BaseOrMember);
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000395 Proto += "~";
396 Proto += FD->getNameAsString();
397 }
398 else // FIXME. skip dependent types for now.
399 if (const RecordType *RT =
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000400 DDecl->getAnyBaseClassToDestroy(BaseOrMember)
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000401 ->getAs<RecordType>()) {
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000402 const CXXRecordDecl *BaseDecl =
403 cast<CXXRecordDecl>(RT->getDecl());
404 Proto += "~";
405 Proto += BaseDecl->getNameAsString();
406 }
407 Proto += "()";
408 }
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000409 Proto += " */";
Fariborz Jahanian21e25e72009-07-15 22:34:08 +0000410 }
411 }
Fariborz Jahaniandf0e6782009-07-13 20:18:13 +0000412 else
413 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000414 } else {
415 D->getType().getAsStringInternal(Proto, Policy);
416 }
417
418 Out << Proto;
419
420 if (D->isPure())
421 Out << " = 0";
422 else if (D->isDeleted())
423 Out << " = delete";
424 else if (D->isThisDeclarationADefinition()) {
425 if (!D->hasPrototype() && D->getNumParams()) {
426 // This is a K&R function definition, so we need to print the
427 // parameters.
428 Out << '\n';
Douglas Gregor25133312009-05-30 05:39:39 +0000429 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000430 Indentation += Policy.Indentation;
431 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
432 Indent();
Douglas Gregor25133312009-05-30 05:39:39 +0000433 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor996677c2009-05-30 00:08:05 +0000434 Out << ";\n";
435 }
436 Indentation -= Policy.Indentation;
437 } else
438 Out << ' ';
439
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000440 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000441 Out << '\n';
442 }
443}
444
445void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000446 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000447 Out << "mutable ";
448
449 std::string Name = D->getNameAsString();
450 D->getType().getAsStringInternal(Name, Policy);
451 Out << Name;
452
453 if (D->isBitField()) {
454 Out << " : ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000455 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000456 }
457}
458
459void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000460 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000461 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
462
Eli Friedmand73364a2009-05-30 04:20:30 +0000463 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000464 Out << "__thread ";
465
466 std::string Name = D->getNameAsString();
467 QualType T = D->getType();
468 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
469 T = Parm->getOriginalType();
470 T.getAsStringInternal(Name, Policy);
471 Out << Name;
472 if (D->getInit()) {
473 if (D->hasCXXDirectInitializer())
474 Out << "(";
475 else
476 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000477 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000478 if (D->hasCXXDirectInitializer())
479 Out << ")";
480 }
481}
482
483void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
484 VisitVarDecl(D);
485}
486
Douglas Gregor1d27d692009-05-30 06:31:56 +0000487void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
488 VisitVarDecl(D);
489}
490
Douglas Gregor996677c2009-05-30 00:08:05 +0000491void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
492 Out << "__asm (";
Eli Friedman4bdae722009-05-30 05:03:24 +0000493 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000494 Out << ")";
495}
496
497//----------------------------------------------------------------------------
498// C++ declarations
499//----------------------------------------------------------------------------
Douglas Gregor1d27d692009-05-30 06:31:56 +0000500void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
501 assert(false &&
502 "OverloadedFunctionDecls aren't really decls and are never printed");
503}
504
Douglas Gregor6185ec02009-05-30 06:58:37 +0000505void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
506 Out << "namespace " << D->getNameAsString() << " {\n";
507 VisitDeclContext(D);
508 Indent() << "}";
509}
510
Douglas Gregor1d27d692009-05-30 06:31:56 +0000511void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
512 Out << "using namespace ";
513 if (D->getQualifier())
514 D->getQualifier()->print(Out, Policy);
515 Out << D->getNominatedNamespace()->getNameAsString();
516}
517
Douglas Gregor8d8ddca2009-05-30 06:48:27 +0000518void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
519 Out << "namespace " << D->getNameAsString() << " = ";
520 if (D->getQualifier())
521 D->getQualifier()->print(Out, Policy);
522 Out << D->getAliasedNamespace()->getNameAsString();
523}
524
Douglas Gregor6185ec02009-05-30 06:58:37 +0000525void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
526 Out << D->getKindName();
527 if (D->getIdentifier()) {
528 Out << " ";
529 Out << D->getNameAsString();
530 }
531
532 if (D->isDefinition()) {
533 // Print the base classes
534 if (D->getNumBases()) {
535 Out << " : ";
536 for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
537 BaseEnd = D->bases_end();
538 Base != BaseEnd; ++Base) {
539 if (Base != D->bases_begin())
540 Out << ", ";
541
542 if (Base->isVirtual())
543 Out << "virtual ";
544
545 switch(Base->getAccessSpecifierAsWritten()) {
546 case AS_none: break;
547 case AS_public: Out << "public "; break;
548 case AS_protected: Out << "protected "; break;
549 case AS_private: Out << " private "; break;
550 }
551
552 Out << Base->getType().getAsString(Policy);
553 }
554 }
555
556 // Print the class definition
Douglas Gregor0af57572009-05-31 07:13:39 +0000557 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor6185ec02009-05-30 06:58:37 +0000558 Out << " {\n";
559 VisitDeclContext(D);
560 Indent() << "}";
561 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000562}
563
564void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
565 const char *l;
566 if (D->getLanguage() == LinkageSpecDecl::lang_c)
567 l = "C";
568 else {
569 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
570 "unknown language in linkage specification");
571 l = "C++";
572 }
573
574 Out << "extern \"" << l << "\" ";
575 if (D->hasBraces()) {
576 Out << "{\n";
577 VisitDeclContext(D);
578 Indent() << "}";
579 } else
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000580 Visit(*D->decls_begin());
Douglas Gregor996677c2009-05-30 00:08:05 +0000581}
582
583void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson56b309a2009-06-04 05:37:43 +0000584 Out << "template <";
585
586 TemplateParameterList *Params = D->getTemplateParameters();
587 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
588 if (i != 0)
589 Out << ", ";
590
591 const Decl *Param = Params->getParam(i);
592 if (const TemplateTypeParmDecl *TTP =
593 dyn_cast<TemplateTypeParmDecl>(Param)) {
594
595 QualType ParamType =
596 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
597
598 if (TTP->wasDeclaredWithTypename())
599 Out << "typename ";
600 else
601 Out << "class ";
602
Anders Carlssoneebbf9a2009-06-12 22:23:22 +0000603 if (TTP->isParameterPack())
604 Out << "... ";
605
Anders Carlsson56b309a2009-06-04 05:37:43 +0000606 Out << ParamType.getAsString(Policy);
607
608 if (TTP->hasDefaultArgument()) {
609 Out << " = ";
610 Out << TTP->getDefaultArgument().getAsString(Policy);
611 };
612 } else if (const NonTypeTemplateParmDecl *NTTP =
613 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
614 Out << NTTP->getType().getAsString(Policy);
615
616 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
617 Out << ' ';
618 Out << Name->getName();
619 }
620
621 if (NTTP->hasDefaultArgument()) {
622 Out << " = ";
623 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
624 Indentation);
625 }
626 }
627 }
628
629 Out << "> ";
630
Douglas Gregor996677c2009-05-30 00:08:05 +0000631 Visit(D->getTemplatedDecl());
632}
633
634//----------------------------------------------------------------------------
635// Objective-C declarations
636//----------------------------------------------------------------------------
637
638void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
639 Out << "@class ";
640 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
641 I != E; ++I) {
642 if (I != D->begin()) Out << ", ";
643 Out << (*I)->getNameAsString();
644 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000645}
646
647void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
648 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000649 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000650 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000651 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000652 if (!OMD->getResultType().isNull())
Douglas Gregor6185ec02009-05-30 06:58:37 +0000653 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Douglas Gregor996677c2009-05-30 00:08:05 +0000654
655 std::string name = OMD->getSelector().getAsString();
656 std::string::size_type pos, lastPos = 0;
657 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
658 E = OMD->param_end(); PI != E; ++PI) {
659 // FIXME: selector is missing here!
660 pos = name.find_first_of(":", lastPos);
661 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor6185ec02009-05-30 06:58:37 +0000662 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Douglas Gregor996677c2009-05-30 00:08:05 +0000663 << (*PI)->getNameAsString();
664 lastPos = pos + 1;
665 }
666
667 if (OMD->param_begin() == OMD->param_end())
668 Out << " " << name;
669
670 if (OMD->isVariadic())
671 Out << ", ...";
672
673 if (OMD->getBody()) {
674 Out << ' ';
Eli Friedman4bdae722009-05-30 05:03:24 +0000675 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000676 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000677 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000678}
679
680void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
681 std::string I = OID->getNameAsString();
682 ObjCInterfaceDecl *SID = OID->getSuperClass();
683
684 if (SID)
685 Out << "@implementation " << I << " : " << SID->getNameAsString();
686 else
687 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000688 Out << "\n";
689 VisitDeclContext(OID, false);
690 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000691}
692
693void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
694 std::string I = OID->getNameAsString();
695 ObjCInterfaceDecl *SID = OID->getSuperClass();
696
697 if (SID)
698 Out << "@interface " << I << " : " << SID->getNameAsString();
699 else
700 Out << "@interface " << I;
701
702 // Protocols?
703 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
704 if (!Protocols.empty()) {
705 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
706 E = Protocols.end(); I != E; ++I)
707 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
708 }
709
710 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000711 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000712
713 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000714 Out << "{\n";
715 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000716 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
717 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000718 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000719 << ' ' << (*I)->getNameAsString() << ";\n";
720 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000721 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000722 Out << "}\n";
723 }
724
725 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000726 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000727 // FIXME: implement the rest...
728}
729
730void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
731 Out << "@protocol ";
732 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
733 E = D->protocol_end();
734 I != E; ++I) {
735 if (I != D->protocol_begin()) Out << ", ";
736 Out << (*I)->getNameAsString();
737 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000738}
739
740void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
741 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000742 VisitDeclContext(PID, false);
743 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000744}
745
746void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
747 Out << "@implementation "
748 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000749 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000750
751 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000752 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000753 // FIXME: implement the rest...
754}
755
756void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
757 Out << "@interface "
758 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000759 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000760 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000761 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000762
763 // FIXME: implement the rest...
764}
765
766void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
767 Out << "@compatibility_alias " << AID->getNameAsString()
768 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
769}
770
771/// PrintObjCPropertyDecl - print a property declaration.
772///
773void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
774 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
775 Out << "@required\n";
776 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
777 Out << "@optional\n";
778
779 Out << "@property";
780 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
781 bool first = true;
782 Out << " (";
783 if (PDecl->getPropertyAttributes() &
784 ObjCPropertyDecl::OBJC_PR_readonly) {
785 Out << (first ? ' ' : ',') << "readonly";
786 first = false;
787 }
788
789 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
790 Out << (first ? ' ' : ',') << "getter = "
791 << PDecl->getGetterName().getAsString();
792 first = false;
793 }
794 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
795 Out << (first ? ' ' : ',') << "setter = "
796 << PDecl->getSetterName().getAsString();
797 first = false;
798 }
799
800 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
801 Out << (first ? ' ' : ',') << "assign";
802 first = false;
803 }
804
805 if (PDecl->getPropertyAttributes() &
806 ObjCPropertyDecl::OBJC_PR_readwrite) {
807 Out << (first ? ' ' : ',') << "readwrite";
808 first = false;
809 }
810
811 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
812 Out << (first ? ' ' : ',') << "retain";
813 first = false;
814 }
815
816 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
817 Out << (first ? ' ' : ',') << "copy";
818 first = false;
819 }
820
821 if (PDecl->getPropertyAttributes() &
822 ObjCPropertyDecl::OBJC_PR_nonatomic) {
823 Out << (first ? ' ' : ',') << "nonatomic";
824 first = false;
825 }
826 Out << " )";
827 }
828 Out << ' ' << PDecl->getType().getAsString(Policy)
829 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000830}
831
832void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
833 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000834 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000835 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000836 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000837 Out << PID->getPropertyDecl()->getNameAsString();
838 if (PID->getPropertyIvarDecl())
839 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000840}
Anders Carlssoncd2f2f12009-08-28 19:16:39 +0000841
842void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
843 Out << "using ";
844 D->getTargetNestedNameDecl()->print(Out, Policy);
845 Out << D->getTargetDecl()->getNameAsString();
846}
847
848void DeclPrinter::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
849 Out << "using ";
850 D->getTargetNestedNameSpecifier()->print(Out, Policy);
851 Out << D->getTargetName().getAsString();
852}
853
854