[clang] Revert "Reapply "[Clang][Sema] Refactor collection of multi-level template argument lists (#106585)" (#111173)" (PR #111766)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 9 14:49:47 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Krystian Stasiowski (sdkrystian)
<details>
<summary>Changes</summary>
This reverts commit 4da8ac34f76e707ab94380b94f616457cfd2cb83.
---
Patch is 103.80 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/111766.diff
20 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (-3)
- (modified) clang/include/clang/AST/DeclTemplate.h (+39-27)
- (modified) clang/include/clang/Sema/Sema.h (+19-6)
- (modified) clang/lib/AST/Decl.cpp (+19-30)
- (modified) clang/lib/AST/DeclCXX.cpp (+6-14)
- (modified) clang/lib/AST/DeclTemplate.cpp (+16-14)
- (modified) clang/lib/Sema/SemaConcept.cpp (+17-12)
- (modified) clang/lib/Sema/SemaDecl.cpp (+17-14)
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+2-2)
- (modified) clang/lib/Sema/SemaTemplate.cpp (+94-85)
- (modified) clang/lib/Sema/SemaTemplateDeduction.cpp (+30-3)
- (modified) clang/lib/Sema/SemaTemplateDeductionGuide.cpp (+29-16)
- (modified) clang/lib/Sema/SemaTemplateInstantiate.cpp (+382-370)
- (modified) clang/lib/Sema/SemaTemplateInstantiateDecl.cpp (+9-37)
- (modified) clang/lib/Serialization/ASTReader.cpp (+1-2)
- (modified) clang/lib/Serialization/ASTReaderDecl.cpp (+9-9)
- (modified) clang/lib/Serialization/ASTWriterDecl.cpp (+10-7)
- (removed) clang/test/CXX/temp/temp.constr/temp.constr.decl/p4.cpp (-175)
- (removed) clang/test/CXX/temp/temp.spec/temp.expl.spec/p7.cpp (-178)
- (modified) clang/test/Modules/cxx-templates.cpp (+3-1)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 29b9fe07f545f9..16e6a230ef428e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -477,9 +477,6 @@ Bug Fixes to C++ Support
in certain friend declarations. (#GH93099)
- Clang now instantiates the correct lambda call operator when a lambda's class type is
merged across modules. (#GH110401)
-- Clang now uses the correct set of template argument lists when comparing the constraints of
- out-of-line definitions and member templates explicitly specialized for a given implicit instantiation of
- a class template. (#GH102320)
- Fix a crash when parsing a pseudo destructor involving an invalid type. (#GH111460)
Bug Fixes to AST Handling
diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h
index 05739f39d2a496..687715a22e9fd3 100644
--- a/clang/include/clang/AST/DeclTemplate.h
+++ b/clang/include/clang/AST/DeclTemplate.h
@@ -781,11 +781,15 @@ class RedeclarableTemplateDecl : public TemplateDecl,
EntryType *Entry, void *InsertPos);
struct CommonBase {
- CommonBase() {}
+ CommonBase() : InstantiatedFromMember(nullptr, false) {}
/// The template from which this was most
/// directly instantiated (or null).
- RedeclarableTemplateDecl *InstantiatedFromMember = nullptr;
+ ///
+ /// The boolean value indicates whether this template
+ /// was explicitly specialized.
+ llvm::PointerIntPair<RedeclarableTemplateDecl*, 1, bool>
+ InstantiatedFromMember;
/// If non-null, points to an array of specializations (including
/// partial specializations) known only by their external declaration IDs.
@@ -805,19 +809,14 @@ class RedeclarableTemplateDecl : public TemplateDecl,
};
/// Pointer to the common data shared by all declarations of this
- /// template, and a flag indicating if the template is a member
- /// specialization.
- mutable llvm::PointerIntPair<CommonBase *, 1, bool> Common;
-
- CommonBase *getCommonPtrInternal() const { return Common.getPointer(); }
+ /// template.
+ mutable CommonBase *Common = nullptr;
/// Retrieves the "common" pointer shared by all (re-)declarations of
/// the same template. Calling this routine may implicitly allocate memory
/// for the common pointer.
CommonBase *getCommonPtr() const;
- void setCommonPtr(CommonBase *C) const { Common.setPointer(C); }
-
virtual CommonBase *newCommon(ASTContext &C) const = 0;
// Construct a template decl with name, parameters, and templated element.
@@ -858,12 +857,15 @@ class RedeclarableTemplateDecl : public TemplateDecl,
/// template<> template<typename T>
/// struct X<int>::Inner { /* ... */ };
/// \endcode
- bool isMemberSpecialization() const { return Common.getInt(); }
+ bool isMemberSpecialization() const {
+ return getCommonPtr()->InstantiatedFromMember.getInt();
+ }
/// Note that this member template is a specialization.
void setMemberSpecialization() {
- assert(!isMemberSpecialization() && "already a member specialization");
- Common.setInt(true);
+ assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
+ "Only member templates can be member template specializations");
+ getCommonPtr()->InstantiatedFromMember.setInt(true);
}
/// Retrieve the member template from which this template was
@@ -903,12 +905,12 @@ class RedeclarableTemplateDecl : public TemplateDecl,
/// void X<T>::f(T, U);
/// \endcode
RedeclarableTemplateDecl *getInstantiatedFromMemberTemplate() const {
- return getCommonPtr()->InstantiatedFromMember;
+ return getCommonPtr()->InstantiatedFromMember.getPointer();
}
void setInstantiatedFromMemberTemplate(RedeclarableTemplateDecl *TD) {
- assert(!getCommonPtr()->InstantiatedFromMember);
- getCommonPtr()->InstantiatedFromMember = TD;
+ assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
+ getCommonPtr()->InstantiatedFromMember.setPointer(TD);
}
/// Retrieve the "injected" template arguments that correspond to the
@@ -1987,8 +1989,6 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
/// template arguments have been deduced.
void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
const TemplateArgumentList *TemplateArgs) {
- assert(!isa<ClassTemplatePartialSpecializationDecl>(this) &&
- "A partial specialization cannot be instantiated from a template");
assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
"Already set to a class template partial specialization!");
auto *PS = new (getASTContext()) SpecializedPartialSpecialization();
@@ -2000,8 +2000,6 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
/// Note that this class template specialization is an instantiation
/// of the given class template.
void setInstantiationOf(ClassTemplateDecl *TemplDecl) {
- assert(!isa<ClassTemplatePartialSpecializationDecl>(this) &&
- "A partial specialization cannot be instantiated from a template");
assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
"Previously set to a class template partial specialization!");
SpecializedTemplate = TemplDecl;
@@ -2189,11 +2187,18 @@ class ClassTemplatePartialSpecializationDecl
/// struct X<int>::Inner<T*> { /* ... */ };
/// \endcode
bool isMemberSpecialization() const {
- return InstantiatedFromMember.getInt();
+ const auto *First =
+ cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
+ return First->InstantiatedFromMember.getInt();
}
/// Note that this member template is a specialization.
- void setMemberSpecialization() { return InstantiatedFromMember.setInt(true); }
+ void setMemberSpecialization() {
+ auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
+ assert(First->InstantiatedFromMember.getPointer() &&
+ "Only member templates can be member template specializations");
+ return First->InstantiatedFromMember.setInt(true);
+ }
/// Retrieves the injected specialization type for this partial
/// specialization. This is not the same as the type-decl-type for
@@ -2263,6 +2268,10 @@ class ClassTemplateDecl : public RedeclarableTemplateDecl {
return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
}
+ void setCommonPtr(Common *C) {
+ RedeclarableTemplateDecl::Common = C;
+ }
+
public:
friend class ASTDeclReader;
@@ -2745,8 +2754,6 @@ class VarTemplateSpecializationDecl : public VarDecl,
/// template arguments have been deduced.
void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec,
const TemplateArgumentList *TemplateArgs) {
- assert(!isa<VarTemplatePartialSpecializationDecl>(this) &&
- "A partial specialization cannot be instantiated from a template");
assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
"Already set to a variable template partial specialization!");
auto *PS = new (getASTContext()) SpecializedPartialSpecialization();
@@ -2758,8 +2765,6 @@ class VarTemplateSpecializationDecl : public VarDecl,
/// Note that this variable template specialization is an instantiation
/// of the given variable template.
void setInstantiationOf(VarTemplateDecl *TemplDecl) {
- assert(!isa<VarTemplatePartialSpecializationDecl>(this) &&
- "A partial specialization cannot be instantiated from a template");
assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
"Previously set to a variable template partial specialization!");
SpecializedTemplate = TemplDecl;
@@ -2944,11 +2949,18 @@ class VarTemplatePartialSpecializationDecl
/// U* X<int>::Inner<T*> = (T*)(0) + 1;
/// \endcode
bool isMemberSpecialization() const {
- return InstantiatedFromMember.getInt();
+ const auto *First =
+ cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
+ return First->InstantiatedFromMember.getInt();
}
/// Note that this member template is a specialization.
- void setMemberSpecialization() { return InstantiatedFromMember.setInt(true); }
+ void setMemberSpecialization() {
+ auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
+ assert(First->InstantiatedFromMember.getPointer() &&
+ "Only member templates can be member template specializations");
+ return First->InstantiatedFromMember.setInt(true);
+ }
SourceRange getSourceRange() const override LLVM_READONLY;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 67a6dbeb520a89..51f38a90f3ea05 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -11325,9 +11325,9 @@ class Sema final : public SemaBase {
CXXScopeSpec &SS, IdentifierInfo *Name, SourceLocation NameLoc,
const ParsedAttributesView &Attr, TemplateParameterList *TemplateParams,
AccessSpecifier AS, SourceLocation ModulePrivateLoc,
- SourceLocation FriendLoc,
- ArrayRef<TemplateParameterList *> OuterTemplateParamLists,
- bool IsMemberSpecialization, SkipBodyInfo *SkipBody = nullptr);
+ SourceLocation FriendLoc, unsigned NumOuterTemplateParamLists,
+ TemplateParameterList **OuterTemplateParamLists,
+ SkipBodyInfo *SkipBody = nullptr);
/// Translates template arguments as provided by the parser
/// into template arguments used by semantic analysis.
@@ -11366,8 +11366,7 @@ class Sema final : public SemaBase {
DeclResult ActOnVarTemplateSpecialization(
Scope *S, Declarator &D, TypeSourceInfo *DI, LookupResult &Previous,
SourceLocation TemplateKWLoc, TemplateParameterList *TemplateParams,
- StorageClass SC, bool IsPartialSpecialization,
- bool IsMemberSpecialization);
+ StorageClass SC, bool IsPartialSpecialization);
/// Get the specialization of the given variable template corresponding to
/// the specified argument list, or a null-but-valid result if the arguments
@@ -13008,14 +13007,28 @@ class Sema final : public SemaBase {
/// dealing with a specialization. This is only relevant for function
/// template specializations.
///
+ /// \param Pattern If non-NULL, indicates the pattern from which we will be
+ /// instantiating the definition of the given declaration, \p ND. This is
+ /// used to determine the proper set of template instantiation arguments for
+ /// friend function template specializations.
+ ///
/// \param ForConstraintInstantiation when collecting arguments,
/// ForConstraintInstantiation indicates we should continue looking when
/// encountering a lambda generic call operator, and continue looking for
/// arguments on an enclosing class template.
+ ///
+ /// \param SkipForSpecialization when specified, any template specializations
+ /// in a traversal would be ignored.
+ /// \param ForDefaultArgumentSubstitution indicates we should continue looking
+ /// when encountering a specialized member function template, rather than
+ /// returning immediately.
MultiLevelTemplateArgumentList getTemplateInstantiationArgs(
const NamedDecl *D, const DeclContext *DC = nullptr, bool Final = false,
std::optional<ArrayRef<TemplateArgument>> Innermost = std::nullopt,
- bool RelativeToPrimary = false, bool ForConstraintInstantiation = false);
+ bool RelativeToPrimary = false, const FunctionDecl *Pattern = nullptr,
+ bool ForConstraintInstantiation = false,
+ bool SkipForSpecialization = false,
+ bool ForDefaultArgumentSubstitution = false);
/// RAII object to handle the state changes required to synthesize
/// a function body.
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 58d11a0312c505..84ef9f74582ef6 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -2696,27 +2696,21 @@ VarDecl *VarDecl::getTemplateInstantiationPattern() const {
if (isTemplateInstantiation(VDTemplSpec->getTemplateSpecializationKind())) {
auto From = VDTemplSpec->getInstantiatedFrom();
if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) {
- while (true) {
- VTD = VTD->getMostRecentDecl();
- if (VTD->isMemberSpecialization())
- break;
- if (auto *NewVTD = VTD->getInstantiatedFromMemberTemplate())
- VTD = NewVTD;
- else
+ while (!VTD->isMemberSpecialization()) {
+ auto *NewVTD = VTD->getInstantiatedFromMemberTemplate();
+ if (!NewVTD)
break;
+ VTD = NewVTD;
}
return getDefinitionOrSelf(VTD->getTemplatedDecl());
}
if (auto *VTPSD =
From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) {
- while (true) {
- VTPSD = VTPSD->getMostRecentDecl();
- if (VTPSD->isMemberSpecialization())
- break;
- if (auto *NewVTPSD = VTPSD->getInstantiatedFromMember())
- VTPSD = NewVTPSD;
- else
+ while (!VTPSD->isMemberSpecialization()) {
+ auto *NewVTPSD = VTPSD->getInstantiatedFromMember();
+ if (!NewVTPSD)
break;
+ VTPSD = NewVTPSD;
}
return getDefinitionOrSelf<VarDecl>(VTPSD);
}
@@ -2725,17 +2719,15 @@ VarDecl *VarDecl::getTemplateInstantiationPattern() const {
// If this is the pattern of a variable template, find where it was
// instantiated from. FIXME: Is this necessary?
- if (VarTemplateDecl *VTD = VD->getDescribedVarTemplate()) {
- while (true) {
- VTD = VTD->getMostRecentDecl();
- if (VTD->isMemberSpecialization())
- break;
- if (auto *NewVTD = VTD->getInstantiatedFromMemberTemplate())
- VTD = NewVTD;
- else
+ if (VarTemplateDecl *VarTemplate = VD->getDescribedVarTemplate()) {
+ while (!VarTemplate->isMemberSpecialization()) {
+ auto *NewVT = VarTemplate->getInstantiatedFromMemberTemplate();
+ if (!NewVT)
break;
+ VarTemplate = NewVT;
}
- return getDefinitionOrSelf(VTD->getTemplatedDecl());
+
+ return getDefinitionOrSelf(VarTemplate->getTemplatedDecl());
}
if (VD == this)
@@ -4150,14 +4142,11 @@ FunctionDecl::getTemplateInstantiationPattern(bool ForDefinition) const {
if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
// If we hit a point where the user provided a specialization of this
// template, we're done looking.
- while (true) {
- Primary = Primary->getMostRecentDecl();
- if (ForDefinition && Primary->isMemberSpecialization())
- break;
- if (auto *NewPrimary = Primary->getInstantiatedFromMemberTemplate())
- Primary = NewPrimary;
- else
+ while (!ForDefinition || !Primary->isMemberSpecialization()) {
+ auto *NewPrimary = Primary->getInstantiatedFromMemberTemplate();
+ if (!NewPrimary)
break;
+ Primary = NewPrimary;
}
return getDefinitionOrSelf(Primary->getTemplatedDecl());
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index cfc7a9a218f251..1364ccc745ba01 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -2023,27 +2023,19 @@ const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
auto From = TD->getInstantiatedFrom();
if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
- while (true) {
- CTD = CTD->getMostRecentDecl();
- if (CTD->isMemberSpecialization())
- break;
- if (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate())
- CTD = NewCTD;
- else
+ while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
+ if (NewCTD->isMemberSpecialization())
break;
+ CTD = NewCTD;
}
return GetDefinitionOrSelf(CTD->getTemplatedDecl());
}
if (auto *CTPSD =
From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
- while (true) {
- CTPSD = CTPSD->getMostRecentDecl();
- if (CTPSD->isMemberSpecialization())
- break;
- if (auto *NewCTPSD = CTPSD->getInstantiatedFromMemberTemplate())
- CTPSD = NewCTPSD;
- else
+ while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
+ if (NewCTPSD->isMemberSpecialization())
break;
+ CTPSD = NewCTPSD;
}
return GetDefinitionOrSelf(CTPSD);
}
diff --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp
index d9b67b7bedf5a5..6fe817c5ef1c6b 100644
--- a/clang/lib/AST/DeclTemplate.cpp
+++ b/clang/lib/AST/DeclTemplate.cpp
@@ -309,16 +309,16 @@ bool TemplateDecl::isTypeAlias() const {
void RedeclarableTemplateDecl::anchor() {}
RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
- if (CommonBase *C = getCommonPtrInternal())
- return C;
+ if (Common)
+ return Common;
// Walk the previous-declaration chain until we either find a declaration
// with a common pointer or we run out of previous declarations.
SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
Prev = Prev->getPreviousDecl()) {
- if (CommonBase *C = Prev->getCommonPtrInternal()) {
- setCommonPtr(C);
+ if (Prev->Common) {
+ Common = Prev->Common;
break;
}
@@ -326,18 +326,18 @@ RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() c
}
// If we never found a common pointer, allocate one now.
- if (!getCommonPtrInternal()) {
+ if (!Common) {
// FIXME: If any of the declarations is from an AST file, we probably
// need an update record to add the common data.
- setCommonPtr(newCommon(getASTContext()));
+ Common = newCommon(getASTContext());
}
// Update any previous declarations we saw with the common pointer.
for (const RedeclarableTemplateDecl *Prev : PrevDecls)
- Prev->setCommonPtr(getCommonPtrInternal());
+ Prev->Common = Common;
- return getCommonPtrInternal();
+ return Common;
}
void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const {
@@ -463,17 +463,19 @@ void FunctionTemplateDecl::addSpecialization(
}
void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
+ using Base = RedeclarableTemplateDecl;
+
// If we haven't created a common pointer yet, then it can just be created
// with the usual method.
- if (!getCommonPtrInternal())
+ if (!Base::Common)
return;
- Common *ThisCommon = static_cast<Common *>(getCommonPtrInternal());
+ Common *ThisCommon = static_cast<Common *>(Base::Common);
Common *PrevCommon = nullptr;
SmallVector<FunctionTemplateDecl *, 8> PreviousDecls;
for (; Prev; Prev = Prev->getPreviousDecl()) {
- if (CommonBase *C = Prev->getCommonPtrInternal()) {
- PrevCommon = static_cast<Common *>(C);
+ if (Prev->Base::Common) {
+ PrevCommon = static_cast<Common *>(Prev->Base::Common);
break;
}
PreviousDecls.push_back(Prev);
@@ -483,7 +485,7 @@ void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
// use this common pointer.
if (!PrevCommon) {
for (auto *D : PreviousDecls)
- D->setCommonPtr(ThisCommon);
+ D->Base::Common = ThisCommon;
return;
}
@@ -491,7 +493,7 @@ void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
assert(ThisCommon->Specializations.size() == 0 &&
"Can't merge incompatible declarations!");
- setCommonPtr(PrevCommon);
+ Base::Common = PrevCommon;
}
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index e36ee062213716..998a148a7d24a1 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -585,8 +585,8 @@ static bool CheckConstraintSatisfaction(
ArrayRef<TemplateArgument> TemplateArgs =
TemplateArgsLists.getNumSubstitutedLevels() > 0
- ? TemplateArgsLists.getInnermost()
- : ArrayRef<TemplateArgument>{};
+ ? TemplateArgsLists.getOutermost()
+ : ArrayRef<...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/111766
More information about the cfe-commits
mailing list