DeclPrinter.cpp revision 195341
1193326Sed//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This file implements the Decl::dump method, which pretty print the
11193326Sed// AST back out to C/Objective-C/C++/Objective-C++ code.
12193326Sed//
13193326Sed//===----------------------------------------------------------------------===//
14193326Sed#include "clang/AST/ASTContext.h"
15193326Sed#include "clang/AST/DeclVisitor.h"
16193326Sed#include "clang/AST/Decl.h"
17193326Sed#include "clang/AST/DeclCXX.h"
18193326Sed#include "clang/AST/DeclObjC.h"
19193326Sed#include "clang/AST/Expr.h"
20193326Sed#include "clang/AST/PrettyPrinter.h"
21193326Sed#include "llvm/Support/Compiler.h"
22193326Sed#include "llvm/Support/Streams.h"
23193326Sed#include "llvm/Support/Format.h"
24193326Sed#include "llvm/Support/raw_ostream.h"
25193326Sedusing namespace clang;
26193326Sed
27193326Sednamespace {
28193326Sed  class VISIBILITY_HIDDEN DeclPrinter : public DeclVisitor<DeclPrinter> {
29193326Sed    llvm::raw_ostream &Out;
30193326Sed    ASTContext &Context;
31193326Sed    PrintingPolicy Policy;
32193326Sed    unsigned Indentation;
33193326Sed
34193326Sed    llvm::raw_ostream& Indent();
35193326Sed    void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
36193326Sed
37193326Sed  public:
38193326Sed    DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
39193326Sed                const PrintingPolicy &Policy,
40193326Sed                unsigned Indentation = 0)
41193326Sed      : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
42193326Sed
43193326Sed    void VisitDeclContext(DeclContext *DC, bool Indent = true);
44193326Sed
45193326Sed    void VisitTranslationUnitDecl(TranslationUnitDecl *D);
46193326Sed    void VisitTypedefDecl(TypedefDecl *D);
47193326Sed    void VisitEnumDecl(EnumDecl *D);
48193326Sed    void VisitRecordDecl(RecordDecl *D);
49193326Sed    void VisitEnumConstantDecl(EnumConstantDecl *D);
50193326Sed    void VisitFunctionDecl(FunctionDecl *D);
51193326Sed    void VisitFieldDecl(FieldDecl *D);
52193326Sed    void VisitVarDecl(VarDecl *D);
53193326Sed    void VisitParmVarDecl(ParmVarDecl *D);
54193326Sed    void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
55193326Sed    void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
56193326Sed    void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D);
57193326Sed    void VisitNamespaceDecl(NamespaceDecl *D);
58193326Sed    void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
59193326Sed    void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
60193326Sed    void VisitCXXRecordDecl(CXXRecordDecl *D);
61193326Sed    void VisitLinkageSpecDecl(LinkageSpecDecl *D);
62193326Sed    void VisitTemplateDecl(TemplateDecl *D);
63193326Sed    void VisitObjCMethodDecl(ObjCMethodDecl *D);
64193326Sed    void VisitObjCClassDecl(ObjCClassDecl *D);
65193326Sed    void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
66193326Sed    void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
67193326Sed    void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
68193326Sed    void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
69193326Sed    void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
70193326Sed    void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
71193326Sed    void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
72193326Sed    void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
73193326Sed    void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
74193326Sed  };
75193326Sed}
76193326Sed
77195341Sedvoid Decl::print(llvm::raw_ostream &Out, unsigned Indentation) {
78195341Sed  print(Out, getASTContext().PrintingPolicy, Indentation);
79193326Sed}
80193326Sed
81195341Sedvoid Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
82195341Sed                 unsigned Indentation) {
83195341Sed  DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
84193326Sed  Printer.Visit(this);
85193326Sed}
86193326Sed
87193326Sedstatic QualType GetBaseType(QualType T) {
88193326Sed  // FIXME: This should be on the Type class!
89193326Sed  QualType BaseType = T;
90193326Sed  while (!BaseType->isSpecifierType()) {
91193326Sed    if (isa<TypedefType>(BaseType))
92193326Sed      break;
93193326Sed    else if (const PointerType* PTy = BaseType->getAsPointerType())
94193326Sed      BaseType = PTy->getPointeeType();
95193326Sed    else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
96193326Sed      BaseType = ATy->getElementType();
97193326Sed    else if (const FunctionType* FTy = BaseType->getAsFunctionType())
98193326Sed      BaseType = FTy->getResultType();
99195341Sed    else if (const VectorType *VTy = BaseType->getAsVectorType())
100195341Sed      BaseType = VTy->getElementType();
101193326Sed    else
102193326Sed      assert(0 && "Unknown declarator!");
103193326Sed  }
104193326Sed  return BaseType;
105193326Sed}
106193326Sed
107193326Sedstatic QualType getDeclType(Decl* D) {
108193326Sed  if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
109193326Sed    return TDD->getUnderlyingType();
110193326Sed  if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
111193326Sed    return VD->getType();
112193326Sed  return QualType();
113193326Sed}
114193326Sed
115193326Sedvoid Decl::printGroup(Decl** Begin, unsigned NumDecls,
116195341Sed                      llvm::raw_ostream &Out, const PrintingPolicy &Policy,
117193326Sed                      unsigned Indentation) {
118193326Sed  if (NumDecls == 1) {
119195341Sed    (*Begin)->print(Out, Policy, Indentation);
120193326Sed    return;
121193326Sed  }
122193326Sed
123193326Sed  Decl** End = Begin + NumDecls;
124193326Sed  TagDecl* TD = dyn_cast<TagDecl>(*Begin);
125193326Sed  if (TD)
126193326Sed    ++Begin;
127193326Sed
128193326Sed  PrintingPolicy SubPolicy(Policy);
129193326Sed  if (TD && TD->isDefinition()) {
130195341Sed    TD->print(Out, Policy, Indentation);
131193326Sed    Out << " ";
132193326Sed    SubPolicy.SuppressTag = true;
133193326Sed  }
134193326Sed
135193326Sed  bool isFirst = true;
136193326Sed  for ( ; Begin != End; ++Begin) {
137193326Sed    if (isFirst) {
138193326Sed      SubPolicy.SuppressSpecifiers = false;
139193326Sed      isFirst = false;
140193326Sed    } else {
141193326Sed      if (!isFirst) Out << ", ";
142193326Sed      SubPolicy.SuppressSpecifiers = true;
143193326Sed    }
144193326Sed
145195341Sed    (*Begin)->print(Out, SubPolicy, Indentation);
146193326Sed  }
147193326Sed}
148193326Sed
149195341Sedvoid Decl::dump() {
150195341Sed  print(llvm::errs());
151193326Sed}
152193326Sed
153193326Sedllvm::raw_ostream& DeclPrinter::Indent() {
154193326Sed  for (unsigned i = 0; i < Indentation; ++i)
155193326Sed    Out << "  ";
156193326Sed  return Out;
157193326Sed}
158193326Sed
159193326Sedvoid DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
160193326Sed  this->Indent();
161195341Sed  Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
162193326Sed  Out << ";\n";
163193326Sed  Decls.clear();
164193326Sed
165193326Sed}
166193326Sed
167193326Sed//----------------------------------------------------------------------------
168193326Sed// Common C declarations
169193326Sed//----------------------------------------------------------------------------
170193326Sed
171193326Sedvoid DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
172193326Sed  if (Indent)
173193326Sed    Indentation += Policy.Indentation;
174193326Sed
175193326Sed  llvm::SmallVector<Decl*, 2> Decls;
176195341Sed  for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
177193326Sed       D != DEnd; ++D) {
178193326Sed    if (!Policy.Dump) {
179193326Sed      // Skip over implicit declarations in pretty-printing mode.
180193326Sed      if (D->isImplicit()) continue;
181193326Sed      // FIXME: Ugly hack so we don't pretty-print the builtin declaration
182193326Sed      // of __builtin_va_list.  There should be some other way to check that.
183193326Sed      if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
184193326Sed          "__builtin_va_list")
185193326Sed        continue;
186193326Sed    }
187193326Sed
188193326Sed    // The next bits of code handles stuff like "struct {int x;} a,b"; we're
189193326Sed    // forced to merge the declarations because there's no other way to
190193326Sed    // refer to the struct in question.  This limited merging is safe without
191193326Sed    // a bunch of other checks because it only merges declarations directly
192193326Sed    // referring to the tag, not typedefs.
193193326Sed    //
194193326Sed    // Check whether the current declaration should be grouped with a previous
195193326Sed    // unnamed struct.
196193326Sed    QualType CurDeclType = getDeclType(*D);
197193326Sed    if (!Decls.empty() && !CurDeclType.isNull()) {
198193326Sed      QualType BaseType = GetBaseType(CurDeclType);
199193326Sed      if (!BaseType.isNull() && isa<TagType>(BaseType) &&
200193326Sed          cast<TagType>(BaseType)->getDecl() == Decls[0]) {
201193326Sed        Decls.push_back(*D);
202193326Sed        continue;
203193326Sed      }
204193326Sed    }
205193326Sed
206193326Sed    // If we have a merged group waiting to be handled, handle it now.
207193326Sed    if (!Decls.empty())
208193326Sed      ProcessDeclGroup(Decls);
209193326Sed
210193326Sed    // If the current declaration is an unnamed tag type, save it
211193326Sed    // so we can merge it with the subsequent declaration(s) using it.
212193326Sed    if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
213193326Sed      Decls.push_back(*D);
214193326Sed      continue;
215193326Sed    }
216193326Sed    this->Indent();
217193326Sed    Visit(*D);
218193326Sed
219193326Sed    // FIXME: Need to be able to tell the DeclPrinter when
220193326Sed    const char *Terminator = 0;
221193326Sed    if (isa<FunctionDecl>(*D) &&
222193326Sed        cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
223193326Sed      Terminator = 0;
224193326Sed    else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
225193326Sed      Terminator = 0;
226193326Sed    else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
227193326Sed             isa<ObjCImplementationDecl>(*D) ||
228193326Sed             isa<ObjCInterfaceDecl>(*D) ||
229193326Sed             isa<ObjCProtocolDecl>(*D) ||
230193326Sed             isa<ObjCCategoryImplDecl>(*D) ||
231193326Sed             isa<ObjCCategoryDecl>(*D))
232193326Sed      Terminator = 0;
233193326Sed    else if (isa<EnumConstantDecl>(*D)) {
234193326Sed      DeclContext::decl_iterator Next = D;
235193326Sed      ++Next;
236193326Sed      if (Next != DEnd)
237193326Sed        Terminator = ",";
238193326Sed    } else
239193326Sed      Terminator = ";";
240193326Sed
241193326Sed    if (Terminator)
242193326Sed      Out << Terminator;
243193326Sed    Out << "\n";
244193326Sed  }
245193326Sed
246193326Sed  if (!Decls.empty())
247193326Sed    ProcessDeclGroup(Decls);
248193326Sed
249193326Sed  if (Indent)
250193326Sed    Indentation -= Policy.Indentation;
251193326Sed}
252193326Sed
253193326Sedvoid DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
254193326Sed  VisitDeclContext(D, false);
255193326Sed}
256193326Sed
257193326Sedvoid DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
258193326Sed  std::string S = D->getNameAsString();
259193326Sed  D->getUnderlyingType().getAsStringInternal(S, Policy);
260193326Sed  if (!Policy.SuppressSpecifiers)
261193326Sed    Out << "typedef ";
262193326Sed  Out << S;
263193326Sed}
264193326Sed
265193326Sedvoid DeclPrinter::VisitEnumDecl(EnumDecl *D) {
266193326Sed  Out << "enum " << D->getNameAsString() << " {\n";
267193326Sed  VisitDeclContext(D);
268193326Sed  Indent() << "}";
269193326Sed}
270193326Sed
271193326Sedvoid DeclPrinter::VisitRecordDecl(RecordDecl *D) {
272193326Sed  Out << D->getKindName();
273193326Sed  if (D->getIdentifier()) {
274193326Sed    Out << " ";
275193326Sed    Out << D->getNameAsString();
276193326Sed  }
277193326Sed
278193326Sed  if (D->isDefinition()) {
279193326Sed    Out << " {\n";
280193326Sed    VisitDeclContext(D);
281193326Sed    Indent() << "}";
282193326Sed  }
283193326Sed}
284193326Sed
285193326Sedvoid DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
286193326Sed  Out << D->getNameAsString();
287193326Sed  if (Expr *Init = D->getInitExpr()) {
288193326Sed    Out << " = ";
289193326Sed    Init->printPretty(Out, Context, 0, Policy, Indentation);
290193326Sed  }
291193326Sed}
292193326Sed
293193326Sedvoid DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
294193326Sed  if (!Policy.SuppressSpecifiers) {
295193326Sed    switch (D->getStorageClass()) {
296193326Sed    case FunctionDecl::None: break;
297193326Sed    case FunctionDecl::Extern: Out << "extern "; break;
298193326Sed    case FunctionDecl::Static: Out << "static "; break;
299193326Sed    case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
300193326Sed    }
301193326Sed
302193326Sed    if (D->isInline())           Out << "inline ";
303193326Sed    if (D->isVirtualAsWritten()) Out << "virtual ";
304193326Sed  }
305193326Sed
306193326Sed  PrintingPolicy SubPolicy(Policy);
307193326Sed  SubPolicy.SuppressSpecifiers = false;
308193326Sed  std::string Proto = D->getNameAsString();
309193326Sed  if (isa<FunctionType>(D->getType().getTypePtr())) {
310193326Sed    const FunctionType *AFT = D->getType()->getAsFunctionType();
311193326Sed
312193326Sed    const FunctionProtoType *FT = 0;
313193326Sed    if (D->hasWrittenPrototype())
314193326Sed      FT = dyn_cast<FunctionProtoType>(AFT);
315193326Sed
316193326Sed    Proto += "(";
317193326Sed    if (FT) {
318193326Sed      llvm::raw_string_ostream POut(Proto);
319193326Sed      DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
320193326Sed      for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
321193326Sed        if (i) POut << ", ";
322193326Sed        ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
323193326Sed      }
324193326Sed
325193326Sed      if (FT->isVariadic()) {
326193326Sed        if (D->getNumParams()) POut << ", ";
327193326Sed        POut << "...";
328193326Sed      }
329193326Sed    } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
330193326Sed      for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
331193326Sed        if (i)
332193326Sed          Proto += ", ";
333193326Sed        Proto += D->getParamDecl(i)->getNameAsString();
334193326Sed      }
335193326Sed    }
336193326Sed
337193326Sed    Proto += ")";
338193326Sed    AFT->getResultType().getAsStringInternal(Proto, Policy);
339193326Sed  } else {
340193326Sed    D->getType().getAsStringInternal(Proto, Policy);
341193326Sed  }
342193326Sed
343193326Sed  Out << Proto;
344193326Sed
345193326Sed  if (D->isPure())
346193326Sed    Out << " = 0";
347193326Sed  else if (D->isDeleted())
348193326Sed    Out << " = delete";
349193326Sed  else if (D->isThisDeclarationADefinition()) {
350193326Sed    if (!D->hasPrototype() && D->getNumParams()) {
351193326Sed      // This is a K&R function definition, so we need to print the
352193326Sed      // parameters.
353193326Sed      Out << '\n';
354193326Sed      DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
355193326Sed      Indentation += Policy.Indentation;
356193326Sed      for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
357193326Sed        Indent();
358193326Sed        ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
359193326Sed        Out << ";\n";
360193326Sed      }
361193326Sed      Indentation -= Policy.Indentation;
362193326Sed    } else
363193326Sed      Out << ' ';
364193326Sed
365195341Sed    D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
366193326Sed    Out << '\n';
367193326Sed  }
368193326Sed}
369193326Sed
370193326Sedvoid DeclPrinter::VisitFieldDecl(FieldDecl *D) {
371193326Sed  if (!Policy.SuppressSpecifiers && D->isMutable())
372193326Sed    Out << "mutable ";
373193326Sed
374193326Sed  std::string Name = D->getNameAsString();
375193326Sed  D->getType().getAsStringInternal(Name, Policy);
376193326Sed  Out << Name;
377193326Sed
378193326Sed  if (D->isBitField()) {
379193326Sed    Out << " : ";
380193326Sed    D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
381193326Sed  }
382193326Sed}
383193326Sed
384193326Sedvoid DeclPrinter::VisitVarDecl(VarDecl *D) {
385193326Sed  if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
386193326Sed    Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
387193326Sed
388193326Sed  if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
389193326Sed    Out << "__thread ";
390193326Sed
391193326Sed  std::string Name = D->getNameAsString();
392193326Sed  QualType T = D->getType();
393193326Sed  if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
394193326Sed    T = Parm->getOriginalType();
395193326Sed  T.getAsStringInternal(Name, Policy);
396193326Sed  Out << Name;
397193326Sed  if (D->getInit()) {
398193326Sed    if (D->hasCXXDirectInitializer())
399193326Sed      Out << "(";
400193326Sed    else
401193326Sed      Out << " = ";
402193326Sed    D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
403193326Sed    if (D->hasCXXDirectInitializer())
404193326Sed      Out << ")";
405193326Sed  }
406193326Sed}
407193326Sed
408193326Sedvoid DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
409193326Sed  VisitVarDecl(D);
410193326Sed}
411193326Sed
412193326Sedvoid DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
413193326Sed  VisitVarDecl(D);
414193326Sed}
415193326Sed
416193326Sedvoid DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
417193326Sed  Out << "__asm (";
418193326Sed  D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
419193326Sed  Out << ")";
420193326Sed}
421193326Sed
422193326Sed//----------------------------------------------------------------------------
423193326Sed// C++ declarations
424193326Sed//----------------------------------------------------------------------------
425193326Sedvoid DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
426193326Sed  assert(false &&
427193326Sed         "OverloadedFunctionDecls aren't really decls and are never printed");
428193326Sed}
429193326Sed
430193326Sedvoid DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
431193326Sed  Out << "namespace " << D->getNameAsString() << " {\n";
432193326Sed  VisitDeclContext(D);
433193326Sed  Indent() << "}";
434193326Sed}
435193326Sed
436193326Sedvoid DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
437193326Sed  Out << "using namespace ";
438193326Sed  if (D->getQualifier())
439193326Sed    D->getQualifier()->print(Out, Policy);
440193326Sed  Out << D->getNominatedNamespace()->getNameAsString();
441193326Sed}
442193326Sed
443193326Sedvoid DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
444193326Sed  Out << "namespace " << D->getNameAsString() << " = ";
445193326Sed  if (D->getQualifier())
446193326Sed    D->getQualifier()->print(Out, Policy);
447193326Sed  Out << D->getAliasedNamespace()->getNameAsString();
448193326Sed}
449193326Sed
450193326Sedvoid DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
451193326Sed  Out << D->getKindName();
452193326Sed  if (D->getIdentifier()) {
453193326Sed    Out << " ";
454193326Sed    Out << D->getNameAsString();
455193326Sed  }
456193326Sed
457193326Sed  if (D->isDefinition()) {
458193326Sed    // Print the base classes
459193326Sed    if (D->getNumBases()) {
460193326Sed      Out << " : ";
461193326Sed      for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
462193326Sed                                          BaseEnd = D->bases_end();
463193326Sed          Base != BaseEnd; ++Base) {
464193326Sed        if (Base != D->bases_begin())
465193326Sed          Out << ", ";
466193326Sed
467193326Sed        if (Base->isVirtual())
468193326Sed          Out << "virtual ";
469193326Sed
470193326Sed        switch(Base->getAccessSpecifierAsWritten()) {
471193326Sed        case AS_none:      break;
472193326Sed        case AS_public:    Out << "public "; break;
473193326Sed        case AS_protected: Out << "protected "; break;
474193326Sed        case AS_private:   Out << " private "; break;
475193326Sed        }
476193326Sed
477193326Sed        Out << Base->getType().getAsString(Policy);
478193326Sed      }
479193326Sed    }
480193326Sed
481193326Sed    // Print the class definition
482193326Sed    // FIXME: Doesn't print access specifiers, e.g., "public:"
483193326Sed    Out << " {\n";
484193326Sed    VisitDeclContext(D);
485193326Sed    Indent() << "}";
486193326Sed  }
487193326Sed}
488193326Sed
489193326Sedvoid DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
490193326Sed  const char *l;
491193326Sed  if (D->getLanguage() == LinkageSpecDecl::lang_c)
492193326Sed    l = "C";
493193326Sed  else {
494193326Sed    assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
495193326Sed           "unknown language in linkage specification");
496193326Sed    l = "C++";
497193326Sed  }
498193326Sed
499193326Sed  Out << "extern \"" << l << "\" ";
500193326Sed  if (D->hasBraces()) {
501193326Sed    Out << "{\n";
502193326Sed    VisitDeclContext(D);
503193326Sed    Indent() << "}";
504193326Sed  } else
505195341Sed    Visit(*D->decls_begin());
506193326Sed}
507193326Sed
508193326Sedvoid DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
509193576Sed  Out << "template <";
510193576Sed
511193576Sed  TemplateParameterList *Params = D->getTemplateParameters();
512193576Sed  for (unsigned i = 0, e = Params->size(); i != e; ++i) {
513193576Sed    if (i != 0)
514193576Sed      Out << ", ";
515193576Sed
516193576Sed    const Decl *Param = Params->getParam(i);
517193576Sed    if (const TemplateTypeParmDecl *TTP =
518193576Sed          dyn_cast<TemplateTypeParmDecl>(Param)) {
519193576Sed
520193576Sed      QualType ParamType =
521193576Sed        Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
522193576Sed
523193576Sed      if (TTP->wasDeclaredWithTypename())
524193576Sed        Out << "typename ";
525193576Sed      else
526193576Sed        Out << "class ";
527193576Sed
528194179Sed      if (TTP->isParameterPack())
529194179Sed        Out << "... ";
530194179Sed
531193576Sed      Out << ParamType.getAsString(Policy);
532193576Sed
533193576Sed      if (TTP->hasDefaultArgument()) {
534193576Sed        Out << " = ";
535193576Sed        Out << TTP->getDefaultArgument().getAsString(Policy);
536193576Sed      };
537193576Sed    } else if (const NonTypeTemplateParmDecl *NTTP =
538193576Sed                 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
539193576Sed      Out << NTTP->getType().getAsString(Policy);
540193576Sed
541193576Sed      if (IdentifierInfo *Name = NTTP->getIdentifier()) {
542193576Sed        Out << ' ';
543193576Sed        Out << Name->getName();
544193576Sed      }
545193576Sed
546193576Sed      if (NTTP->hasDefaultArgument()) {
547193576Sed        Out << " = ";
548193576Sed        NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
549193576Sed                                                Indentation);
550193576Sed      }
551193576Sed    }
552193576Sed  }
553193576Sed
554193576Sed  Out << "> ";
555193576Sed
556193326Sed  Visit(D->getTemplatedDecl());
557193326Sed}
558193326Sed
559193326Sed//----------------------------------------------------------------------------
560193326Sed// Objective-C declarations
561193326Sed//----------------------------------------------------------------------------
562193326Sed
563193326Sedvoid DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
564193326Sed  Out << "@class ";
565193326Sed  for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
566193326Sed       I != E; ++I) {
567193326Sed    if (I != D->begin()) Out << ", ";
568193326Sed    Out << (*I)->getNameAsString();
569193326Sed  }
570193326Sed}
571193326Sed
572193326Sedvoid DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
573193326Sed  if (OMD->isInstanceMethod())
574193326Sed    Out << "- ";
575193326Sed  else
576193326Sed    Out << "+ ";
577193326Sed  if (!OMD->getResultType().isNull())
578193326Sed    Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
579193326Sed
580193326Sed  std::string name = OMD->getSelector().getAsString();
581193326Sed  std::string::size_type pos, lastPos = 0;
582193326Sed  for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
583193326Sed       E = OMD->param_end(); PI != E; ++PI) {
584193326Sed    // FIXME: selector is missing here!
585193326Sed    pos = name.find_first_of(":", lastPos);
586193326Sed    Out << " " << name.substr(lastPos, pos - lastPos);
587193326Sed    Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
588193326Sed        << (*PI)->getNameAsString();
589193326Sed    lastPos = pos + 1;
590193326Sed  }
591193326Sed
592193326Sed  if (OMD->param_begin() == OMD->param_end())
593193326Sed    Out << " " << name;
594193326Sed
595193326Sed  if (OMD->isVariadic())
596193326Sed      Out << ", ...";
597193326Sed
598193326Sed  if (OMD->getBody()) {
599193326Sed    Out << ' ';
600193326Sed    OMD->getBody()->printPretty(Out, Context, 0, Policy);
601193326Sed    Out << '\n';
602193326Sed  }
603193326Sed}
604193326Sed
605193326Sedvoid DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
606193326Sed  std::string I = OID->getNameAsString();
607193326Sed  ObjCInterfaceDecl *SID = OID->getSuperClass();
608193326Sed
609193326Sed  if (SID)
610193326Sed    Out << "@implementation " << I << " : " << SID->getNameAsString();
611193326Sed  else
612193326Sed    Out << "@implementation " << I;
613193326Sed  Out << "\n";
614193326Sed  VisitDeclContext(OID, false);
615193326Sed  Out << "@end";
616193326Sed}
617193326Sed
618193326Sedvoid DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
619193326Sed  std::string I = OID->getNameAsString();
620193326Sed  ObjCInterfaceDecl *SID = OID->getSuperClass();
621193326Sed
622193326Sed  if (SID)
623193326Sed    Out << "@interface " << I << " : " << SID->getNameAsString();
624193326Sed  else
625193326Sed    Out << "@interface " << I;
626193326Sed
627193326Sed  // Protocols?
628193326Sed  const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
629193326Sed  if (!Protocols.empty()) {
630193326Sed    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
631193326Sed         E = Protocols.end(); I != E; ++I)
632193326Sed      Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
633193326Sed  }
634193326Sed
635193326Sed  if (!Protocols.empty())
636193326Sed    Out << "> ";
637193326Sed
638193326Sed  if (OID->ivar_size() > 0) {
639193326Sed    Out << "{\n";
640193326Sed    Indentation += Policy.Indentation;
641193326Sed    for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
642193326Sed         E = OID->ivar_end(); I != E; ++I) {
643193326Sed      Indent() << (*I)->getType().getAsString(Policy)
644193326Sed          << ' '  << (*I)->getNameAsString() << ";\n";
645193326Sed    }
646193326Sed    Indentation -= Policy.Indentation;
647193326Sed    Out << "}\n";
648193326Sed  }
649193326Sed
650193326Sed  VisitDeclContext(OID, false);
651193326Sed  Out << "@end";
652193326Sed  // FIXME: implement the rest...
653193326Sed}
654193326Sed
655193326Sedvoid DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
656193326Sed  Out << "@protocol ";
657193326Sed  for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
658193326Sed         E = D->protocol_end();
659193326Sed       I != E; ++I) {
660193326Sed    if (I != D->protocol_begin()) Out << ", ";
661193326Sed    Out << (*I)->getNameAsString();
662193326Sed  }
663193326Sed}
664193326Sed
665193326Sedvoid DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
666193326Sed  Out << "@protocol " << PID->getNameAsString() << '\n';
667193326Sed  VisitDeclContext(PID, false);
668193326Sed  Out << "@end";
669193326Sed}
670193326Sed
671193326Sedvoid DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
672193326Sed  Out << "@implementation "
673193326Sed      << PID->getClassInterface()->getNameAsString()
674193326Sed      << '(' << PID->getNameAsString() << ")\n";
675193326Sed
676193326Sed  VisitDeclContext(PID, false);
677193326Sed  Out << "@end";
678193326Sed  // FIXME: implement the rest...
679193326Sed}
680193326Sed
681193326Sedvoid DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
682193326Sed  Out << "@interface "
683193326Sed      << PID->getClassInterface()->getNameAsString()
684193326Sed      << '(' << PID->getNameAsString() << ")\n";
685193326Sed  VisitDeclContext(PID, false);
686193326Sed  Out << "@end";
687193326Sed
688193326Sed  // FIXME: implement the rest...
689193326Sed}
690193326Sed
691193326Sedvoid DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
692193326Sed  Out << "@compatibility_alias " << AID->getNameAsString()
693193326Sed      << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
694193326Sed}
695193326Sed
696193326Sed/// PrintObjCPropertyDecl - print a property declaration.
697193326Sed///
698193326Sedvoid DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
699193326Sed  if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
700193326Sed    Out << "@required\n";
701193326Sed  else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
702193326Sed    Out << "@optional\n";
703193326Sed
704193326Sed  Out << "@property";
705193326Sed  if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
706193326Sed    bool first = true;
707193326Sed    Out << " (";
708193326Sed    if (PDecl->getPropertyAttributes() &
709193326Sed        ObjCPropertyDecl::OBJC_PR_readonly) {
710193326Sed      Out << (first ? ' ' : ',') << "readonly";
711193326Sed      first = false;
712193326Sed  }
713193326Sed
714193326Sed  if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
715193326Sed    Out << (first ? ' ' : ',') << "getter = "
716193326Sed        << PDecl->getGetterName().getAsString();
717193326Sed    first = false;
718193326Sed  }
719193326Sed  if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
720193326Sed    Out << (first ? ' ' : ',') << "setter = "
721193326Sed        << PDecl->getSetterName().getAsString();
722193326Sed    first = false;
723193326Sed  }
724193326Sed
725193326Sed  if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
726193326Sed    Out << (first ? ' ' : ',') << "assign";
727193326Sed    first = false;
728193326Sed  }
729193326Sed
730193326Sed  if (PDecl->getPropertyAttributes() &
731193326Sed      ObjCPropertyDecl::OBJC_PR_readwrite) {
732193326Sed    Out << (first ? ' ' : ',') << "readwrite";
733193326Sed    first = false;
734193326Sed  }
735193326Sed
736193326Sed  if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
737193326Sed    Out << (first ? ' ' : ',') << "retain";
738193326Sed    first = false;
739193326Sed  }
740193326Sed
741193326Sed  if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
742193326Sed    Out << (first ? ' ' : ',') << "copy";
743193326Sed    first = false;
744193326Sed  }
745193326Sed
746193326Sed  if (PDecl->getPropertyAttributes() &
747193326Sed      ObjCPropertyDecl::OBJC_PR_nonatomic) {
748193326Sed    Out << (first ? ' ' : ',') << "nonatomic";
749193326Sed    first = false;
750193326Sed  }
751193326Sed  Out << " )";
752193326Sed  }
753193326Sed  Out << ' ' << PDecl->getType().getAsString(Policy)
754193326Sed  << ' ' << PDecl->getNameAsString();
755193326Sed}
756193326Sed
757193326Sedvoid DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
758193326Sed  if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
759193326Sed    Out << "@synthesize ";
760193326Sed  else
761193326Sed    Out << "@dynamic ";
762193326Sed  Out << PID->getPropertyDecl()->getNameAsString();
763193326Sed  if (PID->getPropertyIvarDecl())
764193326Sed    Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
765193326Sed}
766