Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1728,10 +1728,10 @@ class ASTContext : public RefCountedBase<ASTContext> {
QualType Wrapped, QualType Contained,
const HLSLAttributedResourceType::Attributes &Attrs);

QualType
getSubstTemplateTypeParmType(QualType Replacement, Decl *AssociatedDecl,
unsigned Index,
std::optional<unsigned> PackIndex) const;
QualType getSubstTemplateTypeParmType(QualType Replacement,
Decl *AssociatedDecl, unsigned Index,
std::optional<unsigned> PackIndex,
bool ExpandPacksInPlace = false) const;
QualType getSubstTemplateTypeParmPackType(Decl *AssociatedDecl,
unsigned Index, bool Final,
const TemplateArgument &ArgPack);
Expand Down
16 changes: 13 additions & 3 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,9 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
LLVM_PREFERRED_TYPE(bool)
unsigned HasNonCanonicalUnderlyingType : 1;

LLVM_PREFERRED_TYPE(bool)
unsigned ExpandPacksInPlace : 1;

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

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

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

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

bool expandPacksInPlace() const {
return SubstTemplateTypeParmTypeBits.ExpandPacksInPlace;
}

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

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

static void Profile(llvm::FoldingSetNodeID &ID, QualType Replacement,
const Decl *AssociatedDecl, unsigned Index,
std::optional<unsigned> PackIndex) {
std::optional<unsigned> PackIndex,
bool ExpandPacksInPlace) {
Replacement.Profile(ID);
ID.AddPointer(AssociatedDecl);
ID.AddInteger(Index);
ID.AddInteger(PackIndex ? *PackIndex - 1 : 0);
ID.AddInteger(ExpandPacksInPlace);
}

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<"ExpandPacksInPlace", Bool> {
let Read = [{ node->expandPacksInPlace() }];
}

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

Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5248,10 +5248,10 @@ 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, bool ExpandPacksInPlace) const {
llvm::FoldingSetNodeID ID;
SubstTemplateTypeParmType::Profile(ID, Replacement, AssociatedDecl, Index,
PackIndex);
PackIndex, ExpandPacksInPlace);
void *InsertPos = nullptr;
SubstTemplateTypeParmType *SubstParm =
SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
Expand All @@ -5260,8 +5260,8 @@ QualType ASTContext::getSubstTemplateTypeParmType(
void *Mem = Allocate(SubstTemplateTypeParmType::totalSizeToAlloc<QualType>(
!Replacement.isCanonical()),
alignof(SubstTemplateTypeParmType));
SubstParm = new (Mem) SubstTemplateTypeParmType(Replacement, AssociatedDecl,
Index, PackIndex);
SubstParm = new (Mem) SubstTemplateTypeParmType(
Replacement, AssociatedDecl, Index, PackIndex, ExpandPacksInPlace);
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->expandPacksInPlace());
}

ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmPackType(
Expand Down
3 changes: 2 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, bool ExpandPacksInPlace)
: Type(SubstTemplateTypeParm, Replacement.getCanonicalType(),
Replacement->getDependence()),
AssociatedDecl(AssociatedDecl) {
Expand All @@ -4205,6 +4205,7 @@ SubstTemplateTypeParmType::SubstTemplateTypeParmType(

SubstTemplateTypeParmTypeBits.Index = Index;
SubstTemplateTypeParmTypeBits.PackIndex = PackIndex ? *PackIndex + 1 : 0;
SubstTemplateTypeParmTypeBits.ExpandPacksInPlace = ExpandPacksInPlace;
assert(AssociatedDecl != nullptr);
}

Expand Down
34 changes: 23 additions & 11 deletions clang/lib/Sema/SemaTemplateInstantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1648,14 +1648,16 @@ namespace {
QualType
TransformSubstTemplateTypeParmType(TypeLocBuilder &TLB,
SubstTemplateTypeParmTypeLoc TL) {
if (SemaRef.CodeSynthesisContexts.back().Kind !=
Sema::CodeSynthesisContext::ConstraintSubstitution)
const SubstTemplateTypeParmType *Type = TL.getTypePtr();
if (!Type->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 +3135,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 +3155,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,
/*ExpandPacksInPlace=*/true);
SubstTemplateTypeParmTypeLoc NewTL =
TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
NewTL.setNameLoc(TL.getNameLoc());
Expand Down Expand Up @@ -3210,8 +3222,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