14fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
24fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//
34fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//                     The LLVM Compiler Infrastructure
44fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//
54fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor// This file is distributed under the University of Illinois Open Source
64fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor// License. See LICENSE.TXT for details.
74fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//
84fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//===----------------------------------------------------------------------===//
94fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//
10d538ed9b2a617239d5bd56357663de62f6f9224cAlexander Kornienko// This file implements the Decl::print method, which pretty prints the
114fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor// AST back out to C/Objective-C/C++/Objective-C++ code.
124fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//
134fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//===----------------------------------------------------------------------===//
144fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor#include "clang/AST/ASTContext.h"
152fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/Attr.h"
164fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor#include "clang/AST/Decl.h"
174fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor#include "clang/AST/DeclCXX.h"
184fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor#include "clang/AST/DeclObjC.h"
192fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/DeclVisitor.h"
204fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor#include "clang/AST/Expr.h"
219db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor#include "clang/AST/ExprCXX.h"
224fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor#include "clang/AST/PrettyPrinter.h"
2315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor#include "clang/Basic/Module.h"
244fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor#include "llvm/Support/raw_ostream.h"
254fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregorusing namespace clang;
264fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
274fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregornamespace {
28770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramer  class DeclPrinter : public DeclVisitor<DeclPrinter> {
295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    raw_ostream &Out;
304fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    PrintingPolicy Policy;
314fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    unsigned Indentation;
325cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu    bool PrintInstantiation;
334fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    raw_ostream& Indent() { return Indent(Indentation); }
355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    raw_ostream& Indent(unsigned Indentation);
365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
374fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
380d59292581dbaa4203451709077de40e0ad72c18Anders Carlsson    void Print(AccessSpecifier AS);
391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  public:
41d1420c6fa788669e49f21e184927c7833881e399Richard Smith    DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
42d1420c6fa788669e49f21e184927c7833881e399Richard Smith                unsigned Indentation = 0, bool PrintInstantiation = false)
43d1420c6fa788669e49f21e184927c7833881e399Richard Smith      : Out(Out), Policy(Policy), Indentation(Indentation),
445cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu        PrintInstantiation(PrintInstantiation) { }
454fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
464fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitDeclContext(DeclContext *DC, bool Indent = true);
474fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
484fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitTranslationUnitDecl(TranslationUnitDecl *D);
494fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitTypedefDecl(TypedefDecl *D);
50162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    void VisitTypeAliasDecl(TypeAliasDecl *D);
514fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitEnumDecl(EnumDecl *D);
524fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitRecordDecl(RecordDecl *D);
534fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitEnumConstantDecl(EnumConstantDecl *D);
54684aa73192d92850a926870be62a1787eb5b7ed9Michael Han    void VisitEmptyDecl(EmptyDecl *D);
554fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitFunctionDecl(FunctionDecl *D);
568920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian    void VisitFriendDecl(FriendDecl *D);
574fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitFieldDecl(FieldDecl *D);
584fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitVarDecl(VarDecl *D);
5957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    void VisitLabelDecl(LabelDecl *D);
604fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitParmVarDecl(ParmVarDecl *D);
614fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
6215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    void VisitImportDecl(ImportDecl *D);
6328ac87e1a22ee15f284643d9007640d25b1aab5bPeter Collingbourne    void VisitStaticAssertDecl(StaticAssertDecl *D);
6459e63570359b8adb9eb257759fe766d7aac1995aDouglas Gregor    void VisitNamespaceDecl(NamespaceDecl *D);
658419fa3af97208eb00f0cd6c62354ce4ff986677Douglas Gregor    void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
666c9c94053132e5ca0655124b70f1c386a332e71dDouglas Gregor    void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
6759e63570359b8adb9eb257759fe766d7aac1995aDouglas Gregor    void VisitCXXRecordDecl(CXXRecordDecl *D);
684fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitLinkageSpecDecl(LinkageSpecDecl *D);
6998a57868d696cb5faf6195a609ad084a711c6bbeRichard Smith    void VisitTemplateDecl(const TemplateDecl *D);
705cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu    void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
715cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu    void VisitClassTemplateDecl(ClassTemplateDecl *D);
724fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitObjCMethodDecl(ObjCMethodDecl *D);
734fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
744fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
754fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
764fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
774fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
784fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
794fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
804fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
817ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
827ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
83f53eaa59c4936f9b095e69557ce7366b26ca90cbAnders Carlsson    void VisitUsingDecl(UsingDecl *D);
849488ea120e093068021f944176c3d610dd540914John McCall    void VisitUsingShadowDecl(UsingShadowDecl *D);
85c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
865cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu
875cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu    void PrintTemplateParameters(const TemplateParameterList *Params,
886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 const TemplateArgumentList *Args = nullptr);
891bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor    void prettyPrintAttributes(Decl *D);
904fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  };
914fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
924fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
935cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieuvoid Decl::print(raw_ostream &Out, unsigned Indentation,
945cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu                 bool PrintInstantiation) const {
9530c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor  print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
964fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
974fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
995cb3d699f0e37de1767eb23d26d03953a46cf1ffRichard Trieu                 unsigned Indentation, bool PrintInstantiation) const {
100d1420c6fa788669e49f21e184927c7833881e399Richard Smith  DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
101f88df8652e0726fe9eee5d78f92e3c5b34d0a537Anders Carlsson  Printer.Visit(const_cast<Decl*>(this));
1024fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
1034fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
10442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedmanstatic QualType GetBaseType(QualType T) {
10542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  // FIXME: This should be on the Type class!
10642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  QualType BaseType = T;
10742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  while (!BaseType->isSpecifierType()) {
10842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    if (isa<TypedefType>(BaseType))
10942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      break;
1106217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    else if (const PointerType* PTy = BaseType->getAs<PointerType>())
11142f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      BaseType = PTy->getPointeeType();
11241c1f21c00d12f58a73199026b8041db9818988aTed Kremenek    else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
11341c1f21c00d12f58a73199026b8041db9818988aTed Kremenek      BaseType = BPy->getPointeeType();
11442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
11542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      BaseType = ATy->getElementType();
116183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
117651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      BaseType = FTy->getReturnType();
118183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    else if (const VectorType *VTy = BaseType->getAs<VectorType>())
1195068ab669970ba62020541251f1193b237d24ae3Douglas Gregor      BaseType = VTy->getElementType();
12015631b4d354d7fe99ad018de82a9d636d4bd8e92Eli Friedman    else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
12115631b4d354d7fe99ad018de82a9d636d4bd8e92Eli Friedman      BaseType = RTy->getPointeeType();
12242f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    else
123b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unknown declarator!");
12442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  }
12542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  return BaseType;
12642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman}
12742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
12842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedmanstatic QualType getDeclType(Decl* D) {
129162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
13042f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    return TDD->getUnderlyingType();
13142f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
13242f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    return VD->getType();
13342f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  return QualType();
13442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman}
13542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
13642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedmanvoid Decl::printGroup(Decl** Begin, unsigned NumDecls,
1375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                      raw_ostream &Out, const PrintingPolicy &Policy,
13842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman                      unsigned Indentation) {
13942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  if (NumDecls == 1) {
140f1d60eaf3f70975ee262852af2d6aeabd140ed58Argyrios Kyrtzidis    (*Begin)->print(Out, Policy, Indentation);
14142f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    return;
14242f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  }
14342f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
14442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  Decl** End = Begin + NumDecls;
14542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  TagDecl* TD = dyn_cast<TagDecl>(*Begin);
14642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  if (TD)
14742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    ++Begin;
14842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
14942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  PrintingPolicy SubPolicy(Policy);
1505e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  if (TD && TD->isCompleteDefinition()) {
151f1d60eaf3f70975ee262852af2d6aeabd140ed58Argyrios Kyrtzidis    TD->print(Out, Policy, Indentation);
15242f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    Out << " ";
15342f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    SubPolicy.SuppressTag = true;
15442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  }
15542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
15642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  bool isFirst = true;
15742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  for ( ; Begin != End; ++Begin) {
15842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    if (isFirst) {
15942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      SubPolicy.SuppressSpecifiers = false;
16042f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      isFirst = false;
16142f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    } else {
16242f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      if (!isFirst) Out << ", ";
16342f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      SubPolicy.SuppressSpecifiers = true;
16442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    }
16542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
166f1d60eaf3f70975ee262852af2d6aeabd140ed58Argyrios Kyrtzidis    (*Begin)->print(Out, SubPolicy, Indentation);
16742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  }
16842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman}
16942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
170651f13cea278ec967336033dd032faef0e9fc2ecStephen HinesLLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
1712b7d8ddc746a9096f8af4bd0d8b07eae523864b5Anders Carlsson  // Get the translation unit
1722b7d8ddc746a9096f8af4bd0d8b07eae523864b5Anders Carlsson  const DeclContext *DC = this;
1732b7d8ddc746a9096f8af4bd0d8b07eae523864b5Anders Carlsson  while (!DC->isTranslationUnit())
1742b7d8ddc746a9096f8af4bd0d8b07eae523864b5Anders Carlsson    DC = DC->getParent();
1752b7d8ddc746a9096f8af4bd0d8b07eae523864b5Anders Carlsson
1762b7d8ddc746a9096f8af4bd0d8b07eae523864b5Anders Carlsson  ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
177d1420c6fa788669e49f21e184927c7833881e399Richard Smith  DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
1782b7d8ddc746a9096f8af4bd0d8b07eae523864b5Anders Carlsson  Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
1792b7d8ddc746a9096f8af4bd0d8b07eae523864b5Anders Carlsson}
1802b7d8ddc746a9096f8af4bd0d8b07eae523864b5Anders Carlsson
1815f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerraw_ostream& DeclPrinter::Indent(unsigned Indentation) {
182512ce60311b93b6f9a144fb2cf9fe4225f5d57b2Daniel Dunbar  for (unsigned i = 0; i != Indentation; ++i)
1834fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Out << "  ";
1844fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  return Out;
1854fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
1864fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
1871bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregorvoid DeclPrinter::prettyPrintAttributes(Decl *D) {
18840902d817e5a73850045d8a0c9795bc5047ee000Fariborz Jahanian  if (Policy.PolishForDeclaration)
1891bfb00dabf83d8c8b95b7276b4c0ae3fd64832c8Fariborz Jahanian    return;
1901bfb00dabf83d8c8b95b7276b4c0ae3fd64832c8Fariborz Jahanian
1911bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor  if (D->hasAttrs()) {
1921bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor    AttrVec &Attrs = D->getAttrs();
1931bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor    for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
1940dae729a69c4fccc38c97d5dd44f7b20bb20de1cRichard Smith      Attr *A = *i;
1950dae729a69c4fccc38c97d5dd44f7b20bb20de1cRichard Smith      A->printPretty(Out, Policy);
1961bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor    }
1971bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor  }
1981bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor}
1991bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor
2005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
20142f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  this->Indent();
202f1d60eaf3f70975ee262852af2d6aeabd140ed58Argyrios Kyrtzidis  Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
20342f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  Out << ";\n";
20442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  Decls.clear();
20542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
20642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman}
20742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
2080d59292581dbaa4203451709077de40e0ad72c18Anders Carlssonvoid DeclPrinter::Print(AccessSpecifier AS) {
2090d59292581dbaa4203451709077de40e0ad72c18Anders Carlsson  switch(AS) {
210eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27David Blaikie  case AS_none:      llvm_unreachable("No access specifier!");
2110d59292581dbaa4203451709077de40e0ad72c18Anders Carlsson  case AS_public:    Out << "public"; break;
2120d59292581dbaa4203451709077de40e0ad72c18Anders Carlsson  case AS_protected: Out << "protected"; break;
2136206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  case AS_private:   Out << "private"; break;
2140d59292581dbaa4203451709077de40e0ad72c18Anders Carlsson  }
2150d59292581dbaa4203451709077de40e0ad72c18Anders Carlsson}
2160d59292581dbaa4203451709077de40e0ad72c18Anders Carlsson
2174fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//----------------------------------------------------------------------------
2184fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor// Common C declarations
2194fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor//----------------------------------------------------------------------------
2204fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
2214fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregorvoid DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
222d1fc82efd53ffda30f4f16041399d78f3bf0705fDmitri Gribenko  if (Policy.TerseOutput)
22349795ae2c7cbb0845ed07b6626ac24275234e3d1Dmitri Gribenko    return;
22449795ae2c7cbb0845ed07b6626ac24275234e3d1Dmitri Gribenko
2254fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  if (Indent)
2264fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Indentation += Policy.Indentation;
2274fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
2285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 2> Decls;
22917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2304fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor       D != DEnd; ++D) {
2312d6c906585c292c9de78eff3b42af06332857252Ted Kremenek
2322d6c906585c292c9de78eff3b42af06332857252Ted Kremenek    // Don't print ObjCIvarDecls, as they are printed when visiting the
2332d6c906585c292c9de78eff3b42af06332857252Ted Kremenek    // containing ObjCInterfaceDecl.
2342d6c906585c292c9de78eff3b42af06332857252Ted Kremenek    if (isa<ObjCIvarDecl>(*D))
2352d6c906585c292c9de78eff3b42af06332857252Ted Kremenek      continue;
2362d6c906585c292c9de78eff3b42af06332857252Ted Kremenek
237d538ed9b2a617239d5bd56357663de62f6f9224cAlexander Kornienko    // Skip over implicit declarations in pretty-printing mode.
238d538ed9b2a617239d5bd56357663de62f6f9224cAlexander Kornienko    if (D->isImplicit())
239d538ed9b2a617239d5bd56357663de62f6f9224cAlexander Kornienko      continue;
240d538ed9b2a617239d5bd56357663de62f6f9224cAlexander Kornienko
24142f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    // The next bits of code handles stuff like "struct {int x;} a,b"; we're
24242f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    // forced to merge the declarations because there's no other way to
24342f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    // refer to the struct in question.  This limited merging is safe without
24442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    // a bunch of other checks because it only merges declarations directly
24542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    // referring to the tag, not typedefs.
24642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    //
24742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    // Check whether the current declaration should be grouped with a previous
24842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    // unnamed struct.
24942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    QualType CurDeclType = getDeclType(*D);
25042f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    if (!Decls.empty() && !CurDeclType.isNull()) {
25142f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      QualType BaseType = GetBaseType(CurDeclType);
25242acc0c357557574514814863cab950f23cbff5eEli Friedman      if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
25342acc0c357557574514814863cab950f23cbff5eEli Friedman        BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
25442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      if (!BaseType.isNull() && isa<TagType>(BaseType) &&
25542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman          cast<TagType>(BaseType)->getDecl() == Decls[0]) {
25642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman        Decls.push_back(*D);
25742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman        continue;
25842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      }
25942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    }
26042f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
26142f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    // If we have a merged group waiting to be handled, handle it now.
26242f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    if (!Decls.empty())
26342f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      ProcessDeclGroup(Decls);
26442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
26542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    // If the current declaration is an unnamed tag type, save it
26642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    // so we can merge it with the subsequent declaration(s) using it.
26742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
26842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      Decls.push_back(*D);
26942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      continue;
27042f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    }
2716206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
2726206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    if (isa<AccessSpecDecl>(*D)) {
2736206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara      Indentation -= Policy.Indentation;
2746206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara      this->Indent();
2756206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara      Print(D->getAccess());
2766206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara      Out << ":\n";
2776206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara      Indentation += Policy.Indentation;
2786206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara      continue;
2796206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    }
2806206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
2814fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    this->Indent();
2824fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Visit(*D);
2831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // FIXME: Need to be able to tell the DeclPrinter when
2856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    const char *Terminator = nullptr;
286c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    if (isa<OMPThreadPrivateDecl>(*D))
2876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Terminator = nullptr;
288c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    else if (isa<FunctionDecl>(*D) &&
289c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev             cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
2906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Terminator = nullptr;
29164f650062fbe5e2bc6fb6d341c46a2ec0284694fDouglas Gregor    else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
2926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Terminator = nullptr;
29364f650062fbe5e2bc6fb6d341c46a2ec0284694fDouglas Gregor    else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
2941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump             isa<ObjCImplementationDecl>(*D) ||
29564f650062fbe5e2bc6fb6d341c46a2ec0284694fDouglas Gregor             isa<ObjCInterfaceDecl>(*D) ||
29664f650062fbe5e2bc6fb6d341c46a2ec0284694fDouglas Gregor             isa<ObjCProtocolDecl>(*D) ||
29764f650062fbe5e2bc6fb6d341c46a2ec0284694fDouglas Gregor             isa<ObjCCategoryImplDecl>(*D) ||
29864f650062fbe5e2bc6fb6d341c46a2ec0284694fDouglas Gregor             isa<ObjCCategoryDecl>(*D))
2996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Terminator = nullptr;
3004fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    else if (isa<EnumConstantDecl>(*D)) {
3014fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      DeclContext::decl_iterator Next = D;
3024fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      ++Next;
3034fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      if (Next != DEnd)
3044fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor        Terminator = ",";
3054fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    } else
3064fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      Terminator = ";";
3074fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
3084fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    if (Terminator)
3094fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      Out << Terminator;
3104fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Out << "\n";
3114fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  }
3124fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
31342f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  if (!Decls.empty())
31442f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    ProcessDeclGroup(Decls);
31542f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman
3164fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  if (Indent)
3174fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Indentation -= Policy.Indentation;
3184fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
3194fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
3204fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregorvoid DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
3214fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  VisitDeclContext(D, false);
3224fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
3234fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
3244fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregorvoid DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
3258d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor  if (!Policy.SuppressSpecifiers) {
32642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    Out << "typedef ";
3278d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor
3288d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor    if (D->isModulePrivate())
3298d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor      Out << "__module_private__ ";
3308d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor  }
331c2fa6b6f9a89f55397355937544fbbe8258ead5dEnea Zaffanella  D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
3321bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor  prettyPrintAttributes(D);
3334fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
3344fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
335162e1c1b487352434552147967c3dd296ebee2f7Richard Smithvoid DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
336c2fa6b6f9a89f55397355937544fbbe8258ead5dEnea Zaffanella  Out << "using " << *D;
337c2fa6b6f9a89f55397355937544fbbe8258ead5dEnea Zaffanella  prettyPrintAttributes(D);
338c2fa6b6f9a89f55397355937544fbbe8258ead5dEnea Zaffanella  Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
339162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
340162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
3414fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregorvoid DeclPrinter::VisitEnumDecl(EnumDecl *D) {
3428d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
3438d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor    Out << "__module_private__ ";
3449fa8c4652afd4ee0faa70b563688d56c51c7fc93Douglas Gregor  Out << "enum ";
345a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara  if (D->isScoped()) {
346a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara    if (D->isScopedUsingClassTag())
347a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara      Out << "class ";
348a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara    else
349a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara      Out << "struct ";
350a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara  }
351b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer  Out << *D;
3529fa8c4652afd4ee0faa70b563688d56c51c7fc93Douglas Gregor
3537ad5c996e9519ed4e9afd1f0166be1cd2be8415aArgyrios Kyrtzidis  if (D->isFixed())
3547ad5c996e9519ed4e9afd1f0166be1cd2be8415aArgyrios Kyrtzidis    Out << " : " << D->getIntegerType().stream(Policy);
3559fa8c4652afd4ee0faa70b563688d56c51c7fc93Douglas Gregor
3565e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  if (D->isCompleteDefinition()) {
3579fa8c4652afd4ee0faa70b563688d56c51c7fc93Douglas Gregor    Out << " {\n";
3589fa8c4652afd4ee0faa70b563688d56c51c7fc93Douglas Gregor    VisitDeclContext(D);
3599fa8c4652afd4ee0faa70b563688d56c51c7fc93Douglas Gregor    Indent() << "}";
3609fa8c4652afd4ee0faa70b563688d56c51c7fc93Douglas Gregor  }
3611bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor  prettyPrintAttributes(D);
3624fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
3634fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
3644fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregorvoid DeclPrinter::VisitRecordDecl(RecordDecl *D) {
3658d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
3668d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor    Out << "__module_private__ ";
3674fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  Out << D->getKindName();
368900fc6388e803868a34b9483510c345e9b49d7ebBenjamin Kramer  if (D->getIdentifier())
369b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer    Out << ' ' << *D;
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3715e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  if (D->isCompleteDefinition()) {
3724fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Out << " {\n";
3734fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    VisitDeclContext(D);
3744fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Indent() << "}";
3754fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  }
3764fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
3774fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
3784fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregorvoid DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
379b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer  Out << *D;
3804fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  if (Expr *Init = D->getInitExpr()) {
3814fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Out << " = ";
3826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Init->printPretty(Out, nullptr, Policy, Indentation);
3834fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  }
3844fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
3854fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
3861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
38765bcdabba34fddc303ab97f60dfea6079989306aFariborz Jahanian  CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
388651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
38942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  if (!Policy.SuppressSpecifiers) {
390d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola    switch (D->getStorageClass()) {
391d931b086984257de68868a64a235c2b4b34003fbJohn McCall    case SC_None: break;
392d931b086984257de68868a64a235c2b4b34003fbJohn McCall    case SC_Extern: Out << "extern "; break;
393d931b086984257de68868a64a235c2b4b34003fbJohn McCall    case SC_Static: Out << "static "; break;
394d931b086984257de68868a64a235c2b4b34003fbJohn McCall    case SC_PrivateExtern: Out << "__private_extern__ "; break;
3958c25fc584ce27d59df9923f153e8a132dde58d04Peter Collingbourne    case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
3968c25fc584ce27d59df9923f153e8a132dde58d04Peter Collingbourne      llvm_unreachable("invalid for functions");
39742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    }
3984fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
3998d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor    if (D->isInlineSpecified())  Out << "inline ";
40042f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman    if (D->isVirtualAsWritten()) Out << "virtual ";
4018d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor    if (D->isModulePrivate())    Out << "__module_private__ ";
402651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
403651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if ((CDecl && CDecl->isExplicitSpecified()) ||
404651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        (ConversionDecl && ConversionDecl->isExplicit()))
40565bcdabba34fddc303ab97f60dfea6079989306aFariborz Jahanian      Out << "explicit ";
40642f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  }
4074fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
4086620a628b0a02c78741b8f31790d4c1186aa4038Douglas Gregor  PrintingPolicy SubPolicy(Policy);
4096620a628b0a02c78741b8f31790d4c1186aa4038Douglas Gregor  SubPolicy.SuppressSpecifiers = false;
4102577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  std::string Proto = D->getNameInfo().getAsString();
4114fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
412723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  QualType Ty = D->getType();
413f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
414723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara    Proto = '(' + Proto + ')';
415723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara    Ty = PT->getInnerType();
416723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  }
417723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara
418651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
4196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    const FunctionProtoType *FT = nullptr;
4204fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    if (D->hasWrittenPrototype())
4214fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      FT = dyn_cast<FunctionProtoType>(AFT);
4224fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
4234fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Proto += "(";
4244fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    if (FT) {
4254fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      llvm::raw_string_ostream POut(Proto);
426d1420c6fa788669e49f21e184927c7833881e399Richard Smith      DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
4274fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
4284fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor        if (i) POut << ", ";
4294fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor        ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
4304fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      }
4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4324fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      if (FT->isVariadic()) {
4334fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor        if (D->getNumParams()) POut << ", ";
4344fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor        POut << "...";
4354fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      }
43610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
43742f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
43842f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman        if (i)
43942f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman          Proto += ", ";
44042f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman        Proto += D->getParamDecl(i)->getNameAsString();
44142f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman      }
4424fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    }
4434fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
4444fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Proto += ")";
4450ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
4464ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie    if (FT) {
4474ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie      if (FT->isConst())
448b95cfe4bb01f18a112bcb7eea3b82bc8d6dfe20bDouglas Gregor        Proto += " const";
4494ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie      if (FT->isVolatile())
450b95cfe4bb01f18a112bcb7eea3b82bc8d6dfe20bDouglas Gregor        Proto += " volatile";
4514ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie      if (FT->isRestrict())
452b95cfe4bb01f18a112bcb7eea3b82bc8d6dfe20bDouglas Gregor        Proto += " restrict";
453651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
454651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      switch (FT->getRefQualifier()) {
455651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      case RQ_None:
456651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        break;
457651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      case RQ_LValue:
458651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Proto += " &";
459651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        break;
460651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      case RQ_RValue:
461651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Proto += " &&";
462651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        break;
463651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      }
464b95cfe4bb01f18a112bcb7eea3b82bc8d6dfe20bDouglas Gregor    }
46560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
46660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    if (FT && FT->hasDynamicExceptionSpec()) {
4670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto += " throw(";
46860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      if (FT->getExceptionSpecType() == EST_MSAny)
4690ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        Proto += "...";
4700ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      else
4710ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
4720ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          if (I)
4730ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor            Proto += ", ";
47460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
4751ad23d62007162df82b58bca31b4aa277a5f6586Dmitri Gribenko          Proto += FT->getExceptionType(I).getAsString(SubPolicy);
4760ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        }
4770ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto += ")";
47860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
47960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      Proto += " noexcept";
48060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
48160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        Proto += "(";
48260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        llvm::raw_string_ostream EOut(Proto);
4836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
48460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                           Indentation);
48560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        EOut.flush();
48660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        Proto += EOut.str();
48760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        Proto += ")";
48860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      }
4890ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
4900ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
49165bcdabba34fddc303ab97f60dfea6079989306aFariborz Jahanian    if (CDecl) {
4927a614d8380297fcd2bc23986241905d97222948cRichard Smith      bool HasInitializerList = false;
493651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      for (const auto *BMInitializer : CDecl->inits()) {
4947a614d8380297fcd2bc23986241905d97222948cRichard Smith        if (BMInitializer->isInClassMemberInitializer())
4957a614d8380297fcd2bc23986241905d97222948cRichard Smith          continue;
4967a614d8380297fcd2bc23986241905d97222948cRichard Smith
4977a614d8380297fcd2bc23986241905d97222948cRichard Smith        if (!HasInitializerList) {
4987a614d8380297fcd2bc23986241905d97222948cRichard Smith          Proto += " : ";
4997a614d8380297fcd2bc23986241905d97222948cRichard Smith          Out << Proto;
5007a614d8380297fcd2bc23986241905d97222948cRichard Smith          Proto.clear();
5017a614d8380297fcd2bc23986241905d97222948cRichard Smith          HasInitializerList = true;
5027a614d8380297fcd2bc23986241905d97222948cRichard Smith        } else
5037a614d8380297fcd2bc23986241905d97222948cRichard Smith          Out << ", ";
5047a614d8380297fcd2bc23986241905d97222948cRichard Smith
5057a614d8380297fcd2bc23986241905d97222948cRichard Smith        if (BMInitializer->isAnyMemberInitializer()) {
5067a614d8380297fcd2bc23986241905d97222948cRichard Smith          FieldDecl *FD = BMInitializer->getAnyMember();
507b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer          Out << *FD;
5087a614d8380297fcd2bc23986241905d97222948cRichard Smith        } else {
509b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer          Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
5107a614d8380297fcd2bc23986241905d97222948cRichard Smith        }
5117a614d8380297fcd2bc23986241905d97222948cRichard Smith
5127a614d8380297fcd2bc23986241905d97222948cRichard Smith        Out << "(";
5137a614d8380297fcd2bc23986241905d97222948cRichard Smith        if (!BMInitializer->getInit()) {
5147a614d8380297fcd2bc23986241905d97222948cRichard Smith          // Nothing to print
5157a614d8380297fcd2bc23986241905d97222948cRichard Smith        } else {
5167a614d8380297fcd2bc23986241905d97222948cRichard Smith          Expr *Init = BMInitializer->getInit();
5177a614d8380297fcd2bc23986241905d97222948cRichard Smith          if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
5187a614d8380297fcd2bc23986241905d97222948cRichard Smith            Init = Tmp->getSubExpr();
5197a614d8380297fcd2bc23986241905d97222948cRichard Smith
5207a614d8380297fcd2bc23986241905d97222948cRichard Smith          Init = Init->IgnoreParens();
5216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
5226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          Expr *SimpleInit = nullptr;
5236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          Expr **Args = nullptr;
5247a614d8380297fcd2bc23986241905d97222948cRichard Smith          unsigned NumArgs = 0;
5257a614d8380297fcd2bc23986241905d97222948cRichard Smith          if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
5267a614d8380297fcd2bc23986241905d97222948cRichard Smith            Args = ParenList->getExprs();
5277a614d8380297fcd2bc23986241905d97222948cRichard Smith            NumArgs = ParenList->getNumExprs();
5287a614d8380297fcd2bc23986241905d97222948cRichard Smith          } else if (CXXConstructExpr *Construct
5297a614d8380297fcd2bc23986241905d97222948cRichard Smith                                        = dyn_cast<CXXConstructExpr>(Init)) {
5307a614d8380297fcd2bc23986241905d97222948cRichard Smith            Args = Construct->getArgs();
5317a614d8380297fcd2bc23986241905d97222948cRichard Smith            NumArgs = Construct->getNumArgs();
5327a614d8380297fcd2bc23986241905d97222948cRichard Smith          } else
5337a614d8380297fcd2bc23986241905d97222948cRichard Smith            SimpleInit = Init;
5347a614d8380297fcd2bc23986241905d97222948cRichard Smith
5357a614d8380297fcd2bc23986241905d97222948cRichard Smith          if (SimpleInit)
5366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
5377a614d8380297fcd2bc23986241905d97222948cRichard Smith          else {
5387a614d8380297fcd2bc23986241905d97222948cRichard Smith            for (unsigned I = 0; I != NumArgs; ++I) {
539ef8225444452a1486bd721f3285301fe84643b00Stephen Hines              assert(Args[I] != nullptr && "Expected non-null Expr");
5407a614d8380297fcd2bc23986241905d97222948cRichard Smith              if (isa<CXXDefaultArgExpr>(Args[I]))
5417a614d8380297fcd2bc23986241905d97222948cRichard Smith                break;
5427a614d8380297fcd2bc23986241905d97222948cRichard Smith
5437a614d8380297fcd2bc23986241905d97222948cRichard Smith              if (I)
5447a614d8380297fcd2bc23986241905d97222948cRichard Smith                Out << ", ";
5456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              Args[I]->printPretty(Out, nullptr, Policy, Indentation);
54666192aded85fc1140ea4fbfa3e10f1dbc232769dFariborz Jahanian            }
5479db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor          }
54866192aded85fc1140ea4fbfa3e10f1dbc232769dFariborz Jahanian        }
5497a614d8380297fcd2bc23986241905d97222948cRichard Smith        Out << ")";
550651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (BMInitializer->isPackExpansion())
551651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          Out << "...";
55266192aded85fc1140ea4fbfa3e10f1dbc232769dFariborz Jahanian      }
553651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
554425663a16cc0d1bbd6764300f8a59251057dffe8Richard Smith      if (FT && FT->hasTrailingReturn()) {
555425663a16cc0d1bbd6764300f8a59251057dffe8Richard Smith        Out << "auto " << Proto << " -> ";
556425663a16cc0d1bbd6764300f8a59251057dffe8Richard Smith        Proto.clear();
557425663a16cc0d1bbd6764300f8a59251057dffe8Richard Smith      }
558651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      AFT->getReturnType().print(Out, Policy, Proto);
559651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Proto.clear();
560425663a16cc0d1bbd6764300f8a59251057dffe8Richard Smith    }
561651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Out << Proto;
5624fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  } else {
5637ad5c996e9519ed4e9afd1f0166be1cd2be8415aArgyrios Kyrtzidis    Ty.print(Out, Policy, Proto);
5644fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  }
5654fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
5661bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor  prettyPrintAttributes(D);
5674fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
5684fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  if (D->isPure())
5694fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Out << " = 0";
57010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  else if (D->isDeletedAsWritten())
5714fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Out << " = delete";
572ddb2968ba69e14bafec765401d70f2477bc977f6Fariborz Jahanian  else if (D->isExplicitlyDefaulted())
573ddb2968ba69e14bafec765401d70f2477bc977f6Fariborz Jahanian    Out << " = default";
5742e0b8d9c0b16c4f3e3bed992205ce43a0908915dDmitri Gribenko  else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
5754fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    if (!D->hasPrototype() && D->getNumParams()) {
5764fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      // This is a K&R function definition, so we need to print the
5774fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      // parameters.
5784fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      Out << '\n';
579d1420c6fa788669e49f21e184927c7833881e399Richard Smith      DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
5804fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      Indentation += Policy.Indentation;
5814fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
5824fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor        Indent();
5836620a628b0a02c78741b8f31790d4c1186aa4038Douglas Gregor        ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
5844fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor        Out << ";\n";
5854fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      }
5864fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      Indentation -= Policy.Indentation;
5874fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    } else
5884fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor      Out << ' ';
5894fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
590ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    if (D->getBody())
591ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
5924fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Out << '\n';
5934fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  }
5944fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
5954fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
5968920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanianvoid DeclPrinter::VisitFriendDecl(FriendDecl *D) {
5978920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian  if (TypeSourceInfo *TSI = D->getFriendType()) {
5988c84028ed9aa0dfd54ab729dee78f29c961d7f37Enea Zaffanella    unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
5998c84028ed9aa0dfd54ab729dee78f29c961d7f37Enea Zaffanella    for (unsigned i = 0; i < NumTPLists; ++i)
6008c84028ed9aa0dfd54ab729dee78f29c961d7f37Enea Zaffanella      PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
601d4ae6535a667d515afc0af3e67ca287548aba985Fariborz Jahanian    Out << "friend ";
602d4ae6535a667d515afc0af3e67ca287548aba985Fariborz Jahanian    Out << " " << TSI->getType().getAsString(Policy);
6038920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian  }
6048920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian  else if (FunctionDecl *FD =
6058920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian      dyn_cast<FunctionDecl>(D->getFriendDecl())) {
6068920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian    Out << "friend ";
6078920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian    VisitFunctionDecl(FD);
6088920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian  }
6098920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian  else if (FunctionTemplateDecl *FTD =
6108920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian           dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
6118920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian    Out << "friend ";
6128920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian    VisitFunctionTemplateDecl(FTD);
6138920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian  }
6148920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian  else if (ClassTemplateDecl *CTD =
6158920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian           dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
6168920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian    Out << "friend ";
617d4ae6535a667d515afc0af3e67ca287548aba985Fariborz Jahanian    VisitRedeclarableTemplateDecl(CTD);
6188920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian  }
6198920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian}
6208920eb7a957f3b0bc4a8213ecdaec9a7fcd139eaFariborz Jahanian
6214fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregorvoid DeclPrinter::VisitFieldDecl(FieldDecl *D) {
62242f42c0dd5cf71fbfc6fa282d03079a902f6e342Eli Friedman  if (!Policy.SuppressSpecifiers && D->isMutable())
6234fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Out << "mutable ";
6248d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
6258d267c57afb3af418ed5281b7a9bb4555d701a82Douglas Gregor    Out << "__module_private__ ";
6264fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
627a3ae52b63aff9d4478084e40b1f683f45eb06ab7Fariborz Jahanian  Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
628db1519a95687622d08340f9b7522ef84a53b331aFariborz Jahanian            stream(Policy, D->getName());
6294fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
6304fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  if (D->isBitField()) {
6314fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor    Out << " : ";
6326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
6334fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor  }
6347a614d8380297fcd2bc23986241905d97222948cRichard Smith
6357a614d8380297fcd2bc23986241905d97222948cRichard Smith  Expr *Init = D->getInClassInitializer();
6367a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (!Policy.SuppressInitializers && Init) {
637ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith    if (D->getInClassInitStyle() == ICIS_ListInit)
638ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith      Out << " ";
639ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith    else
640ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith      Out << " = ";
6416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Init->printPretty(Out, nullptr, Policy, Indentation);
6427a614d8380297fcd2bc23986241905d97222948cRichard Smith  }
6431bea8807bcd2be10bf6309a3a848489434464cedDouglas Gregor  prettyPrintAttributes(D);
6444fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor}
6454fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
64657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattnervoid DeclPrinter::VisitLabelDecl(LabelDecl *D) {
647a59d20b135bfde058a5a69045bab5ec4e2553f74Benjamin Kramer  Out << *D << ":";
64857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
64957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
65057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
6514fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregorvoid DeclPrinter::VisitVarDecl(VarDecl *D) {
65238afbc7361d861968232defaeaf8e302af75b5eeRichard Smith  if (!Policy.SuppressSpecifiers) {
65338afbc7361d861968232defaeaf8e302af75b5eeRichard Smith    StorageClass SC = D->getStorageClass();
65438afbc7361d861968232defaeaf8e302af75b5eeRichard Smith    if (SC != SC_None)
65538afbc7361d861968232defaeaf8e302af75b5eeRichard Smith      Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
6564fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
657dc17384581e37436582a007be4d9185bcf7003ecEnea Zaffanella    switch (D->getTSCSpec()) {
658dc17384581e37436582a007be4d9185bcf7003ecEnea Zaffanella    case TSCS_unspecified:
65938afbc7361d861968232defaeaf8e302af75b5eeRichard Smith      break;
660dc17384581e37436582a007be4d9185bcf7003ecEnea Zaffanella    case TSCS___thread:
661dc17384581e37436582a007be4d9185bcf7003ecEnea Zaffanella      Out << "__thread ";
662dc17384581e37436582a007be4d9185bcf7003ecEnea Zaffanella      break;
663dc17384581e37436582a007be4d9185bcf7003ecEnea Zaffanella    case TSCS__Thread_local:
66438afbc7361d861968232defaeaf8e302af75b5eeRichard Smith      Out << "_Thread_local ";
66538afbc7361d861968232defaeaf8e302af75b5eeRichard Smith      break;
666dc17384581e37436582a007be4d9185bcf7003ecEnea Zaffanella    case TSCS_thread_local:
66738afbc7361d861968232defaeaf8e302af75b5eeRichard Smith      Out << "thread_local ";
66838afbc7361d861968232defaeaf8e302af75b5eeRichard Smith      break;
66938afbc7361d861968232defaeaf8e302af75b5eeRichard Smith    }
67038afbc7361d861968232defaeaf8e302af75b5eeRichard Smith
67138afbc7361d861968232defaeaf8e302af75b5eeRichard Smith    if (D->isModulePrivate())
67238afbc7361d861968232defaeaf8e302af75b5eeRichard Smith      Out << "__module_private__ ";
67338afbc7361d861968232defaeaf8e302af75b5eeRichard Smith  }
6744fe0c8e9c76b96e7aff21696a40dacc09d0237bcDouglas Gregor
675c2fa6b6f9a89f55397355937544fbbe8258ead5dEnea Zaffanella  QualType T = D->getTypeSourceInfo()
676c2fa6b6f9a89f55397355937544fbbe8258ead5dEnea Zaffanella    ? D->getTypeSourceInfo()->getType()
677c2fa6b6f9a89f55397355937544fbbe8258ead5dEnea Zaffanella    : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
6787ad5c996e9519ed4e9afd1f0166be1cd2be8415aArgyrios Kyrtzidis  T.print(Out, Policy, D->getName());
679ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  Expr *Init = D->getInit();
680ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (!Policy.SuppressInitializers && Init) {
6815b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    bool ImplicitInit = false;
68293aa2db250a0e67192ab2a5417e4dfd5530d6f56Dmitri Gribenko    if (CXXConstructExpr *Construct =
68393aa2db250a0e67192ab2a5417e4dfd5530d6f56Dmitri Gribenko            dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
6845250e2b24f7288fb31dc6357831fcbc4ced26693Dmitri Gribenko      if (D->getInitStyle() == VarDecl::CallInit &&
6855250e2b24f7288fb31dc6357831fcbc4ced26693Dmitri Gribenko          !Construct->isListInitialization()) {
6865250e2b24f7288fb31dc6357831fcbc4ced26693Dmitri Gribenko        ImplicitInit = Construct->getNumArgs() == 0 ||
6875250e2b24f7288fb31dc6357831fcbc4ced26693Dmitri Gribenko          Construct->getArg(0)->isDefaultArgument();
6885250e2b24f7288fb31dc6357831fcbc4ced26693Dmitri Gribenko      }
6895250e2b24f7288fb31dc6357831fcbc4ced26693Dmitri Gribenko    }
6905b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (!ImplicitInit) {
691e1aebe1e3aabe955e3805e994647e5e31dcc4d8cEli Friedman      if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
6925b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        Out << "(";
6935b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      else if (D->getInitStyle() == VarDecl::CInit) {
6945b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        Out << " = ";
6955b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      }
6966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Init->printPretty(Out, nullptr, Policy, Indentation);
697e1aebe1e3aabe955e3805e994647e5e31dcc4d8cEli Friedman      if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
698