From 89afb52800035ce6357347cf0902e2023e3c06c1 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 24 Feb 2015 01:23:23 +0000 Subject: [PATCH] Refactor *TemplateDecl::addSpecialization to reduce duplication and add some more asserts. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@230296 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclTemplate.h | 22 ++++++++--- lib/AST/DeclTemplate.cpp | 68 +++++++++++++++++--------------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/include/clang/AST/DeclTemplate.h b/include/clang/AST/DeclTemplate.h index 61ca105e33..ff226e437c 100644 --- a/include/clang/AST/DeclTemplate.h +++ b/include/clang/AST/DeclTemplate.h @@ -545,8 +545,11 @@ protected: template struct SpecEntryTraits { typedef EntryType DeclType; - static DeclType *getMostRecentDecl(EntryType *D) { - return D->getMostRecentDecl(); + static DeclType *getDecl(EntryType *D) { + return D; + } + static ArrayRef getTemplateArgs(EntryType *D) { + return D->getTemplateArgs().asArray(); } }; @@ -565,7 +568,7 @@ protected: : SpecIterator::iterator_adaptor_base(std::move(SetIter)) {} DeclType *operator*() const { - return SETraits::getMostRecentDecl(&*this->I); + return SETraits::getDecl(&*this->I)->getMostRecentDecl(); } DeclType *operator->() const { return **this; } }; @@ -580,6 +583,10 @@ protected: findSpecializationImpl(llvm::FoldingSetVector &Specs, ArrayRef Args, void *&InsertPos); + template + void addSpecializationImpl(llvm::FoldingSetVector &Specs, + EntryType *Entry, void *InsertPos); + struct CommonBase { CommonBase() : InstantiatedFromMember(nullptr, false) { } @@ -719,9 +726,12 @@ template <> struct RedeclarableTemplateDecl:: SpecEntryTraits { typedef FunctionDecl DeclType; - static DeclType * - getMostRecentDecl(FunctionTemplateSpecializationInfo *I) { - return I->Function->getMostRecentDecl(); + static DeclType *getDecl(FunctionTemplateSpecializationInfo *I) { + return I->Function; + } + static ArrayRef + getTemplateArgs(FunctionTemplateSpecializationInfo *I) { + return I->TemplateArguments->asArray(); } }; diff --git a/lib/AST/DeclTemplate.cpp b/lib/AST/DeclTemplate.cpp index 0d1d2a4613..2c64e5933b 100644 --- a/lib/AST/DeclTemplate.cpp +++ b/lib/AST/DeclTemplate.cpp @@ -160,17 +160,43 @@ RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() c return Common; } -template -typename RedeclarableTemplateDecl::SpecEntryTraits::DeclType* +template +typename RedeclarableTemplateDecl::SpecEntryTraits::DeclType * RedeclarableTemplateDecl::findSpecializationImpl( - llvm::FoldingSetVector &Specs, - ArrayRef Args, - void *&InsertPos) { + llvm::FoldingSetVector &Specs, ArrayRef Args, + void *&InsertPos) { typedef SpecEntryTraits SETraits; llvm::FoldingSetNodeID ID; EntryType::Profile(ID,Args, getASTContext()); EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos); - return Entry ? SETraits::getMostRecentDecl(Entry) : nullptr; + return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr; +} + +template +void RedeclarableTemplateDecl::addSpecializationImpl( + llvm::FoldingSetVector &Specializations, EntryType *Entry, + void *InsertPos) { + typedef SpecEntryTraits SETraits; + if (InsertPos) { +#ifndef NDEBUG + void *CorrectInsertPos; + assert(!findSpecializationImpl(Specializations, + SETraits::getTemplateArgs(Entry), + CorrectInsertPos) && + InsertPos == CorrectInsertPos && + "given incorrect InsertPos for specialization"); +#endif + Specializations.InsertNode(Entry, InsertPos); + } else { + EntryType *Existing = Specializations.GetOrInsertNode(Entry); + (void)Existing; + assert(SETraits::getDecl(Existing)->isCanonicalDecl() && + "non-canonical specialization?"); + } + + if (ASTMutationListener *L = getASTMutationListener()) + L->AddedCXXTemplateSpecialization(cast(this), + SETraits::getDecl(Entry)); } /// \brief Generate the injected template arguments for the given template @@ -270,12 +296,8 @@ FunctionTemplateDecl::findSpecialization(ArrayRef Args, void FunctionTemplateDecl::addSpecialization( FunctionTemplateSpecializationInfo *Info, void *InsertPos) { - if (InsertPos) - getSpecializations().InsertNode(Info, InsertPos); - else - getSpecializations().GetOrInsertNode(Info); - if (ASTMutationListener *L = getASTMutationListener()) - L->AddedCXXTemplateSpecialization(this, Info->Function); + addSpecializationImpl(getSpecializations(), Info, + InsertPos); } ArrayRef FunctionTemplateDecl::getInjectedTemplateArgs() { @@ -357,16 +379,7 @@ ClassTemplateDecl::findSpecialization(ArrayRef Args, void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos) { - if (InsertPos) - getSpecializations().InsertNode(D, InsertPos); - else { - ClassTemplateSpecializationDecl *Existing - = getSpecializations().GetOrInsertNode(D); - (void)Existing; - assert(Existing->isCanonicalDecl() && "Non-canonical specialization?"); - } - if (ASTMutationListener *L = getASTMutationListener()) - L->AddedCXXTemplateSpecialization(this, D); + addSpecializationImpl(getSpecializations(), D, InsertPos); } ClassTemplatePartialSpecializationDecl * @@ -990,16 +1003,7 @@ VarTemplateDecl::findSpecialization(ArrayRef Args, void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos) { - if (InsertPos) - getSpecializations().InsertNode(D, InsertPos); - else { - VarTemplateSpecializationDecl *Existing = - getSpecializations().GetOrInsertNode(D); - (void)Existing; - assert(Existing->isCanonicalDecl() && "Non-canonical specialization?"); - } - if (ASTMutationListener *L = getASTMutationListener()) - L->AddedCXXTemplateSpecialization(this, D); + addSpecializationImpl(getSpecializations(), D, InsertPos); } VarTemplatePartialSpecializationDecl * -- 2.40.0