types.
We previously did not treat a function type as dependent if it had a
parameter pack with a non-dependent type -- such a function type depends
on the arity of the pack so is dependent even though none of the
parameter types is dependent. In order to properly handle this, we now
treat pack expansion types as always being dependent types (depending on
at least the pack arity), and always canonically being pack expansion
types, even in the unusual case when the pattern is not a dependent
type. This does mean that we can have canonical types that are pack
expansions that contain no unexpanded packs, which is unfortunate but
not inaccurate.
We also previously did not treat a typedef type as
instantiation-dependent if its canonical type was not
instantiation-dependent. That's wrong because instantiation-dependence
is a property of the type sugar, not of the type; an
instantiation-dependent type can have a non-instantiation-dependent
canonical type.
void getInjectedTemplateArgs(const TemplateParameterList *Params,
SmallVectorImpl<TemplateArgument> &Args);
+ /// Form a pack expansion type with the given pattern.
+ /// \param NumExpansions The number of expansions for the pack, if known.
+ /// \param ExpectPackInType If \c false, we should not expect \p Pattern to
+ /// contain an unexpanded pack. This only makes sense if the pack
+ /// expansion is used in a context where the arity is inferred from
+ /// elsewhere, such as if the pattern contains a placeholder type or
+ /// if this is the canonical type of another pack expansion type.
QualType getPackExpansionType(QualType Pattern,
- Optional<unsigned> NumExpansions);
+ Optional<unsigned> NumExpansions,
+ bool ExpectPackInType = true);
QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
ObjCInterfaceDecl *PrevDecl = nullptr) const;
protected:
friend class ASTContext; // ASTContext creates these.
- TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can)
- : Type(tc, can, can->getDependence() & ~TypeDependence::UnexpandedPack),
- Decl(const_cast<TypedefNameDecl *>(D)) {
- assert(!isa<TypedefType>(can) && "Invalid canonical type");
- }
+ TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can);
public:
TypedefNameDecl *getDecl() const { return Decl; }
PackExpansionType(QualType Pattern, QualType Canon,
Optional<unsigned> NumExpansions)
: Type(PackExpansion, Canon,
- (Pattern->getDependence() | TypeDependence::Instantiation) &
+ (Pattern->getDependence() | TypeDependence::Dependent |
+ TypeDependence::Instantiation) &
~TypeDependence::UnexpandedPack),
Pattern(Pattern) {
PackExpansionTypeBits.NumExpansions =
return None;
}
- bool isSugared() const { return !Pattern->isDependentType(); }
- QualType desugar() const { return isSugared() ? Pattern : QualType(this, 0); }
+ bool isSugared() const { return false; }
+ QualType desugar() const { return QualType(this, 0); }
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getPattern(), getNumExpansions());
def InjectedClassNameType : TypeNode<Type>, AlwaysDependent, LeafType;
def DependentNameType : TypeNode<Type>, AlwaysDependent;
def DependentTemplateSpecializationType : TypeNode<Type>, AlwaysDependent;
-def PackExpansionType : TypeNode<Type>, NeverCanonicalUnlessDependent;
+def PackExpansionType : TypeNode<Type>, AlwaysDependent;
def ObjCTypeParamType : TypeNode<Type>, NeverCanonical;
def ObjCObjectType : TypeNode<Type>;
def ObjCInterfaceType : TypeNode<ObjCObjectType>, LeafType;
}
QualType ASTContext::getPackExpansionType(QualType Pattern,
- Optional<unsigned> NumExpansions) {
+ Optional<unsigned> NumExpansions,
+ bool ExpectPackInType) {
+ assert((!ExpectPackInType || Pattern->containsUnexpandedParameterPack()) &&
+ "Pack expansions must expand one or more parameter packs");
+
llvm::FoldingSetNodeID ID;
PackExpansionType::Profile(ID, Pattern, NumExpansions);
- // A deduced type can deduce to a pack, eg
- // auto ...x = some_pack;
- // That declaration isn't (yet) valid, but is created as part of building an
- // init-capture pack:
- // [...x = some_pack] {}
- assert((Pattern->containsUnexpandedParameterPack() ||
- Pattern->getContainedDeducedType()) &&
- "Pack expansions must expand one or more parameter packs");
void *InsertPos = nullptr;
- PackExpansionType *T
- = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
+ PackExpansionType *T = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
if (T)
return QualType(T, 0);
QualType Canon;
if (!Pattern.isCanonical()) {
- Canon = getCanonicalType(Pattern);
- // The canonical type might not contain an unexpanded parameter pack, if it
- // contains an alias template specialization which ignores one of its
- // parameters.
- if (Canon->containsUnexpandedParameterPack()) {
- Canon = getPackExpansionType(Canon, NumExpansions);
-
- // Find the insert position again, in case we inserted an element into
- // PackExpansionTypes and invalidated our insert position.
- PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
- }
+ Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions,
+ /*ExpectPackInType=*/false);
+
+ // Find the insert position again, in case we inserted an element into
+ // PackExpansionTypes and invalidated our insert position.
+ PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
}
T = new (*this, TypeAlignment)
T->getTypeConstraintArguments());
}
- // FIXME: Non-trivial to implement, but important for C++
- SUGARED_TYPE_CLASS(PackExpansion)
-
QualType VisitObjCObjectType(const ObjCObjectType *T) {
QualType baseType = recurse(T->getBaseType());
if (baseType.isNull())
getExtProtoInfo(), Ctx, isCanonicalUnqualified());
}
+TypedefType::TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can)
+ : Type(tc, can, D->getUnderlyingType()->getDependence()),
+ Decl(const_cast<TypedefNameDecl *>(D)) {
+ assert(!isa<TypedefType>(can) && "Invalid canonical type");
+}
+
QualType TypedefType::desugar() const {
return getDecl()->getUnderlyingType();
}
case Type::TypeOf:
case Type::Decltype:
case Type::UnaryTransform:
- case Type::PackExpansion:
break;
}
case Type::UnaryTransform:
case Type::Attributed:
case Type::SubstTemplateTypeParm:
- case Type::PackExpansion:
case Type::MacroQualified:
// Keep walking after single level desugaring.
type = type.getSingleStepDesugaredType(getContext());
case Type::UnaryTransform:
case Type::Attributed:
case Type::SubstTemplateTypeParm:
- case Type::PackExpansion:
case Type::MacroQualified:
// Keep walking after single level desugaring.
T = T.getSingleStepDesugaredType(Context);
Diag(EllipsisLoc, getLangOpts().CPlusPlus20
? diag::warn_cxx17_compat_init_capture_pack
: diag::ext_init_capture_pack);
- DeductType = Context.getPackExpansionType(DeductType, NumExpansions);
+ DeductType = Context.getPackExpansionType(DeductType, NumExpansions,
+ /*ExpectPackInType=*/false);
TLB.push<PackExpansionTypeLoc>(DeductType).setEllipsisLoc(EllipsisLoc);
} else {
// Just ignore the ellipsis for now and form a non-pack variable. We'll
return QualType();
}
- return Context.getPackExpansionType(Pattern, NumExpansions);
+ return Context.getPackExpansionType(Pattern, NumExpansions,
+ /*ExpectPackInType=*/false);
}
ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
<< T << D.getSourceRange();
D.setEllipsisLoc(SourceLocation());
} else {
- T = Context.getPackExpansionType(T, None);
+ T = Context.getPackExpansionType(T, None, /*ExpectPackInType=*/false);
}
break;
case DeclaratorContext::TemplateParamContext:
--- /dev/null
+// RUN: %clang_cc1 -std=c++20 -verify %s
+
+namespace PR46377 {
+ template<typename> using IntPtr = int*;
+ template<typename ...T> auto non_dependent_typedef() {
+ typedef int(*P)(IntPtr<T>...);
+ return P();
+ }
+ template<typename ...T> auto non_dependent_alias() {
+ using P = int(*)(IntPtr<T>...);
+ return P();
+ }
+ template<typename ...T> auto non_dependent_via_sizeof() {
+ using P = int(*)(int(...pack)[sizeof(sizeof(T))]); // expected-error {{invalid application of 'sizeof'}}
+ return P();
+ }
+
+ using a = int (*)(int*, int*);
+ using a = decltype(non_dependent_typedef<void, void>());
+ using a = decltype(non_dependent_alias<void, void>());
+ using a = decltype(non_dependent_via_sizeof<float, float>());
+
+ using b = decltype(non_dependent_via_sizeof<float, void>()); // expected-note {{instantiation of}}
+}