Skip to content

Commit b50ea7d

Browse files
committed
[clang] Track final substitution for SubstTemplateTemplateParm nodes
1 parent e25187b commit b50ea7d

File tree

9 files changed

+33
-23
lines changed

9 files changed

+33
-23
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,10 +2393,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
23932393
TemplateName
23942394
getDependentTemplateName(const DependentTemplateStorage &Name) const;
23952395

2396-
TemplateName
2397-
getSubstTemplateTemplateParm(TemplateName replacement, Decl *AssociatedDecl,
2398-
unsigned Index,
2399-
std::optional<unsigned> PackIndex) const;
2396+
TemplateName getSubstTemplateTemplateParm(TemplateName replacement,
2397+
Decl *AssociatedDecl,
2398+
unsigned Index,
2399+
std::optional<unsigned> PackIndex,
2400+
bool Final) const;
24002401
TemplateName getSubstTemplateTemplateParmPack(const TemplateArgument &ArgPack,
24012402
Decl *AssociatedDecl,
24022403
unsigned Index,

clang/include/clang/AST/PropertiesBase.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,9 @@ let Class = PropertyTypeCase<TemplateName, "SubstTemplateTemplateParm"> in {
730730
def : Property<"packIndex", Optional<UInt32>> {
731731
let Read = [{ parm->getPackIndex() }];
732732
}
733+
def : Property<"final", Bool> { let Read = [{ parm->getFinal() }]; }
733734
def : Creator<[{
734-
return ctx.getSubstTemplateTemplateParm(replacement, associatedDecl, index, packIndex);
735+
return ctx.getSubstTemplateTemplateParm(replacement, associatedDecl, index, packIndex, final);
735736
}]>;
736737
}
737738
let Class = PropertyTypeCase<TemplateName, "SubstTemplateTemplateParmPack"> in {

clang/include/clang/AST/TemplateName.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,11 @@ class SubstTemplateTemplateParmStorage
414414

415415
SubstTemplateTemplateParmStorage(TemplateName Replacement,
416416
Decl *AssociatedDecl, unsigned Index,
417-
std::optional<unsigned> PackIndex)
417+
std::optional<unsigned> PackIndex,
418+
bool Final)
418419
: UncommonTemplateNameStorage(SubstTemplateTemplateParm, Index,
419-
PackIndex ? *PackIndex + 1 : 0),
420+
((PackIndex ? *PackIndex + 1 : 0) << 1) |
421+
Final),
420422
Replacement(Replacement), AssociatedDecl(AssociatedDecl) {
421423
assert(AssociatedDecl != nullptr);
422424
}
@@ -430,10 +432,15 @@ class SubstTemplateTemplateParmStorage
430432
/// This should match the result of `getParameter()->getIndex()`.
431433
unsigned getIndex() const { return Bits.Index; }
432434

435+
// This substitution is Final, which means the substitution is fully
436+
// sugared: it doesn't need to be resugared later.
437+
bool getFinal() const { return Bits.Data & 1; }
438+
433439
std::optional<unsigned> getPackIndex() const {
434-
if (Bits.Data == 0)
440+
auto Data = Bits.Data >> 1;
441+
if (Data == 0)
435442
return std::nullopt;
436-
return Bits.Data - 1;
443+
return Data - 1;
437444
}
438445

439446
TemplateTemplateParmDecl *getParameter() const;
@@ -443,7 +450,7 @@ class SubstTemplateTemplateParmStorage
443450

444451
static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Replacement,
445452
Decl *AssociatedDecl, unsigned Index,
446-
std::optional<unsigned> PackIndex);
453+
std::optional<unsigned> PackIndex, bool Final);
447454
};
448455

449456
class DeducedTemplateStorage : public UncommonTemplateNameStorage,

clang/include/clang/AST/Type.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6488,7 +6488,8 @@ class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode {
64886488
/// This should match the result of `getReplacedParameter()->getIndex()`.
64896489
unsigned getIndex() const { return SubstTemplateTypeParmPackTypeBits.Index; }
64906490

6491-
// When true the substitution will be 'Final' (subst node won't be placed).
6491+
// This substitution will be Final, which means the substitution will be fully
6492+
// sugared: it doesn't need to be resugared later.
64926493
bool getFinal() const;
64936494

64946495
unsigned getNumArgs() const {

clang/lib/AST/ASTContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10090,18 +10090,18 @@ ASTContext::getDependentTemplateName(const DependentTemplateStorage &S) const {
1009010090

1009110091
TemplateName ASTContext::getSubstTemplateTemplateParm(
1009210092
TemplateName Replacement, Decl *AssociatedDecl, unsigned Index,
10093-
std::optional<unsigned> PackIndex) const {
10093+
std::optional<unsigned> PackIndex, bool Final) const {
1009410094
llvm::FoldingSetNodeID ID;
1009510095
SubstTemplateTemplateParmStorage::Profile(ID, Replacement, AssociatedDecl,
10096-
Index, PackIndex);
10096+
Index, PackIndex, Final);
1009710097

1009810098
void *insertPos = nullptr;
1009910099
SubstTemplateTemplateParmStorage *subst
1010010100
= SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
1010110101

1010210102
if (!subst) {
1010310103
subst = new (*this) SubstTemplateTemplateParmStorage(
10104-
Replacement, AssociatedDecl, Index, PackIndex);
10104+
Replacement, AssociatedDecl, Index, PackIndex, Final);
1010510105
SubstTemplateTemplateParms.InsertNode(subst, insertPos);
1010610106
}
1010710107

clang/lib/AST/ASTImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9932,7 +9932,7 @@ Expected<TemplateName> ASTImporter::Import(TemplateName From) {
99329932

99339933
return ToContext.getSubstTemplateTemplateParm(
99349934
*ReplacementOrErr, *AssociatedDeclOrErr, Subst->getIndex(),
9935-
Subst->getPackIndex());
9935+
Subst->getPackIndex(), Subst->getFinal());
99369936
}
99379937

99389938
case TemplateName::SubstTemplateTemplateParmPack: {

clang/lib/AST/TemplateName.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,18 @@ SubstTemplateTemplateParmStorage::getParameter() const {
7777
}
7878

7979
void SubstTemplateTemplateParmStorage::Profile(llvm::FoldingSetNodeID &ID) {
80-
Profile(ID, Replacement, getAssociatedDecl(), getIndex(), getPackIndex());
80+
Profile(ID, Replacement, getAssociatedDecl(), getIndex(), getPackIndex(),
81+
getFinal());
8182
}
8283

8384
void SubstTemplateTemplateParmStorage::Profile(
8485
llvm::FoldingSetNodeID &ID, TemplateName Replacement, Decl *AssociatedDecl,
85-
unsigned Index, std::optional<unsigned> PackIndex) {
86+
unsigned Index, std::optional<unsigned> PackIndex, bool Final) {
8687
Replacement.Profile(ID);
8788
ID.AddPointer(AssociatedDecl);
8889
ID.AddInteger(Index);
8990
ID.AddInteger(PackIndex ? *PackIndex + 1 : 0);
91+
ID.AddBoolean(Final);
9092
}
9193

9294
SubstTemplateTemplateParmPackStorage::SubstTemplateTemplateParmPackStorage(

clang/lib/AST/TextNodeDumper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,8 @@ void TextNodeDumper::dumpBareTemplateName(TemplateName TN) {
12981298
OS << " index " << STS->getIndex();
12991299
if (std::optional<unsigned int> PackIndex = STS->getPackIndex())
13001300
OS << " pack_index " << *PackIndex;
1301+
if (STS->getFinal())
1302+
OS << " final";
13011303
if (const TemplateTemplateParmDecl *P = STS->getParameter())
13021304
AddChild("parameter", [=] { Visit(P); });
13031305
dumpDeclRef(STS->getAssociatedDecl(), "associated");

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,10 +2083,8 @@ TemplateName TemplateInstantiator::TransformTemplateName(
20832083
TemplateName Template = Arg.getAsTemplate();
20842084
assert(!Template.isNull() && "Null template template argument");
20852085

2086-
if (Final)
2087-
return Template;
20882086
return getSema().Context.getSubstTemplateTemplateParm(
2089-
Template, AssociatedDecl, TTP->getIndex(), PackIndex);
2087+
Template, AssociatedDecl, TTP->getIndex(), PackIndex, Final);
20902088
}
20912089
}
20922090

@@ -2098,11 +2096,9 @@ TemplateName TemplateInstantiator::TransformTemplateName(
20982096
TemplateArgument Pack = SubstPack->getArgumentPack();
20992097
TemplateName Template =
21002098
getPackSubstitutedTemplateArgument(getSema(), Pack).getAsTemplate();
2101-
if (SubstPack->getFinal())
2102-
return Template;
21032099
return getSema().Context.getSubstTemplateTemplateParm(
21042100
Template, SubstPack->getAssociatedDecl(), SubstPack->getIndex(),
2105-
getPackIndex(Pack));
2101+
getPackIndex(Pack), SubstPack->getFinal());
21062102
}
21072103

21082104
return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,

0 commit comments

Comments
 (0)