diff options
author | rdivacky <rdivacky@FreeBSD.org> | 2009-10-14 18:03:49 +0000 |
---|---|---|
committer | rdivacky <rdivacky@FreeBSD.org> | 2009-10-14 18:03:49 +0000 |
commit | 9092c3e0fa01f3139b016d05d267a89e3b07747a (patch) | |
tree | 137ebebcae16fb0ce7ab4af456992bbd8d22fced /lib/AST/DeclPrinter.cpp | |
parent | 4981926bf654fe5a2c3893f24ca44106b217e71e (diff) | |
download | FreeBSD-src-9092c3e0fa01f3139b016d05d267a89e3b07747a.zip FreeBSD-src-9092c3e0fa01f3139b016d05d267a89e3b07747a.tar.gz |
Update clang to r84119.
Diffstat (limited to 'lib/AST/DeclPrinter.cpp')
-rw-r--r-- | lib/AST/DeclPrinter.cpp | 244 |
1 files changed, 175 insertions, 69 deletions
diff --git a/lib/AST/DeclPrinter.cpp b/lib/AST/DeclPrinter.cpp index 12e89cd..9d0d836 100644 --- a/lib/AST/DeclPrinter.cpp +++ b/lib/AST/DeclPrinter.cpp @@ -19,7 +19,6 @@ #include "clang/AST/Expr.h" #include "clang/AST/PrettyPrinter.h" #include "llvm/Support/Compiler.h" -#include "llvm/Support/Streams.h" #include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" using namespace clang; @@ -34,8 +33,10 @@ namespace { llvm::raw_ostream& Indent(); void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls); + void Print(AccessSpecifier AS); + public: - DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context, + DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context, const PrintingPolicy &Policy, unsigned Indentation = 0) : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { } @@ -71,17 +72,19 @@ namespace { void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); void VisitObjCPropertyDecl(ObjCPropertyDecl *D); void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); + void VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D); + void VisitUsingDecl(UsingDecl *D); }; } -void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) { +void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const { print(Out, getASTContext().PrintingPolicy, Indentation); } void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy, - unsigned Indentation) { + unsigned Indentation) const { DeclPrinter Printer(Out, getASTContext(), Policy, Indentation); - Printer.Visit(this); + Printer.Visit(const_cast<Decl*>(this)); } static QualType GetBaseType(QualType T) { @@ -90,13 +93,13 @@ static QualType GetBaseType(QualType T) { while (!BaseType->isSpecifierType()) { if (isa<TypedefType>(BaseType)) break; - else if (const PointerType* PTy = BaseType->getAsPointerType()) + else if (const PointerType* PTy = BaseType->getAs<PointerType>()) BaseType = PTy->getPointeeType(); else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) BaseType = ATy->getElementType(); - else if (const FunctionType* FTy = BaseType->getAsFunctionType()) + else if (const FunctionType* FTy = BaseType->getAs<FunctionType>()) BaseType = FTy->getResultType(); - else if (const VectorType *VTy = BaseType->getAsVectorType()) + else if (const VectorType *VTy = BaseType->getAs<VectorType>()) BaseType = VTy->getElementType(); else assert(0 && "Unknown declarator!"); @@ -146,7 +149,7 @@ void Decl::printGroup(Decl** Begin, unsigned NumDecls, } } -void Decl::dump() { +void Decl::dump() const { print(llvm::errs()); } @@ -164,6 +167,15 @@ void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) { } +void DeclPrinter::Print(AccessSpecifier AS) { + switch(AS) { + case AS_none: assert(0 && "No access specifier!"); break; + case AS_public: Out << "public"; break; + case AS_protected: Out << "protected"; break; + case AS_private: Out << " private"; break; + } +} + //---------------------------------------------------------------------------- // Common C declarations //---------------------------------------------------------------------------- @@ -172,6 +184,9 @@ void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { if (Indent) Indentation += Policy.Indentation; + bool PrintAccess = isa<CXXRecordDecl>(DC); + AccessSpecifier CurAS = AS_none; + llvm::SmallVector<Decl*, 2> Decls; for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); D != DEnd; ++D) { @@ -185,6 +200,16 @@ void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { continue; } + if (PrintAccess) { + AccessSpecifier AS = D->getAccess(); + + if (AS != CurAS) { + Print(AS); + Out << ":\n"; + CurAS = AS; + } + } + // The next bits of code handles stuff like "struct {int x;} a,b"; we're // forced to merge the declarations because there's no other way to // refer to the struct in question. This limited merging is safe without @@ -215,16 +240,16 @@ void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { } this->Indent(); Visit(*D); - - // FIXME: Need to be able to tell the DeclPrinter when + + // FIXME: Need to be able to tell the DeclPrinter when const char *Terminator = 0; - if (isa<FunctionDecl>(*D) && + if (isa<FunctionDecl>(*D) && cast<FunctionDecl>(*D)->isThisDeclarationADefinition()) Terminator = 0; else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody()) Terminator = 0; else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) || - isa<ObjCImplementationDecl>(*D) || + isa<ObjCImplementationDecl>(*D) || isa<ObjCInterfaceDecl>(*D) || isa<ObjCProtocolDecl>(*D) || isa<ObjCCategoryImplDecl>(*D) || @@ -274,7 +299,7 @@ void DeclPrinter::VisitRecordDecl(RecordDecl *D) { Out << " "; Out << D->getNameAsString(); } - + if (D->isDefinition()) { Out << " {\n"; VisitDeclContext(D); @@ -290,7 +315,7 @@ void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { } } -void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { +void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { if (!Policy.SuppressSpecifiers) { switch (D->getStorageClass()) { case FunctionDecl::None: break; @@ -307,7 +332,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { SubPolicy.SuppressSpecifiers = false; std::string Proto = D->getNameAsString(); if (isa<FunctionType>(D->getType().getTypePtr())) { - const FunctionType *AFT = D->getType()->getAsFunctionType(); + const FunctionType *AFT = D->getType()->getAs<FunctionType>(); const FunctionProtoType *FT = 0; if (D->hasWrittenPrototype()) @@ -321,7 +346,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { if (i) POut << ", "; ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); } - + if (FT->isVariadic()) { if (D->getNumParams()) POut << ", "; POut << "..."; @@ -335,7 +360,81 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { } Proto += ")"; - AFT->getResultType().getAsStringInternal(Proto, Policy); + if (D->hasAttr<NoReturnAttr>()) + Proto += " __attribute((noreturn))"; + if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) { + if (CDecl->getNumBaseOrMemberInitializers() > 0) { + Proto += " : "; + Out << Proto; + Proto.clear(); + for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(), + E = CDecl->init_end(); + B != E; ++B) { + CXXBaseOrMemberInitializer * BMInitializer = (*B); + if (B != CDecl->init_begin()) + Out << ", "; + bool hasArguments = (BMInitializer->arg_begin() != + BMInitializer->arg_end()); + if (BMInitializer->isMemberInitializer()) { + FieldDecl *FD = BMInitializer->getMember(); + Out << FD->getNameAsString(); + } + else // FIXME. skip dependent types for now. + if (const RecordType *RT = + BMInitializer->getBaseClass()->getAs<RecordType>()) { + const CXXRecordDecl *BaseDecl = + cast<CXXRecordDecl>(RT->getDecl()); + Out << BaseDecl->getNameAsString(); + } + if (hasArguments) { + Out << "("; + for (CXXBaseOrMemberInitializer::const_arg_iterator BE = + BMInitializer->const_arg_begin(), + EE = BMInitializer->const_arg_end(); BE != EE; ++BE) { + if (BE != BMInitializer->const_arg_begin()) + Out<< ", "; + const Expr *Exp = (*BE); + Exp->printPretty(Out, Context, 0, Policy, Indentation); + } + Out << ")"; + } else + Out << "()"; + } + } + } + else if (CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(D)) { + if (DDecl->getNumBaseOrMemberDestructions() > 0) { + // List order of base/member destruction for visualization purposes. + assert (D->isThisDeclarationADefinition() && "Destructor with dtor-list"); + Proto += "/* : "; + for (CXXDestructorDecl::destr_const_iterator *B = DDecl->destr_begin(), + *E = DDecl->destr_end(); + B != E; ++B) { + uintptr_t BaseOrMember = (*B); + if (B != DDecl->destr_begin()) + Proto += ", "; + + if (DDecl->isMemberToDestroy(BaseOrMember)) { + FieldDecl *FD = DDecl->getMemberToDestroy(BaseOrMember); + Proto += "~"; + Proto += FD->getNameAsString(); + } + else // FIXME. skip dependent types for now. + if (const RecordType *RT = + DDecl->getAnyBaseClassToDestroy(BaseOrMember) + ->getAs<RecordType>()) { + const CXXRecordDecl *BaseDecl = + cast<CXXRecordDecl>(RT->getDecl()); + Proto += "~"; + Proto += BaseDecl->getNameAsString(); + } + Proto += "()"; + } + Proto += " */"; + } + } + else + AFT->getResultType().getAsStringInternal(Proto, Policy); } else { D->getType().getAsStringInternal(Proto, Policy); } @@ -423,7 +522,7 @@ void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { // C++ declarations //---------------------------------------------------------------------------- void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) { - assert(false && + assert(false && "OverloadedFunctionDecls aren't really decls and are never printed"); } @@ -453,28 +552,23 @@ void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { Out << " "; Out << D->getNameAsString(); } - + if (D->isDefinition()) { // Print the base classes if (D->getNumBases()) { Out << " : "; - for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(), - BaseEnd = D->bases_end(); - Base != BaseEnd; ++Base) { + for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), + BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { if (Base != D->bases_begin()) Out << ", "; if (Base->isVirtual()) Out << "virtual "; - switch(Base->getAccessSpecifierAsWritten()) { - case AS_none: break; - case AS_public: Out << "public "; break; - case AS_protected: Out << "protected "; break; - case AS_private: Out << " private "; break; - } - - Out << Base->getType().getAsString(Policy); + AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); + if (AS != AS_none) + Print(AS); + Out << " " << Base->getType().getAsString(Policy); } } @@ -483,7 +577,7 @@ void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { Out << " {\n"; VisitDeclContext(D); Indent() << "}"; - } + } } void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { @@ -507,17 +601,17 @@ void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) { Out << "template <"; - + TemplateParameterList *Params = D->getTemplateParameters(); for (unsigned i = 0, e = Params->size(); i != e; ++i) { if (i != 0) Out << ", "; - + const Decl *Param = Params->getParam(i); - if (const TemplateTypeParmDecl *TTP = + if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { - - QualType ParamType = + + QualType ParamType = Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP)); if (TTP->wasDeclaredWithTypename()) @@ -527,14 +621,14 @@ void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) { if (TTP->isParameterPack()) Out << "... "; - + Out << ParamType.getAsString(Policy); if (TTP->hasDefaultArgument()) { Out << " = "; Out << TTP->getDefaultArgument().getAsString(Policy); }; - } else if (const NonTypeTemplateParmDecl *NTTP = + } else if (const NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { Out << NTTP->getType().getAsString(Policy); @@ -542,15 +636,15 @@ void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) { Out << ' '; Out << Name->getName(); } - + if (NTTP->hasDefaultArgument()) { Out << " = "; - NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy, + NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy, Indentation); } } } - + Out << "> "; Visit(D->getTemplatedDecl()); @@ -572,29 +666,29 @@ void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) { void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { if (OMD->isInstanceMethod()) Out << "- "; - else + else Out << "+ "; if (!OMD->getResultType().isNull()) Out << '(' << OMD->getResultType().getAsString(Policy) << ")"; - + std::string name = OMD->getSelector().getAsString(); std::string::size_type pos, lastPos = 0; for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), E = OMD->param_end(); PI != E; ++PI) { - // FIXME: selector is missing here! + // FIXME: selector is missing here! pos = name.find_first_of(":", lastPos); Out << " " << name.substr(lastPos, pos - lastPos); Out << ":(" << (*PI)->getType().getAsString(Policy) << ")" - << (*PI)->getNameAsString(); + << (*PI)->getNameAsString(); lastPos = pos + 1; } - + if (OMD->param_begin() == OMD->param_end()) Out << " " << name; - + if (OMD->isVariadic()) Out << ", ..."; - + if (OMD->getBody()) { Out << ' '; OMD->getBody()->printPretty(Out, Context, 0, Policy); @@ -623,7 +717,7 @@ void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { Out << "@interface " << I << " : " << SID->getNameAsString(); else Out << "@interface " << I; - + // Protocols? const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); if (!Protocols.empty()) { @@ -631,22 +725,22 @@ void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { E = Protocols.end(); I != E; ++I) Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString(); } - + if (!Protocols.empty()) Out << "> "; - + if (OID->ivar_size() > 0) { Out << "{\n"; Indentation += Policy.Indentation; for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), E = OID->ivar_end(); I != E; ++I) { Indent() << (*I)->getType().getAsString(Policy) - << ' ' << (*I)->getNameAsString() << ";\n"; + << ' ' << (*I)->getNameAsString() << ";\n"; } Indentation -= Policy.Indentation; Out << "}\n"; } - + VisitDeclContext(OID, false); Out << "@end"; // FIXME: implement the rest... @@ -654,7 +748,7 @@ void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { Out << "@protocol "; - for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), + for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I) { if (I != D->protocol_begin()) Out << ", "; @@ -671,7 +765,7 @@ void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { Out << "@implementation " << PID->getClassInterface()->getNameAsString() - << '(' << PID->getNameAsString() << ")\n"; + << '(' << PID->getNameAsString() << ")\n"; VisitDeclContext(PID, false); Out << "@end"; @@ -679,18 +773,18 @@ void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { } void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { - Out << "@interface " + Out << "@interface " << PID->getClassInterface()->getNameAsString() << '(' << PID->getNameAsString() << ")\n"; VisitDeclContext(PID, false); Out << "@end"; - + // FIXME: implement the rest... } void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { - Out << "@compatibility_alias " << AID->getNameAsString() - << ' ' << AID->getClassInterface()->getNameAsString() << ";\n"; + Out << "@compatibility_alias " << AID->getNameAsString() + << ' ' << AID->getClassInterface()->getNameAsString() << ";\n"; } /// PrintObjCPropertyDecl - print a property declaration. @@ -700,17 +794,17 @@ void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { Out << "@required\n"; else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) Out << "@optional\n"; - + Out << "@property"; if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { bool first = true; Out << " ("; - if (PDecl->getPropertyAttributes() & + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) { Out << (first ? ' ' : ',') << "readonly"; first = false; } - + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { Out << (first ? ' ' : ',') << "getter = " << PDecl->getGetterName().getAsString(); @@ -721,29 +815,29 @@ void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { << PDecl->getSetterName().getAsString(); first = false; } - + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { Out << (first ? ' ' : ',') << "assign"; first = false; } - + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) { Out << (first ? ' ' : ',') << "readwrite"; first = false; } - + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { Out << (first ? ' ' : ',') << "retain"; first = false; } - + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { Out << (first ? ' ' : ',') << "copy"; first = false; } - - if (PDecl->getPropertyAttributes() & + + if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) { Out << (first ? ' ' : ',') << "nonatomic"; first = false; @@ -763,3 +857,15 @@ void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { if (PID->getPropertyIvarDecl()) Out << "=" << PID->getPropertyIvarDecl()->getNameAsString(); } + +void DeclPrinter::VisitUsingDecl(UsingDecl *D) { + Out << "using "; + D->getTargetNestedNameDecl()->print(Out, Policy); + Out << D->getTargetDecl()->getNameAsString(); +} + +void DeclPrinter::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) { + Out << "using "; + D->getTargetNestedNameSpecifier()->print(Out, Policy); + Out << D->getTargetName().getAsString(); +} |