blob: 90cd07a6ec521f915bc6e0b70a320c90b807d130 [file] [log] [blame]
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl::dump method, which pretty print the
11// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclVisitor.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/PrettyPrinter.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/Streams.h"
23#include "llvm/Support/Format.h"
24#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26
27namespace {
28 class VISIBILITY_HIDDEN DeclPrinter : public DeclVisitor<DeclPrinter> {
29 llvm::raw_ostream &Out;
30 ASTContext &Context;
31 PrintingPolicy Policy;
32 unsigned Indentation;
33
34 llvm::raw_ostream& Indent();
Eli Friedman42f42c02009-05-30 04:20:30 +000035 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000036
37 public:
38 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
39 const PrintingPolicy &Policy,
40 unsigned Indentation = 0)
41 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
42
43 void VisitDeclContext(DeclContext *DC, bool Indent = true);
44
45 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
46 void VisitTypedefDecl(TypedefDecl *D);
47 void VisitEnumDecl(EnumDecl *D);
48 void VisitRecordDecl(RecordDecl *D);
49 void VisitEnumConstantDecl(EnumConstantDecl *D);
50 void VisitFunctionDecl(FunctionDecl *D);
51 void VisitFieldDecl(FieldDecl *D);
52 void VisitVarDecl(VarDecl *D);
53 void VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000054 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000055 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000056 void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000057 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000058 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000059 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000060 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000061 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
62 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000063 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000064 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000065 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
66 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
67 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
68 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
69 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
70 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
71 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
72 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
73 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
74 };
75}
76
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000077void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) {
78 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000079}
80
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000081void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
82 unsigned Indentation) {
83 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000084 Printer.Visit(this);
85}
86
Eli Friedman42f42c02009-05-30 04:20:30 +000087static QualType GetBaseType(QualType T) {
88 // FIXME: This should be on the Type class!
89 QualType BaseType = T;
90 while (!BaseType->isSpecifierType()) {
91 if (isa<TypedefType>(BaseType))
92 break;
Ted Kremenek6217b802009-07-29 21:53:49 +000093 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +000094 BaseType = PTy->getPointeeType();
95 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
96 BaseType = ATy->getElementType();
97 else if (const FunctionType* FTy = BaseType->getAsFunctionType())
98 BaseType = FTy->getResultType();
Douglas Gregor5068ab62009-07-01 23:58:14 +000099 else if (const VectorType *VTy = BaseType->getAsVectorType())
100 BaseType = VTy->getElementType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000101 else
102 assert(0 && "Unknown declarator!");
103 }
104 return BaseType;
105}
106
107static QualType getDeclType(Decl* D) {
108 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
109 return TDD->getUnderlyingType();
110 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
111 return VD->getType();
112 return QualType();
113}
114
115void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000116 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000117 unsigned Indentation) {
118 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000119 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000120 return;
121 }
122
123 Decl** End = Begin + NumDecls;
124 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
125 if (TD)
126 ++Begin;
127
128 PrintingPolicy SubPolicy(Policy);
129 if (TD && TD->isDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000130 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000131 Out << " ";
132 SubPolicy.SuppressTag = true;
133 }
134
135 bool isFirst = true;
136 for ( ; Begin != End; ++Begin) {
137 if (isFirst) {
138 SubPolicy.SuppressSpecifiers = false;
139 isFirst = false;
140 } else {
141 if (!isFirst) Out << ", ";
142 SubPolicy.SuppressSpecifiers = true;
143 }
144
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000145 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000146 }
147}
148
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000149void Decl::dump() {
150 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000151}
152
153llvm::raw_ostream& DeclPrinter::Indent() {
154 for (unsigned i = 0; i < Indentation; ++i)
155 Out << " ";
156 return Out;
157}
158
Eli Friedman42f42c02009-05-30 04:20:30 +0000159void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
160 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000161 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000162 Out << ";\n";
163 Decls.clear();
164
165}
166
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000167//----------------------------------------------------------------------------
168// Common C declarations
169//----------------------------------------------------------------------------
170
171void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
172 if (Indent)
173 Indentation += Policy.Indentation;
174
Eli Friedman42f42c02009-05-30 04:20:30 +0000175 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000176 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000177 D != DEnd; ++D) {
Eli Friedman48d14a22009-05-30 05:03:24 +0000178 if (!Policy.Dump) {
179 // Skip over implicit declarations in pretty-printing mode.
180 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000181 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
182 // of __builtin_va_list. There should be some other way to check that.
183 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
184 "__builtin_va_list")
185 continue;
Eli Friedman48d14a22009-05-30 05:03:24 +0000186 }
187
Eli Friedman42f42c02009-05-30 04:20:30 +0000188 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
189 // forced to merge the declarations because there's no other way to
190 // refer to the struct in question. This limited merging is safe without
191 // a bunch of other checks because it only merges declarations directly
192 // referring to the tag, not typedefs.
193 //
194 // Check whether the current declaration should be grouped with a previous
195 // unnamed struct.
196 QualType CurDeclType = getDeclType(*D);
197 if (!Decls.empty() && !CurDeclType.isNull()) {
198 QualType BaseType = GetBaseType(CurDeclType);
199 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
200 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
201 Decls.push_back(*D);
202 continue;
203 }
204 }
205
206 // If we have a merged group waiting to be handled, handle it now.
207 if (!Decls.empty())
208 ProcessDeclGroup(Decls);
209
210 // If the current declaration is an unnamed tag type, save it
211 // so we can merge it with the subsequent declaration(s) using it.
212 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
213 Decls.push_back(*D);
214 continue;
215 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000216 this->Indent();
217 Visit(*D);
218
219 // FIXME: Need to be able to tell the DeclPrinter when
220 const char *Terminator = 0;
221 if (isa<FunctionDecl>(*D) &&
222 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
223 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000224 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
225 Terminator = 0;
226 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
227 isa<ObjCImplementationDecl>(*D) ||
228 isa<ObjCInterfaceDecl>(*D) ||
229 isa<ObjCProtocolDecl>(*D) ||
230 isa<ObjCCategoryImplDecl>(*D) ||
231 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000232 Terminator = 0;
233 else if (isa<EnumConstantDecl>(*D)) {
234 DeclContext::decl_iterator Next = D;
235 ++Next;
236 if (Next != DEnd)
237 Terminator = ",";
238 } else
239 Terminator = ";";
240
241 if (Terminator)
242 Out << Terminator;
243 Out << "\n";
244 }
245
Eli Friedman42f42c02009-05-30 04:20:30 +0000246 if (!Decls.empty())
247 ProcessDeclGroup(Decls);
248
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000249 if (Indent)
250 Indentation -= Policy.Indentation;
251}
252
253void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
254 VisitDeclContext(D, false);
255}
256
257void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
258 std::string S = D->getNameAsString();
259 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +0000260 if (!Policy.SuppressSpecifiers)
261 Out << "typedef ";
262 Out << S;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000263}
264
265void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
266 Out << "enum " << D->getNameAsString() << " {\n";
267 VisitDeclContext(D);
268 Indent() << "}";
269}
270
271void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000272 Out << D->getKindName();
Eli Friedman42f42c02009-05-30 04:20:30 +0000273 if (D->getIdentifier()) {
274 Out << " ";
275 Out << D->getNameAsString();
276 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000277
278 if (D->isDefinition()) {
279 Out << " {\n";
280 VisitDeclContext(D);
281 Indent() << "}";
282 }
283}
284
285void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
286 Out << D->getNameAsString();
287 if (Expr *Init = D->getInitExpr()) {
288 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000289 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000290 }
291}
292
293void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000294 if (!Policy.SuppressSpecifiers) {
295 switch (D->getStorageClass()) {
296 case FunctionDecl::None: break;
297 case FunctionDecl::Extern: Out << "extern "; break;
298 case FunctionDecl::Static: Out << "static "; break;
299 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
300 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000301
Eli Friedman42f42c02009-05-30 04:20:30 +0000302 if (D->isInline()) Out << "inline ";
303 if (D->isVirtualAsWritten()) Out << "virtual ";
304 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000305
Douglas Gregor6620a622009-05-30 05:39:39 +0000306 PrintingPolicy SubPolicy(Policy);
307 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000308 std::string Proto = D->getNameAsString();
309 if (isa<FunctionType>(D->getType().getTypePtr())) {
310 const FunctionType *AFT = D->getType()->getAsFunctionType();
311
312 const FunctionProtoType *FT = 0;
313 if (D->hasWrittenPrototype())
314 FT = dyn_cast<FunctionProtoType>(AFT);
315
316 Proto += "(";
317 if (FT) {
318 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000319 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000320 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
321 if (i) POut << ", ";
322 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
323 }
324
325 if (FT->isVariadic()) {
326 if (D->getNumParams()) POut << ", ";
327 POut << "...";
328 }
Eli Friedman42f42c02009-05-30 04:20:30 +0000329 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
330 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
331 if (i)
332 Proto += ", ";
333 Proto += D->getParamDecl(i)->getNameAsString();
334 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000335 }
336
337 Proto += ")";
Mike Stumpfd350b52009-07-27 21:33:40 +0000338 if (D->hasAttr<NoReturnAttr>())
339 Proto += " __attribute((noreturn))";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000340 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
341 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
342 Proto += " : ";
343 Out << Proto;
344 Proto.clear();
345 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
346 E = CDecl->init_end();
347 B != E; ++B) {
348 CXXBaseOrMemberInitializer * BMInitializer = (*B);
349 if (B != CDecl->init_begin())
350 Out << ", ";
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000351 bool hasArguments = (BMInitializer->arg_begin() !=
352 BMInitializer->arg_end());
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000353 if (BMInitializer->isMemberInitializer()) {
354 FieldDecl *FD = BMInitializer->getMember();
355 Out << FD->getNameAsString();
356 }
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000357 else // FIXME. skip dependent types for now.
358 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000359 BMInitializer->getBaseClass()->getAs<RecordType>()) {
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000360 const CXXRecordDecl *BaseDecl =
361 cast<CXXRecordDecl>(RT->getDecl());
362 Out << BaseDecl->getNameAsString();
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000363 }
364 if (hasArguments) {
365 Out << "(";
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000366 for (CXXBaseOrMemberInitializer::const_arg_iterator BE =
367 BMInitializer->const_arg_begin(),
368 EE = BMInitializer->const_arg_end(); BE != EE; ++BE) {
369 if (BE != BMInitializer->const_arg_begin())
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000370 Out<< ", ";
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000371 const Expr *Exp = (*BE);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000372 Exp->printPretty(Out, Context, 0, Policy, Indentation);
373 }
374 Out << ")";
Fariborz Jahanian939afd82009-07-13 23:31:10 +0000375 } else
376 Out << "()";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000377 }
378 }
379 }
Fariborz Jahanian560de452009-07-15 22:34:08 +0000380 else if (CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(D)) {
381 if (DDecl->getNumBaseOrMemberDestructions() > 0) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000382 // List order of base/member destruction for visualization purposes.
Fariborz Jahanian560de452009-07-15 22:34:08 +0000383 assert (D->isThisDeclarationADefinition() && "Destructor with dtor-list");
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000384 Proto += "/* : ";
385 for (CXXDestructorDecl::destr_const_iterator *B = DDecl->destr_begin(),
386 *E = DDecl->destr_end();
Fariborz Jahanian560de452009-07-15 22:34:08 +0000387 B != E; ++B) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000388 uintptr_t BaseOrMember = (*B);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000389 if (B != DDecl->destr_begin())
390 Proto += ", ";
391
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000392 if (DDecl->isMemberToDestroy(BaseOrMember)) {
393 FieldDecl *FD = DDecl->getMemberToDestroy(BaseOrMember);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000394 Proto += "~";
395 Proto += FD->getNameAsString();
396 }
397 else // FIXME. skip dependent types for now.
398 if (const RecordType *RT =
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000399 DDecl->getAnyBaseClassToDestroy(BaseOrMember)
Ted Kremenek6217b802009-07-29 21:53:49 +0000400 ->getAs<RecordType>()) {
Fariborz Jahanian560de452009-07-15 22:34:08 +0000401 const CXXRecordDecl *BaseDecl =
402 cast<CXXRecordDecl>(RT->getDecl());
403 Proto += "~";
404 Proto += BaseDecl->getNameAsString();
405 }
406 Proto += "()";
407 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000408 Proto += " */";
Fariborz Jahanian560de452009-07-15 22:34:08 +0000409 }
410 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000411 else
412 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000413 } else {
414 D->getType().getAsStringInternal(Proto, Policy);
415 }
416
417 Out << Proto;
418
419 if (D->isPure())
420 Out << " = 0";
421 else if (D->isDeleted())
422 Out << " = delete";
423 else if (D->isThisDeclarationADefinition()) {
424 if (!D->hasPrototype() && D->getNumParams()) {
425 // This is a K&R function definition, so we need to print the
426 // parameters.
427 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000428 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000429 Indentation += Policy.Indentation;
430 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
431 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000432 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000433 Out << ";\n";
434 }
435 Indentation -= Policy.Indentation;
436 } else
437 Out << ' ';
438
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000439 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000440 Out << '\n';
441 }
442}
443
444void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000445 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000446 Out << "mutable ";
447
448 std::string Name = D->getNameAsString();
449 D->getType().getAsStringInternal(Name, Policy);
450 Out << Name;
451
452 if (D->isBitField()) {
453 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000454 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000455 }
456}
457
458void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000459 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000460 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
461
Eli Friedman42f42c02009-05-30 04:20:30 +0000462 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000463 Out << "__thread ";
464
465 std::string Name = D->getNameAsString();
466 QualType T = D->getType();
467 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
468 T = Parm->getOriginalType();
469 T.getAsStringInternal(Name, Policy);
470 Out << Name;
471 if (D->getInit()) {
472 if (D->hasCXXDirectInitializer())
473 Out << "(";
474 else
475 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000476 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000477 if (D->hasCXXDirectInitializer())
478 Out << ")";
479 }
480}
481
482void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
483 VisitVarDecl(D);
484}
485
Douglas Gregor8419fa32009-05-30 06:31:56 +0000486void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
487 VisitVarDecl(D);
488}
489
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000490void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
491 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000492 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000493 Out << ")";
494}
495
496//----------------------------------------------------------------------------
497// C++ declarations
498//----------------------------------------------------------------------------
Douglas Gregor8419fa32009-05-30 06:31:56 +0000499void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
500 assert(false &&
501 "OverloadedFunctionDecls aren't really decls and are never printed");
502}
503
Douglas Gregor59e63572009-05-30 06:58:37 +0000504void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
505 Out << "namespace " << D->getNameAsString() << " {\n";
506 VisitDeclContext(D);
507 Indent() << "}";
508}
509
Douglas Gregor8419fa32009-05-30 06:31:56 +0000510void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
511 Out << "using namespace ";
512 if (D->getQualifier())
513 D->getQualifier()->print(Out, Policy);
514 Out << D->getNominatedNamespace()->getNameAsString();
515}
516
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000517void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
518 Out << "namespace " << D->getNameAsString() << " = ";
519 if (D->getQualifier())
520 D->getQualifier()->print(Out, Policy);
521 Out << D->getAliasedNamespace()->getNameAsString();
522}
523
Douglas Gregor59e63572009-05-30 06:58:37 +0000524void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
525 Out << D->getKindName();
526 if (D->getIdentifier()) {
527 Out << " ";
528 Out << D->getNameAsString();
529 }
530
531 if (D->isDefinition()) {
532 // Print the base classes
533 if (D->getNumBases()) {
534 Out << " : ";
535 for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
536 BaseEnd = D->bases_end();
537 Base != BaseEnd; ++Base) {
538 if (Base != D->bases_begin())
539 Out << ", ";
540
541 if (Base->isVirtual())
542 Out << "virtual ";
543
544 switch(Base->getAccessSpecifierAsWritten()) {
545 case AS_none: break;
546 case AS_public: Out << "public "; break;
547 case AS_protected: Out << "protected "; break;
548 case AS_private: Out << " private "; break;
549 }
550
551 Out << Base->getType().getAsString(Policy);
552 }
553 }
554
555 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000556 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000557 Out << " {\n";
558 VisitDeclContext(D);
559 Indent() << "}";
560 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000561}
562
563void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
564 const char *l;
565 if (D->getLanguage() == LinkageSpecDecl::lang_c)
566 l = "C";
567 else {
568 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
569 "unknown language in linkage specification");
570 l = "C++";
571 }
572
573 Out << "extern \"" << l << "\" ";
574 if (D->hasBraces()) {
575 Out << "{\n";
576 VisitDeclContext(D);
577 Indent() << "}";
578 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000579 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000580}
581
582void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000583 Out << "template <";
584
585 TemplateParameterList *Params = D->getTemplateParameters();
586 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
587 if (i != 0)
588 Out << ", ";
589
590 const Decl *Param = Params->getParam(i);
591 if (const TemplateTypeParmDecl *TTP =
592 dyn_cast<TemplateTypeParmDecl>(Param)) {
593
594 QualType ParamType =
595 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
596
597 if (TTP->wasDeclaredWithTypename())
598 Out << "typename ";
599 else
600 Out << "class ";
601
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000602 if (TTP->isParameterPack())
603 Out << "... ";
604
Anders Carlsson0487f662009-06-04 05:37:43 +0000605 Out << ParamType.getAsString(Policy);
606
607 if (TTP->hasDefaultArgument()) {
608 Out << " = ";
609 Out << TTP->getDefaultArgument().getAsString(Policy);
610 };
611 } else if (const NonTypeTemplateParmDecl *NTTP =
612 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
613 Out << NTTP->getType().getAsString(Policy);
614
615 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
616 Out << ' ';
617 Out << Name->getName();
618 }
619
620 if (NTTP->hasDefaultArgument()) {
621 Out << " = ";
622 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
623 Indentation);
624 }
625 }
626 }
627
628 Out << "> ";
629
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000630 Visit(D->getTemplatedDecl());
631}
632
633//----------------------------------------------------------------------------
634// Objective-C declarations
635//----------------------------------------------------------------------------
636
637void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
638 Out << "@class ";
639 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
640 I != E; ++I) {
641 if (I != D->begin()) Out << ", ";
642 Out << (*I)->getNameAsString();
643 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000644}
645
646void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
647 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000648 Out << "- ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000649 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000650 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000651 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000652 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000653
654 std::string name = OMD->getSelector().getAsString();
655 std::string::size_type pos, lastPos = 0;
656 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
657 E = OMD->param_end(); PI != E; ++PI) {
658 // FIXME: selector is missing here!
659 pos = name.find_first_of(":", lastPos);
660 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor59e63572009-05-30 06:58:37 +0000661 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000662 << (*PI)->getNameAsString();
663 lastPos = pos + 1;
664 }
665
666 if (OMD->param_begin() == OMD->param_end())
667 Out << " " << name;
668
669 if (OMD->isVariadic())
670 Out << ", ...";
671
672 if (OMD->getBody()) {
673 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000674 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000675 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000676 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000677}
678
679void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
680 std::string I = OID->getNameAsString();
681 ObjCInterfaceDecl *SID = OID->getSuperClass();
682
683 if (SID)
684 Out << "@implementation " << I << " : " << SID->getNameAsString();
685 else
686 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000687 Out << "\n";
688 VisitDeclContext(OID, false);
689 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000690}
691
692void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
693 std::string I = OID->getNameAsString();
694 ObjCInterfaceDecl *SID = OID->getSuperClass();
695
696 if (SID)
697 Out << "@interface " << I << " : " << SID->getNameAsString();
698 else
699 Out << "@interface " << I;
700
701 // Protocols?
702 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
703 if (!Protocols.empty()) {
704 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
705 E = Protocols.end(); I != E; ++I)
706 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
707 }
708
709 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000710 Out << "> ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000711
712 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000713 Out << "{\n";
714 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000715 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
716 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000717 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000718 << ' ' << (*I)->getNameAsString() << ";\n";
719 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000720 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000721 Out << "}\n";
722 }
723
724 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000725 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000726 // FIXME: implement the rest...
727}
728
729void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
730 Out << "@protocol ";
731 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
732 E = D->protocol_end();
733 I != E; ++I) {
734 if (I != D->protocol_begin()) Out << ", ";
735 Out << (*I)->getNameAsString();
736 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000737}
738
739void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
740 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000741 VisitDeclContext(PID, false);
742 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000743}
744
745void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
746 Out << "@implementation "
747 << PID->getClassInterface()->getNameAsString()
Douglas Gregor64f65002009-05-30 00:56:08 +0000748 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000749
750 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000751 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000752 // FIXME: implement the rest...
753}
754
755void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
756 Out << "@interface "
757 << PID->getClassInterface()->getNameAsString()
Douglas Gregor64f65002009-05-30 00:56:08 +0000758 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000759 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000760 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000761
762 // FIXME: implement the rest...
763}
764
765void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
766 Out << "@compatibility_alias " << AID->getNameAsString()
767 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
768}
769
770/// PrintObjCPropertyDecl - print a property declaration.
771///
772void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
773 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
774 Out << "@required\n";
775 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
776 Out << "@optional\n";
777
778 Out << "@property";
779 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
780 bool first = true;
781 Out << " (";
782 if (PDecl->getPropertyAttributes() &
783 ObjCPropertyDecl::OBJC_PR_readonly) {
784 Out << (first ? ' ' : ',') << "readonly";
785 first = false;
786 }
787
788 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
789 Out << (first ? ' ' : ',') << "getter = "
790 << PDecl->getGetterName().getAsString();
791 first = false;
792 }
793 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
794 Out << (first ? ' ' : ',') << "setter = "
795 << PDecl->getSetterName().getAsString();
796 first = false;
797 }
798
799 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
800 Out << (first ? ' ' : ',') << "assign";
801 first = false;
802 }
803
804 if (PDecl->getPropertyAttributes() &
805 ObjCPropertyDecl::OBJC_PR_readwrite) {
806 Out << (first ? ' ' : ',') << "readwrite";
807 first = false;
808 }
809
810 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
811 Out << (first ? ' ' : ',') << "retain";
812 first = false;
813 }
814
815 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
816 Out << (first ? ' ' : ',') << "copy";
817 first = false;
818 }
819
820 if (PDecl->getPropertyAttributes() &
821 ObjCPropertyDecl::OBJC_PR_nonatomic) {
822 Out << (first ? ' ' : ',') << "nonatomic";
823 first = false;
824 }
825 Out << " )";
826 }
827 Out << ' ' << PDecl->getType().getAsString(Policy)
828 << ' ' << PDecl->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000829}
830
831void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
832 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000833 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000834 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000835 Out << "@dynamic ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000836 Out << PID->getPropertyDecl()->getNameAsString();
837 if (PID->getPropertyIvarDecl())
838 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000839}