Skip to content

Commit 14e3543

Browse files
committed
[clang] Track final substitution for SubstTemplateTemplateParm nodes
1 parent 426bec0 commit 14e3543

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
@@ -2396,10 +2396,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
23962396
const IdentifierInfo *Name) const;
23972397
TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
23982398
OverloadedOperatorKind Operator) const;
2399-
TemplateName
2400-
getSubstTemplateTemplateParm(TemplateName replacement, Decl *AssociatedDecl,
2401-
unsigned Index,
2402-
std::optional<unsigned> PackIndex) const;
2399+
TemplateName getSubstTemplateTemplateParm(TemplateName replacement,
2400+
Decl *AssociatedDecl,
2401+
unsigned Index,
2402+
std::optional<unsigned> PackIndex,
2403+
bool Final) const;
24032404
TemplateName getSubstTemplateTemplateParmPack(const TemplateArgument &ArgPack,
24042405
Decl *AssociatedDecl,
24052406
unsigned Index,

clang/include/clang/AST/PropertiesBase.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,9 @@ let Class = PropertyTypeCase<TemplateName, "SubstTemplateTemplateParm"> in {
729729
def : Property<"packIndex", Optional<UInt32>> {
730730
let Read = [{ parm->getPackIndex() }];
731731
}
732+
def : Property<"final", Bool> { let Read = [{ parm->getFinal() }]; }
732733
def : Creator<[{
733-
return ctx.getSubstTemplateTemplateParm(replacement, associatedDecl, index, packIndex);
734+
return ctx.getSubstTemplateTemplateParm(replacement, associatedDecl, index, packIndex, final);
734735
}]>;
735736
}
736737
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
@@ -413,9 +413,11 @@ class SubstTemplateTemplateParmStorage
413413

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

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

438445
TemplateTemplateParmDecl *getParameter() const;
@@ -442,7 +449,7 @@ class SubstTemplateTemplateParmStorage
442449

443450
static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Replacement,
444451
Decl *AssociatedDecl, unsigned Index,
445-
std::optional<unsigned> PackIndex);
452+
std::optional<unsigned> PackIndex, bool Final);
446453
};
447454

448455
class DeducedTemplateStorage : public UncommonTemplateNameStorage,

clang/include/clang/AST/Type.h

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

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

64816482
unsigned getNumArgs() const {

clang/lib/AST/ASTContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10133,18 +10133,18 @@ ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
1013310133

1013410134
TemplateName ASTContext::getSubstTemplateTemplateParm(
1013510135
TemplateName Replacement, Decl *AssociatedDecl, unsigned Index,
10136-
std::optional<unsigned> PackIndex) const {
10136+
std::optional<unsigned> PackIndex, bool Final) const {
1013710137
llvm::FoldingSetNodeID ID;
1013810138
SubstTemplateTemplateParmStorage::Profile(ID, Replacement, AssociatedDecl,
10139-
Index, PackIndex);
10139+
Index, PackIndex, Final);
1014010140

1014110141
void *insertPos = nullptr;
1014210142
SubstTemplateTemplateParmStorage *subst
1014310143
= SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
1014410144

1014510145
if (!subst) {
1014610146
subst = new (*this) SubstTemplateTemplateParmStorage(
10147-
Replacement, AssociatedDecl, Index, PackIndex);
10147+
Replacement, AssociatedDecl, Index, PackIndex, Final);
1014810148
SubstTemplateTemplateParms.InsertNode(subst, insertPos);
1014910149
}
1015010150

clang/lib/AST/ASTImporter.cpp

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

99609960
return ToContext.getSubstTemplateTemplateParm(
99619961
*ReplacementOrErr, *AssociatedDeclOrErr, Subst->getIndex(),
9962-
Subst->getPackIndex());
9962+
Subst->getPackIndex(), Subst->getFinal());
99639963
}
99649964

99659965
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
@@ -1302,6 +1302,8 @@ void TextNodeDumper::dumpBareTemplateName(TemplateName TN) {
13021302
OS << " index " << STS->getIndex();
13031303
if (std::optional<unsigned int> PackIndex = STS->getPackIndex())
13041304
OS << " pack_index " << *PackIndex;
1305+
if (STS->getFinal())
1306+
OS << " final";
13051307
if (const TemplateTemplateParmDecl *P = STS->getParameter())
13061308
AddChild("parameter", [=] { Visit(P); });
13071309
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)