blob: bfd3dca3e6d3260d03c740bd38dd0802d705a822 [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);
57 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000058 void VisitNamespaceDecl(NamespaceDecl *D);
59 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
60 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000061 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000062 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000063 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
64 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
65 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
66 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
67 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
68 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
69 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
70 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
71 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
72 };
73}
74
75void Decl::print(llvm::raw_ostream &Out, ASTContext &Context,
76 unsigned Indentation) {
77 print(Out, Context, Context.PrintingPolicy, Indentation);
78}
79
80void Decl::print(llvm::raw_ostream &Out, ASTContext &Context,
81 const PrintingPolicy &Policy, unsigned Indentation) {
82 DeclPrinter Printer(Out, Context, Policy, Indentation);
83 Printer.Visit(this);
84}
85
Eli Friedmand73364a2009-05-30 04:20:30 +000086static QualType GetBaseType(QualType T) {
87 // FIXME: This should be on the Type class!
88 QualType BaseType = T;
89 while (!BaseType->isSpecifierType()) {
90 if (isa<TypedefType>(BaseType))
91 break;
92 else if (const PointerType* PTy = BaseType->getAsPointerType())
93 BaseType = PTy->getPointeeType();
94 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
95 BaseType = ATy->getElementType();
96 else if (const FunctionType* FTy = BaseType->getAsFunctionType())
97 BaseType = FTy->getResultType();
98 else
99 assert(0 && "Unknown declarator!");
100 }
101 return BaseType;
102}
103
104static QualType getDeclType(Decl* D) {
105 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
106 return TDD->getUnderlyingType();
107 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
108 return VD->getType();
109 return QualType();
110}
111
112void Decl::printGroup(Decl** Begin, unsigned NumDecls,
113 llvm::raw_ostream &Out, ASTContext &Context,
114 const PrintingPolicy &Policy,
115 unsigned Indentation) {
116 if (NumDecls == 1) {
117 (*Begin)->print(Out, Context, Policy, Indentation);
118 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()) {
128 TD->print(Out, Context, Policy, Indentation);
129 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
143 (*Begin)->print(Out, Context, SubPolicy, Indentation);
144 }
145}
146
Douglas Gregor996677c2009-05-30 00:08:05 +0000147void Decl::dump(ASTContext &Context) {
148 print(llvm::errs(), Context);
149}
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();
159 Decl::printGroup(Decls.data(), Decls.size(), Out, Context,
160 Policy, Indentation);
161 Out << ";\n";
162 Decls.clear();
163
164}
165
Douglas Gregor996677c2009-05-30 00:08:05 +0000166//----------------------------------------------------------------------------
167// Common C declarations
168//----------------------------------------------------------------------------
169
170void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
171 if (Indent)
172 Indentation += Policy.Indentation;
173
Eli Friedmand73364a2009-05-30 04:20:30 +0000174 llvm::SmallVector<Decl*, 2> Decls;
Douglas Gregor996677c2009-05-30 00:08:05 +0000175 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
176 DEnd = DC->decls_end(Context);
177 D != DEnd; ++D) {
Eli Friedman4bdae722009-05-30 05:03:24 +0000178 if (!Policy.Dump) {
179 // Skip over implicit declarations in pretty-printing mode.
180 if (D->isImplicit()) continue;
181 }
182
Eli Friedmand73364a2009-05-30 04:20:30 +0000183 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
184 // forced to merge the declarations because there's no other way to
185 // refer to the struct in question. This limited merging is safe without
186 // a bunch of other checks because it only merges declarations directly
187 // referring to the tag, not typedefs.
188 //
189 // Check whether the current declaration should be grouped with a previous
190 // unnamed struct.
191 QualType CurDeclType = getDeclType(*D);
192 if (!Decls.empty() && !CurDeclType.isNull()) {
193 QualType BaseType = GetBaseType(CurDeclType);
194 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
195 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
196 Decls.push_back(*D);
197 continue;
198 }
199 }
200
201 // If we have a merged group waiting to be handled, handle it now.
202 if (!Decls.empty())
203 ProcessDeclGroup(Decls);
204
205 // If the current declaration is an unnamed tag type, save it
206 // so we can merge it with the subsequent declaration(s) using it.
207 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
208 Decls.push_back(*D);
209 continue;
210 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000211 this->Indent();
212 Visit(*D);
213
214 // FIXME: Need to be able to tell the DeclPrinter when
215 const char *Terminator = 0;
216 if (isa<FunctionDecl>(*D) &&
217 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
218 Terminator = 0;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000219 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
220 Terminator = 0;
221 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
222 isa<ObjCImplementationDecl>(*D) ||
223 isa<ObjCInterfaceDecl>(*D) ||
224 isa<ObjCProtocolDecl>(*D) ||
225 isa<ObjCCategoryImplDecl>(*D) ||
226 isa<ObjCCategoryDecl>(*D))
Douglas Gregor996677c2009-05-30 00:08:05 +0000227 Terminator = 0;
228 else if (isa<EnumConstantDecl>(*D)) {
229 DeclContext::decl_iterator Next = D;
230 ++Next;
231 if (Next != DEnd)
232 Terminator = ",";
233 } else
234 Terminator = ";";
235
236 if (Terminator)
237 Out << Terminator;
238 Out << "\n";
239 }
240
Eli Friedmand73364a2009-05-30 04:20:30 +0000241 if (!Decls.empty())
242 ProcessDeclGroup(Decls);
243
Douglas Gregor996677c2009-05-30 00:08:05 +0000244 if (Indent)
245 Indentation -= Policy.Indentation;
246}
247
248void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
249 VisitDeclContext(D, false);
250}
251
252void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
253 std::string S = D->getNameAsString();
254 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedmand73364a2009-05-30 04:20:30 +0000255 if (!Policy.SuppressSpecifiers)
256 Out << "typedef ";
257 Out << S;
Douglas Gregor996677c2009-05-30 00:08:05 +0000258}
259
260void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
261 Out << "enum " << D->getNameAsString() << " {\n";
262 VisitDeclContext(D);
263 Indent() << "}";
264}
265
266void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
267 // print a free standing tag decl (e.g. "struct x;").
268 Out << D->getKindName();
Eli Friedmand73364a2009-05-30 04:20:30 +0000269 if (D->getIdentifier()) {
270 Out << " ";
271 Out << D->getNameAsString();
272 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000273
274 if (D->isDefinition()) {
275 Out << " {\n";
276 VisitDeclContext(D);
277 Indent() << "}";
278 }
279}
280
281void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
282 Out << D->getNameAsString();
283 if (Expr *Init = D->getInitExpr()) {
284 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000285 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000286 }
287}
288
289void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000290 if (!Policy.SuppressSpecifiers) {
291 switch (D->getStorageClass()) {
292 case FunctionDecl::None: break;
293 case FunctionDecl::Extern: Out << "extern "; break;
294 case FunctionDecl::Static: Out << "static "; break;
295 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
296 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000297
Eli Friedmand73364a2009-05-30 04:20:30 +0000298 if (D->isInline()) Out << "inline ";
299 if (D->isVirtualAsWritten()) Out << "virtual ";
300 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000301
Douglas Gregor25133312009-05-30 05:39:39 +0000302 PrintingPolicy SubPolicy(Policy);
303 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor996677c2009-05-30 00:08:05 +0000304 std::string Proto = D->getNameAsString();
305 if (isa<FunctionType>(D->getType().getTypePtr())) {
306 const FunctionType *AFT = D->getType()->getAsFunctionType();
307
308 const FunctionProtoType *FT = 0;
309 if (D->hasWrittenPrototype())
310 FT = dyn_cast<FunctionProtoType>(AFT);
311
312 Proto += "(";
313 if (FT) {
314 llvm::raw_string_ostream POut(Proto);
Douglas Gregor25133312009-05-30 05:39:39 +0000315 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000316 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
317 if (i) POut << ", ";
318 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
319 }
320
321 if (FT->isVariadic()) {
322 if (D->getNumParams()) POut << ", ";
323 POut << "...";
324 }
Eli Friedmand73364a2009-05-30 04:20:30 +0000325 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
326 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
327 if (i)
328 Proto += ", ";
329 Proto += D->getParamDecl(i)->getNameAsString();
330 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000331 }
332
333 Proto += ")";
334 AFT->getResultType().getAsStringInternal(Proto, Policy);
335 } else {
336 D->getType().getAsStringInternal(Proto, Policy);
337 }
338
339 Out << Proto;
340
341 if (D->isPure())
342 Out << " = 0";
343 else if (D->isDeleted())
344 Out << " = delete";
345 else if (D->isThisDeclarationADefinition()) {
346 if (!D->hasPrototype() && D->getNumParams()) {
347 // This is a K&R function definition, so we need to print the
348 // parameters.
349 Out << '\n';
Douglas Gregor25133312009-05-30 05:39:39 +0000350 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000351 Indentation += Policy.Indentation;
352 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
353 Indent();
Douglas Gregor25133312009-05-30 05:39:39 +0000354 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor996677c2009-05-30 00:08:05 +0000355 Out << ";\n";
356 }
357 Indentation -= Policy.Indentation;
358 } else
359 Out << ' ';
360
Douglas Gregor25133312009-05-30 05:39:39 +0000361 D->getBody(Context)->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000362 Out << '\n';
363 }
364}
365
366void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000367 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000368 Out << "mutable ";
369
370 std::string Name = D->getNameAsString();
371 D->getType().getAsStringInternal(Name, Policy);
372 Out << Name;
373
374 if (D->isBitField()) {
375 Out << " : ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000376 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000377 }
378}
379
380void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000381 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000382 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
383
Eli Friedmand73364a2009-05-30 04:20:30 +0000384 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000385 Out << "__thread ";
386
387 std::string Name = D->getNameAsString();
388 QualType T = D->getType();
389 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
390 T = Parm->getOriginalType();
391 T.getAsStringInternal(Name, Policy);
392 Out << Name;
393 if (D->getInit()) {
394 if (D->hasCXXDirectInitializer())
395 Out << "(";
396 else
397 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000398 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000399 if (D->hasCXXDirectInitializer())
400 Out << ")";
401 }
402}
403
404void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
405 VisitVarDecl(D);
406}
407
Douglas Gregor1d27d692009-05-30 06:31:56 +0000408void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
409 VisitVarDecl(D);
410}
411
Douglas Gregor996677c2009-05-30 00:08:05 +0000412void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
413 Out << "__asm (";
Eli Friedman4bdae722009-05-30 05:03:24 +0000414 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000415 Out << ")";
416}
417
418//----------------------------------------------------------------------------
419// C++ declarations
420//----------------------------------------------------------------------------
Douglas Gregor1d27d692009-05-30 06:31:56 +0000421void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
422 assert(false &&
423 "OverloadedFunctionDecls aren't really decls and are never printed");
424}
425
426void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
427 Out << "using namespace ";
428 if (D->getQualifier())
429 D->getQualifier()->print(Out, Policy);
430 Out << D->getNominatedNamespace()->getNameAsString();
431}
432
Douglas Gregor996677c2009-05-30 00:08:05 +0000433void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
434 Out << "namespace " << D->getNameAsString() << " {\n";
435 VisitDeclContext(D);
436 Indent() << "}";
437}
438
439void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
440 const char *l;
441 if (D->getLanguage() == LinkageSpecDecl::lang_c)
442 l = "C";
443 else {
444 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
445 "unknown language in linkage specification");
446 l = "C++";
447 }
448
449 Out << "extern \"" << l << "\" ";
450 if (D->hasBraces()) {
451 Out << "{\n";
452 VisitDeclContext(D);
453 Indent() << "}";
454 } else
455 Visit(*D->decls_begin(Context));
456}
457
458void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
459 // TODO: Write template parameters.
460 Out << "template <...> ";
461 Visit(D->getTemplatedDecl());
462}
463
464//----------------------------------------------------------------------------
465// Objective-C declarations
466//----------------------------------------------------------------------------
467
468void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
469 Out << "@class ";
470 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
471 I != E; ++I) {
472 if (I != D->begin()) Out << ", ";
473 Out << (*I)->getNameAsString();
474 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000475}
476
477void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
478 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000479 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000480 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000481 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000482 if (!OMD->getResultType().isNull())
483 Out << '(' << OMD->getResultType().getAsString() << ")";
484
485 std::string name = OMD->getSelector().getAsString();
486 std::string::size_type pos, lastPos = 0;
487 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
488 E = OMD->param_end(); PI != E; ++PI) {
489 // FIXME: selector is missing here!
490 pos = name.find_first_of(":", lastPos);
491 Out << " " << name.substr(lastPos, pos - lastPos);
492 Out << ":(" << (*PI)->getType().getAsString() << ")"
493 << (*PI)->getNameAsString();
494 lastPos = pos + 1;
495 }
496
497 if (OMD->param_begin() == OMD->param_end())
498 Out << " " << name;
499
500 if (OMD->isVariadic())
501 Out << ", ...";
502
503 if (OMD->getBody()) {
504 Out << ' ';
Eli Friedman4bdae722009-05-30 05:03:24 +0000505 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000506 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000507 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000508}
509
510void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
511 std::string I = OID->getNameAsString();
512 ObjCInterfaceDecl *SID = OID->getSuperClass();
513
514 if (SID)
515 Out << "@implementation " << I << " : " << SID->getNameAsString();
516 else
517 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000518 Out << "\n";
519 VisitDeclContext(OID, false);
520 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000521}
522
523void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
524 std::string I = OID->getNameAsString();
525 ObjCInterfaceDecl *SID = OID->getSuperClass();
526
527 if (SID)
528 Out << "@interface " << I << " : " << SID->getNameAsString();
529 else
530 Out << "@interface " << I;
531
532 // Protocols?
533 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
534 if (!Protocols.empty()) {
535 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
536 E = Protocols.end(); I != E; ++I)
537 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
538 }
539
540 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000541 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000542
543 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000544 Out << "{\n";
545 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000546 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
547 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000548 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000549 << ' ' << (*I)->getNameAsString() << ";\n";
550 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000551 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000552 Out << "}\n";
553 }
554
555 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000556 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000557 // FIXME: implement the rest...
558}
559
560void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
561 Out << "@protocol ";
562 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
563 E = D->protocol_end();
564 I != E; ++I) {
565 if (I != D->protocol_begin()) Out << ", ";
566 Out << (*I)->getNameAsString();
567 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000568}
569
570void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
571 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000572 VisitDeclContext(PID, false);
573 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000574}
575
576void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
577 Out << "@implementation "
578 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000579 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000580
581 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000582 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000583 // FIXME: implement the rest...
584}
585
586void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
587 Out << "@interface "
588 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000589 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000590 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000591 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000592
593 // FIXME: implement the rest...
594}
595
596void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
597 Out << "@compatibility_alias " << AID->getNameAsString()
598 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
599}
600
601/// PrintObjCPropertyDecl - print a property declaration.
602///
603void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
604 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
605 Out << "@required\n";
606 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
607 Out << "@optional\n";
608
609 Out << "@property";
610 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
611 bool first = true;
612 Out << " (";
613 if (PDecl->getPropertyAttributes() &
614 ObjCPropertyDecl::OBJC_PR_readonly) {
615 Out << (first ? ' ' : ',') << "readonly";
616 first = false;
617 }
618
619 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
620 Out << (first ? ' ' : ',') << "getter = "
621 << PDecl->getGetterName().getAsString();
622 first = false;
623 }
624 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
625 Out << (first ? ' ' : ',') << "setter = "
626 << PDecl->getSetterName().getAsString();
627 first = false;
628 }
629
630 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
631 Out << (first ? ' ' : ',') << "assign";
632 first = false;
633 }
634
635 if (PDecl->getPropertyAttributes() &
636 ObjCPropertyDecl::OBJC_PR_readwrite) {
637 Out << (first ? ' ' : ',') << "readwrite";
638 first = false;
639 }
640
641 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
642 Out << (first ? ' ' : ',') << "retain";
643 first = false;
644 }
645
646 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
647 Out << (first ? ' ' : ',') << "copy";
648 first = false;
649 }
650
651 if (PDecl->getPropertyAttributes() &
652 ObjCPropertyDecl::OBJC_PR_nonatomic) {
653 Out << (first ? ' ' : ',') << "nonatomic";
654 first = false;
655 }
656 Out << " )";
657 }
658 Out << ' ' << PDecl->getType().getAsString(Policy)
659 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000660}
661
662void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
663 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000664 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000665 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000666 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000667 Out << PID->getPropertyDecl()->getNameAsString();
668 if (PID->getPropertyIvarDecl())
669 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000670}