Skip to content

[Clang] Distinguish expanding-packs-in-place cases for SubstTemplateTypeParmTypes #114220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1731,7 +1731,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
QualType
getSubstTemplateTypeParmType(QualType Replacement, Decl *AssociatedDecl,
unsigned Index,
std::optional<unsigned> PackIndex) const;
std::optional<unsigned> PackIndex,
SubstTemplateTypeParmTypeFlag Flag =
SubstTemplateTypeParmTypeFlag::None) const;
QualType getSubstTemplateTypeParmPackType(Decl *AssociatedDecl,
unsigned Index, bool Final,
const TemplateArgument &ArgPack);
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/AST/PropertiesBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def Selector : PropertyType;
def SourceLocation : PropertyType;
def StmtRef : RefPropertyType<"Stmt"> { let ConstWhenWriting = 1; }
def ExprRef : SubclassPropertyType<"Expr", StmtRef>;
def SubstTemplateTypeParmTypeFlag : EnumPropertyType;
def TemplateArgument : PropertyType;
def TemplateArgumentKind : EnumPropertyType<"TemplateArgument::ArgKind">;
def TemplateName : DefaultValuePropertyType;
Expand Down
29 changes: 26 additions & 3 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,15 @@ enum class AutoTypeKeyword {
GNUAutoType
};

enum class SubstTemplateTypeParmTypeFlag {
None,

/// Whether to expand the pack using the stored PackIndex in place. This is
/// useful for e.g. substituting into an atomic constraint expression, where
/// that expression is part of an unexpanded pack.
ExpandPacksInPlace,
};

enum class ArraySizeModifier;
enum class ElaboratedTypeKeyword;
enum class VectorKind;
Expand Down Expand Up @@ -2170,6 +2179,9 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
LLVM_PREFERRED_TYPE(bool)
unsigned HasNonCanonicalUnderlyingType : 1;

LLVM_PREFERRED_TYPE(SubstTemplateTypeParmTypeFlag)
unsigned SubstitutionFlag : 1;

// The index of the template parameter this substitution represents.
unsigned Index : 15;

Expand Down Expand Up @@ -6393,7 +6405,8 @@ class SubstTemplateTypeParmType final
Decl *AssociatedDecl;

SubstTemplateTypeParmType(QualType Replacement, Decl *AssociatedDecl,
unsigned Index, std::optional<unsigned> PackIndex);
unsigned Index, std::optional<unsigned> PackIndex,
SubstTemplateTypeParmTypeFlag Flag);

public:
/// Gets the type that was substituted for the template
Expand Down Expand Up @@ -6422,21 +6435,31 @@ class SubstTemplateTypeParmType final
return SubstTemplateTypeParmTypeBits.PackIndex - 1;
}

SubstTemplateTypeParmTypeFlag getSubstitutionFlag() const {
return static_cast<SubstTemplateTypeParmTypeFlag>(
SubstTemplateTypeParmTypeBits.SubstitutionFlag);
}

bool isSugared() const { return true; }
QualType desugar() const { return getReplacementType(); }

void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getReplacementType(), getAssociatedDecl(), getIndex(),
getPackIndex());
getPackIndex(), getSubstitutionFlag());
}

static void Profile(llvm::FoldingSetNodeID &ID, QualType Replacement,
const Decl *AssociatedDecl, unsigned Index,
std::optional<unsigned> PackIndex) {
std::optional<unsigned> PackIndex,
SubstTemplateTypeParmTypeFlag Flag) {
Replacement.Profile(ID);
ID.AddPointer(AssociatedDecl);
ID.AddInteger(Index);
ID.AddInteger(PackIndex ? *PackIndex - 1 : 0);
ID.AddInteger(llvm::to_underlying(Flag));
assert((Flag != SubstTemplateTypeParmTypeFlag::ExpandPacksInPlace ||
PackIndex) &&
"ExpandPacksInPlace needs a valid PackIndex");
}

static bool classof(const Type *T) {
Expand Down
5 changes: 4 additions & 1 deletion clang/include/clang/AST/TypeProperties.td
Original file line number Diff line number Diff line change
Expand Up @@ -820,11 +820,14 @@ let Class = SubstTemplateTypeParmType in {
def : Property<"PackIndex", Optional<UInt32>> {
let Read = [{ node->getPackIndex() }];
}
def : Property<"SubstitutionFlag", SubstTemplateTypeParmTypeFlag> {
let Read = [{ node->getSubstitutionFlag() }];
}

// The call to getCanonicalType here existed in ASTReader.cpp, too.
def : Creator<[{
return ctx.getSubstTemplateTypeParmType(
replacementType, associatedDecl, Index, PackIndex);
replacementType, associatedDecl, Index, PackIndex, SubstitutionFlag);
}]>;
}

Expand Down
7 changes: 4 additions & 3 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5248,10 +5248,11 @@ QualType ASTContext::getHLSLAttributedResourceType(
/// Retrieve a substitution-result type.
QualType ASTContext::getSubstTemplateTypeParmType(
QualType Replacement, Decl *AssociatedDecl, unsigned Index,
std::optional<unsigned> PackIndex) const {
std::optional<unsigned> PackIndex,
SubstTemplateTypeParmTypeFlag Flag) const {
llvm::FoldingSetNodeID ID;
SubstTemplateTypeParmType::Profile(ID, Replacement, AssociatedDecl, Index,
PackIndex);
PackIndex, Flag);
void *InsertPos = nullptr;
SubstTemplateTypeParmType *SubstParm =
SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
Expand All @@ -5261,7 +5262,7 @@ QualType ASTContext::getSubstTemplateTypeParmType(
!Replacement.isCanonical()),
alignof(SubstTemplateTypeParmType));
SubstParm = new (Mem) SubstTemplateTypeParmType(Replacement, AssociatedDecl,
Index, PackIndex);
Index, PackIndex, Flag);
Types.push_back(SubstParm);
SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,8 +1627,8 @@ ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
return ToReplacementTypeOrErr.takeError();

return Importer.getToContext().getSubstTemplateTypeParmType(
*ToReplacementTypeOrErr, *ReplacedOrErr, T->getIndex(),
T->getPackIndex());
*ToReplacementTypeOrErr, *ReplacedOrErr, T->getIndex(), T->getPackIndex(),
T->getSubstitutionFlag());
}

ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmPackType(
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4194,7 +4194,7 @@ static const TemplateTypeParmDecl *getReplacedParameter(Decl *D,

SubstTemplateTypeParmType::SubstTemplateTypeParmType(
QualType Replacement, Decl *AssociatedDecl, unsigned Index,
std::optional<unsigned> PackIndex)
std::optional<unsigned> PackIndex, SubstTemplateTypeParmTypeFlag Flag)
: Type(SubstTemplateTypeParm, Replacement.getCanonicalType(),
Replacement->getDependence()),
AssociatedDecl(AssociatedDecl) {
Expand All @@ -4205,6 +4205,10 @@ SubstTemplateTypeParmType::SubstTemplateTypeParmType(

SubstTemplateTypeParmTypeBits.Index = Index;
SubstTemplateTypeParmTypeBits.PackIndex = PackIndex ? *PackIndex + 1 : 0;
SubstTemplateTypeParmTypeBits.SubstitutionFlag = llvm::to_underlying(Flag);
assert((Flag != SubstTemplateTypeParmTypeFlag::ExpandPacksInPlace ||
PackIndex) &&
"ExpandPacksInPlace needs a valid PackIndex");
assert(AssociatedDecl != nullptr);
}

Expand Down
35 changes: 24 additions & 11 deletions clang/lib/Sema/SemaTemplateInstantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1648,14 +1648,17 @@ namespace {
QualType
TransformSubstTemplateTypeParmType(TypeLocBuilder &TLB,
SubstTemplateTypeParmTypeLoc TL) {
if (SemaRef.CodeSynthesisContexts.back().Kind !=
Sema::CodeSynthesisContext::ConstraintSubstitution)
const SubstTemplateTypeParmType *Type = TL.getTypePtr();
if (Type->getSubstitutionFlag() !=
SubstTemplateTypeParmTypeFlag::ExpandPacksInPlace)
return inherited::TransformSubstTemplateTypeParmType(TLB, TL);

auto PackIndex = TL.getTypePtr()->getPackIndex();
std::optional<Sema::ArgumentPackSubstitutionIndexRAII> SubstIndex;
if (SemaRef.ArgumentPackSubstitutionIndex == -1 && PackIndex)
SubstIndex.emplace(SemaRef, *PackIndex);
assert(Type->getPackIndex());
TemplateArgument TA = TemplateArgs(
Type->getReplacedParameter()->getDepth(), Type->getIndex());
assert(*Type->getPackIndex() + 1 <= TA.pack_size());
Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(
SemaRef, TA.pack_size() - 1 - *Type->getPackIndex());

return inherited::TransformSubstTemplateTypeParmType(TLB, TL);
}
Expand Down Expand Up @@ -3133,7 +3136,11 @@ struct ExpandPackedTypeConstraints

using inherited = TreeTransform<ExpandPackedTypeConstraints>;

ExpandPackedTypeConstraints(Sema &SemaRef) : inherited(SemaRef) {}
const MultiLevelTemplateArgumentList &TemplateArgs;

ExpandPackedTypeConstraints(
Sema &SemaRef, const MultiLevelTemplateArgumentList &TemplateArgs)
: inherited(SemaRef), TemplateArgs(TemplateArgs) {}

using inherited::TransformTemplateTypeParmType;

Expand All @@ -3149,9 +3156,15 @@ struct ExpandPackedTypeConstraints

assert(SemaRef.ArgumentPackSubstitutionIndex != -1);

TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());

std::optional<unsigned> PackIndex;
if (Arg.getKind() == TemplateArgument::Pack)
PackIndex = Arg.pack_size() - 1 - SemaRef.ArgumentPackSubstitutionIndex;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These index transformations also come up in other places, where some other index is used, instead of the current Sema pack subst index. That would be worth refactoring, also consolidating asserts.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it seems like a valuable refactor to me.

When I see hand math like that it always raises my spidey senses and I look to see where else we are doing something like that.


QualType Result = SemaRef.Context.getSubstTemplateTypeParmType(
TL.getType(), T->getDecl(), T->getIndex(),
SemaRef.ArgumentPackSubstitutionIndex);
TL.getType(), T->getDecl(), T->getIndex(), PackIndex,
SubstTemplateTypeParmTypeFlag::ExpandPacksInPlace);
SubstTemplateTypeParmTypeLoc NewTL =
TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
NewTL.setNameLoc(TL.getNameLoc());
Expand Down Expand Up @@ -3210,8 +3223,8 @@ bool Sema::SubstTypeConstraint(
TemplateArgumentListInfo InstArgs;
InstArgs.setLAngleLoc(TemplArgInfo->LAngleLoc);
InstArgs.setRAngleLoc(TemplArgInfo->RAngleLoc);
if (ExpandPackedTypeConstraints(*this).SubstTemplateArguments(
TemplArgInfo->arguments(), InstArgs))
if (ExpandPackedTypeConstraints(*this, TemplateArgs)
.SubstTemplateArguments(TemplArgInfo->arguments(), InstArgs))
return true;

// The type of the original parameter.
Expand Down
19 changes: 19 additions & 0 deletions clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,22 @@ template <typename V> using Alias = S<V>;
Alias A(42);

} // namespace GH111508

namespace GH113518 {

template <class T, unsigned N> struct array {
T value[N];
};

template <typename Tp, typename... Up>
array(Tp, Up...) -> array<Tp, 1 + sizeof...(Up)>;

template <typename T> struct ArrayType {
template <unsigned size> using Array = array<T, size>;
};

template <ArrayType<int>::Array array> void test() {}

void foo() { test<{1, 2, 3}>(); }

} // namespace GH113518
Loading