blob: d3268300c3ceea9249a169ba3c2f13e41ce7278d [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();
99 else
100 assert(0 && "Unknown declarator!");
101 }
102 return BaseType;
103}
104
105static QualType getDeclType(Decl* D) {
106 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
107 return TDD->getUnderlyingType();
108 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
109 return VD->getType();
110 return QualType();
111}
112
113void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000114 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedmand73364a2009-05-30 04:20:30 +0000115 unsigned Indentation) {
116 if (NumDecls == 1) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000117 (*Begin)->print(Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000118 return;
119 }
120
121 Decl** End = Begin + NumDecls;
122 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
123 if (TD)
124 ++Begin;
125
126 PrintingPolicy SubPolicy(Policy);
127 if (TD && TD->isDefinition()) {
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000128 TD->print(Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000129 Out << " ";
130 SubPolicy.SuppressTag = true;
131 }
132
133 bool isFirst = true;
134 for ( ; Begin != End; ++Begin) {
135 if (isFirst) {
136 SubPolicy.SuppressSpecifiers = false;
137 isFirst = false;
138 } else {
139 if (!isFirst) Out << ", ";
140 SubPolicy.SuppressSpecifiers = true;
141 }
142
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000143 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000144 }
145}
146
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000147void Decl::dump() {
148 print(llvm::errs());
Douglas Gregor996677c2009-05-30 00:08:05 +0000149}
150
151llvm::raw_ostream& DeclPrinter::Indent() {
152 for (unsigned i = 0; i < Indentation; ++i)
153 Out << " ";
154 return Out;
155}
156
Eli Friedmand73364a2009-05-30 04:20:30 +0000157void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
158 this->Indent();
Argiris Kirtzidis9a6fb962009-06-30 02:35:04 +0000159 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedmand73364a2009-05-30 04:20:30 +0000160 Out << ";\n";
161 Decls.clear();
162
163}
164
Douglas Gregor996677c2009-05-30 00:08:05 +0000165//----------------------------------------------------------------------------
166// Common C declarations
167//----------------------------------------------------------------------------
168
169void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
170 if (Indent)
171 Indentation += Policy.Indentation;
172
Eli Friedmand73364a2009-05-30 04:20:30 +0000173 llvm::SmallVector<Decl*, 2> Decls;
Douglas Gregor996677c2009-05-30 00:08:05 +0000174 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
175 DEnd = DC->decls_end(Context);
176 D != DEnd; ++D) {
Eli Friedman4bdae722009-05-30 05:03:24 +0000177 if (!Policy.Dump) {
178 // Skip over implicit declarations in pretty-printing mode.
179 if (D->isImplicit()) continue;
Eli Friedman68ff4682009-05-30 06:35:22 +0000180 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
181 // of __builtin_va_list. There should be some other way to check that.
182 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
183 "__builtin_va_list")
184 continue;
Eli Friedman4bdae722009-05-30 05:03:24 +0000185 }
186
Eli Friedmand73364a2009-05-30 04:20:30 +0000187 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
188 // forced to merge the declarations because there's no other way to
189 // refer to the struct in question. This limited merging is safe without
190 // a bunch of other checks because it only merges declarations directly
191 // referring to the tag, not typedefs.
192 //
193 // Check whether the current declaration should be grouped with a previous
194 // unnamed struct.
195 QualType CurDeclType = getDeclType(*D);
196 if (!Decls.empty() && !CurDeclType.isNull()) {
197 QualType BaseType = GetBaseType(CurDeclType);
198 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
199 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
200 Decls.push_back(*D);
201 continue;
202 }
203 }
204
205 // If we have a merged group waiting to be handled, handle it now.
206 if (!Decls.empty())
207 ProcessDeclGroup(Decls);
208
209 // If the current declaration is an unnamed tag type, save it
210 // so we can merge it with the subsequent declaration(s) using it.
211 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
212 Decls.push_back(*D);
213 continue;
214 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000215 this->Indent();
216 Visit(*D);
217
218 // FIXME: Need to be able to tell the DeclPrinter when
219 const char *Terminator = 0;
220 if (isa<FunctionDecl>(*D) &&
221 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
222 Terminator = 0;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000223 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
224 Terminator = 0;
225 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
226 isa<ObjCImplementationDecl>(*D) ||
227 isa<ObjCInterfaceDecl>(*D) ||
228 isa<ObjCProtocolDecl>(*D) ||
229 isa<ObjCCategoryImplDecl>(*D) ||
230 isa<ObjCCategoryDecl>(*D))
Douglas Gregor996677c2009-05-30 00:08:05 +0000231 Terminator = 0;
232 else if (isa<EnumConstantDecl>(*D)) {
233 DeclContext::decl_iterator Next = D;
234 ++Next;
235 if (Next != DEnd)
236 Terminator = ",";
237 } else
238 Terminator = ";";
239
240 if (Terminator)
241 Out << Terminator;
242 Out << "\n";
243 }
244
Eli Friedmand73364a2009-05-30 04:20:30 +0000245 if (!Decls.empty())
246 ProcessDeclGroup(Decls);
247
Douglas Gregor996677c2009-05-30 00:08:05 +0000248 if (Indent)
249 Indentation -= Policy.Indentation;
250}
251
252void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
253 VisitDeclContext(D, false);
254}
255
256void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
257 std::string S = D->getNameAsString();
258 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedmand73364a2009-05-30 04:20:30 +0000259 if (!Policy.SuppressSpecifiers)
260 Out << "typedef ";
261 Out << S;
Douglas Gregor996677c2009-05-30 00:08:05 +0000262}
263
264void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
265 Out << "enum " << D->getNameAsString() << " {\n";
266 VisitDeclContext(D);
267 Indent() << "}";
268}
269
270void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor996677c2009-05-30 00:08:05 +0000271 Out << D->getKindName();
Eli Friedmand73364a2009-05-30 04:20:30 +0000272 if (D->getIdentifier()) {
273 Out << " ";
274 Out << D->getNameAsString();
275 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000276
277 if (D->isDefinition()) {
278 Out << " {\n";
279 VisitDeclContext(D);
280 Indent() << "}";
281 }
282}
283
284void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
285 Out << D->getNameAsString();
286 if (Expr *Init = D->getInitExpr()) {
287 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000288 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000289 }
290}
291
292void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000293 if (!Policy.SuppressSpecifiers) {
294 switch (D->getStorageClass()) {
295 case FunctionDecl::None: break;
296 case FunctionDecl::Extern: Out << "extern "; break;
297 case FunctionDecl::Static: Out << "static "; break;
298 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
299 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000300
Eli Friedmand73364a2009-05-30 04:20:30 +0000301 if (D->isInline()) Out << "inline ";
302 if (D->isVirtualAsWritten()) Out << "virtual ";
303 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000304
Douglas Gregor25133312009-05-30 05:39:39 +0000305 PrintingPolicy SubPolicy(Policy);
306 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor996677c2009-05-30 00:08:05 +0000307 std::string Proto = D->getNameAsString();
308 if (isa<FunctionType>(D->getType().getTypePtr())) {
309 const FunctionType *AFT = D->getType()->getAsFunctionType();
310
311 const FunctionProtoType *FT = 0;
312 if (D->hasWrittenPrototype())
313 FT = dyn_cast<FunctionProtoType>(AFT);
314
315 Proto += "(";
316 if (FT) {
317 llvm::raw_string_ostream POut(Proto);
Douglas Gregor25133312009-05-30 05:39:39 +0000318 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000319 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
320 if (i) POut << ", ";
321 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
322 }
323
324 if (FT->isVariadic()) {
325 if (D->getNumParams()) POut << ", ";
326 POut << "...";
327 }
Eli Friedmand73364a2009-05-30 04:20:30 +0000328 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
329 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
330 if (i)
331 Proto += ", ";
332 Proto += D->getParamDecl(i)->getNameAsString();
333 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000334 }
335
336 Proto += ")";
337 AFT->getResultType().getAsStringInternal(Proto, Policy);
338 } else {
339 D->getType().getAsStringInternal(Proto, Policy);
340 }
341
342 Out << Proto;
343
344 if (D->isPure())
345 Out << " = 0";
346 else if (D->isDeleted())
347 Out << " = delete";
348 else if (D->isThisDeclarationADefinition()) {
349 if (!D->hasPrototype() && D->getNumParams()) {
350 // This is a K&R function definition, so we need to print the
351 // parameters.
352 Out << '\n';
Douglas Gregor25133312009-05-30 05:39:39 +0000353 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000354 Indentation += Policy.Indentation;
355 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
356 Indent();
Douglas Gregor25133312009-05-30 05:39:39 +0000357 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor996677c2009-05-30 00:08:05 +0000358 Out << ";\n";
359 }
360 Indentation -= Policy.Indentation;
361 } else
362 Out << ' ';
363
Douglas Gregor25133312009-05-30 05:39:39 +0000364 D->getBody(Context)->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000365 Out << '\n';
366 }
367}
368
369void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000370 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000371 Out << "mutable ";
372
373 std::string Name = D->getNameAsString();
374 D->getType().getAsStringInternal(Name, Policy);
375 Out << Name;
376
377 if (D->isBitField()) {
378 Out << " : ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000379 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000380 }
381}
382
383void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000384 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000385 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
386
Eli Friedmand73364a2009-05-30 04:20:30 +0000387 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000388 Out << "__thread ";
389
390 std::string Name = D->getNameAsString();
391 QualType T = D->getType();
392 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
393 T = Parm->getOriginalType();
394 T.getAsStringInternal(Name, Policy);
395 Out << Name;
396 if (D->getInit()) {
397 if (D->hasCXXDirectInitializer())
398 Out << "(";
399 else
400 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000401 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000402 if (D->hasCXXDirectInitializer())
403 Out << ")";
404 }
405}
406
407void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
408 VisitVarDecl(D);
409}
410
Douglas Gregor1d27d692009-05-30 06:31:56 +0000411void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
412 VisitVarDecl(D);
413}
414
Douglas Gregor996677c2009-05-30 00:08:05 +0000415void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
416 Out << "__asm (";
Eli Friedman4bdae722009-05-30 05:03:24 +0000417 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000418 Out << ")";
419}
420
421//----------------------------------------------------------------------------
422// C++ declarations
423//----------------------------------------------------------------------------
Douglas Gregor1d27d692009-05-30 06:31:56 +0000424void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
425 assert(false &&
426 "OverloadedFunctionDecls aren't really decls and are never printed");
427}
428
Douglas Gregor6185ec02009-05-30 06:58:37 +0000429void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
430 Out << "namespace " << D->getNameAsString() << " {\n";
431 VisitDeclContext(D);
432 Indent() << "}";
433}
434
Douglas Gregor1d27d692009-05-30 06:31:56 +0000435void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
436 Out << "using namespace ";
437 if (D->getQualifier())
438 D->getQualifier()->print(Out, Policy);
439 Out << D->getNominatedNamespace()->getNameAsString();
440}
441
Douglas Gregor8d8ddca2009-05-30 06:48:27 +0000442void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
443 Out << "namespace " << D->getNameAsString() << " = ";
444 if (D->getQualifier())
445 D->getQualifier()->print(Out, Policy);
446 Out << D->getAliasedNamespace()->getNameAsString();
447}
448
Douglas Gregor6185ec02009-05-30 06:58:37 +0000449void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
450 Out << D->getKindName();
451 if (D->getIdentifier()) {
452 Out << " ";
453 Out << D->getNameAsString();
454 }
455
456 if (D->isDefinition()) {
457 // Print the base classes
458 if (D->getNumBases()) {
459 Out << " : ";
460 for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
461 BaseEnd = D->bases_end();
462 Base != BaseEnd; ++Base) {
463 if (Base != D->bases_begin())
464 Out << ", ";
465
466 if (Base->isVirtual())
467 Out << "virtual ";
468
469 switch(Base->getAccessSpecifierAsWritten()) {
470 case AS_none: break;
471 case AS_public: Out << "public "; break;
472 case AS_protected: Out << "protected "; break;
473 case AS_private: Out << " private "; break;
474 }
475
476 Out << Base->getType().getAsString(Policy);
477 }
478 }
479
480 // Print the class definition
Douglas Gregor0af57572009-05-31 07:13:39 +0000481 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor6185ec02009-05-30 06:58:37 +0000482 Out << " {\n";
483 VisitDeclContext(D);
484 Indent() << "}";
485 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000486}
487
488void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
489 const char *l;
490 if (D->getLanguage() == LinkageSpecDecl::lang_c)
491 l = "C";
492 else {
493 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
494 "unknown language in linkage specification");
495 l = "C++";
496 }
497
498 Out << "extern \"" << l << "\" ";
499 if (D->hasBraces()) {
500 Out << "{\n";
501 VisitDeclContext(D);
502 Indent() << "}";
503 } else
504 Visit(*D->decls_begin(Context));
505}
506
507void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson56b309a2009-06-04 05:37:43 +0000508 Out << "template <";
509
510 TemplateParameterList *Params = D->getTemplateParameters();
511 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
512 if (i != 0)
513 Out << ", ";
514
515 const Decl *Param = Params->getParam(i);
516 if (const TemplateTypeParmDecl *TTP =
517 dyn_cast<TemplateTypeParmDecl>(Param)) {
518
519 QualType ParamType =
520 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
521
522 if (TTP->wasDeclaredWithTypename())
523 Out << "typename ";
524 else
525 Out << "class ";
526
Anders Carlssoneebbf9a2009-06-12 22:23:22 +0000527 if (TTP->isParameterPack())
528 Out << "... ";
529
Anders Carlsson56b309a2009-06-04 05:37:43 +0000530 Out << ParamType.getAsString(Policy);
531
532 if (TTP->hasDefaultArgument()) {
533 Out << " = ";
534 Out << TTP->getDefaultArgument().getAsString(Policy);
535 };
536 } else if (const NonTypeTemplateParmDecl *NTTP =
537 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
538 Out << NTTP->getType().getAsString(Policy);
539
540 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
541 Out << ' ';
542 Out << Name->getName();
543 }
544
545 if (NTTP->hasDefaultArgument()) {
546 Out << " = ";
547 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
548 Indentation);
549 }
550 }
551 }
552
553 Out << "> ";
554
Douglas Gregor996677c2009-05-30 00:08:05 +0000555 Visit(D->getTemplatedDecl());
556}
557
558//----------------------------------------------------------------------------
559// Objective-C declarations
560//----------------------------------------------------------------------------
561
562void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
563 Out << "@class ";
564 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
565 I != E; ++I) {
566 if (I != D->begin()) Out << ", ";
567 Out << (*I)->getNameAsString();
568 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000569}
570
571void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
572 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000573 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000574 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000575 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000576 if (!OMD->getResultType().isNull())
Douglas Gregor6185ec02009-05-30 06:58:37 +0000577 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Douglas Gregor996677c2009-05-30 00:08:05 +0000578
579 std::string name = OMD->getSelector().getAsString();
580 std::string::size_type pos, lastPos = 0;
581 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
582 E = OMD->param_end(); PI != E; ++PI) {
583 // FIXME: selector is missing here!
584 pos = name.find_first_of(":", lastPos);
585 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor6185ec02009-05-30 06:58:37 +0000586 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Douglas Gregor996677c2009-05-30 00:08:05 +0000587 << (*PI)->getNameAsString();
588 lastPos = pos + 1;
589 }
590
591 if (OMD->param_begin() == OMD->param_end())
592 Out << " " << name;
593
594 if (OMD->isVariadic())
595 Out << ", ...";
596
597 if (OMD->getBody()) {
598 Out << ' ';
Eli Friedman4bdae722009-05-30 05:03:24 +0000599 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000600 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000601 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000602}
603
604void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
605 std::string I = OID->getNameAsString();
606 ObjCInterfaceDecl *SID = OID->getSuperClass();
607
608 if (SID)
609 Out << "@implementation " << I << " : " << SID->getNameAsString();
610 else
611 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000612 Out << "\n";
613 VisitDeclContext(OID, false);
614 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000615}
616
617void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
618 std::string I = OID->getNameAsString();
619 ObjCInterfaceDecl *SID = OID->getSuperClass();
620
621 if (SID)
622 Out << "@interface " << I << " : " << SID->getNameAsString();
623 else
624 Out << "@interface " << I;
625
626 // Protocols?
627 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
628 if (!Protocols.empty()) {
629 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
630 E = Protocols.end(); I != E; ++I)
631 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
632 }
633
634 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000635 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000636
637 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000638 Out << "{\n";
639 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000640 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
641 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000642 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000643 << ' ' << (*I)->getNameAsString() << ";\n";
644 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000645 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000646 Out << "}\n";
647 }
648
649 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000650 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000651 // FIXME: implement the rest...
652}
653
654void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
655 Out << "@protocol ";
656 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
657 E = D->protocol_end();
658 I != E; ++I) {
659 if (I != D->protocol_begin()) Out << ", ";
660 Out << (*I)->getNameAsString();
661 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000662}
663
664void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
665 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000666 VisitDeclContext(PID, false);
667 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000668}
669
670void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
671 Out << "@implementation "
672 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000673 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000674
675 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000676 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000677 // FIXME: implement the rest...
678}
679
680void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
681 Out << "@interface "
682 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000683 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000684 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000685 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000686
687 // FIXME: implement the rest...
688}
689
690void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
691 Out << "@compatibility_alias " << AID->getNameAsString()
692 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
693}
694
695/// PrintObjCPropertyDecl - print a property declaration.
696///
697void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
698 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
699 Out << "@required\n";
700 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
701 Out << "@optional\n";
702
703 Out << "@property";
704 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
705 bool first = true;
706 Out << " (";
707 if (PDecl->getPropertyAttributes() &
708 ObjCPropertyDecl::OBJC_PR_readonly) {
709 Out << (first ? ' ' : ',') << "readonly";
710 first = false;
711 }
712
713 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
714 Out << (first ? ' ' : ',') << "getter = "
715 << PDecl->getGetterName().getAsString();
716 first = false;
717 }
718 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
719 Out << (first ? ' ' : ',') << "setter = "
720 << PDecl->getSetterName().getAsString();
721 first = false;
722 }
723
724 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
725 Out << (first ? ' ' : ',') << "assign";
726 first = false;
727 }
728
729 if (PDecl->getPropertyAttributes() &
730 ObjCPropertyDecl::OBJC_PR_readwrite) {
731 Out << (first ? ' ' : ',') << "readwrite";
732 first = false;
733 }
734
735 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
736 Out << (first ? ' ' : ',') << "retain";
737 first = false;
738 }
739
740 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
741 Out << (first ? ' ' : ',') << "copy";
742 first = false;
743 }
744
745 if (PDecl->getPropertyAttributes() &
746 ObjCPropertyDecl::OBJC_PR_nonatomic) {
747 Out << (first ? ' ' : ',') << "nonatomic";
748 first = false;
749 }
750 Out << " )";
751 }
752 Out << ' ' << PDecl->getType().getAsString(Policy)
753 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000754}
755
756void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
757 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000758 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000759 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000760 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000761 Out << PID->getPropertyDecl()->getNameAsString();
762 if (PID->getPropertyIvarDecl())
763 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000764}