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