]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/AST/DeclPrinter.cpp
Update clang to r89205.
[FreeBSD/FreeBSD.git] / lib / AST / DeclPrinter.cpp
1 //===--- 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/Format.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
25
26 namespace {
27   class VISIBILITY_HIDDEN DeclPrinter : public DeclVisitor<DeclPrinter> {
28     llvm::raw_ostream &Out;
29     ASTContext &Context;
30     PrintingPolicy Policy;
31     unsigned Indentation;
32
33     llvm::raw_ostream& Indent();
34     void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
35
36     void Print(AccessSpecifier AS);
37
38   public:
39     DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
40                 const PrintingPolicy &Policy,
41                 unsigned Indentation = 0)
42       : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
43
44     void VisitDeclContext(DeclContext *DC, bool Indent = true);
45
46     void VisitTranslationUnitDecl(TranslationUnitDecl *D);
47     void VisitTypedefDecl(TypedefDecl *D);
48     void VisitEnumDecl(EnumDecl *D);
49     void VisitRecordDecl(RecordDecl *D);
50     void VisitEnumConstantDecl(EnumConstantDecl *D);
51     void VisitFunctionDecl(FunctionDecl *D);
52     void VisitFieldDecl(FieldDecl *D);
53     void VisitVarDecl(VarDecl *D);
54     void VisitParmVarDecl(ParmVarDecl *D);
55     void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
56     void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D);
57     void VisitNamespaceDecl(NamespaceDecl *D);
58     void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
59     void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
60     void VisitCXXRecordDecl(CXXRecordDecl *D);
61     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
62     void VisitTemplateDecl(TemplateDecl *D);
63     void VisitObjCMethodDecl(ObjCMethodDecl *D);
64     void VisitObjCClassDecl(ObjCClassDecl *D);
65     void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
66     void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
67     void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
68     void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
69     void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
70     void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
71     void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
72     void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
73     void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
74     void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
75     void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
76     void VisitUsingDecl(UsingDecl *D);
77     void VisitUsingShadowDecl(UsingShadowDecl *D);
78   };
79 }
80
81 void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
82   print(Out, getASTContext().PrintingPolicy, Indentation);
83 }
84
85 void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
86                  unsigned Indentation) const {
87   DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
88   Printer.Visit(const_cast<Decl*>(this));
89 }
90
91 static QualType GetBaseType(QualType T) {
92   // FIXME: This should be on the Type class!
93   QualType BaseType = T;
94   while (!BaseType->isSpecifierType()) {
95     if (isa<TypedefType>(BaseType))
96       break;
97     else if (const PointerType* PTy = BaseType->getAs<PointerType>())
98       BaseType = PTy->getPointeeType();
99     else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
100       BaseType = ATy->getElementType();
101     else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
102       BaseType = FTy->getResultType();
103     else if (const VectorType *VTy = BaseType->getAs<VectorType>())
104       BaseType = VTy->getElementType();
105     else
106       assert(0 && "Unknown declarator!");
107   }
108   return BaseType;
109 }
110
111 static QualType getDeclType(Decl* D) {
112   if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
113     return TDD->getUnderlyingType();
114   if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
115     return VD->getType();
116   return QualType();
117 }
118
119 void Decl::printGroup(Decl** Begin, unsigned NumDecls,
120                       llvm::raw_ostream &Out, const PrintingPolicy &Policy,
121                       unsigned Indentation) {
122   if (NumDecls == 1) {
123     (*Begin)->print(Out, Policy, Indentation);
124     return;
125   }
126
127   Decl** End = Begin + NumDecls;
128   TagDecl* TD = dyn_cast<TagDecl>(*Begin);
129   if (TD)
130     ++Begin;
131
132   PrintingPolicy SubPolicy(Policy);
133   if (TD && TD->isDefinition()) {
134     TD->print(Out, Policy, Indentation);
135     Out << " ";
136     SubPolicy.SuppressTag = true;
137   }
138
139   bool isFirst = true;
140   for ( ; Begin != End; ++Begin) {
141     if (isFirst) {
142       SubPolicy.SuppressSpecifiers = false;
143       isFirst = false;
144     } else {
145       if (!isFirst) Out << ", ";
146       SubPolicy.SuppressSpecifiers = true;
147     }
148
149     (*Begin)->print(Out, SubPolicy, Indentation);
150   }
151 }
152
153 void Decl::dump() const {
154   print(llvm::errs());
155 }
156
157 llvm::raw_ostream& DeclPrinter::Indent() {
158   for (unsigned i = 0; i < Indentation; ++i)
159     Out << "  ";
160   return Out;
161 }
162
163 void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
164   this->Indent();
165   Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
166   Out << ";\n";
167   Decls.clear();
168
169 }
170
171 void DeclPrinter::Print(AccessSpecifier AS) {
172   switch(AS) {
173   case AS_none:      assert(0 && "No access specifier!"); break;
174   case AS_public:    Out << "public"; break;
175   case AS_protected: Out << "protected"; break;
176   case AS_private:   Out << " private"; break;
177   }
178 }
179
180 //----------------------------------------------------------------------------
181 // Common C declarations
182 //----------------------------------------------------------------------------
183
184 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
185   if (Indent)
186     Indentation += Policy.Indentation;
187
188   bool PrintAccess = isa<CXXRecordDecl>(DC);
189   AccessSpecifier CurAS = AS_none;
190
191   llvm::SmallVector<Decl*, 2> Decls;
192   for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
193        D != DEnd; ++D) {
194     if (!Policy.Dump) {
195       // Skip over implicit declarations in pretty-printing mode.
196       if (D->isImplicit()) continue;
197       // FIXME: Ugly hack so we don't pretty-print the builtin declaration
198       // of __builtin_va_list.  There should be some other way to check that.
199       if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
200           "__builtin_va_list")
201         continue;
202     }
203
204     if (PrintAccess) {
205       AccessSpecifier AS = D->getAccess();
206
207       if (AS != CurAS) {
208         Print(AS);
209         Out << ":\n";
210         CurAS = AS;
211       }
212     }
213
214     // The next bits of code handles stuff like "struct {int x;} a,b"; we're
215     // forced to merge the declarations because there's no other way to
216     // refer to the struct in question.  This limited merging is safe without
217     // a bunch of other checks because it only merges declarations directly
218     // referring to the tag, not typedefs.
219     //
220     // Check whether the current declaration should be grouped with a previous
221     // unnamed struct.
222     QualType CurDeclType = getDeclType(*D);
223     if (!Decls.empty() && !CurDeclType.isNull()) {
224       QualType BaseType = GetBaseType(CurDeclType);
225       if (!BaseType.isNull() && isa<TagType>(BaseType) &&
226           cast<TagType>(BaseType)->getDecl() == Decls[0]) {
227         Decls.push_back(*D);
228         continue;
229       }
230     }
231
232     // If we have a merged group waiting to be handled, handle it now.
233     if (!Decls.empty())
234       ProcessDeclGroup(Decls);
235
236     // If the current declaration is an unnamed tag type, save it
237     // so we can merge it with the subsequent declaration(s) using it.
238     if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
239       Decls.push_back(*D);
240       continue;
241     }
242     this->Indent();
243     Visit(*D);
244
245     // FIXME: Need to be able to tell the DeclPrinter when
246     const char *Terminator = 0;
247     if (isa<FunctionDecl>(*D) &&
248         cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
249       Terminator = 0;
250     else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
251       Terminator = 0;
252     else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
253              isa<ObjCImplementationDecl>(*D) ||
254              isa<ObjCInterfaceDecl>(*D) ||
255              isa<ObjCProtocolDecl>(*D) ||
256              isa<ObjCCategoryImplDecl>(*D) ||
257              isa<ObjCCategoryDecl>(*D))
258       Terminator = 0;
259     else if (isa<EnumConstantDecl>(*D)) {
260       DeclContext::decl_iterator Next = D;
261       ++Next;
262       if (Next != DEnd)
263         Terminator = ",";
264     } else
265       Terminator = ";";
266
267     if (Terminator)
268       Out << Terminator;
269     Out << "\n";
270   }
271
272   if (!Decls.empty())
273     ProcessDeclGroup(Decls);
274
275   if (Indent)
276     Indentation -= Policy.Indentation;
277 }
278
279 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
280   VisitDeclContext(D, false);
281 }
282
283 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
284   std::string S = D->getNameAsString();
285   D->getUnderlyingType().getAsStringInternal(S, Policy);
286   if (!Policy.SuppressSpecifiers)
287     Out << "typedef ";
288   Out << S;
289 }
290
291 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
292   Out << "enum " << D->getNameAsString() << " {\n";
293   VisitDeclContext(D);
294   Indent() << "}";
295 }
296
297 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
298   Out << D->getKindName();
299   if (D->getIdentifier()) {
300     Out << " ";
301     Out << D->getNameAsString();
302   }
303
304   if (D->isDefinition()) {
305     Out << " {\n";
306     VisitDeclContext(D);
307     Indent() << "}";
308   }
309 }
310
311 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
312   Out << D->getNameAsString();
313   if (Expr *Init = D->getInitExpr()) {
314     Out << " = ";
315     Init->printPretty(Out, Context, 0, Policy, Indentation);
316   }
317 }
318
319 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
320   if (!Policy.SuppressSpecifiers) {
321     switch (D->getStorageClass()) {
322     case FunctionDecl::None: break;
323     case FunctionDecl::Extern: Out << "extern "; break;
324     case FunctionDecl::Static: Out << "static "; break;
325     case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
326     }
327
328     if (D->isInlineSpecified())           Out << "inline ";
329     if (D->isVirtualAsWritten()) Out << "virtual ";
330   }
331
332   PrintingPolicy SubPolicy(Policy);
333   SubPolicy.SuppressSpecifiers = false;
334   std::string Proto = D->getNameAsString();
335   if (isa<FunctionType>(D->getType().getTypePtr())) {
336     const FunctionType *AFT = D->getType()->getAs<FunctionType>();
337
338     const FunctionProtoType *FT = 0;
339     if (D->hasWrittenPrototype())
340       FT = dyn_cast<FunctionProtoType>(AFT);
341
342     Proto += "(";
343     if (FT) {
344       llvm::raw_string_ostream POut(Proto);
345       DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
346       for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
347         if (i) POut << ", ";
348         ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
349       }
350
351       if (FT->isVariadic()) {
352         if (D->getNumParams()) POut << ", ";
353         POut << "...";
354       }
355     } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
356       for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
357         if (i)
358           Proto += ", ";
359         Proto += D->getParamDecl(i)->getNameAsString();
360       }
361     }
362
363     Proto += ")";
364     if (D->hasAttr<NoReturnAttr>())
365       Proto += " __attribute((noreturn))";
366     if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
367       if (CDecl->getNumBaseOrMemberInitializers() > 0) {
368         Proto += " : ";
369         Out << Proto;
370         Proto.clear();
371         for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
372              E = CDecl->init_end();
373              B != E; ++B) {
374           CXXBaseOrMemberInitializer * BMInitializer = (*B);
375           if (B != CDecl->init_begin())
376             Out << ", ";
377           bool hasArguments = (BMInitializer->arg_begin() !=
378                                BMInitializer->arg_end());
379           if (BMInitializer->isMemberInitializer()) {
380             FieldDecl *FD = BMInitializer->getMember();
381             Out <<  FD->getNameAsString();
382           }
383           else // FIXME. skip dependent types for now.
384             if (const RecordType *RT =
385                 BMInitializer->getBaseClass()->getAs<RecordType>()) {
386               const CXXRecordDecl *BaseDecl =
387                 cast<CXXRecordDecl>(RT->getDecl());
388               Out << BaseDecl->getNameAsString();
389           }
390           if (hasArguments) {
391             Out << "(";
392             for (CXXBaseOrMemberInitializer::const_arg_iterator BE =
393                  BMInitializer->const_arg_begin(),
394                  EE =  BMInitializer->const_arg_end(); BE != EE; ++BE) {
395               if (BE != BMInitializer->const_arg_begin())
396                 Out<< ", ";
397               const Expr *Exp = (*BE);
398               Exp->printPretty(Out, Context, 0, Policy, Indentation);
399             }
400             Out << ")";
401           } else
402             Out << "()";
403         }
404       }
405     }
406     else
407       AFT->getResultType().getAsStringInternal(Proto, Policy);
408   } else {
409     D->getType().getAsStringInternal(Proto, Policy);
410   }
411
412   Out << Proto;
413
414   if (D->isPure())
415     Out << " = 0";
416   else if (D->isDeleted())
417     Out << " = delete";
418   else if (D->isThisDeclarationADefinition()) {
419     if (!D->hasPrototype() && D->getNumParams()) {
420       // This is a K&R function definition, so we need to print the
421       // parameters.
422       Out << '\n';
423       DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
424       Indentation += Policy.Indentation;
425       for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
426         Indent();
427         ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
428         Out << ";\n";
429       }
430       Indentation -= Policy.Indentation;
431     } else
432       Out << ' ';
433
434     D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
435     Out << '\n';
436   }
437 }
438
439 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
440   if (!Policy.SuppressSpecifiers && D->isMutable())
441     Out << "mutable ";
442
443   std::string Name = D->getNameAsString();
444   D->getType().getAsStringInternal(Name, Policy);
445   Out << Name;
446
447   if (D->isBitField()) {
448     Out << " : ";
449     D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
450   }
451 }
452
453 void DeclPrinter::VisitVarDecl(VarDecl *D) {
454   if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
455     Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
456
457   if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
458     Out << "__thread ";
459
460   std::string Name = D->getNameAsString();
461   QualType T = D->getType();
462   if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
463     T = Parm->getOriginalType();
464   T.getAsStringInternal(Name, Policy);
465   Out << Name;
466   if (D->getInit()) {
467     if (D->hasCXXDirectInitializer())
468       Out << "(";
469     else
470       Out << " = ";
471     D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
472     if (D->hasCXXDirectInitializer())
473       Out << ")";
474   }
475 }
476
477 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
478   VisitVarDecl(D);
479 }
480
481 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
482   Out << "__asm (";
483   D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
484   Out << ")";
485 }
486
487 //----------------------------------------------------------------------------
488 // C++ declarations
489 //----------------------------------------------------------------------------
490 void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
491   assert(false &&
492          "OverloadedFunctionDecls aren't really decls and are never printed");
493 }
494
495 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
496   Out << "namespace " << D->getNameAsString() << " {\n";
497   VisitDeclContext(D);
498   Indent() << "}";
499 }
500
501 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
502   Out << "using namespace ";
503   if (D->getQualifier())
504     D->getQualifier()->print(Out, Policy);
505   Out << D->getNominatedNamespace()->getNameAsString();
506 }
507
508 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
509   Out << "namespace " << D->getNameAsString() << " = ";
510   if (D->getQualifier())
511     D->getQualifier()->print(Out, Policy);
512   Out << D->getAliasedNamespace()->getNameAsString();
513 }
514
515 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
516   Out << D->getKindName();
517   if (D->getIdentifier()) {
518     Out << " ";
519     Out << D->getNameAsString();
520   }
521
522   if (D->isDefinition()) {
523     // Print the base classes
524     if (D->getNumBases()) {
525       Out << " : ";
526       for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
527              BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
528         if (Base != D->bases_begin())
529           Out << ", ";
530
531         if (Base->isVirtual())
532           Out << "virtual ";
533
534         AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
535         if (AS != AS_none)
536           Print(AS);
537         Out << " " << Base->getType().getAsString(Policy);
538       }
539     }
540
541     // Print the class definition
542     // FIXME: Doesn't print access specifiers, e.g., "public:"
543     Out << " {\n";
544     VisitDeclContext(D);
545     Indent() << "}";
546   }
547 }
548
549 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
550   const char *l;
551   if (D->getLanguage() == LinkageSpecDecl::lang_c)
552     l = "C";
553   else {
554     assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
555            "unknown language in linkage specification");
556     l = "C++";
557   }
558
559   Out << "extern \"" << l << "\" ";
560   if (D->hasBraces()) {
561     Out << "{\n";
562     VisitDeclContext(D);
563     Indent() << "}";
564   } else
565     Visit(*D->decls_begin());
566 }
567
568 void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
569   Out << "template <";
570
571   TemplateParameterList *Params = D->getTemplateParameters();
572   for (unsigned i = 0, e = Params->size(); i != e; ++i) {
573     if (i != 0)
574       Out << ", ";
575
576     const Decl *Param = Params->getParam(i);
577     if (const TemplateTypeParmDecl *TTP =
578           dyn_cast<TemplateTypeParmDecl>(Param)) {
579
580       QualType ParamType =
581         Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
582
583       if (TTP->wasDeclaredWithTypename())
584         Out << "typename ";
585       else
586         Out << "class ";
587
588       if (TTP->isParameterPack())
589         Out << "... ";
590
591       Out << ParamType.getAsString(Policy);
592
593       if (TTP->hasDefaultArgument()) {
594         Out << " = ";
595         Out << TTP->getDefaultArgument().getAsString(Policy);
596       };
597     } else if (const NonTypeTemplateParmDecl *NTTP =
598                  dyn_cast<NonTypeTemplateParmDecl>(Param)) {
599       Out << NTTP->getType().getAsString(Policy);
600
601       if (IdentifierInfo *Name = NTTP->getIdentifier()) {
602         Out << ' ';
603         Out << Name->getName();
604       }
605
606       if (NTTP->hasDefaultArgument()) {
607         Out << " = ";
608         NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
609                                                 Indentation);
610       }
611     }
612   }
613
614   Out << "> ";
615
616   Visit(D->getTemplatedDecl());
617 }
618
619 //----------------------------------------------------------------------------
620 // Objective-C declarations
621 //----------------------------------------------------------------------------
622
623 void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
624   Out << "@class ";
625   for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
626        I != E; ++I) {
627     if (I != D->begin()) Out << ", ";
628     Out << I->getInterface()->getNameAsString();
629   }
630 }
631
632 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
633   if (OMD->isInstanceMethod())
634     Out << "- ";
635   else
636     Out << "+ ";
637   if (!OMD->getResultType().isNull())
638     Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
639
640   std::string name = OMD->getSelector().getAsString();
641   std::string::size_type pos, lastPos = 0;
642   for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
643        E = OMD->param_end(); PI != E; ++PI) {
644     // FIXME: selector is missing here!
645     pos = name.find_first_of(":", lastPos);
646     Out << " " << name.substr(lastPos, pos - lastPos);
647     Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
648         << (*PI)->getNameAsString();
649     lastPos = pos + 1;
650   }
651
652   if (OMD->param_begin() == OMD->param_end())
653     Out << " " << name;
654
655   if (OMD->isVariadic())
656       Out << ", ...";
657
658   if (OMD->getBody()) {
659     Out << ' ';
660     OMD->getBody()->printPretty(Out, Context, 0, Policy);
661     Out << '\n';
662   }
663 }
664
665 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
666   std::string I = OID->getNameAsString();
667   ObjCInterfaceDecl *SID = OID->getSuperClass();
668
669   if (SID)
670     Out << "@implementation " << I << " : " << SID->getNameAsString();
671   else
672     Out << "@implementation " << I;
673   Out << "\n";
674   VisitDeclContext(OID, false);
675   Out << "@end";
676 }
677
678 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
679   std::string I = OID->getNameAsString();
680   ObjCInterfaceDecl *SID = OID->getSuperClass();
681
682   if (SID)
683     Out << "@interface " << I << " : " << SID->getNameAsString();
684   else
685     Out << "@interface " << I;
686
687   // Protocols?
688   const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
689   if (!Protocols.empty()) {
690     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
691          E = Protocols.end(); I != E; ++I)
692       Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
693   }
694
695   if (!Protocols.empty())
696     Out << "> ";
697
698   if (OID->ivar_size() > 0) {
699     Out << "{\n";
700     Indentation += Policy.Indentation;
701     for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
702          E = OID->ivar_end(); I != E; ++I) {
703       Indent() << (*I)->getType().getAsString(Policy)
704           << ' '  << (*I)->getNameAsString() << ";\n";
705     }
706     Indentation -= Policy.Indentation;
707     Out << "}\n";
708   }
709
710   VisitDeclContext(OID, false);
711   Out << "@end";
712   // FIXME: implement the rest...
713 }
714
715 void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
716   Out << "@protocol ";
717   for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
718          E = D->protocol_end();
719        I != E; ++I) {
720     if (I != D->protocol_begin()) Out << ", ";
721     Out << (*I)->getNameAsString();
722   }
723 }
724
725 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
726   Out << "@protocol " << PID->getNameAsString() << '\n';
727   VisitDeclContext(PID, false);
728   Out << "@end";
729 }
730
731 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
732   Out << "@implementation "
733       << PID->getClassInterface()->getNameAsString()
734       << '(' << PID->getNameAsString() << ")\n";
735
736   VisitDeclContext(PID, false);
737   Out << "@end";
738   // FIXME: implement the rest...
739 }
740
741 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
742   Out << "@interface "
743       << PID->getClassInterface()->getNameAsString()
744       << '(' << PID->getNameAsString() << ")\n";
745   VisitDeclContext(PID, false);
746   Out << "@end";
747
748   // FIXME: implement the rest...
749 }
750
751 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
752   Out << "@compatibility_alias " << AID->getNameAsString()
753       << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
754 }
755
756 /// PrintObjCPropertyDecl - print a property declaration.
757 ///
758 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
759   if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
760     Out << "@required\n";
761   else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
762     Out << "@optional\n";
763
764   Out << "@property";
765   if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
766     bool first = true;
767     Out << " (";
768     if (PDecl->getPropertyAttributes() &
769         ObjCPropertyDecl::OBJC_PR_readonly) {
770       Out << (first ? ' ' : ',') << "readonly";
771       first = false;
772   }
773
774   if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
775     Out << (first ? ' ' : ',') << "getter = "
776         << PDecl->getGetterName().getAsString();
777     first = false;
778   }
779   if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
780     Out << (first ? ' ' : ',') << "setter = "
781         << PDecl->getSetterName().getAsString();
782     first = false;
783   }
784
785   if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
786     Out << (first ? ' ' : ',') << "assign";
787     first = false;
788   }
789
790   if (PDecl->getPropertyAttributes() &
791       ObjCPropertyDecl::OBJC_PR_readwrite) {
792     Out << (first ? ' ' : ',') << "readwrite";
793     first = false;
794   }
795
796   if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
797     Out << (first ? ' ' : ',') << "retain";
798     first = false;
799   }
800
801   if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
802     Out << (first ? ' ' : ',') << "copy";
803     first = false;
804   }
805
806   if (PDecl->getPropertyAttributes() &
807       ObjCPropertyDecl::OBJC_PR_nonatomic) {
808     Out << (first ? ' ' : ',') << "nonatomic";
809     first = false;
810   }
811   Out << " )";
812   }
813   Out << ' ' << PDecl->getType().getAsString(Policy)
814   << ' ' << PDecl->getNameAsString();
815 }
816
817 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
818   if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
819     Out << "@synthesize ";
820   else
821     Out << "@dynamic ";
822   Out << PID->getPropertyDecl()->getNameAsString();
823   if (PID->getPropertyIvarDecl())
824     Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
825 }
826
827 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
828   Out << "using ";
829   D->getTargetNestedNameDecl()->print(Out, Policy);
830   Out << D->getNameAsString();
831 }
832
833 void
834 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
835   Out << "using typename ";
836   D->getTargetNestedNameSpecifier()->print(Out, Policy);
837   Out << D->getDeclName().getAsString();
838 }
839
840 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
841   Out << "using ";
842   D->getTargetNestedNameSpecifier()->print(Out, Policy);
843   Out << D->getDeclName().getAsString();
844 }
845
846 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
847   // ignore
848 }