Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9f95c1

Browse files
committed
[C++20][Modules] Number the anonymous members if the decl was instantiated locally.
1 parent 63519ed commit c9f95c1

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

clang/include/clang/AST/DeclTemplate.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,13 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
18741874
LLVM_PREFERRED_TYPE(bool)
18751875
unsigned StrictPackMatch : 1;
18761876

1877+
/// Indicate whether this template was instantiated, and instantiated locally.
1878+
/// Specifically, this is false if the template has not been instantiated,
1879+
/// or was intantiated externally and loaded via an AST file. This only tracks
1880+
/// class template specializations for proper handling of anonymous members.
1881+
LLVM_PREFERRED_TYPE(bool)
1882+
unsigned InstantiatedLocally : 1;
1883+
18771884
protected:
18781885
ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
18791886
DeclContext *DC, SourceLocation StartLoc,
@@ -1968,6 +1975,10 @@ class ClassTemplateSpecializationDecl : public CXXRecordDecl,
19681975

19691976
void setStrictPackMatch(bool Val) { StrictPackMatch = Val; }
19701977

1978+
bool isInstantiatedLocally() { return InstantiatedLocally; }
1979+
1980+
void setInstantiatedLocally() { InstantiatedLocally = true; }
1981+
19711982
/// Get the point of instantiation (if any), or null if none.
19721983
SourceLocation getPointOfInstantiation() const {
19731984
return PointOfInstantiation;

clang/lib/AST/DeclTemplate.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,15 +974,16 @@ ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(
974974
SpecializedTemplate->getIdentifier(), PrevDecl),
975975
SpecializedTemplate(SpecializedTemplate),
976976
TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
977-
SpecializationKind(TSK_Undeclared), StrictPackMatch(StrictPackMatch) {
977+
SpecializationKind(TSK_Undeclared), StrictPackMatch(StrictPackMatch),
978+
InstantiatedLocally(false) {
978979
assert(DK == Kind::ClassTemplateSpecialization || StrictPackMatch == false);
979980
}
980981

981982
ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
982983
Kind DK)
983984
: CXXRecordDecl(DK, TagTypeKind::Struct, C, nullptr, SourceLocation(),
984985
SourceLocation(), nullptr, nullptr),
985-
SpecializationKind(TSK_Undeclared) {}
986+
SpecializationKind(TSK_Undeclared), InstantiatedLocally(false) {}
986987

987988
ClassTemplateSpecializationDecl *ClassTemplateSpecializationDecl::Create(
988989
ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc,

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3699,6 +3699,7 @@ Sema::InstantiateClass(SourceLocation PointOfInstantiation,
36993699
= dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
37003700
Spec->setTemplateSpecializationKind(TSK);
37013701
Spec->setPointOfInstantiation(PointOfInstantiation);
3702+
Spec->setInstantiatedLocally();
37023703
}
37033704

37043705
InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3425,7 +3425,16 @@ NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
34253425
// If this is the first time, but we have parsed a declaration of the context,
34263426
// build the anonymous declaration list from the parsed declaration.
34273427
auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3428-
if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3428+
auto needToNumberAnonymousDeclsWithin = [](Decl *D) {
3429+
if (!D->isFromASTFile())
3430+
return true;
3431+
// If this is a class template specialization from an AST file
3432+
// but the instantiation occurred locally, we still need to number
3433+
// the anonymous decls.
3434+
auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D);
3435+
return CTSD && CTSD->isInstantiatedLocally();
3436+
};
3437+
if (PrimaryDC && needToNumberAnonymousDeclsWithin(cast<Decl>(PrimaryDC))) {
34293438
numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
34303439
if (Previous.size() == Number)
34313440
Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));

0 commit comments

Comments
 (0)