blob: 950dd9e223decb8213d6748e94d145d9ac21e353 [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 Friedmand73364a2009-05-30 04:20:30 +0000175 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
176 // forced to merge the declarations because there's no other way to
177 // refer to the struct in question. This limited merging is safe without
178 // a bunch of other checks because it only merges declarations directly
179 // referring to the tag, not typedefs.
180 //
181 // Check whether the current declaration should be grouped with a previous
182 // unnamed struct.
183 QualType CurDeclType = getDeclType(*D);
184 if (!Decls.empty() && !CurDeclType.isNull()) {
185 QualType BaseType = GetBaseType(CurDeclType);
186 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
187 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
188 Decls.push_back(*D);
189 continue;
190 }
191 }
192
193 // If we have a merged group waiting to be handled, handle it now.
194 if (!Decls.empty())
195 ProcessDeclGroup(Decls);
196
197 // If the current declaration is an unnamed tag type, save it
198 // so we can merge it with the subsequent declaration(s) using it.
199 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
200 Decls.push_back(*D);
201 continue;
202 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000203 this->Indent();
204 Visit(*D);
205
206 // FIXME: Need to be able to tell the DeclPrinter when
207 const char *Terminator = 0;
208 if (isa<FunctionDecl>(*D) &&
209 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
210 Terminator = 0;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000211 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
212 Terminator = 0;
213 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
214 isa<ObjCImplementationDecl>(*D) ||
215 isa<ObjCInterfaceDecl>(*D) ||
216 isa<ObjCProtocolDecl>(*D) ||
217 isa<ObjCCategoryImplDecl>(*D) ||
218 isa<ObjCCategoryDecl>(*D))
Douglas Gregor996677c2009-05-30 00:08:05 +0000219 Terminator = 0;
220 else if (isa<EnumConstantDecl>(*D)) {
221 DeclContext::decl_iterator Next = D;
222 ++Next;
223 if (Next != DEnd)
224 Terminator = ",";
225 } else
226 Terminator = ";";
227
228 if (Terminator)
229 Out << Terminator;
230 Out << "\n";
231 }
232
Eli Friedmand73364a2009-05-30 04:20:30 +0000233 if (!Decls.empty())
234 ProcessDeclGroup(Decls);
235
Douglas Gregor996677c2009-05-30 00:08:05 +0000236 if (Indent)
237 Indentation -= Policy.Indentation;
238}
239
240void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
241 VisitDeclContext(D, false);
242}
243
244void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
245 std::string S = D->getNameAsString();
246 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedmand73364a2009-05-30 04:20:30 +0000247 if (!Policy.SuppressSpecifiers)
248 Out << "typedef ";
249 Out << S;
Douglas Gregor996677c2009-05-30 00:08:05 +0000250}
251
252void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
253 Out << "enum " << D->getNameAsString() << " {\n";
254 VisitDeclContext(D);
255 Indent() << "}";
256}
257
258void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
259 // print a free standing tag decl (e.g. "struct x;").
260 Out << D->getKindName();
Eli Friedmand73364a2009-05-30 04:20:30 +0000261 if (D->getIdentifier()) {
262 Out << " ";
263 Out << D->getNameAsString();
264 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000265
266 if (D->isDefinition()) {
267 Out << " {\n";
268 VisitDeclContext(D);
269 Indent() << "}";
270 }
271}
272
273void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
274 Out << D->getNameAsString();
275 if (Expr *Init = D->getInitExpr()) {
276 Out << " = ";
277 Init->printPretty(Out, 0, Policy, Indentation);
278 }
279}
280
281void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000282 if (!Policy.SuppressSpecifiers) {
283 switch (D->getStorageClass()) {
284 case FunctionDecl::None: break;
285 case FunctionDecl::Extern: Out << "extern "; break;
286 case FunctionDecl::Static: Out << "static "; break;
287 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
288 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000289
Eli Friedmand73364a2009-05-30 04:20:30 +0000290 if (D->isInline()) Out << "inline ";
291 if (D->isVirtualAsWritten()) Out << "virtual ";
292 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000293
294 std::string Proto = D->getNameAsString();
295 if (isa<FunctionType>(D->getType().getTypePtr())) {
296 const FunctionType *AFT = D->getType()->getAsFunctionType();
297
298 const FunctionProtoType *FT = 0;
299 if (D->hasWrittenPrototype())
300 FT = dyn_cast<FunctionProtoType>(AFT);
301
302 Proto += "(";
303 if (FT) {
304 llvm::raw_string_ostream POut(Proto);
305 DeclPrinter ParamPrinter(POut, Context, Policy, Indentation);
306 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
307 if (i) POut << ", ";
308 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
309 }
310
311 if (FT->isVariadic()) {
312 if (D->getNumParams()) POut << ", ";
313 POut << "...";
314 }
Eli Friedmand73364a2009-05-30 04:20:30 +0000315 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
316 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
317 if (i)
318 Proto += ", ";
319 Proto += D->getParamDecl(i)->getNameAsString();
320 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000321 }
322
323 Proto += ")";
324 AFT->getResultType().getAsStringInternal(Proto, Policy);
325 } else {
326 D->getType().getAsStringInternal(Proto, Policy);
327 }
328
329 Out << Proto;
330
331 if (D->isPure())
332 Out << " = 0";
333 else if (D->isDeleted())
334 Out << " = delete";
335 else if (D->isThisDeclarationADefinition()) {
336 if (!D->hasPrototype() && D->getNumParams()) {
337 // This is a K&R function definition, so we need to print the
338 // parameters.
339 Out << '\n';
340 Indentation += Policy.Indentation;
341 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
342 Indent();
343 VisitParmVarDecl(D->getParamDecl(i));
344 Out << ";\n";
345 }
346 Indentation -= Policy.Indentation;
347 } else
348 Out << ' ';
349
350 D->getBody(Context)->printPretty(Out, 0, Policy, Indentation);
351 Out << '\n';
352 }
353}
354
355void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000356 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000357 Out << "mutable ";
358
359 std::string Name = D->getNameAsString();
360 D->getType().getAsStringInternal(Name, Policy);
361 Out << Name;
362
363 if (D->isBitField()) {
364 Out << " : ";
365 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation);
366 }
367}
368
369void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000370 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000371 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
372
Eli Friedmand73364a2009-05-30 04:20:30 +0000373 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000374 Out << "__thread ";
375
376 std::string Name = D->getNameAsString();
377 QualType T = D->getType();
378 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
379 T = Parm->getOriginalType();
380 T.getAsStringInternal(Name, Policy);
381 Out << Name;
382 if (D->getInit()) {
383 if (D->hasCXXDirectInitializer())
384 Out << "(";
385 else
386 Out << " = ";
387 D->getInit()->printPretty(Out, 0, Policy, Indentation);
388 if (D->hasCXXDirectInitializer())
389 Out << ")";
390 }
391}
392
393void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
394 VisitVarDecl(D);
395}
396
397void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
398 Out << "__asm (";
399 D->getAsmString()->printPretty(Out, 0, Policy, Indentation);
400 Out << ")";
401}
402
403//----------------------------------------------------------------------------
404// C++ declarations
405//----------------------------------------------------------------------------
406void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
407 Out << "namespace " << D->getNameAsString() << " {\n";
408 VisitDeclContext(D);
409 Indent() << "}";
410}
411
412void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
413 const char *l;
414 if (D->getLanguage() == LinkageSpecDecl::lang_c)
415 l = "C";
416 else {
417 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
418 "unknown language in linkage specification");
419 l = "C++";
420 }
421
422 Out << "extern \"" << l << "\" ";
423 if (D->hasBraces()) {
424 Out << "{\n";
425 VisitDeclContext(D);
426 Indent() << "}";
427 } else
428 Visit(*D->decls_begin(Context));
429}
430
431void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
432 // TODO: Write template parameters.
433 Out << "template <...> ";
434 Visit(D->getTemplatedDecl());
435}
436
437//----------------------------------------------------------------------------
438// Objective-C declarations
439//----------------------------------------------------------------------------
440
441void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
442 Out << "@class ";
443 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
444 I != E; ++I) {
445 if (I != D->begin()) Out << ", ";
446 Out << (*I)->getNameAsString();
447 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000448}
449
450void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
451 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000452 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000453 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000454 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000455 if (!OMD->getResultType().isNull())
456 Out << '(' << OMD->getResultType().getAsString() << ")";
457
458 std::string name = OMD->getSelector().getAsString();
459 std::string::size_type pos, lastPos = 0;
460 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
461 E = OMD->param_end(); PI != E; ++PI) {
462 // FIXME: selector is missing here!
463 pos = name.find_first_of(":", lastPos);
464 Out << " " << name.substr(lastPos, pos - lastPos);
465 Out << ":(" << (*PI)->getType().getAsString() << ")"
466 << (*PI)->getNameAsString();
467 lastPos = pos + 1;
468 }
469
470 if (OMD->param_begin() == OMD->param_end())
471 Out << " " << name;
472
473 if (OMD->isVariadic())
474 Out << ", ...";
475
476 if (OMD->getBody()) {
477 Out << ' ';
478 OMD->getBody()->printPretty(Out, 0, Policy);
479 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000480 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000481}
482
483void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
484 std::string I = OID->getNameAsString();
485 ObjCInterfaceDecl *SID = OID->getSuperClass();
486
487 if (SID)
488 Out << "@implementation " << I << " : " << SID->getNameAsString();
489 else
490 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000491 Out << "\n";
492 VisitDeclContext(OID, false);
493 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000494}
495
496void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
497 std::string I = OID->getNameAsString();
498 ObjCInterfaceDecl *SID = OID->getSuperClass();
499
500 if (SID)
501 Out << "@interface " << I << " : " << SID->getNameAsString();
502 else
503 Out << "@interface " << I;
504
505 // Protocols?
506 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
507 if (!Protocols.empty()) {
508 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
509 E = Protocols.end(); I != E; ++I)
510 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
511 }
512
513 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000514 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000515
516 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000517 Out << "{\n";
518 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000519 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
520 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000521 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000522 << ' ' << (*I)->getNameAsString() << ";\n";
523 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000524 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000525 Out << "}\n";
526 }
527
528 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000529 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000530 // FIXME: implement the rest...
531}
532
533void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
534 Out << "@protocol ";
535 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
536 E = D->protocol_end();
537 I != E; ++I) {
538 if (I != D->protocol_begin()) Out << ", ";
539 Out << (*I)->getNameAsString();
540 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000541}
542
543void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
544 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000545 VisitDeclContext(PID, false);
546 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000547}
548
549void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
550 Out << "@implementation "
551 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000552 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000553
554 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000555 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000556 // FIXME: implement the rest...
557}
558
559void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
560 Out << "@interface "
561 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000562 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000563 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000564 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000565
566 // FIXME: implement the rest...
567}
568
569void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
570 Out << "@compatibility_alias " << AID->getNameAsString()
571 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
572}
573
574/// PrintObjCPropertyDecl - print a property declaration.
575///
576void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
577 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
578 Out << "@required\n";
579 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
580 Out << "@optional\n";
581
582 Out << "@property";
583 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
584 bool first = true;
585 Out << " (";
586 if (PDecl->getPropertyAttributes() &
587 ObjCPropertyDecl::OBJC_PR_readonly) {
588 Out << (first ? ' ' : ',') << "readonly";
589 first = false;
590 }
591
592 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
593 Out << (first ? ' ' : ',') << "getter = "
594 << PDecl->getGetterName().getAsString();
595 first = false;
596 }
597 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
598 Out << (first ? ' ' : ',') << "setter = "
599 << PDecl->getSetterName().getAsString();
600 first = false;
601 }
602
603 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
604 Out << (first ? ' ' : ',') << "assign";
605 first = false;
606 }
607
608 if (PDecl->getPropertyAttributes() &
609 ObjCPropertyDecl::OBJC_PR_readwrite) {
610 Out << (first ? ' ' : ',') << "readwrite";
611 first = false;
612 }
613
614 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
615 Out << (first ? ' ' : ',') << "retain";
616 first = false;
617 }
618
619 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
620 Out << (first ? ' ' : ',') << "copy";
621 first = false;
622 }
623
624 if (PDecl->getPropertyAttributes() &
625 ObjCPropertyDecl::OBJC_PR_nonatomic) {
626 Out << (first ? ' ' : ',') << "nonatomic";
627 first = false;
628 }
629 Out << " )";
630 }
631 Out << ' ' << PDecl->getType().getAsString(Policy)
632 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000633}
634
635void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
636 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000637 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000638 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000639 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000640 Out << PID->getPropertyDecl()->getNameAsString();
641 if (PID->getPropertyIvarDecl())
642 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000643}