DeclPrinter.cpp revision 5948ae1021122164b22f74353bb7fe325a64f616
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/ExprCXX.h"
21#include "clang/AST/PrettyPrinter.h"
22#include "clang/Basic/Module.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25
26namespace {
27  class DeclPrinter : public DeclVisitor<DeclPrinter> {
28    raw_ostream &Out;
29    ASTContext &Context;
30    PrintingPolicy Policy;
31    unsigned Indentation;
32    bool PrintInstantiation;
33
34    raw_ostream& Indent() { return Indent(Indentation); }
35    raw_ostream& Indent(unsigned Indentation);
36    void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
37
38    void Print(AccessSpecifier AS);
39
40  public:
41    DeclPrinter(raw_ostream &Out, ASTContext &Context,
42                const PrintingPolicy &Policy,
43                unsigned Indentation = 0,
44                bool PrintInstantiation = false)
45      : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation),
46        PrintInstantiation(PrintInstantiation) { }
47
48    void VisitDeclContext(DeclContext *DC, bool Indent = true);
49
50    void VisitTranslationUnitDecl(TranslationUnitDecl *D);
51    void VisitTypedefDecl(TypedefDecl *D);
52    void VisitTypeAliasDecl(TypeAliasDecl *D);
53    void VisitEnumDecl(EnumDecl *D);
54    void VisitRecordDecl(RecordDecl *D);
55    void VisitEnumConstantDecl(EnumConstantDecl *D);
56    void VisitFunctionDecl(FunctionDecl *D);
57    void VisitFieldDecl(FieldDecl *D);
58    void VisitVarDecl(VarDecl *D);
59    void VisitLabelDecl(LabelDecl *D);
60    void VisitParmVarDecl(ParmVarDecl *D);
61    void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
62    void VisitImportDecl(ImportDecl *D);
63    void VisitStaticAssertDecl(StaticAssertDecl *D);
64    void VisitNamespaceDecl(NamespaceDecl *D);
65    void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
66    void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
67    void VisitCXXRecordDecl(CXXRecordDecl *D);
68    void VisitLinkageSpecDecl(LinkageSpecDecl *D);
69    void VisitTemplateDecl(const TemplateDecl *D);
70    void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
71    void VisitClassTemplateDecl(ClassTemplateDecl *D);
72    void VisitObjCMethodDecl(ObjCMethodDecl *D);
73    void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
74    void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
75    void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
76    void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
77    void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
78    void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
79    void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
80    void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
81    void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
82    void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
83    void VisitUsingDecl(UsingDecl *D);
84    void VisitUsingShadowDecl(UsingShadowDecl *D);
85
86    void PrintTemplateParameters(const TemplateParameterList *Params,
87                                 const TemplateArgumentList *Args);
88    void prettyPrintAttributes(Decl *D);
89  };
90}
91
92void Decl::print(raw_ostream &Out, unsigned Indentation,
93                 bool PrintInstantiation) const {
94  print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
95}
96
97void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
98                 unsigned Indentation, bool PrintInstantiation) const {
99  DeclPrinter Printer(Out, getASTContext(), Policy, Indentation, PrintInstantiation);
100  Printer.Visit(const_cast<Decl*>(this));
101}
102
103static QualType GetBaseType(QualType T) {
104  // FIXME: This should be on the Type class!
105  QualType BaseType = T;
106  while (!BaseType->isSpecifierType()) {
107    if (isa<TypedefType>(BaseType))
108      break;
109    else if (const PointerType* PTy = BaseType->getAs<PointerType>())
110      BaseType = PTy->getPointeeType();
111    else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
112      BaseType = ATy->getElementType();
113    else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
114      BaseType = FTy->getResultType();
115    else if (const VectorType *VTy = BaseType->getAs<VectorType>())
116      BaseType = VTy->getElementType();
117    else
118      llvm_unreachable("Unknown declarator!");
119  }
120  return BaseType;
121}
122
123static QualType getDeclType(Decl* D) {
124  if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
125    return TDD->getUnderlyingType();
126  if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
127    return VD->getType();
128  return QualType();
129}
130
131void Decl::printGroup(Decl** Begin, unsigned NumDecls,
132                      raw_ostream &Out, const PrintingPolicy &Policy,
133                      unsigned Indentation) {
134  if (NumDecls == 1) {
135    (*Begin)->print(Out, Policy, Indentation);
136    return;
137  }
138
139  Decl** End = Begin + NumDecls;
140  TagDecl* TD = dyn_cast<TagDecl>(*Begin);
141  if (TD)
142    ++Begin;
143
144  PrintingPolicy SubPolicy(Policy);
145  if (TD && TD->isCompleteDefinition()) {
146    TD->print(Out, Policy, Indentation);
147    Out << " ";
148    SubPolicy.SuppressTag = true;
149  }
150
151  bool isFirst = true;
152  for ( ; Begin != End; ++Begin) {
153    if (isFirst) {
154      SubPolicy.SuppressSpecifiers = false;
155      isFirst = false;
156    } else {
157      if (!isFirst) Out << ", ";
158      SubPolicy.SuppressSpecifiers = true;
159    }
160
161    (*Begin)->print(Out, SubPolicy, Indentation);
162  }
163}
164
165void DeclContext::dumpDeclContext() const {
166  // Get the translation unit
167  const DeclContext *DC = this;
168  while (!DC->isTranslationUnit())
169    DC = DC->getParent();
170
171  ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
172  DeclPrinter Printer(llvm::errs(), Ctx, Ctx.getPrintingPolicy(), 0);
173  Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
174}
175
176void Decl::dump() const {
177  print(llvm::errs());
178}
179
180raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
181  for (unsigned i = 0; i != Indentation; ++i)
182    Out << "  ";
183  return Out;
184}
185
186void DeclPrinter::prettyPrintAttributes(Decl *D) {
187  if (D->hasAttrs()) {
188    AttrVec &Attrs = D->getAttrs();
189    for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
190        Attr *A = *i;
191        A->printPretty(Out, Context);
192    }
193  }
194}
195
196void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
197  this->Indent();
198  Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
199  Out << ";\n";
200  Decls.clear();
201
202}
203
204void DeclPrinter::Print(AccessSpecifier AS) {
205  switch(AS) {
206  case AS_none:      llvm_unreachable("No access specifier!");
207  case AS_public:    Out << "public"; break;
208  case AS_protected: Out << "protected"; break;
209  case AS_private:   Out << "private"; break;
210  }
211}
212
213//----------------------------------------------------------------------------
214// Common C declarations
215//----------------------------------------------------------------------------
216
217void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
218  if (Indent)
219    Indentation += Policy.Indentation;
220
221  SmallVector<Decl*, 2> Decls;
222  for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
223       D != DEnd; ++D) {
224
225    // Don't print ObjCIvarDecls, as they are printed when visiting the
226    // containing ObjCInterfaceDecl.
227    if (isa<ObjCIvarDecl>(*D))
228      continue;
229
230    if (!Policy.Dump) {
231      // Skip over implicit declarations in pretty-printing mode.
232      if (D->isImplicit()) continue;
233      // FIXME: Ugly hack so we don't pretty-print the builtin declaration
234      // of __builtin_va_list or __[u]int128_t.  There should be some other way
235      // to check that.
236      if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
237        if (IdentifierInfo *II = ND->getIdentifier()) {
238          if (II->isStr("__builtin_va_list") ||
239              II->isStr("__int128_t") || II->isStr("__uint128_t"))
240            continue;
241        }
242      }
243    }
244
245    // The next bits of code handles stuff like "struct {int x;} a,b"; we're
246    // forced to merge the declarations because there's no other way to
247    // refer to the struct in question.  This limited merging is safe without
248    // a bunch of other checks because it only merges declarations directly
249    // referring to the tag, not typedefs.
250    //
251    // Check whether the current declaration should be grouped with a previous
252    // unnamed struct.
253    QualType CurDeclType = getDeclType(*D);
254    if (!Decls.empty() && !CurDeclType.isNull()) {
255      QualType BaseType = GetBaseType(CurDeclType);
256      if (!BaseType.isNull() && isa<TagType>(BaseType) &&
257          cast<TagType>(BaseType)->getDecl() == Decls[0]) {
258        Decls.push_back(*D);
259        continue;
260      }
261    }
262
263    // If we have a merged group waiting to be handled, handle it now.
264    if (!Decls.empty())
265      ProcessDeclGroup(Decls);
266
267    // If the current declaration is an unnamed tag type, save it
268    // so we can merge it with the subsequent declaration(s) using it.
269    if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
270      Decls.push_back(*D);
271      continue;
272    }
273
274    if (isa<AccessSpecDecl>(*D)) {
275      Indentation -= Policy.Indentation;
276      this->Indent();
277      Print(D->getAccess());
278      Out << ":\n";
279      Indentation += Policy.Indentation;
280      continue;
281    }
282
283    this->Indent();
284    Visit(*D);
285
286    // FIXME: Need to be able to tell the DeclPrinter when
287    const char *Terminator = 0;
288    if (isa<FunctionDecl>(*D) &&
289        cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
290      Terminator = 0;
291    else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
292      Terminator = 0;
293    else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
294             isa<ObjCImplementationDecl>(*D) ||
295             isa<ObjCInterfaceDecl>(*D) ||
296             isa<ObjCProtocolDecl>(*D) ||
297             isa<ObjCCategoryImplDecl>(*D) ||
298             isa<ObjCCategoryDecl>(*D))
299      Terminator = 0;
300    else if (isa<EnumConstantDecl>(*D)) {
301      DeclContext::decl_iterator Next = D;
302      ++Next;
303      if (Next != DEnd)
304        Terminator = ",";
305    } else
306      Terminator = ";";
307
308    if (Terminator)
309      Out << Terminator;
310    Out << "\n";
311  }
312
313  if (!Decls.empty())
314    ProcessDeclGroup(Decls);
315
316  if (Indent)
317    Indentation -= Policy.Indentation;
318}
319
320void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
321  VisitDeclContext(D, false);
322}
323
324void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
325  std::string S = D->getNameAsString();
326  D->getUnderlyingType().getAsStringInternal(S, Policy);
327  if (!Policy.SuppressSpecifiers) {
328    Out << "typedef ";
329
330    if (D->isModulePrivate())
331      Out << "__module_private__ ";
332  }
333  Out << S;
334  prettyPrintAttributes(D);
335}
336
337void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
338  Out << "using " << D->getNameAsString() << " = "
339      << D->getUnderlyingType().getAsString(Policy);
340}
341
342void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
343  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
344    Out << "__module_private__ ";
345  Out << "enum ";
346  if (D->isScoped()) {
347    if (D->isScopedUsingClassTag())
348      Out << "class ";
349    else
350      Out << "struct ";
351  }
352  Out << *D;
353
354  if (D->isFixed()) {
355    std::string Underlying;
356    D->getIntegerType().getAsStringInternal(Underlying, Policy);
357    Out << " : " << Underlying;
358  }
359
360  if (D->isCompleteDefinition()) {
361    Out << " {\n";
362    VisitDeclContext(D);
363    Indent() << "}";
364  }
365  prettyPrintAttributes(D);
366}
367
368void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
369  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
370    Out << "__module_private__ ";
371  Out << D->getKindName();
372  if (D->getIdentifier())
373    Out << ' ' << *D;
374
375  if (D->isCompleteDefinition()) {
376    Out << " {\n";
377    VisitDeclContext(D);
378    Indent() << "}";
379  }
380}
381
382void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
383  Out << *D;
384  if (Expr *Init = D->getInitExpr()) {
385    Out << " = ";
386    Init->printPretty(Out, Context, 0, Policy, Indentation);
387  }
388}
389
390void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
391  if (!Policy.SuppressSpecifiers) {
392    switch (D->getStorageClassAsWritten()) {
393    case SC_None: break;
394    case SC_Extern: Out << "extern "; break;
395    case SC_Static: Out << "static "; break;
396    case SC_PrivateExtern: Out << "__private_extern__ "; break;
397    case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
398      llvm_unreachable("invalid for functions");
399    }
400
401    if (D->isInlineSpecified())  Out << "inline ";
402    if (D->isVirtualAsWritten()) Out << "virtual ";
403    if (D->isModulePrivate())    Out << "__module_private__ ";
404  }
405
406  PrintingPolicy SubPolicy(Policy);
407  SubPolicy.SuppressSpecifiers = false;
408  std::string Proto = D->getNameInfo().getAsString();
409
410  QualType Ty = D->getType();
411  while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
412    Proto = '(' + Proto + ')';
413    Ty = PT->getInnerType();
414  }
415
416  if (isa<FunctionType>(Ty)) {
417    const FunctionType *AFT = Ty->getAs<FunctionType>();
418    const FunctionProtoType *FT = 0;
419    if (D->hasWrittenPrototype())
420      FT = dyn_cast<FunctionProtoType>(AFT);
421
422    Proto += "(";
423    if (FT) {
424      llvm::raw_string_ostream POut(Proto);
425      DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
426      for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
427        if (i) POut << ", ";
428        ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
429      }
430
431      if (FT->isVariadic()) {
432        if (D->getNumParams()) POut << ", ";
433        POut << "...";
434      }
435    } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
436      for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
437        if (i)
438          Proto += ", ";
439        Proto += D->getParamDecl(i)->getNameAsString();
440      }
441    }
442
443    Proto += ")";
444
445    if (FT && FT->getTypeQuals()) {
446      unsigned TypeQuals = FT->getTypeQuals();
447      if (TypeQuals & Qualifiers::Const)
448        Proto += " const";
449      if (TypeQuals & Qualifiers::Volatile)
450        Proto += " volatile";
451      if (TypeQuals & Qualifiers::Restrict)
452        Proto += " restrict";
453    }
454
455    if (FT && FT->hasDynamicExceptionSpec()) {
456      Proto += " throw(";
457      if (FT->getExceptionSpecType() == EST_MSAny)
458        Proto += "...";
459      else
460        for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
461          if (I)
462            Proto += ", ";
463
464          std::string ExceptionType;
465          FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
466          Proto += ExceptionType;
467        }
468      Proto += ")";
469    } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
470      Proto += " noexcept";
471      if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
472        Proto += "(";
473        llvm::raw_string_ostream EOut(Proto);
474        FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
475                                           Indentation);
476        EOut.flush();
477        Proto += EOut.str();
478        Proto += ")";
479      }
480    }
481
482    if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
483      bool HasInitializerList = false;
484      for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
485           E = CDecl->init_end();
486           B != E; ++B) {
487        CXXCtorInitializer * BMInitializer = (*B);
488        if (BMInitializer->isInClassMemberInitializer())
489          continue;
490
491        if (!HasInitializerList) {
492          Proto += " : ";
493          Out << Proto;
494          Proto.clear();
495          HasInitializerList = true;
496        } else
497          Out << ", ";
498
499        if (BMInitializer->isAnyMemberInitializer()) {
500          FieldDecl *FD = BMInitializer->getAnyMember();
501          Out << *FD;
502        } else {
503          Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
504        }
505
506        Out << "(";
507        if (!BMInitializer->getInit()) {
508          // Nothing to print
509        } else {
510          Expr *Init = BMInitializer->getInit();
511          if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
512            Init = Tmp->getSubExpr();
513
514          Init = Init->IgnoreParens();
515
516          Expr *SimpleInit = 0;
517          Expr **Args = 0;
518          unsigned NumArgs = 0;
519          if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
520            Args = ParenList->getExprs();
521            NumArgs = ParenList->getNumExprs();
522          } else if (CXXConstructExpr *Construct
523                                        = dyn_cast<CXXConstructExpr>(Init)) {
524            Args = Construct->getArgs();
525            NumArgs = Construct->getNumArgs();
526          } else
527            SimpleInit = Init;
528
529          if (SimpleInit)
530            SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
531          else {
532            for (unsigned I = 0; I != NumArgs; ++I) {
533              if (isa<CXXDefaultArgExpr>(Args[I]))
534                break;
535
536              if (I)
537                Out << ", ";
538              Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
539            }
540          }
541        }
542        Out << ")";
543      }
544    }
545    else
546      AFT->getResultType().getAsStringInternal(Proto, Policy);
547  } else {
548    Ty.getAsStringInternal(Proto, Policy);
549  }
550
551  Out << Proto;
552  prettyPrintAttributes(D);
553
554  if (D->isPure())
555    Out << " = 0";
556  else if (D->isDeletedAsWritten())
557    Out << " = delete";
558  else if (D->doesThisDeclarationHaveABody()) {
559    if (!D->hasPrototype() && D->getNumParams()) {
560      // This is a K&R function definition, so we need to print the
561      // parameters.
562      Out << '\n';
563      DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
564      Indentation += Policy.Indentation;
565      for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
566        Indent();
567        ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
568        Out << ";\n";
569      }
570      Indentation -= Policy.Indentation;
571    } else
572      Out << ' ';
573
574    D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
575    Out << '\n';
576  }
577}
578
579void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
580  if (!Policy.SuppressSpecifiers && D->isMutable())
581    Out << "mutable ";
582  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
583    Out << "__module_private__ ";
584
585  std::string Name = D->getNameAsString();
586  D->getType().getAsStringInternal(Name, Policy);
587  Out << Name;
588
589  if (D->isBitField()) {
590    Out << " : ";
591    D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
592  }
593
594  Expr *Init = D->getInClassInitializer();
595  if (!Policy.SuppressInitializers && Init) {
596    Out << " = ";
597    Init->printPretty(Out, Context, 0, Policy, Indentation);
598  }
599  prettyPrintAttributes(D);
600}
601
602void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
603  Out << D->getNameAsString() << ":";
604}
605
606
607void DeclPrinter::VisitVarDecl(VarDecl *D) {
608  StorageClass SCAsWritten = D->getStorageClassAsWritten();
609  if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None)
610    Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " ";
611
612  if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
613    Out << "__thread ";
614  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
615    Out << "__module_private__ ";
616
617  std::string Name = D->getNameAsString();
618  QualType T = D->getType();
619  if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
620    T = Parm->getOriginalType();
621  T.getAsStringInternal(Name, Policy);
622  Out << Name;
623  Expr *Init = D->getInit();
624  if (!Policy.SuppressInitializers && Init) {
625    if (D->hasCXXDirectInitializer())
626      Out << "(";
627    else {
628        CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
629        if (!CCE || CCE->getConstructor()->isCopyOrMoveConstructor())
630          Out << " = ";
631    }
632    Init->printPretty(Out, Context, 0, Policy, Indentation);
633    if (D->hasCXXDirectInitializer())
634      Out << ")";
635  }
636  prettyPrintAttributes(D);
637}
638
639void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
640  VisitVarDecl(D);
641}
642
643void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
644  Out << "__asm (";
645  D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
646  Out << ")";
647}
648
649void DeclPrinter::VisitImportDecl(ImportDecl *D) {
650  Out << "@import " << D->getImportedModule()->getFullModuleName()
651      << ";\n";
652}
653
654void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
655  Out << "static_assert(";
656  D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
657  Out << ", ";
658  D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
659  Out << ")";
660}
661
662//----------------------------------------------------------------------------
663// C++ declarations
664//----------------------------------------------------------------------------
665void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
666  Out << "namespace " << *D << " {\n";
667  VisitDeclContext(D);
668  Indent() << "}";
669}
670
671void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
672  Out << "using namespace ";
673  if (D->getQualifier())
674    D->getQualifier()->print(Out, Policy);
675  Out << *D->getNominatedNamespaceAsWritten();
676}
677
678void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
679  Out << "namespace " << *D << " = ";
680  if (D->getQualifier())
681    D->getQualifier()->print(Out, Policy);
682  Out << *D->getAliasedNamespace();
683}
684
685void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
686  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
687    Out << "__module_private__ ";
688  Out << D->getKindName();
689  if (D->getIdentifier())
690    Out << ' ' << *D;
691
692  if (D->isCompleteDefinition()) {
693    // Print the base classes
694    if (D->getNumBases()) {
695      Out << " : ";
696      for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
697             BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
698        if (Base != D->bases_begin())
699          Out << ", ";
700
701        if (Base->isVirtual())
702          Out << "virtual ";
703
704        AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
705        if (AS != AS_none)
706          Print(AS);
707        Out << " " << Base->getType().getAsString(Policy);
708
709        if (Base->isPackExpansion())
710          Out << "...";
711      }
712    }
713
714    // Print the class definition
715    // FIXME: Doesn't print access specifiers, e.g., "public:"
716    Out << " {\n";
717    VisitDeclContext(D);
718    Indent() << "}";
719  }
720}
721
722void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
723  const char *l;
724  if (D->getLanguage() == LinkageSpecDecl::lang_c)
725    l = "C";
726  else {
727    assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
728           "unknown language in linkage specification");
729    l = "C++";
730  }
731
732  Out << "extern \"" << l << "\" ";
733  if (D->hasBraces()) {
734    Out << "{\n";
735    VisitDeclContext(D);
736    Indent() << "}";
737  } else
738    Visit(*D->decls_begin());
739}
740
741void DeclPrinter::PrintTemplateParameters(
742    const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) {
743  assert(Params);
744  assert(!Args || Params->size() == Args->size());
745
746  Out << "template <";
747
748  for (unsigned i = 0, e = Params->size(); i != e; ++i) {
749    if (i != 0)
750      Out << ", ";
751
752    const Decl *Param = Params->getParam(i);
753    if (const TemplateTypeParmDecl *TTP =
754          dyn_cast<TemplateTypeParmDecl>(Param)) {
755
756      if (TTP->wasDeclaredWithTypename())
757        Out << "typename ";
758      else
759        Out << "class ";
760
761      if (TTP->isParameterPack())
762        Out << "... ";
763
764      Out << TTP->getNameAsString();
765
766      if (Args) {
767        Out << " = ";
768        Args->get(i).print(Policy, Out);
769      } else if (TTP->hasDefaultArgument()) {
770        Out << " = ";
771        Out << TTP->getDefaultArgument().getAsString(Policy);
772      };
773    } else if (const NonTypeTemplateParmDecl *NTTP =
774                 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
775      Out << NTTP->getType().getAsString(Policy);
776
777      if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
778        Out << "...";
779
780      if (IdentifierInfo *Name = NTTP->getIdentifier()) {
781        Out << ' ';
782        Out << Name->getName();
783      }
784
785      if (Args) {
786        Out << " = ";
787        Args->get(i).print(Policy, Out);
788      } else if (NTTP->hasDefaultArgument()) {
789        Out << " = ";
790        NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
791                                                Indentation);
792      }
793    } else if (const TemplateTemplateParmDecl *TTPD =
794                 dyn_cast<TemplateTemplateParmDecl>(Param)) {
795      VisitTemplateDecl(TTPD);
796      // FIXME: print the default argument, if present.
797    }
798  }
799
800  Out << "> ";
801}
802
803void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
804  PrintTemplateParameters(D->getTemplateParameters());
805
806  if (const TemplateTemplateParmDecl *TTP =
807        dyn_cast<TemplateTemplateParmDecl>(D)) {
808    Out << "class ";
809    if (TTP->isParameterPack())
810      Out << "...";
811    Out << D->getName();
812  } else {
813    Visit(D->getTemplatedDecl());
814  }
815}
816
817void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
818  if (PrintInstantiation) {
819    TemplateParameterList *Params = D->getTemplateParameters();
820    for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
821         I != E; ++I) {
822      PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs());
823      Visit(*I);
824    }
825  }
826
827  return VisitRedeclarableTemplateDecl(D);
828}
829
830void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
831  if (PrintInstantiation) {
832    TemplateParameterList *Params = D->getTemplateParameters();
833    for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end();
834         I != E; ++I) {
835      PrintTemplateParameters(Params, &(*I)->getTemplateArgs());
836      Visit(*I);
837      Out << '\n';
838    }
839  }
840
841  return VisitRedeclarableTemplateDecl(D);
842}
843
844//----------------------------------------------------------------------------
845// Objective-C declarations
846//----------------------------------------------------------------------------
847
848void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
849  if (OMD->isInstanceMethod())
850    Out << "- ";
851  else
852    Out << "+ ";
853  if (!OMD->getResultType().isNull())
854    Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
855
856  std::string name = OMD->getSelector().getAsString();
857  std::string::size_type pos, lastPos = 0;
858  for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
859       E = OMD->param_end(); PI != E; ++PI) {
860    // FIXME: selector is missing here!
861    pos = name.find_first_of(':', lastPos);
862    Out << " " << name.substr(lastPos, pos - lastPos);
863    Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI;
864    lastPos = pos + 1;
865  }
866
867  if (OMD->param_begin() == OMD->param_end())
868    Out << " " << name;
869
870  if (OMD->isVariadic())
871      Out << ", ...";
872
873  if (OMD->getBody()) {
874    Out << ' ';
875    OMD->getBody()->printPretty(Out, Context, 0, Policy);
876    Out << '\n';
877  }
878}
879
880void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
881  std::string I = OID->getNameAsString();
882  ObjCInterfaceDecl *SID = OID->getSuperClass();
883
884  if (SID)
885    Out << "@implementation " << I << " : " << *SID;
886  else
887    Out << "@implementation " << I;
888  Out << "\n";
889  VisitDeclContext(OID, false);
890  Out << "@end";
891}
892
893void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
894  std::string I = OID->getNameAsString();
895  ObjCInterfaceDecl *SID = OID->getSuperClass();
896
897  if (!OID->isThisDeclarationADefinition()) {
898    Out << "@class " << I << ";";
899    return;
900  }
901
902  if (SID)
903    Out << "@interface " << I << " : " << *SID;
904  else
905    Out << "@interface " << I;
906
907  // Protocols?
908  const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
909  if (!Protocols.empty()) {
910    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
911         E = Protocols.end(); I != E; ++I)
912      Out << (I == Protocols.begin() ? '<' : ',') << **I;
913  }
914
915  if (!Protocols.empty())
916    Out << "> ";
917
918  if (OID->ivar_size() > 0) {
919    Out << "{\n";
920    Indentation += Policy.Indentation;
921    for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
922         E = OID->ivar_end(); I != E; ++I) {
923      Indent() << (*I)->getType().getAsString(Policy) << ' ' << **I << ";\n";
924    }
925    Indentation -= Policy.Indentation;
926    Out << "}\n";
927  }
928
929  VisitDeclContext(OID, false);
930  Out << "@end";
931  // FIXME: implement the rest...
932}
933
934void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
935  if (!PID->isThisDeclarationADefinition()) {
936    Out << "@protocol " << PID->getIdentifier() << ";\n";
937    return;
938  }
939
940  Out << "@protocol " << *PID << '\n';
941  VisitDeclContext(PID, false);
942  Out << "@end";
943}
944
945void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
946  Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
947
948  VisitDeclContext(PID, false);
949  Out << "@end";
950  // FIXME: implement the rest...
951}
952
953void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
954  Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n";
955  VisitDeclContext(PID, false);
956  Out << "@end";
957
958  // FIXME: implement the rest...
959}
960
961void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
962  Out << "@compatibility_alias " << *AID
963      << ' ' << *AID->getClassInterface() << ";\n";
964}
965
966/// PrintObjCPropertyDecl - print a property declaration.
967///
968void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
969  if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
970    Out << "@required\n";
971  else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
972    Out << "@optional\n";
973
974  Out << "@property";
975  if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
976    bool first = true;
977    Out << " (";
978    if (PDecl->getPropertyAttributes() &
979        ObjCPropertyDecl::OBJC_PR_readonly) {
980      Out << (first ? ' ' : ',') << "readonly";
981      first = false;
982    }
983
984    if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
985      Out << (first ? ' ' : ',') << "getter = "
986          << PDecl->getGetterName().getAsString();
987      first = false;
988    }
989    if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
990      Out << (first ? ' ' : ',') << "setter = "
991          << PDecl->getSetterName().getAsString();
992      first = false;
993    }
994
995    if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
996      Out << (first ? ' ' : ',') << "assign";
997      first = false;
998    }
999
1000    if (PDecl->getPropertyAttributes() &
1001        ObjCPropertyDecl::OBJC_PR_readwrite) {
1002      Out << (first ? ' ' : ',') << "readwrite";
1003      first = false;
1004    }
1005
1006    if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
1007      Out << (first ? ' ' : ',') << "retain";
1008      first = false;
1009    }
1010
1011    if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
1012      Out << (first ? ' ' : ',') << "strong";
1013      first = false;
1014    }
1015
1016    if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
1017      Out << (first ? ' ' : ',') << "copy";
1018      first = false;
1019    }
1020
1021    if (PDecl->getPropertyAttributes() &
1022        ObjCPropertyDecl::OBJC_PR_nonatomic) {
1023      Out << (first ? ' ' : ',') << "nonatomic";
1024      first = false;
1025    }
1026    if (PDecl->getPropertyAttributes() &
1027        ObjCPropertyDecl::OBJC_PR_atomic) {
1028      Out << (first ? ' ' : ',') << "atomic";
1029      first = false;
1030    }
1031
1032    (void) first; // Silence dead store warning due to idiomatic code.
1033    Out << " )";
1034  }
1035  Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl;
1036}
1037
1038void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1039  if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1040    Out << "@synthesize ";
1041  else
1042    Out << "@dynamic ";
1043  Out << *PID->getPropertyDecl();
1044  if (PID->getPropertyIvarDecl())
1045    Out << '=' << *PID->getPropertyIvarDecl();
1046}
1047
1048void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1049  Out << "using ";
1050  D->getQualifier()->print(Out, Policy);
1051  Out << *D;
1052}
1053
1054void
1055DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1056  Out << "using typename ";
1057  D->getQualifier()->print(Out, Policy);
1058  Out << D->getDeclName();
1059}
1060
1061void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1062  Out << "using ";
1063  D->getQualifier()->print(Out, Policy);
1064  Out << D->getDeclName();
1065}
1066
1067void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1068  // ignore
1069}
1070