From: Reid Kleckner Date: Tue, 14 Oct 2014 20:28:40 +0000 (+0000) Subject: Factor code into CXXRecordDecl::getTemplateInstantiationPattern() helper X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e7367d6bcb9de27237ecab6de7208f4a725eda37;p=platform%2Fupstream%2Fllvm.git Factor code into CXXRecordDecl::getTemplateInstantiationPattern() helper This moves some code from SemaType.cpp's hasVisibleDefinition() into DeclCXX.cpp so that it can be used elsewhere. I found one other instance of code trying to do the same thing, there are probably more. Search for getInstantiatedFrom() to try to find more. No functionality change. Reviewed By: rsmith Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D5783 llvm-svn: 219714 --- diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h index 6d09f49..062c152 100644 --- a/clang/include/clang/AST/DeclCXX.h +++ b/clang/include/clang/AST/DeclCXX.h @@ -1377,6 +1377,15 @@ public: /// \brief Set the kind of specialization or template instantiation this is. void setTemplateSpecializationKind(TemplateSpecializationKind TSK); + /// \brief Retrieve the record declaration from which this record could be + /// instantiated. Returns null if this class is not a template instantiation. + const CXXRecordDecl *getTemplateInstantiationPattern() const; + + CXXRecordDecl *getTemplateInstantiationPattern() { + return const_cast(const_cast(this) + ->getTemplateInstantiationPattern()); + } + /// \brief Returns the destructor decl for this class. CXXDestructorDecl *getDestructor() const; diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp index 37bd050..623cae5 100644 --- a/clang/lib/AST/DeclCXX.cpp +++ b/clang/lib/AST/DeclCXX.cpp @@ -1261,6 +1261,44 @@ CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { llvm_unreachable("Not a class template or member class specialization"); } +const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const { + // If it's a class template specialization, find the template or partial + // specialization from which it was instantiated. + if (auto *TD = dyn_cast(this)) { + auto From = TD->getInstantiatedFrom(); + if (auto *CTD = From.dyn_cast()) { + while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) { + if (NewCTD->isMemberSpecialization()) + break; + CTD = NewCTD; + } + return CTD->getTemplatedDecl(); + } + if (auto *CTPSD = + From.dyn_cast()) { + while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) { + if (NewCTPSD->isMemberSpecialization()) + break; + CTPSD = NewCTPSD; + } + return CTPSD; + } + } + + if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { + if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { + const CXXRecordDecl *RD = this; + while (auto *NewRD = RD->getInstantiatedFromMemberClass()) + RD = NewRD; + return RD; + } + } + + assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) && + "couldn't find pattern for class template instantiation"); + return nullptr; +} + CXXDestructorDecl *CXXRecordDecl::getDestructor() const { ASTContext &Context = getASTContext(); QualType ClassType = Context.getTypeDeclType(this); diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index 639e651..cbeebb2 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -1175,21 +1175,8 @@ static Module *getDefiningModule(Decl *Entity) { if (FunctionDecl *Pattern = FD->getTemplateInstantiationPattern()) Entity = Pattern; } else if (CXXRecordDecl *RD = dyn_cast(Entity)) { - // If it's a class template specialization, find the template or partial - // specialization from which it was instantiated. - if (ClassTemplateSpecializationDecl *SpecRD = - dyn_cast(RD)) { - llvm::PointerUnion From = - SpecRD->getInstantiatedFrom(); - if (ClassTemplateDecl *FromTemplate = From.dyn_cast()) - Entity = FromTemplate->getTemplatedDecl(); - else if (From) - Entity = From.get(); - // Otherwise, it's an explicit specialization. - } else if (MemberSpecializationInfo *MSInfo = - RD->getMemberSpecializationInfo()) - Entity = getInstantiatedFrom(RD, MSInfo); + if (CXXRecordDecl *Pattern = RD->getTemplateInstantiationPattern()) + Entity = Pattern; } else if (EnumDecl *ED = dyn_cast(Entity)) { if (MemberSpecializationInfo *MSInfo = ED->getMemberSpecializationInfo()) Entity = getInstantiatedFrom(ED, MSInfo); diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 31d7f73..3ce9489 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -5085,31 +5085,9 @@ static bool hasVisibleDefinition(Sema &S, NamedDecl *D, NamedDecl **Suggested) { // If this definition was instantiated from a template, map back to the // pattern from which it was instantiated. - // - // FIXME: There must be a better place for this to live. if (auto *RD = dyn_cast(D)) { - if (auto *TD = dyn_cast(RD)) { - auto From = TD->getInstantiatedFrom(); - if (auto *CTD = From.dyn_cast()) { - while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) { - if (NewCTD->isMemberSpecialization()) - break; - CTD = NewCTD; - } - RD = CTD->getTemplatedDecl(); - } else if (auto *CTPSD = From.dyn_cast< - ClassTemplatePartialSpecializationDecl *>()) { - while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) { - if (NewCTPSD->isMemberSpecialization()) - break; - CTPSD = NewCTPSD; - } - RD = CTPSD; - } - } else if (isTemplateInstantiation(RD->getTemplateSpecializationKind())) { - while (auto *NewRD = RD->getInstantiatedFromMemberClass()) - RD = NewRD; - } + if (auto *Pattern = RD->getTemplateInstantiationPattern()) + RD = Pattern; D = RD->getDefinition(); } else if (auto *ED = dyn_cast(D)) { while (auto *NewED = ED->getInstantiatedFromMemberEnum())