blob: c7ad8d0a7113fcddc28525317b5e47035fe9db33 [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;
93 else if (const PointerType* PTy = BaseType->getAsPointerType())
94 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 += ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000338 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
339 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
340 Proto += " : ";
341 Out << Proto;
342 Proto.clear();
343 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
344 E = CDecl->init_end();
345 B != E; ++B) {
346 CXXBaseOrMemberInitializer * BMInitializer = (*B);
347 if (B != CDecl->init_begin())
348 Out << ", ";
349 bool hasArguments = (BMInitializer->begin() != BMInitializer->end());
350 if (BMInitializer->isMemberInitializer()) {
351 FieldDecl *FD = BMInitializer->getMember();
352 Out << FD->getNameAsString();
353 }
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000354 else // FIXME. skip dependent types for now.
355 if (const RecordType *RT =
356 BMInitializer->getBaseClass()->getAsRecordType()) {
357 const CXXRecordDecl *BaseDecl =
358 cast<CXXRecordDecl>(RT->getDecl());
359 Out << BaseDecl->getNameAsString();
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000360 }
361 if (hasArguments) {
362 Out << "(";
363 for (CXXBaseOrMemberInitializer::arg_const_iterator BE =
364 BMInitializer->begin(), EE = BMInitializer->end();
365 BE != EE; BE++) {
366 if (BE != BMInitializer->begin())
367 Out<< ", ";
368 Expr *Exp = (*BE);
369 Exp->printPretty(Out, Context, 0, Policy, Indentation);
370 }
371 Out << ")";
Fariborz Jahanian939afd82009-07-13 23:31:10 +0000372 } else
373 Out << "()";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000374 }
375 }
376 }
377 else
378 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000379 } else {
380 D->getType().getAsStringInternal(Proto, Policy);
381 }
382
383 Out << Proto;
384
385 if (D->isPure())
386 Out << " = 0";
387 else if (D->isDeleted())
388 Out << " = delete";
389 else if (D->isThisDeclarationADefinition()) {
390 if (!D->hasPrototype() && D->getNumParams()) {
391 // This is a K&R function definition, so we need to print the
392 // parameters.
393 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000394 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000395 Indentation += Policy.Indentation;
396 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
397 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000398 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000399 Out << ";\n";
400 }
401 Indentation -= Policy.Indentation;
402 } else
403 Out << ' ';
404
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000405 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000406 Out << '\n';
407 }
408}
409
410void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000411 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000412 Out << "mutable ";
413
414 std::string Name = D->getNameAsString();
415 D->getType().getAsStringInternal(Name, Policy);
416 Out << Name;
417
418 if (D->isBitField()) {
419 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000420 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000421 }
422}
423
424void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000425 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000426 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
427
Eli Friedman42f42c02009-05-30 04:20:30 +0000428 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000429 Out << "__thread ";
430
431 std::string Name = D->getNameAsString();
432 QualType T = D->getType();
433 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
434 T = Parm->getOriginalType();
435 T.getAsStringInternal(Name, Policy);
436 Out << Name;
437 if (D->getInit()) {
438 if (D->hasCXXDirectInitializer())
439 Out << "(";
440 else
441 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000442 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000443 if (D->hasCXXDirectInitializer())
444 Out << ")";
445 }
446}
447
448void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
449 VisitVarDecl(D);
450}
451
Douglas Gregor8419fa32009-05-30 06:31:56 +0000452void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
453 VisitVarDecl(D);
454}
455
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000456void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
457 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000458 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000459 Out << ")";
460}
461
462//----------------------------------------------------------------------------
463// C++ declarations
464//----------------------------------------------------------------------------
Douglas Gregor8419fa32009-05-30 06:31:56 +0000465void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
466 assert(false &&
467 "OverloadedFunctionDecls aren't really decls and are never printed");
468}
469
Douglas Gregor59e63572009-05-30 06:58:37 +0000470void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
471 Out << "namespace " << D->getNameAsString() << " {\n";
472 VisitDeclContext(D);
473 Indent() << "}";
474}
475
Douglas Gregor8419fa32009-05-30 06:31:56 +0000476void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
477 Out << "using namespace ";
478 if (D->getQualifier())
479 D->getQualifier()->print(Out, Policy);
480 Out << D->getNominatedNamespace()->getNameAsString();
481}
482
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000483void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
484 Out << "namespace " << D->getNameAsString() << " = ";
485 if (D->getQualifier())
486 D->getQualifier()->print(Out, Policy);
487 Out << D->getAliasedNamespace()->getNameAsString();
488}
489
Douglas Gregor59e63572009-05-30 06:58:37 +0000490void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
491 Out << D->getKindName();
492 if (D->getIdentifier()) {
493 Out << " ";
494 Out << D->getNameAsString();
495 }
496
497 if (D->isDefinition()) {
498 // Print the base classes
499 if (D->getNumBases()) {
500 Out << " : ";
501 for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
502 BaseEnd = D->bases_end();
503 Base != BaseEnd; ++Base) {
504 if (Base != D->bases_begin())
505 Out << ", ";
506
507 if (Base->isVirtual())
508 Out << "virtual ";
509
510 switch(Base->getAccessSpecifierAsWritten()) {
511 case AS_none: break;
512 case AS_public: Out << "public "; break;
513 case AS_protected: Out << "protected "; break;
514 case AS_private: Out << " private "; break;
515 }
516
517 Out << Base->getType().getAsString(Policy);
518 }
519 }
520
521 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000522 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000523 Out << " {\n";
524 VisitDeclContext(D);
525 Indent() << "}";
526 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000527}
528
529void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
530 const char *l;
531 if (D->getLanguage() == LinkageSpecDecl::lang_c)
532 l = "C";
533 else {
534 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
535 "unknown language in linkage specification");
536 l = "C++";
537 }
538
539 Out << "extern \"" << l << "\" ";
540 if (D->hasBraces()) {
541 Out << "{\n";
542 VisitDeclContext(D);
543 Indent() << "}";
544 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000545 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000546}
547
548void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000549 Out << "template <";
550
551 TemplateParameterList *Params = D->getTemplateParameters();
552 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
553 if (i != 0)
554 Out << ", ";
555
556 const Decl *Param = Params->getParam(i);
557 if (const TemplateTypeParmDecl *TTP =
558 dyn_cast<TemplateTypeParmDecl>(Param)) {
559
560 QualType ParamType =
561 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
562
563 if (TTP->wasDeclaredWithTypename())
564 Out << "typename ";
565 else
566 Out << "class ";
567
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000568 if (TTP->isParameterPack())
569 Out << "... ";
570
Anders Carlsson0487f662009-06-04 05:37:43 +0000571 Out << ParamType.getAsString(Policy);
572
573 if (TTP->hasDefaultArgument()) {
574 Out << " = ";
575 Out << TTP->getDefaultArgument().getAsString(Policy);
576 };
577 } else if (const NonTypeTemplateParmDecl *NTTP =
578 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
579 Out << NTTP->getType().getAsString(Policy);
580
581 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
582 Out << ' ';
583 Out << Name->getName();
584 }
585
586 if (NTTP->hasDefaultArgument()) {
587 Out << " = ";
588 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
589 Indentation);
590 }
591 }
592 }
593
594 Out << "> ";
595
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000596 Visit(D->getTemplatedDecl());
597}
598
599//----------------------------------------------------------------------------
600// Objective-C declarations
601//----------------------------------------------------------------------------
602
603void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
604 Out << "@class ";
605 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
606 I != E; ++I) {
607 if (I != D->begin()) Out << ", ";
608 Out << (*I)->getNameAsString();
609 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000610}
611
612void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
613 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000614 Out << "- ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000615 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000616 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000617 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000618 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000619
620 std::string name = OMD->getSelector().getAsString();
621 std::string::size_type pos, lastPos = 0;
622 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
623 E = OMD->param_end(); PI != E; ++PI) {
624 // FIXME: selector is missing here!
625 pos = name.find_first_of(":", lastPos);
626 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor59e63572009-05-30 06:58:37 +0000627 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000628 << (*PI)->getNameAsString();
629 lastPos = pos + 1;
630 }
631
632 if (OMD->param_begin() == OMD->param_end())
633 Out << " " << name;
634
635 if (OMD->isVariadic())
636 Out << ", ...";
637
638 if (OMD->getBody()) {
639 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000640 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000641 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000642 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000643}
644
645void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
646 std::string I = OID->getNameAsString();
647 ObjCInterfaceDecl *SID = OID->getSuperClass();
648
649 if (SID)
650 Out << "@implementation " << I << " : " << SID->getNameAsString();
651 else
652 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000653 Out << "\n";
654 VisitDeclContext(OID, false);
655 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000656}
657
658void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
659 std::string I = OID->getNameAsString();
660 ObjCInterfaceDecl *SID = OID->getSuperClass();
661
662 if (SID)
663 Out << "@interface " << I << " : " << SID->getNameAsString();
664 else
665 Out << "@interface " << I;
666
667 // Protocols?
668 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
669 if (!Protocols.empty()) {
670 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
671 E = Protocols.end(); I != E; ++I)
672 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
673 }
674
675 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000676 Out << "> ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000677
678 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000679 Out << "{\n";
680 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000681 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
682 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000683 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000684 << ' ' << (*I)->getNameAsString() << ";\n";
685 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000686 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000687 Out << "}\n";
688 }
689
690 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000691 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000692 // FIXME: implement the rest...
693}
694
695void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
696 Out << "@protocol ";
697 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
698 E = D->protocol_end();
699 I != E; ++I) {
700 if (I != D->protocol_begin()) Out << ", ";
701 Out << (*I)->getNameAsString();
702 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000703}
704
705void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
706 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000707 VisitDeclContext(PID, false);
708 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000709}
710
711void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
712 Out << "@implementation "
713 << PID->getClassInterface()->getNameAsString()
Douglas Gregor64f65002009-05-30 00:56:08 +0000714 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000715
716 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000717 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000718 // FIXME: implement the rest...
719}
720
721void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
722 Out << "@interface "
723 << PID->getClassInterface()->getNameAsString()
Douglas Gregor64f65002009-05-30 00:56:08 +0000724 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000725 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000726 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000727
728 // FIXME: implement the rest...
729}
730
731void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
732 Out << "@compatibility_alias " << AID->getNameAsString()
733 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
734}
735
736/// PrintObjCPropertyDecl - print a property declaration.
737///
738void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
739 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
740 Out << "@required\n";
741 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
742 Out << "@optional\n";
743
744 Out << "@property";
745 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
746 bool first = true;
747 Out << " (";
748 if (PDecl->getPropertyAttributes() &
749 ObjCPropertyDecl::OBJC_PR_readonly) {
750 Out << (first ? ' ' : ',') << "readonly";
751 first = false;
752 }
753
754 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
755 Out << (first ? ' ' : ',') << "getter = "
756 << PDecl->getGetterName().getAsString();
757 first = false;
758 }
759 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
760 Out << (first ? ' ' : ',') << "setter = "
761 << PDecl->getSetterName().getAsString();
762 first = false;
763 }
764
765 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
766 Out << (first ? ' ' : ',') << "assign";
767 first = false;
768 }
769
770 if (PDecl->getPropertyAttributes() &
771 ObjCPropertyDecl::OBJC_PR_readwrite) {
772 Out << (first ? ' ' : ',') << "readwrite";
773 first = false;
774 }
775
776 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
777 Out << (first ? ' ' : ',') << "retain";
778 first = false;
779 }
780
781 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
782 Out << (first ? ' ' : ',') << "copy";
783 first = false;
784 }
785
786 if (PDecl->getPropertyAttributes() &
787 ObjCPropertyDecl::OBJC_PR_nonatomic) {
788 Out << (first ? ' ' : ',') << "nonatomic";
789 first = false;
790 }
791 Out << " )";
792 }
793 Out << ' ' << PDecl->getType().getAsString(Policy)
794 << ' ' << PDecl->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000795}
796
797void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
798 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000799 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000800 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000801 Out << "@dynamic ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000802 Out << PID->getPropertyDecl()->getNameAsString();
803 if (PID->getPropertyIvarDecl())
804 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000805}