blob: 6412f750ae9713f201fe60e9a1c8e454ae874adf [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);
54 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
55 void VisitNamespaceDecl(NamespaceDecl *D);
56 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
57 void VisitTemplateDecl(TemplateDecl *D);
58 void VisitObjCClassDecl(ObjCClassDecl *D);
59 void VisitObjCMethodDecl(ObjCMethodDecl *D);
60 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
61 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
62 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
63 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
64 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
65 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
66 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
67 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
68 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
69 };
70}
71
72void Decl::print(llvm::raw_ostream &Out, ASTContext &Context,
73 unsigned Indentation) {
74 print(Out, Context, Context.PrintingPolicy, Indentation);
75}
76
77void Decl::print(llvm::raw_ostream &Out, ASTContext &Context,
78 const PrintingPolicy &Policy, unsigned Indentation) {
79 DeclPrinter Printer(Out, Context, Policy, Indentation);
80 Printer.Visit(this);
81}
82
Eli Friedmand73364a2009-05-30 04:20:30 +000083static QualType GetBaseType(QualType T) {
84 // FIXME: This should be on the Type class!
85 QualType BaseType = T;
86 while (!BaseType->isSpecifierType()) {
87 if (isa<TypedefType>(BaseType))
88 break;
89 else if (const PointerType* PTy = BaseType->getAsPointerType())
90 BaseType = PTy->getPointeeType();
91 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
92 BaseType = ATy->getElementType();
93 else if (const FunctionType* FTy = BaseType->getAsFunctionType())
94 BaseType = FTy->getResultType();
95 else
96 assert(0 && "Unknown declarator!");
97 }
98 return BaseType;
99}
100
101static QualType getDeclType(Decl* D) {
102 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
103 return TDD->getUnderlyingType();
104 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
105 return VD->getType();
106 return QualType();
107}
108
109void Decl::printGroup(Decl** Begin, unsigned NumDecls,
110 llvm::raw_ostream &Out, ASTContext &Context,
111 const PrintingPolicy &Policy,
112 unsigned Indentation) {
113 if (NumDecls == 1) {
114 (*Begin)->print(Out, Context, Policy, Indentation);
115 return;
116 }
117
118 Decl** End = Begin + NumDecls;
119 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
120 if (TD)
121 ++Begin;
122
123 PrintingPolicy SubPolicy(Policy);
124 if (TD && TD->isDefinition()) {
125 TD->print(Out, Context, Policy, Indentation);
126 Out << " ";
127 SubPolicy.SuppressTag = true;
128 }
129
130 bool isFirst = true;
131 for ( ; Begin != End; ++Begin) {
132 if (isFirst) {
133 SubPolicy.SuppressSpecifiers = false;
134 isFirst = false;
135 } else {
136 if (!isFirst) Out << ", ";
137 SubPolicy.SuppressSpecifiers = true;
138 }
139
140 (*Begin)->print(Out, Context, SubPolicy, Indentation);
141 }
142}
143
Douglas Gregor996677c2009-05-30 00:08:05 +0000144void Decl::dump(ASTContext &Context) {
145 print(llvm::errs(), Context);
146}
147
148llvm::raw_ostream& DeclPrinter::Indent() {
149 for (unsigned i = 0; i < Indentation; ++i)
150 Out << " ";
151 return Out;
152}
153
Eli Friedmand73364a2009-05-30 04:20:30 +0000154void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
155 this->Indent();
156 Decl::printGroup(Decls.data(), Decls.size(), Out, Context,
157 Policy, Indentation);
158 Out << ";\n";
159 Decls.clear();
160
161}
162
Douglas Gregor996677c2009-05-30 00:08:05 +0000163//----------------------------------------------------------------------------
164// Common C declarations
165//----------------------------------------------------------------------------
166
167void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
168 if (Indent)
169 Indentation += Policy.Indentation;
170
Eli Friedmand73364a2009-05-30 04:20:30 +0000171 llvm::SmallVector<Decl*, 2> Decls;
Douglas Gregor996677c2009-05-30 00:08:05 +0000172 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
173 DEnd = DC->decls_end(Context);
174 D != DEnd; ++D) {
Eli Friedman4bdae722009-05-30 05:03:24 +0000175 if (!Policy.Dump) {
176 // Skip over implicit declarations in pretty-printing mode.
177 if (D->isImplicit()) continue;
178 }
179
Eli Friedmand73364a2009-05-30 04:20:30 +0000180 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
181 // forced to merge the declarations because there's no other way to
182 // refer to the struct in question. This limited merging is safe without
183 // a bunch of other checks because it only merges declarations directly
184 // referring to the tag, not typedefs.
185 //
186 // Check whether the current declaration should be grouped with a previous
187 // unnamed struct.
188 QualType CurDeclType = getDeclType(*D);
189 if (!Decls.empty() && !CurDeclType.isNull()) {
190 QualType BaseType = GetBaseType(CurDeclType);
191 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
192 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
193 Decls.push_back(*D);
194 continue;
195 }
196 }
197
198 // If we have a merged group waiting to be handled, handle it now.
199 if (!Decls.empty())
200 ProcessDeclGroup(Decls);
201
202 // If the current declaration is an unnamed tag type, save it
203 // so we can merge it with the subsequent declaration(s) using it.
204 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
205 Decls.push_back(*D);
206 continue;
207 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000208 this->Indent();
209 Visit(*D);
210
211 // FIXME: Need to be able to tell the DeclPrinter when
212 const char *Terminator = 0;
213 if (isa<FunctionDecl>(*D) &&
214 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
215 Terminator = 0;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000216 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
217 Terminator = 0;
218 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
219 isa<ObjCImplementationDecl>(*D) ||
220 isa<ObjCInterfaceDecl>(*D) ||
221 isa<ObjCProtocolDecl>(*D) ||
222 isa<ObjCCategoryImplDecl>(*D) ||
223 isa<ObjCCategoryDecl>(*D))
Douglas Gregor996677c2009-05-30 00:08:05 +0000224 Terminator = 0;
225 else if (isa<EnumConstantDecl>(*D)) {
226 DeclContext::decl_iterator Next = D;
227 ++Next;
228 if (Next != DEnd)
229 Terminator = ",";
230 } else
231 Terminator = ";";
232
233 if (Terminator)
234 Out << Terminator;
235 Out << "\n";
236 }
237
Eli Friedmand73364a2009-05-30 04:20:30 +0000238 if (!Decls.empty())
239 ProcessDeclGroup(Decls);
240
Douglas Gregor996677c2009-05-30 00:08:05 +0000241 if (Indent)
242 Indentation -= Policy.Indentation;
243}
244
245void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
246 VisitDeclContext(D, false);
247}
248
249void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
250 std::string S = D->getNameAsString();
251 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedmand73364a2009-05-30 04:20:30 +0000252 if (!Policy.SuppressSpecifiers)
253 Out << "typedef ";
254 Out << S;
Douglas Gregor996677c2009-05-30 00:08:05 +0000255}
256
257void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
258 Out << "enum " << D->getNameAsString() << " {\n";
259 VisitDeclContext(D);
260 Indent() << "}";
261}
262
263void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
264 // print a free standing tag decl (e.g. "struct x;").
265 Out << D->getKindName();
Eli Friedmand73364a2009-05-30 04:20:30 +0000266 if (D->getIdentifier()) {
267 Out << " ";
268 Out << D->getNameAsString();
269 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000270
271 if (D->isDefinition()) {
272 Out << " {\n";
273 VisitDeclContext(D);
274 Indent() << "}";
275 }
276}
277
278void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
279 Out << D->getNameAsString();
280 if (Expr *Init = D->getInitExpr()) {
281 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000282 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000283 }
284}
285
286void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000287 if (!Policy.SuppressSpecifiers) {
288 switch (D->getStorageClass()) {
289 case FunctionDecl::None: break;
290 case FunctionDecl::Extern: Out << "extern "; break;
291 case FunctionDecl::Static: Out << "static "; break;
292 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
293 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000294
Eli Friedmand73364a2009-05-30 04:20:30 +0000295 if (D->isInline()) Out << "inline ";
296 if (D->isVirtualAsWritten()) Out << "virtual ";
297 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000298
Douglas Gregor25133312009-05-30 05:39:39 +0000299 PrintingPolicy SubPolicy(Policy);
300 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor996677c2009-05-30 00:08:05 +0000301 std::string Proto = D->getNameAsString();
302 if (isa<FunctionType>(D->getType().getTypePtr())) {
303 const FunctionType *AFT = D->getType()->getAsFunctionType();
304
305 const FunctionProtoType *FT = 0;
306 if (D->hasWrittenPrototype())
307 FT = dyn_cast<FunctionProtoType>(AFT);
308
309 Proto += "(";
310 if (FT) {
311 llvm::raw_string_ostream POut(Proto);
Douglas Gregor25133312009-05-30 05:39:39 +0000312 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000313 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
314 if (i) POut << ", ";
315 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
316 }
317
318 if (FT->isVariadic()) {
319 if (D->getNumParams()) POut << ", ";
320 POut << "...";
321 }
Eli Friedmand73364a2009-05-30 04:20:30 +0000322 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
323 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
324 if (i)
325 Proto += ", ";
326 Proto += D->getParamDecl(i)->getNameAsString();
327 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000328 }
329
330 Proto += ")";
331 AFT->getResultType().getAsStringInternal(Proto, Policy);
332 } else {
333 D->getType().getAsStringInternal(Proto, Policy);
334 }
335
336 Out << Proto;
337
338 if (D->isPure())
339 Out << " = 0";
340 else if (D->isDeleted())
341 Out << " = delete";
342 else if (D->isThisDeclarationADefinition()) {
343 if (!D->hasPrototype() && D->getNumParams()) {
344 // This is a K&R function definition, so we need to print the
345 // parameters.
346 Out << '\n';
Douglas Gregor25133312009-05-30 05:39:39 +0000347 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000348 Indentation += Policy.Indentation;
349 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
350 Indent();
Douglas Gregor25133312009-05-30 05:39:39 +0000351 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor996677c2009-05-30 00:08:05 +0000352 Out << ";\n";
353 }
354 Indentation -= Policy.Indentation;
355 } else
356 Out << ' ';
357
Douglas Gregor25133312009-05-30 05:39:39 +0000358 D->getBody(Context)->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000359 Out << '\n';
360 }
361}
362
363void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000364 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000365 Out << "mutable ";
366
367 std::string Name = D->getNameAsString();
368 D->getType().getAsStringInternal(Name, Policy);
369 Out << Name;
370
371 if (D->isBitField()) {
372 Out << " : ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000373 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000374 }
375}
376
377void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000378 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000379 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
380
Eli Friedmand73364a2009-05-30 04:20:30 +0000381 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000382 Out << "__thread ";
383
384 std::string Name = D->getNameAsString();
385 QualType T = D->getType();
386 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
387 T = Parm->getOriginalType();
388 T.getAsStringInternal(Name, Policy);
389 Out << Name;
390 if (D->getInit()) {
391 if (D->hasCXXDirectInitializer())
392 Out << "(";
393 else
394 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000395 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000396 if (D->hasCXXDirectInitializer())
397 Out << ")";
398 }
399}
400
401void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
402 VisitVarDecl(D);
403}
404
405void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
406 Out << "__asm (";
Eli Friedman4bdae722009-05-30 05:03:24 +0000407 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000408 Out << ")";
409}
410
411//----------------------------------------------------------------------------
412// C++ declarations
413//----------------------------------------------------------------------------
414void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
415 Out << "namespace " << D->getNameAsString() << " {\n";
416 VisitDeclContext(D);
417 Indent() << "}";
418}
419
420void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
421 const char *l;
422 if (D->getLanguage() == LinkageSpecDecl::lang_c)
423 l = "C";
424 else {
425 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
426 "unknown language in linkage specification");
427 l = "C++";
428 }
429
430 Out << "extern \"" << l << "\" ";
431 if (D->hasBraces()) {
432 Out << "{\n";
433 VisitDeclContext(D);
434 Indent() << "}";
435 } else
436 Visit(*D->decls_begin(Context));
437}
438
439void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
440 // TODO: Write template parameters.
441 Out << "template <...> ";
442 Visit(D->getTemplatedDecl());
443}
444
445//----------------------------------------------------------------------------
446// Objective-C declarations
447//----------------------------------------------------------------------------
448
449void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
450 Out << "@class ";
451 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
452 I != E; ++I) {
453 if (I != D->begin()) Out << ", ";
454 Out << (*I)->getNameAsString();
455 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000456}
457
458void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
459 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000460 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000461 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000462 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000463 if (!OMD->getResultType().isNull())
464 Out << '(' << OMD->getResultType().getAsString() << ")";
465
466 std::string name = OMD->getSelector().getAsString();
467 std::string::size_type pos, lastPos = 0;
468 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
469 E = OMD->param_end(); PI != E; ++PI) {
470 // FIXME: selector is missing here!
471 pos = name.find_first_of(":", lastPos);
472 Out << " " << name.substr(lastPos, pos - lastPos);
473 Out << ":(" << (*PI)->getType().getAsString() << ")"
474 << (*PI)->getNameAsString();
475 lastPos = pos + 1;
476 }
477
478 if (OMD->param_begin() == OMD->param_end())
479 Out << " " << name;
480
481 if (OMD->isVariadic())
482 Out << ", ...";
483
484 if (OMD->getBody()) {
485 Out << ' ';
Eli Friedman4bdae722009-05-30 05:03:24 +0000486 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000487 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000488 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000489}
490
491void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
492 std::string I = OID->getNameAsString();
493 ObjCInterfaceDecl *SID = OID->getSuperClass();
494
495 if (SID)
496 Out << "@implementation " << I << " : " << SID->getNameAsString();
497 else
498 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000499 Out << "\n";
500 VisitDeclContext(OID, false);
501 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000502}
503
504void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
505 std::string I = OID->getNameAsString();
506 ObjCInterfaceDecl *SID = OID->getSuperClass();
507
508 if (SID)
509 Out << "@interface " << I << " : " << SID->getNameAsString();
510 else
511 Out << "@interface " << I;
512
513 // Protocols?
514 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
515 if (!Protocols.empty()) {
516 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
517 E = Protocols.end(); I != E; ++I)
518 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
519 }
520
521 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000522 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000523
524 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000525 Out << "{\n";
526 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000527 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
528 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000529 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000530 << ' ' << (*I)->getNameAsString() << ";\n";
531 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000532 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000533 Out << "}\n";
534 }
535
536 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000537 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000538 // FIXME: implement the rest...
539}
540
541void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
542 Out << "@protocol ";
543 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
544 E = D->protocol_end();
545 I != E; ++I) {
546 if (I != D->protocol_begin()) Out << ", ";
547 Out << (*I)->getNameAsString();
548 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000549}
550
551void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
552 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000553 VisitDeclContext(PID, false);
554 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000555}
556
557void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
558 Out << "@implementation "
559 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000560 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000561
562 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000563 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000564 // FIXME: implement the rest...
565}
566
567void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
568 Out << "@interface "
569 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000570 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000571 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000572 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000573
574 // FIXME: implement the rest...
575}
576
577void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
578 Out << "@compatibility_alias " << AID->getNameAsString()
579 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
580}
581
582/// PrintObjCPropertyDecl - print a property declaration.
583///
584void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
585 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
586 Out << "@required\n";
587 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
588 Out << "@optional\n";
589
590 Out << "@property";
591 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
592 bool first = true;
593 Out << " (";
594 if (PDecl->getPropertyAttributes() &
595 ObjCPropertyDecl::OBJC_PR_readonly) {
596 Out << (first ? ' ' : ',') << "readonly";
597 first = false;
598 }
599
600 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
601 Out << (first ? ' ' : ',') << "getter = "
602 << PDecl->getGetterName().getAsString();
603 first = false;
604 }
605 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
606 Out << (first ? ' ' : ',') << "setter = "
607 << PDecl->getSetterName().getAsString();
608 first = false;
609 }
610
611 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
612 Out << (first ? ' ' : ',') << "assign";
613 first = false;
614 }
615
616 if (PDecl->getPropertyAttributes() &
617 ObjCPropertyDecl::OBJC_PR_readwrite) {
618 Out << (first ? ' ' : ',') << "readwrite";
619 first = false;
620 }
621
622 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
623 Out << (first ? ' ' : ',') << "retain";
624 first = false;
625 }
626
627 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
628 Out << (first ? ' ' : ',') << "copy";
629 first = false;
630 }
631
632 if (PDecl->getPropertyAttributes() &
633 ObjCPropertyDecl::OBJC_PR_nonatomic) {
634 Out << (first ? ' ' : ',') << "nonatomic";
635 first = false;
636 }
637 Out << " )";
638 }
639 Out << ' ' << PDecl->getType().getAsString(Policy)
640 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000641}
642
643void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
644 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000645 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000646 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000647 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000648 Out << PID->getPropertyDecl()->getNameAsString();
649 if (PID->getPropertyIvarDecl())
650 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000651}