blob: 12e89cd80d1978bdd9d1742cfb6b27c7b9a1637d [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"
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 Friedmand73364a2009-05-30 04:20:30 +000035 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor996677c2009-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 Gregor1d27d692009-05-30 06:31:56 +000054 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000055 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000056 void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D);
Douglas Gregor6185ec02009-05-30 06:58:37 +000057 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000058 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor8d8ddca2009-05-30 06:48:27 +000059 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor6185ec02009-05-30 06:58:37 +000060 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000061 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
62 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000063 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000064 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor996677c2009-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
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +000077void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) {
78 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +000079}
80
Argiris Kirtzidis9a6fb962009-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 Gregor996677c2009-05-30 00:08:05 +000084 Printer.Visit(this);
85}
86
Eli Friedmand73364a2009-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 Gregor6b9f4262009-07-01 23:58:14 +000099 else if (const VectorType *VTy = BaseType->getAsVectorType())
100 BaseType = VTy->getElementType();
Eli Friedmand73364a2009-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,
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000116 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedmand73364a2009-05-30 04:20:30 +0000117 unsigned Indentation) {
118 if (NumDecls == 1) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000119 (*Begin)->print(Out, Policy, Indentation);
Eli Friedmand73364a2009-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()) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000130 TD->print(Out, Policy, Indentation);
Eli Friedmand73364a2009-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
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000145 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000146 }
147}
148
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000149void Decl::dump() {
150 print(llvm::errs());
Douglas Gregor996677c2009-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 Friedmand73364a2009-05-30 04:20:30 +0000159void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
160 this->Indent();
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000161 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000162 Out << ";\n";
163 Decls.clear();
164
165}
166
Douglas Gregor996677c2009-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 Friedmand73364a2009-05-30 04:20:30 +0000175 llvm::SmallVector<Decl*, 2> Decls;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000176 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor996677c2009-05-30 00:08:05 +0000177 D != DEnd; ++D) {
Eli Friedman4bdae722009-05-30 05:03:24 +0000178 if (!Policy.Dump) {
179 // Skip over implicit declarations in pretty-printing mode.
180 if (D->isImplicit()) continue;
Eli Friedman68ff4682009-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 Friedman4bdae722009-05-30 05:03:24 +0000186 }
187
Eli Friedmand73364a2009-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 Gregor996677c2009-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 Gregor1fa5d0d2009-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 Gregor996677c2009-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 Friedmand73364a2009-05-30 04:20:30 +0000246 if (!Decls.empty())
247 ProcessDeclGroup(Decls);
248
Douglas Gregor996677c2009-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 Friedmand73364a2009-05-30 04:20:30 +0000260 if (!Policy.SuppressSpecifiers)
261 Out << "typedef ";
262 Out << S;
Douglas Gregor996677c2009-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 Gregor996677c2009-05-30 00:08:05 +0000272 Out << D->getKindName();
Eli Friedmand73364a2009-05-30 04:20:30 +0000273 if (D->getIdentifier()) {
274 Out << " ";
275 Out << D->getNameAsString();
276 }
Douglas Gregor996677c2009-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 Friedman4bdae722009-05-30 05:03:24 +0000289 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000290 }
291}
292
293void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedmand73364a2009-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 Gregor996677c2009-05-30 00:08:05 +0000301
Eli Friedmand73364a2009-05-30 04:20:30 +0000302 if (D->isInline()) Out << "inline ";
303 if (D->isVirtualAsWritten()) Out << "virtual ";
304 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000305
Douglas Gregor25133312009-05-30 05:39:39 +0000306 PrintingPolicy SubPolicy(Policy);
307 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor996677c2009-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 Gregor25133312009-05-30 05:39:39 +0000319 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-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 Friedmand73364a2009-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 Gregor996677c2009-05-30 00:08:05 +0000335 }
336
337 Proto += ")";
338 AFT->getResultType().getAsStringInternal(Proto, Policy);
339 } else {
340 D->getType().getAsStringInternal(Proto, Policy);
341 }
342
343 Out << Proto;
344
345 if (D->isPure())
346 Out << " = 0";
347 else if (D->isDeleted())
348 Out << " = delete";
349 else if (D->isThisDeclarationADefinition()) {
350 if (!D->hasPrototype() && D->getNumParams()) {
351 // This is a K&R function definition, so we need to print the
352 // parameters.
353 Out << '\n';
Douglas Gregor25133312009-05-30 05:39:39 +0000354 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000355 Indentation += Policy.Indentation;
356 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
357 Indent();
Douglas Gregor25133312009-05-30 05:39:39 +0000358 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor996677c2009-05-30 00:08:05 +0000359 Out << ";\n";
360 }
361 Indentation -= Policy.Indentation;
362 } else
363 Out << ' ';
364
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000365 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000366 Out << '\n';
367 }
368}
369
370void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000371 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000372 Out << "mutable ";
373
374 std::string Name = D->getNameAsString();
375 D->getType().getAsStringInternal(Name, Policy);
376 Out << Name;
377
378 if (D->isBitField()) {
379 Out << " : ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000380 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000381 }
382}
383
384void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000385 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000386 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
387
Eli Friedmand73364a2009-05-30 04:20:30 +0000388 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000389 Out << "__thread ";
390
391 std::string Name = D->getNameAsString();
392 QualType T = D->getType();
393 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
394 T = Parm->getOriginalType();
395 T.getAsStringInternal(Name, Policy);
396 Out << Name;
397 if (D->getInit()) {
398 if (D->hasCXXDirectInitializer())
399 Out << "(";
400 else
401 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000402 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000403 if (D->hasCXXDirectInitializer())
404 Out << ")";
405 }
406}
407
408void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
409 VisitVarDecl(D);
410}
411
Douglas Gregor1d27d692009-05-30 06:31:56 +0000412void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
413 VisitVarDecl(D);
414}
415
Douglas Gregor996677c2009-05-30 00:08:05 +0000416void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
417 Out << "__asm (";
Eli Friedman4bdae722009-05-30 05:03:24 +0000418 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000419 Out << ")";
420}
421
422//----------------------------------------------------------------------------
423// C++ declarations
424//----------------------------------------------------------------------------
Douglas Gregor1d27d692009-05-30 06:31:56 +0000425void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
426 assert(false &&
427 "OverloadedFunctionDecls aren't really decls and are never printed");
428}
429
Douglas Gregor6185ec02009-05-30 06:58:37 +0000430void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
431 Out << "namespace " << D->getNameAsString() << " {\n";
432 VisitDeclContext(D);
433 Indent() << "}";
434}
435
Douglas Gregor1d27d692009-05-30 06:31:56 +0000436void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
437 Out << "using namespace ";
438 if (D->getQualifier())
439 D->getQualifier()->print(Out, Policy);
440 Out << D->getNominatedNamespace()->getNameAsString();
441}
442
Douglas Gregor8d8ddca2009-05-30 06:48:27 +0000443void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
444 Out << "namespace " << D->getNameAsString() << " = ";
445 if (D->getQualifier())
446 D->getQualifier()->print(Out, Policy);
447 Out << D->getAliasedNamespace()->getNameAsString();
448}
449
Douglas Gregor6185ec02009-05-30 06:58:37 +0000450void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
451 Out << D->getKindName();
452 if (D->getIdentifier()) {
453 Out << " ";
454 Out << D->getNameAsString();
455 }
456
457 if (D->isDefinition()) {
458 // Print the base classes
459 if (D->getNumBases()) {
460 Out << " : ";
461 for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
462 BaseEnd = D->bases_end();
463 Base != BaseEnd; ++Base) {
464 if (Base != D->bases_begin())
465 Out << ", ";
466
467 if (Base->isVirtual())
468 Out << "virtual ";
469
470 switch(Base->getAccessSpecifierAsWritten()) {
471 case AS_none: break;
472 case AS_public: Out << "public "; break;
473 case AS_protected: Out << "protected "; break;
474 case AS_private: Out << " private "; break;
475 }
476
477 Out << Base->getType().getAsString(Policy);
478 }
479 }
480
481 // Print the class definition
Douglas Gregor0af57572009-05-31 07:13:39 +0000482 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor6185ec02009-05-30 06:58:37 +0000483 Out << " {\n";
484 VisitDeclContext(D);
485 Indent() << "}";
486 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000487}
488
489void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
490 const char *l;
491 if (D->getLanguage() == LinkageSpecDecl::lang_c)
492 l = "C";
493 else {
494 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
495 "unknown language in linkage specification");
496 l = "C++";
497 }
498
499 Out << "extern \"" << l << "\" ";
500 if (D->hasBraces()) {
501 Out << "{\n";
502 VisitDeclContext(D);
503 Indent() << "}";
504 } else
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000505 Visit(*D->decls_begin());
Douglas Gregor996677c2009-05-30 00:08:05 +0000506}
507
508void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson56b309a2009-06-04 05:37:43 +0000509 Out << "template <";
510
511 TemplateParameterList *Params = D->getTemplateParameters();
512 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
513 if (i != 0)
514 Out << ", ";
515
516 const Decl *Param = Params->getParam(i);
517 if (const TemplateTypeParmDecl *TTP =
518 dyn_cast<TemplateTypeParmDecl>(Param)) {
519
520 QualType ParamType =
521 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
522
523 if (TTP->wasDeclaredWithTypename())
524 Out << "typename ";
525 else
526 Out << "class ";
527
Anders Carlssoneebbf9a2009-06-12 22:23:22 +0000528 if (TTP->isParameterPack())
529 Out << "... ";
530
Anders Carlsson56b309a2009-06-04 05:37:43 +0000531 Out << ParamType.getAsString(Policy);
532
533 if (TTP->hasDefaultArgument()) {
534 Out << " = ";
535 Out << TTP->getDefaultArgument().getAsString(Policy);
536 };
537 } else if (const NonTypeTemplateParmDecl *NTTP =
538 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
539 Out << NTTP->getType().getAsString(Policy);
540
541 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
542 Out << ' ';
543 Out << Name->getName();
544 }
545
546 if (NTTP->hasDefaultArgument()) {
547 Out << " = ";
548 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
549 Indentation);
550 }
551 }
552 }
553
554 Out << "> ";
555
Douglas Gregor996677c2009-05-30 00:08:05 +0000556 Visit(D->getTemplatedDecl());
557}
558
559//----------------------------------------------------------------------------
560// Objective-C declarations
561//----------------------------------------------------------------------------
562
563void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
564 Out << "@class ";
565 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
566 I != E; ++I) {
567 if (I != D->begin()) Out << ", ";
568 Out << (*I)->getNameAsString();
569 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000570}
571
572void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
573 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000574 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000575 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000576 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000577 if (!OMD->getResultType().isNull())
Douglas Gregor6185ec02009-05-30 06:58:37 +0000578 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Douglas Gregor996677c2009-05-30 00:08:05 +0000579
580 std::string name = OMD->getSelector().getAsString();
581 std::string::size_type pos, lastPos = 0;
582 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
583 E = OMD->param_end(); PI != E; ++PI) {
584 // FIXME: selector is missing here!
585 pos = name.find_first_of(":", lastPos);
586 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor6185ec02009-05-30 06:58:37 +0000587 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Douglas Gregor996677c2009-05-30 00:08:05 +0000588 << (*PI)->getNameAsString();
589 lastPos = pos + 1;
590 }
591
592 if (OMD->param_begin() == OMD->param_end())
593 Out << " " << name;
594
595 if (OMD->isVariadic())
596 Out << ", ...";
597
598 if (OMD->getBody()) {
599 Out << ' ';
Eli Friedman4bdae722009-05-30 05:03:24 +0000600 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000601 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000602 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000603}
604
605void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
606 std::string I = OID->getNameAsString();
607 ObjCInterfaceDecl *SID = OID->getSuperClass();
608
609 if (SID)
610 Out << "@implementation " << I << " : " << SID->getNameAsString();
611 else
612 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000613 Out << "\n";
614 VisitDeclContext(OID, false);
615 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000616}
617
618void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
619 std::string I = OID->getNameAsString();
620 ObjCInterfaceDecl *SID = OID->getSuperClass();
621
622 if (SID)
623 Out << "@interface " << I << " : " << SID->getNameAsString();
624 else
625 Out << "@interface " << I;
626
627 // Protocols?
628 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
629 if (!Protocols.empty()) {
630 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
631 E = Protocols.end(); I != E; ++I)
632 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
633 }
634
635 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000636 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000637
638 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000639 Out << "{\n";
640 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000641 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
642 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000643 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000644 << ' ' << (*I)->getNameAsString() << ";\n";
645 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000646 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000647 Out << "}\n";
648 }
649
650 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000651 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000652 // FIXME: implement the rest...
653}
654
655void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
656 Out << "@protocol ";
657 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
658 E = D->protocol_end();
659 I != E; ++I) {
660 if (I != D->protocol_begin()) Out << ", ";
661 Out << (*I)->getNameAsString();
662 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000663}
664
665void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
666 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000667 VisitDeclContext(PID, false);
668 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000669}
670
671void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
672 Out << "@implementation "
673 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000674 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000675
676 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000677 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000678 // FIXME: implement the rest...
679}
680
681void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
682 Out << "@interface "
683 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000684 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000685 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000686 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000687
688 // FIXME: implement the rest...
689}
690
691void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
692 Out << "@compatibility_alias " << AID->getNameAsString()
693 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
694}
695
696/// PrintObjCPropertyDecl - print a property declaration.
697///
698void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
699 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
700 Out << "@required\n";
701 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
702 Out << "@optional\n";
703
704 Out << "@property";
705 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
706 bool first = true;
707 Out << " (";
708 if (PDecl->getPropertyAttributes() &
709 ObjCPropertyDecl::OBJC_PR_readonly) {
710 Out << (first ? ' ' : ',') << "readonly";
711 first = false;
712 }
713
714 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
715 Out << (first ? ' ' : ',') << "getter = "
716 << PDecl->getGetterName().getAsString();
717 first = false;
718 }
719 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
720 Out << (first ? ' ' : ',') << "setter = "
721 << PDecl->getSetterName().getAsString();
722 first = false;
723 }
724
725 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
726 Out << (first ? ' ' : ',') << "assign";
727 first = false;
728 }
729
730 if (PDecl->getPropertyAttributes() &
731 ObjCPropertyDecl::OBJC_PR_readwrite) {
732 Out << (first ? ' ' : ',') << "readwrite";
733 first = false;
734 }
735
736 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
737 Out << (first ? ' ' : ',') << "retain";
738 first = false;
739 }
740
741 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
742 Out << (first ? ' ' : ',') << "copy";
743 first = false;
744 }
745
746 if (PDecl->getPropertyAttributes() &
747 ObjCPropertyDecl::OBJC_PR_nonatomic) {
748 Out << (first ? ' ' : ',') << "nonatomic";
749 first = false;
750 }
751 Out << " )";
752 }
753 Out << ' ' << PDecl->getType().getAsString(Policy)
754 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000755}
756
757void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
758 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000759 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000760 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000761 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000762 Out << PID->getPropertyDecl()->getNameAsString();
763 if (PID->getPropertyIvarDecl())
764 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000765}