blob: 72f97035ef1af7ede58c2b69a2e5b15bbffd3e46 [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
299 std::string Proto = D->getNameAsString();
300 if (isa<FunctionType>(D->getType().getTypePtr())) {
301 const FunctionType *AFT = D->getType()->getAsFunctionType();
302
303 const FunctionProtoType *FT = 0;
304 if (D->hasWrittenPrototype())
305 FT = dyn_cast<FunctionProtoType>(AFT);
306
307 Proto += "(";
308 if (FT) {
309 llvm::raw_string_ostream POut(Proto);
310 DeclPrinter ParamPrinter(POut, Context, Policy, Indentation);
311 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
312 if (i) POut << ", ";
313 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
314 }
315
316 if (FT->isVariadic()) {
317 if (D->getNumParams()) POut << ", ";
318 POut << "...";
319 }
Eli Friedmand73364a2009-05-30 04:20:30 +0000320 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
321 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
322 if (i)
323 Proto += ", ";
324 Proto += D->getParamDecl(i)->getNameAsString();
325 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000326 }
327
328 Proto += ")";
329 AFT->getResultType().getAsStringInternal(Proto, Policy);
330 } else {
331 D->getType().getAsStringInternal(Proto, Policy);
332 }
333
334 Out << Proto;
335
336 if (D->isPure())
337 Out << " = 0";
338 else if (D->isDeleted())
339 Out << " = delete";
340 else if (D->isThisDeclarationADefinition()) {
341 if (!D->hasPrototype() && D->getNumParams()) {
342 // This is a K&R function definition, so we need to print the
343 // parameters.
344 Out << '\n';
345 Indentation += Policy.Indentation;
346 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
347 Indent();
348 VisitParmVarDecl(D->getParamDecl(i));
349 Out << ";\n";
350 }
351 Indentation -= Policy.Indentation;
352 } else
353 Out << ' ';
354
Eli Friedman4bdae722009-05-30 05:03:24 +0000355 D->getBody(Context)->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000356 Out << '\n';
357 }
358}
359
360void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000361 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000362 Out << "mutable ";
363
364 std::string Name = D->getNameAsString();
365 D->getType().getAsStringInternal(Name, Policy);
366 Out << Name;
367
368 if (D->isBitField()) {
369 Out << " : ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000370 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000371 }
372}
373
374void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000375 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000376 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
377
Eli Friedmand73364a2009-05-30 04:20:30 +0000378 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000379 Out << "__thread ";
380
381 std::string Name = D->getNameAsString();
382 QualType T = D->getType();
383 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
384 T = Parm->getOriginalType();
385 T.getAsStringInternal(Name, Policy);
386 Out << Name;
387 if (D->getInit()) {
388 if (D->hasCXXDirectInitializer())
389 Out << "(";
390 else
391 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000392 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000393 if (D->hasCXXDirectInitializer())
394 Out << ")";
395 }
396}
397
398void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
399 VisitVarDecl(D);
400}
401
402void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
403 Out << "__asm (";
Eli Friedman4bdae722009-05-30 05:03:24 +0000404 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000405 Out << ")";
406}
407
408//----------------------------------------------------------------------------
409// C++ declarations
410//----------------------------------------------------------------------------
411void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
412 Out << "namespace " << D->getNameAsString() << " {\n";
413 VisitDeclContext(D);
414 Indent() << "}";
415}
416
417void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
418 const char *l;
419 if (D->getLanguage() == LinkageSpecDecl::lang_c)
420 l = "C";
421 else {
422 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
423 "unknown language in linkage specification");
424 l = "C++";
425 }
426
427 Out << "extern \"" << l << "\" ";
428 if (D->hasBraces()) {
429 Out << "{\n";
430 VisitDeclContext(D);
431 Indent() << "}";
432 } else
433 Visit(*D->decls_begin(Context));
434}
435
436void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
437 // TODO: Write template parameters.
438 Out << "template <...> ";
439 Visit(D->getTemplatedDecl());
440}
441
442//----------------------------------------------------------------------------
443// Objective-C declarations
444//----------------------------------------------------------------------------
445
446void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
447 Out << "@class ";
448 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
449 I != E; ++I) {
450 if (I != D->begin()) Out << ", ";
451 Out << (*I)->getNameAsString();
452 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000453}
454
455void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
456 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000457 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000458 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000459 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000460 if (!OMD->getResultType().isNull())
461 Out << '(' << OMD->getResultType().getAsString() << ")";
462
463 std::string name = OMD->getSelector().getAsString();
464 std::string::size_type pos, lastPos = 0;
465 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
466 E = OMD->param_end(); PI != E; ++PI) {
467 // FIXME: selector is missing here!
468 pos = name.find_first_of(":", lastPos);
469 Out << " " << name.substr(lastPos, pos - lastPos);
470 Out << ":(" << (*PI)->getType().getAsString() << ")"
471 << (*PI)->getNameAsString();
472 lastPos = pos + 1;
473 }
474
475 if (OMD->param_begin() == OMD->param_end())
476 Out << " " << name;
477
478 if (OMD->isVariadic())
479 Out << ", ...";
480
481 if (OMD->getBody()) {
482 Out << ' ';
Eli Friedman4bdae722009-05-30 05:03:24 +0000483 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000484 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000485 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000486}
487
488void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
489 std::string I = OID->getNameAsString();
490 ObjCInterfaceDecl *SID = OID->getSuperClass();
491
492 if (SID)
493 Out << "@implementation " << I << " : " << SID->getNameAsString();
494 else
495 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000496 Out << "\n";
497 VisitDeclContext(OID, false);
498 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000499}
500
501void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
502 std::string I = OID->getNameAsString();
503 ObjCInterfaceDecl *SID = OID->getSuperClass();
504
505 if (SID)
506 Out << "@interface " << I << " : " << SID->getNameAsString();
507 else
508 Out << "@interface " << I;
509
510 // Protocols?
511 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
512 if (!Protocols.empty()) {
513 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
514 E = Protocols.end(); I != E; ++I)
515 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
516 }
517
518 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000519 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000520
521 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000522 Out << "{\n";
523 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000524 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
525 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000526 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000527 << ' ' << (*I)->getNameAsString() << ";\n";
528 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000529 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000530 Out << "}\n";
531 }
532
533 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000534 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000535 // FIXME: implement the rest...
536}
537
538void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
539 Out << "@protocol ";
540 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
541 E = D->protocol_end();
542 I != E; ++I) {
543 if (I != D->protocol_begin()) Out << ", ";
544 Out << (*I)->getNameAsString();
545 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000546}
547
548void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
549 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000550 VisitDeclContext(PID, false);
551 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000552}
553
554void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
555 Out << "@implementation "
556 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000557 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000558
559 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000560 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000561 // FIXME: implement the rest...
562}
563
564void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
565 Out << "@interface "
566 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000567 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000568 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000569 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000570
571 // FIXME: implement the rest...
572}
573
574void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
575 Out << "@compatibility_alias " << AID->getNameAsString()
576 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
577}
578
579/// PrintObjCPropertyDecl - print a property declaration.
580///
581void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
582 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
583 Out << "@required\n";
584 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
585 Out << "@optional\n";
586
587 Out << "@property";
588 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
589 bool first = true;
590 Out << " (";
591 if (PDecl->getPropertyAttributes() &
592 ObjCPropertyDecl::OBJC_PR_readonly) {
593 Out << (first ? ' ' : ',') << "readonly";
594 first = false;
595 }
596
597 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
598 Out << (first ? ' ' : ',') << "getter = "
599 << PDecl->getGetterName().getAsString();
600 first = false;
601 }
602 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
603 Out << (first ? ' ' : ',') << "setter = "
604 << PDecl->getSetterName().getAsString();
605 first = false;
606 }
607
608 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
609 Out << (first ? ' ' : ',') << "assign";
610 first = false;
611 }
612
613 if (PDecl->getPropertyAttributes() &
614 ObjCPropertyDecl::OBJC_PR_readwrite) {
615 Out << (first ? ' ' : ',') << "readwrite";
616 first = false;
617 }
618
619 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
620 Out << (first ? ' ' : ',') << "retain";
621 first = false;
622 }
623
624 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
625 Out << (first ? ' ' : ',') << "copy";
626 first = false;
627 }
628
629 if (PDecl->getPropertyAttributes() &
630 ObjCPropertyDecl::OBJC_PR_nonatomic) {
631 Out << (first ? ' ' : ',') << "nonatomic";
632 first = false;
633 }
634 Out << " )";
635 }
636 Out << ' ' << PDecl->getType().getAsString(Policy)
637 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000638}
639
640void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
641 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000642 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000643 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000644 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000645 Out << PID->getPropertyDecl()->getNameAsString();
646 if (PID->getPropertyIvarDecl())
647 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000648}