DeclPrinter.cpp revision 7ad5c996e9519ed4e9afd1f0166be1cd2be8415a
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 64e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// This file implements the Decl::dump method, which pretty print the 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// AST back out to C/Objective-C/C++/Objective-C++ code. 124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// 134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//===----------------------------------------------------------------------===// 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/ASTContext.h" 155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "clang/AST/DeclVisitor.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/Decl.h" 174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "clang/AST/DeclCXX.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/DeclObjC.h" 191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "clang/AST/Expr.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/ExprCXX.h" 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "clang/AST/PrettyPrinter.h" 224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "clang/Basic/Module.h" 234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "llvm/Support/raw_ostream.h" 244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)using namespace clang; 254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class DeclPrinter : public DeclVisitor<DeclPrinter> { 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) raw_ostream &Out; 294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ASTContext &Context; 304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) PrintingPolicy Policy; 314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) unsigned Indentation; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool PrintInstantiation; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) raw_ostream& Indent() { return Indent(Indentation); } 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) raw_ostream& Indent(unsigned Indentation); 361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls); 3723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void Print(AccessSpecifier AS); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) public: 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DeclPrinter(raw_ostream &Out, ASTContext &Context, 424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const PrintingPolicy &Policy, 4323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) unsigned Indentation = 0, 441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool PrintInstantiation = false) 4523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation), 464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) PrintInstantiation(PrintInstantiation) { } 4723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 4823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) void VisitDeclContext(DeclContext *DC, bool Indent = true); 491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitTranslationUnitDecl(TranslationUnitDecl *D); 514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitTypedefDecl(TypedefDecl *D); 5223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) void VisitTypeAliasDecl(TypeAliasDecl *D); 5323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) void VisitEnumDecl(EnumDecl *D); 541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void VisitRecordDecl(RecordDecl *D); 554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitEnumConstantDecl(EnumConstantDecl *D); 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void VisitFunctionDecl(FunctionDecl *D); 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void VisitFieldDecl(FieldDecl *D); 584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitVarDecl(VarDecl *D); 594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitLabelDecl(LabelDecl *D); 604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitParmVarDecl(ParmVarDecl *D); 614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); 624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitImportDecl(ImportDecl *D); 634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitStaticAssertDecl(StaticAssertDecl *D); 644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitNamespaceDecl(NamespaceDecl *D); 654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitCXXRecordDecl(CXXRecordDecl *D); 684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitLinkageSpecDecl(LinkageSpecDecl *D); 694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitTemplateDecl(const TemplateDecl *D); 704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitClassTemplateDecl(ClassTemplateDecl *D); 724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitObjCMethodDecl(ObjCMethodDecl *D); 734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitObjCImplementationDecl(ObjCImplementationDecl *D); 744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitObjCProtocolDecl(ObjCProtocolDecl *D); 764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitObjCCategoryDecl(ObjCCategoryDecl *D); 784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void VisitObjCPropertyDecl(ObjCPropertyDecl *D); 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void VisitUsingDecl(UsingDecl *D); 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void VisitUsingShadowDecl(UsingShadowDecl *D); 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void PrintTemplateParameters(const TemplateParameterList *Params, 8723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) const TemplateArgumentList *Args); 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void prettyPrintAttributes(Decl *D); 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Decl::print(raw_ostream &Out, unsigned Indentation, 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool PrintInstantiation) const { 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy, 984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) unsigned Indentation, bool PrintInstantiation) const { 994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) DeclPrinter Printer(Out, getASTContext(), Policy, Indentation, PrintInstantiation); 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Printer.Visit(const_cast<Decl*>(this)); 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static QualType GetBaseType(QualType T) { 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // FIXME: This should be on the Type class! 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) QualType BaseType = T; 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (!BaseType->isSpecifierType()) { 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (isa<TypedefType>(BaseType)) 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) break; 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (const PointerType* PTy = BaseType->getAs<PointerType>()) 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BaseType = PTy->getPointeeType(); 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BaseType = ATy->getElementType(); 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (const FunctionType* FTy = BaseType->getAs<FunctionType>()) 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BaseType = FTy->getResultType(); 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (const VectorType *VTy = BaseType->getAs<VectorType>()) 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BaseType = VTy->getElementType(); 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) llvm_unreachable("Unknown declarator!"); 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return BaseType; 12123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 12323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)static QualType getDeclType(Decl* D) { 1244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D)) 12523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) return TDD->getUnderlyingType(); 1264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (ValueDecl* VD = dyn_cast<ValueDecl>(D)) 12723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) return VD->getType(); 12823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) return QualType(); 1294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)} 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void Decl::printGroup(Decl** Begin, unsigned NumDecls, 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) raw_ostream &Out, const PrintingPolicy &Policy, 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned Indentation) { 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (NumDecls == 1) { 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (*Begin)->print(Out, Policy, Indentation); 1364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return; 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Decl** End = Begin + NumDecls; 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TagDecl* TD = dyn_cast<TagDecl>(*Begin); 1414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (TD) 1424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ++Begin; 1434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PrintingPolicy SubPolicy(Policy); 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (TD && TD->isCompleteDefinition()) { 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TD->print(Out, Policy, Indentation); 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Out << " "; 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SubPolicy.SuppressTag = true; 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool isFirst = true; 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for ( ; Begin != End; ++Begin) { 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (isFirst) { 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SubPolicy.SuppressSpecifiers = false; 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) isFirst = false; 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 15723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) if (!isFirst) Out << ", "; 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SubPolicy.SuppressSpecifiers = true; 1594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (*Begin)->print(Out, SubPolicy, Indentation); 1624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 1634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)} 1644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DeclContext::dumpDeclContext() const { 1664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Get the translation unit 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const DeclContext *DC = this; 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (!DC->isTranslationUnit()) 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DC = DC->getParent(); 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext(); 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DeclPrinter Printer(llvm::errs(), Ctx, Ctx.getPrintingPolicy(), 0); 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false); 17423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Decl::dump() const { 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) print(llvm::errs()); 1784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)} 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)raw_ostream& DeclPrinter::Indent(unsigned Indentation) { 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (unsigned i = 0; i != Indentation; ++i) 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Out << " "; 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return Out; 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DeclPrinter::prettyPrintAttributes(Decl *D) { 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (D->hasAttrs()) { 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AttrVec &Attrs = D->getAttrs(); 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) { 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Attr *A = *i; 19123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) A->printPretty(Out, Context); 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 19423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) { 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) this->Indent(); 1984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Out << ";\n"; 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Decls.clear(); 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DeclPrinter::Print(AccessSpecifier AS) { 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) switch(AS) { 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case AS_none: llvm_unreachable("No access specifier!"); 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case AS_public: Out << "public"; break; 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case AS_protected: Out << "protected"; break; 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case AS_private: Out << "private"; break; 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//---------------------------------------------------------------------------- 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Common C declarations 2154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//---------------------------------------------------------------------------- 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { 2184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (Indent) 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Indentation += Policy.Indentation; 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SmallVector<Decl*, 2> Decls; 2224ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); 2234ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch D != DEnd; ++D) { 2244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 2254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Don't print ObjCIvarDecls, as they are printed when visiting the 2264ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // containing ObjCInterfaceDecl. 2274ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch if (isa<ObjCIvarDecl>(*D)) 2284ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch continue; 2294ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 2304ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch if (!Policy.Dump) { 2314ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // Skip over implicit declarations in pretty-printing mode. 2324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (D->isImplicit()) continue; 2334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // FIXME: Ugly hack so we don't pretty-print the builtin declaration 2344ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // of __builtin_va_list or __[u]int128_t. There should be some other way 2354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // to check that. 2364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) { 2374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (IdentifierInfo *II = ND->getIdentifier()) { 2384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (II->isStr("__builtin_va_list") || 2394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) II->isStr("__int128_t") || II->isStr("__uint128_t")) 2404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) continue; 2414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 2454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // The next bits of code handles stuff like "struct {int x;} a,b"; we're 2464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // forced to merge the declarations because there's no other way to 2474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // refer to the struct in question. This limited merging is safe without 2484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // a bunch of other checks because it only merges declarations directly 2494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // referring to the tag, not typedefs. 2504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // 2514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Check whether the current declaration should be grouped with a previous 2524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // unnamed struct. 2534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) QualType CurDeclType = getDeclType(*D); 2544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (!Decls.empty() && !CurDeclType.isNull()) { 2554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) QualType BaseType = GetBaseType(CurDeclType); 2564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (!BaseType.isNull() && isa<TagType>(BaseType) && 2574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) cast<TagType>(BaseType)->getDecl() == Decls[0]) { 2584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) Decls.push_back(*D); 2594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) continue; 2601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci } 2611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci } 2624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 26323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) // If we have a merged group waiting to be handled, handle it now. 2644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (!Decls.empty()) 2654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ProcessDeclGroup(Decls); 2664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 2674ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // If the current declaration is an unnamed tag type, save it 2684ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // so we can merge it with the subsequent declaration(s) using it. 2694ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) { 2704ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Decls.push_back(*D); 2714ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch continue; 2724ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch } 2734ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 2744ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch if (isa<AccessSpecDecl>(*D)) { 2754ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Indentation -= Policy.Indentation; 2764ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch this->Indent(); 2774ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Print(D->getAccess()); 2784ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Out << ":\n"; 2794ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Indentation += Policy.Indentation; 2804ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch continue; 2814ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch } 2824ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 2834ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch this->Indent(); 2844ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Visit(*D); 2854ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 2864ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // FIXME: Need to be able to tell the DeclPrinter when 2874ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch const char *Terminator = 0; 2884ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch if (isa<FunctionDecl>(*D) && 2894ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch cast<FunctionDecl>(*D)->isThisDeclarationADefinition()) 2904ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Terminator = 0; 2914ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody()) 2924ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Terminator = 0; 2934ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) || 2944ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch isa<ObjCImplementationDecl>(*D) || 2954ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch isa<ObjCInterfaceDecl>(*D) || 2964ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch isa<ObjCProtocolDecl>(*D) || 2974ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch isa<ObjCCategoryImplDecl>(*D) || 2984ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch isa<ObjCCategoryDecl>(*D)) 2994ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Terminator = 0; 3004ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch else if (isa<EnumConstantDecl>(*D)) { 3014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) DeclContext::decl_iterator Next = D; 3024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ++Next; 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 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 if (!Policy.SuppressSpecifiers) { 326 Out << "typedef "; 327 328 if (D->isModulePrivate()) 329 Out << "__module_private__ "; 330 } 331 D->getUnderlyingType().print(Out, Policy, D->getName()); 332 prettyPrintAttributes(D); 333} 334 335void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) { 336 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy); 337} 338 339void DeclPrinter::VisitEnumDecl(EnumDecl *D) { 340 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 341 Out << "__module_private__ "; 342 Out << "enum "; 343 if (D->isScoped()) { 344 if (D->isScopedUsingClassTag()) 345 Out << "class "; 346 else 347 Out << "struct "; 348 } 349 Out << *D; 350 351 if (D->isFixed()) 352 Out << " : " << D->getIntegerType().stream(Policy); 353 354 if (D->isCompleteDefinition()) { 355 Out << " {\n"; 356 VisitDeclContext(D); 357 Indent() << "}"; 358 } 359 prettyPrintAttributes(D); 360} 361 362void DeclPrinter::VisitRecordDecl(RecordDecl *D) { 363 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 364 Out << "__module_private__ "; 365 Out << D->getKindName(); 366 if (D->getIdentifier()) 367 Out << ' ' << *D; 368 369 if (D->isCompleteDefinition()) { 370 Out << " {\n"; 371 VisitDeclContext(D); 372 Indent() << "}"; 373 } 374} 375 376void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { 377 Out << *D; 378 if (Expr *Init = D->getInitExpr()) { 379 Out << " = "; 380 Init->printPretty(Out, Context, 0, Policy, Indentation); 381 } 382} 383 384void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { 385 if (!Policy.SuppressSpecifiers) { 386 switch (D->getStorageClassAsWritten()) { 387 case SC_None: break; 388 case SC_Extern: Out << "extern "; break; 389 case SC_Static: Out << "static "; break; 390 case SC_PrivateExtern: Out << "__private_extern__ "; break; 391 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal: 392 llvm_unreachable("invalid for functions"); 393 } 394 395 if (D->isInlineSpecified()) Out << "inline "; 396 if (D->isVirtualAsWritten()) Out << "virtual "; 397 if (D->isModulePrivate()) Out << "__module_private__ "; 398 } 399 400 PrintingPolicy SubPolicy(Policy); 401 SubPolicy.SuppressSpecifiers = false; 402 std::string Proto = D->getNameInfo().getAsString(); 403 404 QualType Ty = D->getType(); 405 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 406 Proto = '(' + Proto + ')'; 407 Ty = PT->getInnerType(); 408 } 409 410 if (isa<FunctionType>(Ty)) { 411 const FunctionType *AFT = Ty->getAs<FunctionType>(); 412 const FunctionProtoType *FT = 0; 413 if (D->hasWrittenPrototype()) 414 FT = dyn_cast<FunctionProtoType>(AFT); 415 416 Proto += "("; 417 if (FT) { 418 llvm::raw_string_ostream POut(Proto); 419 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation); 420 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 421 if (i) POut << ", "; 422 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 423 } 424 425 if (FT->isVariadic()) { 426 if (D->getNumParams()) POut << ", "; 427 POut << "..."; 428 } 429 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { 430 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 431 if (i) 432 Proto += ", "; 433 Proto += D->getParamDecl(i)->getNameAsString(); 434 } 435 } 436 437 Proto += ")"; 438 439 if (FT && FT->getTypeQuals()) { 440 unsigned TypeQuals = FT->getTypeQuals(); 441 if (TypeQuals & Qualifiers::Const) 442 Proto += " const"; 443 if (TypeQuals & Qualifiers::Volatile) 444 Proto += " volatile"; 445 if (TypeQuals & Qualifiers::Restrict) 446 Proto += " restrict"; 447 } 448 449 if (FT && FT->hasDynamicExceptionSpec()) { 450 Proto += " throw("; 451 if (FT->getExceptionSpecType() == EST_MSAny) 452 Proto += "..."; 453 else 454 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { 455 if (I) 456 Proto += ", "; 457 458 Proto += FT->getExceptionType(I).getAsString(SubPolicy);; 459 } 460 Proto += ")"; 461 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { 462 Proto += " noexcept"; 463 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) { 464 Proto += "("; 465 llvm::raw_string_ostream EOut(Proto); 466 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy, 467 Indentation); 468 EOut.flush(); 469 Proto += EOut.str(); 470 Proto += ")"; 471 } 472 } 473 474 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) { 475 bool HasInitializerList = false; 476 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(), 477 E = CDecl->init_end(); 478 B != E; ++B) { 479 CXXCtorInitializer * BMInitializer = (*B); 480 if (BMInitializer->isInClassMemberInitializer()) 481 continue; 482 483 if (!HasInitializerList) { 484 Proto += " : "; 485 Out << Proto; 486 Proto.clear(); 487 HasInitializerList = true; 488 } else 489 Out << ", "; 490 491 if (BMInitializer->isAnyMemberInitializer()) { 492 FieldDecl *FD = BMInitializer->getAnyMember(); 493 Out << *FD; 494 } else { 495 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); 496 } 497 498 Out << "("; 499 if (!BMInitializer->getInit()) { 500 // Nothing to print 501 } else { 502 Expr *Init = BMInitializer->getInit(); 503 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) 504 Init = Tmp->getSubExpr(); 505 506 Init = Init->IgnoreParens(); 507 508 Expr *SimpleInit = 0; 509 Expr **Args = 0; 510 unsigned NumArgs = 0; 511 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { 512 Args = ParenList->getExprs(); 513 NumArgs = ParenList->getNumExprs(); 514 } else if (CXXConstructExpr *Construct 515 = dyn_cast<CXXConstructExpr>(Init)) { 516 Args = Construct->getArgs(); 517 NumArgs = Construct->getNumArgs(); 518 } else 519 SimpleInit = Init; 520 521 if (SimpleInit) 522 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation); 523 else { 524 for (unsigned I = 0; I != NumArgs; ++I) { 525 if (isa<CXXDefaultArgExpr>(Args[I])) 526 break; 527 528 if (I) 529 Out << ", "; 530 Args[I]->printPretty(Out, Context, 0, Policy, Indentation); 531 } 532 } 533 } 534 Out << ")"; 535 } 536 } 537 else 538 AFT->getResultType().print(Out, Policy, Proto); 539 } else { 540 Ty.print(Out, Policy, Proto); 541 } 542 543 prettyPrintAttributes(D); 544 545 if (D->isPure()) 546 Out << " = 0"; 547 else if (D->isDeletedAsWritten()) 548 Out << " = delete"; 549 else if (D->doesThisDeclarationHaveABody()) { 550 if (!D->hasPrototype() && D->getNumParams()) { 551 // This is a K&R function definition, so we need to print the 552 // parameters. 553 Out << '\n'; 554 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation); 555 Indentation += Policy.Indentation; 556 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 557 Indent(); 558 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 559 Out << ";\n"; 560 } 561 Indentation -= Policy.Indentation; 562 } else 563 Out << ' '; 564 565 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation); 566 Out << '\n'; 567 } 568} 569 570void DeclPrinter::VisitFieldDecl(FieldDecl *D) { 571 if (!Policy.SuppressSpecifiers && D->isMutable()) 572 Out << "mutable "; 573 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 574 Out << "__module_private__ "; 575 576 Out << D->getType().stream(Policy, D->getName()); 577 578 if (D->isBitField()) { 579 Out << " : "; 580 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation); 581 } 582 583 Expr *Init = D->getInClassInitializer(); 584 if (!Policy.SuppressInitializers && Init) { 585 Out << " = "; 586 Init->printPretty(Out, Context, 0, Policy, Indentation); 587 } 588 prettyPrintAttributes(D); 589} 590 591void DeclPrinter::VisitLabelDecl(LabelDecl *D) { 592 Out << *D << ":"; 593} 594 595 596void DeclPrinter::VisitVarDecl(VarDecl *D) { 597 StorageClass SCAsWritten = D->getStorageClassAsWritten(); 598 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None) 599 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " "; 600 601 if (!Policy.SuppressSpecifiers && D->isThreadSpecified()) 602 Out << "__thread "; 603 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 604 Out << "__module_private__ "; 605 606 QualType T = D->getType(); 607 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) 608 T = Parm->getOriginalType(); 609 T.print(Out, Policy, D->getName()); 610 Expr *Init = D->getInit(); 611 if (!Policy.SuppressInitializers && Init) { 612 bool ImplicitInit = false; 613 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) 614 ImplicitInit = D->getInitStyle() == VarDecl::CallInit && 615 Construct->getNumArgs() == 0 && !Construct->isListInitialization(); 616 if (!ImplicitInit) { 617 if (D->getInitStyle() == VarDecl::CallInit) 618 Out << "("; 619 else if (D->getInitStyle() == VarDecl::CInit) { 620 Out << " = "; 621 } 622 Init->printPretty(Out, Context, 0, Policy, Indentation); 623 if (D->getInitStyle() == VarDecl::CallInit) 624 Out << ")"; 625 } 626 } 627 prettyPrintAttributes(D); 628} 629 630void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { 631 VisitVarDecl(D); 632} 633 634void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 635 Out << "__asm ("; 636 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation); 637 Out << ")"; 638} 639 640void DeclPrinter::VisitImportDecl(ImportDecl *D) { 641 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName() 642 << ";\n"; 643} 644 645void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { 646 Out << "static_assert("; 647 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation); 648 Out << ", "; 649 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation); 650 Out << ")"; 651} 652 653//---------------------------------------------------------------------------- 654// C++ declarations 655//---------------------------------------------------------------------------- 656void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { 657 if (D->isInline()) 658 Out << "inline "; 659 Out << "namespace " << *D << " {\n"; 660 VisitDeclContext(D); 661 Indent() << "}"; 662} 663 664void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 665 Out << "using namespace "; 666 if (D->getQualifier()) 667 D->getQualifier()->print(Out, Policy); 668 Out << *D->getNominatedNamespaceAsWritten(); 669} 670 671void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 672 Out << "namespace " << *D << " = "; 673 if (D->getQualifier()) 674 D->getQualifier()->print(Out, Policy); 675 Out << *D->getAliasedNamespace(); 676} 677 678void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { 679 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 680 Out << "__module_private__ "; 681 Out << D->getKindName(); 682 if (D->getIdentifier()) 683 Out << ' ' << *D; 684 685 if (D->isCompleteDefinition()) { 686 // Print the base classes 687 if (D->getNumBases()) { 688 Out << " : "; 689 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), 690 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { 691 if (Base != D->bases_begin()) 692 Out << ", "; 693 694 if (Base->isVirtual()) 695 Out << "virtual "; 696 697 AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); 698 if (AS != AS_none) 699 Print(AS); 700 Out << " " << Base->getType().getAsString(Policy); 701 702 if (Base->isPackExpansion()) 703 Out << "..."; 704 } 705 } 706 707 // Print the class definition 708 // FIXME: Doesn't print access specifiers, e.g., "public:" 709 Out << " {\n"; 710 VisitDeclContext(D); 711 Indent() << "}"; 712 } 713} 714 715void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 716 const char *l; 717 if (D->getLanguage() == LinkageSpecDecl::lang_c) 718 l = "C"; 719 else { 720 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && 721 "unknown language in linkage specification"); 722 l = "C++"; 723 } 724 725 Out << "extern \"" << l << "\" "; 726 if (D->hasBraces()) { 727 Out << "{\n"; 728 VisitDeclContext(D); 729 Indent() << "}"; 730 } else 731 Visit(*D->decls_begin()); 732} 733 734void DeclPrinter::PrintTemplateParameters( 735 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) { 736 assert(Params); 737 assert(!Args || Params->size() == Args->size()); 738 739 Out << "template <"; 740 741 for (unsigned i = 0, e = Params->size(); i != e; ++i) { 742 if (i != 0) 743 Out << ", "; 744 745 const Decl *Param = Params->getParam(i); 746 if (const TemplateTypeParmDecl *TTP = 747 dyn_cast<TemplateTypeParmDecl>(Param)) { 748 749 if (TTP->wasDeclaredWithTypename()) 750 Out << "typename "; 751 else 752 Out << "class "; 753 754 if (TTP->isParameterPack()) 755 Out << "... "; 756 757 Out << *TTP; 758 759 if (Args) { 760 Out << " = "; 761 Args->get(i).print(Policy, Out); 762 } else if (TTP->hasDefaultArgument()) { 763 Out << " = "; 764 Out << TTP->getDefaultArgument().getAsString(Policy); 765 }; 766 } else if (const NonTypeTemplateParmDecl *NTTP = 767 dyn_cast<NonTypeTemplateParmDecl>(Param)) { 768 Out << NTTP->getType().getAsString(Policy); 769 770 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType())) 771 Out << "..."; 772 773 if (IdentifierInfo *Name = NTTP->getIdentifier()) { 774 Out << ' '; 775 Out << Name->getName(); 776 } 777 778 if (Args) { 779 Out << " = "; 780 Args->get(i).print(Policy, Out); 781 } else if (NTTP->hasDefaultArgument()) { 782 Out << " = "; 783 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy, 784 Indentation); 785 } 786 } else if (const TemplateTemplateParmDecl *TTPD = 787 dyn_cast<TemplateTemplateParmDecl>(Param)) { 788 VisitTemplateDecl(TTPD); 789 // FIXME: print the default argument, if present. 790 } 791 } 792 793 Out << "> "; 794} 795 796void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 797 PrintTemplateParameters(D->getTemplateParameters()); 798 799 if (const TemplateTemplateParmDecl *TTP = 800 dyn_cast<TemplateTemplateParmDecl>(D)) { 801 Out << "class "; 802 if (TTP->isParameterPack()) 803 Out << "..."; 804 Out << D->getName(); 805 } else { 806 Visit(D->getTemplatedDecl()); 807 } 808} 809 810void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 811 if (PrintInstantiation) { 812 TemplateParameterList *Params = D->getTemplateParameters(); 813 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); 814 I != E; ++I) { 815 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs()); 816 Visit(*I); 817 } 818 } 819 820 return VisitRedeclarableTemplateDecl(D); 821} 822 823void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 824 if (PrintInstantiation) { 825 TemplateParameterList *Params = D->getTemplateParameters(); 826 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); 827 I != E; ++I) { 828 PrintTemplateParameters(Params, &(*I)->getTemplateArgs()); 829 Visit(*I); 830 Out << '\n'; 831 } 832 } 833 834 return VisitRedeclarableTemplateDecl(D); 835} 836 837//---------------------------------------------------------------------------- 838// Objective-C declarations 839//---------------------------------------------------------------------------- 840 841void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { 842 if (OMD->isInstanceMethod()) 843 Out << "- "; 844 else 845 Out << "+ "; 846 if (!OMD->getResultType().isNull()) 847 Out << '(' << OMD->getResultType().getAsString(Policy) << ")"; 848 849 std::string name = OMD->getSelector().getAsString(); 850 std::string::size_type pos, lastPos = 0; 851 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), 852 E = OMD->param_end(); PI != E; ++PI) { 853 // FIXME: selector is missing here! 854 pos = name.find_first_of(':', lastPos); 855 Out << " " << name.substr(lastPos, pos - lastPos); 856 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI; 857 lastPos = pos + 1; 858 } 859 860 if (OMD->param_begin() == OMD->param_end()) 861 Out << " " << name; 862 863 if (OMD->isVariadic()) 864 Out << ", ..."; 865 866 if (OMD->getBody()) { 867 Out << ' '; 868 OMD->getBody()->printPretty(Out, Context, 0, Policy); 869 Out << '\n'; 870 } 871} 872 873void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { 874 std::string I = OID->getNameAsString(); 875 ObjCInterfaceDecl *SID = OID->getSuperClass(); 876 877 if (SID) 878 Out << "@implementation " << I << " : " << *SID; 879 else 880 Out << "@implementation " << I; 881 Out << "\n"; 882 VisitDeclContext(OID, false); 883 Out << "@end"; 884} 885 886void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { 887 std::string I = OID->getNameAsString(); 888 ObjCInterfaceDecl *SID = OID->getSuperClass(); 889 890 if (!OID->isThisDeclarationADefinition()) { 891 Out << "@class " << I << ";"; 892 return; 893 } 894 895 if (SID) 896 Out << "@interface " << I << " : " << *SID; 897 else 898 Out << "@interface " << I; 899 900 // Protocols? 901 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); 902 if (!Protocols.empty()) { 903 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 904 E = Protocols.end(); I != E; ++I) 905 Out << (I == Protocols.begin() ? '<' : ',') << **I; 906 } 907 908 if (!Protocols.empty()) 909 Out << "> "; 910 911 if (OID->ivar_size() > 0) { 912 Out << "{\n"; 913 Indentation += Policy.Indentation; 914 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), 915 E = OID->ivar_end(); I != E; ++I) { 916 Indent() << I->getType().getAsString(Policy) << ' ' << *I << ";\n"; 917 } 918 Indentation -= Policy.Indentation; 919 Out << "}\n"; 920 } 921 922 VisitDeclContext(OID, false); 923 Out << "@end"; 924 // FIXME: implement the rest... 925} 926 927void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 928 if (!PID->isThisDeclarationADefinition()) { 929 Out << "@protocol " << PID->getIdentifier() << ";\n"; 930 return; 931 } 932 933 Out << "@protocol " << *PID << '\n'; 934 VisitDeclContext(PID, false); 935 Out << "@end"; 936} 937 938void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { 939 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; 940 941 VisitDeclContext(PID, false); 942 Out << "@end"; 943 // FIXME: implement the rest... 944} 945 946void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { 947 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n"; 948 VisitDeclContext(PID, false); 949 Out << "@end"; 950 951 // FIXME: implement the rest... 952} 953 954void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { 955 Out << "@compatibility_alias " << *AID 956 << ' ' << *AID->getClassInterface() << ";\n"; 957} 958 959/// PrintObjCPropertyDecl - print a property declaration. 960/// 961void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { 962 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) 963 Out << "@required\n"; 964 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) 965 Out << "@optional\n"; 966 967 Out << "@property"; 968 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { 969 bool first = true; 970 Out << " ("; 971 if (PDecl->getPropertyAttributes() & 972 ObjCPropertyDecl::OBJC_PR_readonly) { 973 Out << (first ? ' ' : ',') << "readonly"; 974 first = false; 975 } 976 977 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 978 Out << (first ? ' ' : ',') << "getter = " 979 << PDecl->getGetterName().getAsString(); 980 first = false; 981 } 982 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 983 Out << (first ? ' ' : ',') << "setter = " 984 << PDecl->getSetterName().getAsString(); 985 first = false; 986 } 987 988 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { 989 Out << (first ? ' ' : ',') << "assign"; 990 first = false; 991 } 992 993 if (PDecl->getPropertyAttributes() & 994 ObjCPropertyDecl::OBJC_PR_readwrite) { 995 Out << (first ? ' ' : ',') << "readwrite"; 996 first = false; 997 } 998 999 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { 1000 Out << (first ? ' ' : ',') << "retain"; 1001 first = false; 1002 } 1003 1004 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { 1005 Out << (first ? ' ' : ',') << "strong"; 1006 first = false; 1007 } 1008 1009 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { 1010 Out << (first ? ' ' : ',') << "copy"; 1011 first = false; 1012 } 1013 1014 if (PDecl->getPropertyAttributes() & 1015 ObjCPropertyDecl::OBJC_PR_nonatomic) { 1016 Out << (first ? ' ' : ',') << "nonatomic"; 1017 first = false; 1018 } 1019 if (PDecl->getPropertyAttributes() & 1020 ObjCPropertyDecl::OBJC_PR_atomic) { 1021 Out << (first ? ' ' : ',') << "atomic"; 1022 first = false; 1023 } 1024 1025 (void) first; // Silence dead store warning due to idiomatic code. 1026 Out << " )"; 1027 } 1028 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl; 1029} 1030 1031void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { 1032 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 1033 Out << "@synthesize "; 1034 else 1035 Out << "@dynamic "; 1036 Out << *PID->getPropertyDecl(); 1037 if (PID->getPropertyIvarDecl()) 1038 Out << '=' << *PID->getPropertyIvarDecl(); 1039} 1040 1041void DeclPrinter::VisitUsingDecl(UsingDecl *D) { 1042 Out << "using "; 1043 D->getQualifier()->print(Out, Policy); 1044 Out << *D; 1045} 1046 1047void 1048DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 1049 Out << "using typename "; 1050 D->getQualifier()->print(Out, Policy); 1051 Out << D->getDeclName(); 1052} 1053 1054void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1055 Out << "using "; 1056 D->getQualifier()->print(Out, Policy); 1057 Out << D->getDeclName(); 1058} 1059 1060void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { 1061 // ignore 1062} 1063