blob: 01cbee4d637059a967ae7ccf1113185032d23efe [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;
Eli Friedman68ff4682009-05-30 06:35:22 +0000181 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
182 // of __builtin_va_list. There should be some other way to check that.
183 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
184 "__builtin_va_list")
185 continue;
Eli Friedman4bdae722009-05-30 05:03:24 +0000186 }
187
Eli Friedmand73364a2009-05-30 04:20:30 +0000188 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
189 // forced to merge the declarations because there's no other way to
190 // refer to the struct in question. This limited merging is safe without
191 // a bunch of other checks because it only merges declarations directly
192 // referring to the tag, not typedefs.
193 //
194 // Check whether the current declaration should be grouped with a previous
195 // unnamed struct.
196 QualType CurDeclType = getDeclType(*D);
197 if (!Decls.empty() && !CurDeclType.isNull()) {
198 QualType BaseType = GetBaseType(CurDeclType);
199 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
200 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
201 Decls.push_back(*D);
202 continue;
203 }
204 }
205
206 // If we have a merged group waiting to be handled, handle it now.
207 if (!Decls.empty())
208 ProcessDeclGroup(Decls);
209
210 // If the current declaration is an unnamed tag type, save it
211 // so we can merge it with the subsequent declaration(s) using it.
212 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
213 Decls.push_back(*D);
214 continue;
215 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000216 this->Indent();
217 Visit(*D);
218
219 // FIXME: Need to be able to tell the DeclPrinter when
220 const char *Terminator = 0;
221 if (isa<FunctionDecl>(*D) &&
222 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
223 Terminator = 0;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000224 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
225 Terminator = 0;
226 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
227 isa<ObjCImplementationDecl>(*D) ||
228 isa<ObjCInterfaceDecl>(*D) ||
229 isa<ObjCProtocolDecl>(*D) ||
230 isa<ObjCCategoryImplDecl>(*D) ||
231 isa<ObjCCategoryDecl>(*D))
Douglas Gregor996677c2009-05-30 00:08:05 +0000232 Terminator = 0;
233 else if (isa<EnumConstantDecl>(*D)) {
234 DeclContext::decl_iterator Next = D;
235 ++Next;
236 if (Next != DEnd)
237 Terminator = ",";
238 } else
239 Terminator = ";";
240
241 if (Terminator)
242 Out << Terminator;
243 Out << "\n";
244 }
245
Eli Friedmand73364a2009-05-30 04:20:30 +0000246 if (!Decls.empty())
247 ProcessDeclGroup(Decls);
248
Douglas Gregor996677c2009-05-30 00:08:05 +0000249 if (Indent)
250 Indentation -= Policy.Indentation;
251}
252
253void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
254 VisitDeclContext(D, false);
255}
256
257void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
258 std::string S = D->getNameAsString();
259 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedmand73364a2009-05-30 04:20:30 +0000260 if (!Policy.SuppressSpecifiers)
261 Out << "typedef ";
262 Out << S;
Douglas Gregor996677c2009-05-30 00:08:05 +0000263}
264
265void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
266 Out << "enum " << D->getNameAsString() << " {\n";
267 VisitDeclContext(D);
268 Indent() << "}";
269}
270
271void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
272 // print a free standing tag decl (e.g. "struct x;").
273 Out << D->getKindName();
Eli Friedmand73364a2009-05-30 04:20:30 +0000274 if (D->getIdentifier()) {
275 Out << " ";
276 Out << D->getNameAsString();
277 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000278
279 if (D->isDefinition()) {
280 Out << " {\n";
281 VisitDeclContext(D);
282 Indent() << "}";
283 }
284}
285
286void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
287 Out << D->getNameAsString();
288 if (Expr *Init = D->getInitExpr()) {
289 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000290 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000291 }
292}
293
294void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000295 if (!Policy.SuppressSpecifiers) {
296 switch (D->getStorageClass()) {
297 case FunctionDecl::None: break;
298 case FunctionDecl::Extern: Out << "extern "; break;
299 case FunctionDecl::Static: Out << "static "; break;
300 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
301 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000302
Eli Friedmand73364a2009-05-30 04:20:30 +0000303 if (D->isInline()) Out << "inline ";
304 if (D->isVirtualAsWritten()) Out << "virtual ";
305 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000306
Douglas Gregor25133312009-05-30 05:39:39 +0000307 PrintingPolicy SubPolicy(Policy);
308 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor996677c2009-05-30 00:08:05 +0000309 std::string Proto = D->getNameAsString();
310 if (isa<FunctionType>(D->getType().getTypePtr())) {
311 const FunctionType *AFT = D->getType()->getAsFunctionType();
312
313 const FunctionProtoType *FT = 0;
314 if (D->hasWrittenPrototype())
315 FT = dyn_cast<FunctionProtoType>(AFT);
316
317 Proto += "(";
318 if (FT) {
319 llvm::raw_string_ostream POut(Proto);
Douglas Gregor25133312009-05-30 05:39:39 +0000320 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000321 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
322 if (i) POut << ", ";
323 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
324 }
325
326 if (FT->isVariadic()) {
327 if (D->getNumParams()) POut << ", ";
328 POut << "...";
329 }
Eli Friedmand73364a2009-05-30 04:20:30 +0000330 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
331 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
332 if (i)
333 Proto += ", ";
334 Proto += D->getParamDecl(i)->getNameAsString();
335 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000336 }
337
338 Proto += ")";
339 AFT->getResultType().getAsStringInternal(Proto, Policy);
340 } else {
341 D->getType().getAsStringInternal(Proto, Policy);
342 }
343
344 Out << Proto;
345
346 if (D->isPure())
347 Out << " = 0";
348 else if (D->isDeleted())
349 Out << " = delete";
350 else if (D->isThisDeclarationADefinition()) {
351 if (!D->hasPrototype() && D->getNumParams()) {
352 // This is a K&R function definition, so we need to print the
353 // parameters.
354 Out << '\n';
Douglas Gregor25133312009-05-30 05:39:39 +0000355 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000356 Indentation += Policy.Indentation;
357 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
358 Indent();
Douglas Gregor25133312009-05-30 05:39:39 +0000359 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor996677c2009-05-30 00:08:05 +0000360 Out << ";\n";
361 }
362 Indentation -= Policy.Indentation;
363 } else
364 Out << ' ';
365
Douglas Gregor25133312009-05-30 05:39:39 +0000366 D->getBody(Context)->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000367 Out << '\n';
368 }
369}
370
371void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000372 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000373 Out << "mutable ";
374
375 std::string Name = D->getNameAsString();
376 D->getType().getAsStringInternal(Name, Policy);
377 Out << Name;
378
379 if (D->isBitField()) {
380 Out << " : ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000381 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000382 }
383}
384
385void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000386 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000387 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
388
Eli Friedmand73364a2009-05-30 04:20:30 +0000389 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000390 Out << "__thread ";
391
392 std::string Name = D->getNameAsString();
393 QualType T = D->getType();
394 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
395 T = Parm->getOriginalType();
396 T.getAsStringInternal(Name, Policy);
397 Out << Name;
398 if (D->getInit()) {
399 if (D->hasCXXDirectInitializer())
400 Out << "(";
401 else
402 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000403 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000404 if (D->hasCXXDirectInitializer())
405 Out << ")";
406 }
407}
408
409void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
410 VisitVarDecl(D);
411}
412
Douglas Gregor1d27d692009-05-30 06:31:56 +0000413void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
414 VisitVarDecl(D);
415}
416
Douglas Gregor996677c2009-05-30 00:08:05 +0000417void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
418 Out << "__asm (";
Eli Friedman4bdae722009-05-30 05:03:24 +0000419 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000420 Out << ")";
421}
422
423//----------------------------------------------------------------------------
424// C++ declarations
425//----------------------------------------------------------------------------
Douglas Gregor1d27d692009-05-30 06:31:56 +0000426void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
427 assert(false &&
428 "OverloadedFunctionDecls aren't really decls and are never printed");
429}
430
431void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
432 Out << "using namespace ";
433 if (D->getQualifier())
434 D->getQualifier()->print(Out, Policy);
435 Out << D->getNominatedNamespace()->getNameAsString();
436}
437
Douglas Gregor996677c2009-05-30 00:08:05 +0000438void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
439 Out << "namespace " << D->getNameAsString() << " {\n";
440 VisitDeclContext(D);
441 Indent() << "}";
442}
443
444void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
445 const char *l;
446 if (D->getLanguage() == LinkageSpecDecl::lang_c)
447 l = "C";
448 else {
449 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
450 "unknown language in linkage specification");
451 l = "C++";
452 }
453
454 Out << "extern \"" << l << "\" ";
455 if (D->hasBraces()) {
456 Out << "{\n";
457 VisitDeclContext(D);
458 Indent() << "}";
459 } else
460 Visit(*D->decls_begin(Context));
461}
462
463void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
464 // TODO: Write template parameters.
465 Out << "template <...> ";
466 Visit(D->getTemplatedDecl());
467}
468
469//----------------------------------------------------------------------------
470// Objective-C declarations
471//----------------------------------------------------------------------------
472
473void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
474 Out << "@class ";
475 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
476 I != E; ++I) {
477 if (I != D->begin()) Out << ", ";
478 Out << (*I)->getNameAsString();
479 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000480}
481
482void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
483 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000484 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000485 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000486 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000487 if (!OMD->getResultType().isNull())
488 Out << '(' << OMD->getResultType().getAsString() << ")";
489
490 std::string name = OMD->getSelector().getAsString();
491 std::string::size_type pos, lastPos = 0;
492 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
493 E = OMD->param_end(); PI != E; ++PI) {
494 // FIXME: selector is missing here!
495 pos = name.find_first_of(":", lastPos);
496 Out << " " << name.substr(lastPos, pos - lastPos);
497 Out << ":(" << (*PI)->getType().getAsString() << ")"
498 << (*PI)->getNameAsString();
499 lastPos = pos + 1;
500 }
501
502 if (OMD->param_begin() == OMD->param_end())
503 Out << " " << name;
504
505 if (OMD->isVariadic())
506 Out << ", ...";
507
508 if (OMD->getBody()) {
509 Out << ' ';
Eli Friedman4bdae722009-05-30 05:03:24 +0000510 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000511 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000512 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000513}
514
515void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
516 std::string I = OID->getNameAsString();
517 ObjCInterfaceDecl *SID = OID->getSuperClass();
518
519 if (SID)
520 Out << "@implementation " << I << " : " << SID->getNameAsString();
521 else
522 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000523 Out << "\n";
524 VisitDeclContext(OID, false);
525 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000526}
527
528void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
529 std::string I = OID->getNameAsString();
530 ObjCInterfaceDecl *SID = OID->getSuperClass();
531
532 if (SID)
533 Out << "@interface " << I << " : " << SID->getNameAsString();
534 else
535 Out << "@interface " << I;
536
537 // Protocols?
538 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
539 if (!Protocols.empty()) {
540 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
541 E = Protocols.end(); I != E; ++I)
542 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
543 }
544
545 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000546 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000547
548 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000549 Out << "{\n";
550 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000551 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
552 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000553 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000554 << ' ' << (*I)->getNameAsString() << ";\n";
555 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000556 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000557 Out << "}\n";
558 }
559
560 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000561 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000562 // FIXME: implement the rest...
563}
564
565void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
566 Out << "@protocol ";
567 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
568 E = D->protocol_end();
569 I != E; ++I) {
570 if (I != D->protocol_begin()) Out << ", ";
571 Out << (*I)->getNameAsString();
572 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000573}
574
575void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
576 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000577 VisitDeclContext(PID, false);
578 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000579}
580
581void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
582 Out << "@implementation "
583 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000584 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000585
586 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000587 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000588 // FIXME: implement the rest...
589}
590
591void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
592 Out << "@interface "
593 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000594 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000595 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000596 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000597
598 // FIXME: implement the rest...
599}
600
601void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
602 Out << "@compatibility_alias " << AID->getNameAsString()
603 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
604}
605
606/// PrintObjCPropertyDecl - print a property declaration.
607///
608void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
609 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
610 Out << "@required\n";
611 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
612 Out << "@optional\n";
613
614 Out << "@property";
615 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
616 bool first = true;
617 Out << " (";
618 if (PDecl->getPropertyAttributes() &
619 ObjCPropertyDecl::OBJC_PR_readonly) {
620 Out << (first ? ' ' : ',') << "readonly";
621 first = false;
622 }
623
624 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
625 Out << (first ? ' ' : ',') << "getter = "
626 << PDecl->getGetterName().getAsString();
627 first = false;
628 }
629 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
630 Out << (first ? ' ' : ',') << "setter = "
631 << PDecl->getSetterName().getAsString();
632 first = false;
633 }
634
635 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
636 Out << (first ? ' ' : ',') << "assign";
637 first = false;
638 }
639
640 if (PDecl->getPropertyAttributes() &
641 ObjCPropertyDecl::OBJC_PR_readwrite) {
642 Out << (first ? ' ' : ',') << "readwrite";
643 first = false;
644 }
645
646 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
647 Out << (first ? ' ' : ',') << "retain";
648 first = false;
649 }
650
651 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
652 Out << (first ? ' ' : ',') << "copy";
653 first = false;
654 }
655
656 if (PDecl->getPropertyAttributes() &
657 ObjCPropertyDecl::OBJC_PR_nonatomic) {
658 Out << (first ? ' ' : ',') << "nonatomic";
659 first = false;
660 }
661 Out << " )";
662 }
663 Out << ' ' << PDecl->getType().getAsString(Policy)
664 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000665}
666
667void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
668 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000669 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000670 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000671 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000672 Out << PID->getPropertyDecl()->getNameAsString();
673 if (PID->getPropertyIvarDecl())
674 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000675}