Skip to content

AST: Transition to storing substitution maps in bound generic type nodes #31895

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions include/swift/AST/TypeDifferenceVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ class CanTypeDifferenceVisitor : public CanTypePairVisitor<Impl, bool> {
return asImpl().visitDifferentTypeStructure(type1, type2);

return visitComponentArray(type1, type2,
type1.getGenericArgs(), type2.getGenericArgs());
type1.getDirectGenericArgs(),
type2.getDirectGenericArgs());
}

bool visitAnyMetatypeType(CanAnyMetatypeType type1,
Expand Down Expand Up @@ -388,4 +389,4 @@ class CanTypeDifferenceVisitor : public CanTypePairVisitor<Impl, bool> {

}

#endif
#endif
9 changes: 4 additions & 5 deletions include/swift/AST/TypeMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,10 @@ class TypeMatcher {
sugaredFirstBGT->getParent()))
return false;

for (unsigned i = 0, n = firstBGT->getGenericArgs().size();
i != n; ++i) {
if (!this->visit(firstBGT.getGenericArgs()[i],
secondBGT->getGenericArgs()[i],
sugaredFirstBGT->getGenericArgs()[i]))
for (const auto i: indices(firstBGT->getDirectGenericArgs())) {
if (!this->visit(firstBGT.getDirectGenericArgs()[i],
secondBGT->getDirectGenericArgs()[i],
sugaredFirstBGT->getDirectGenericArgs()[i]))
return false;
}

Expand Down
132 changes: 43 additions & 89 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
Count : 32
);

SWIFT_INLINE_BITFIELD_FULL(BoundGenericType, TypeBase, 32,
: NumPadBits,

/// The number of generic arguments.
GenericArgCount : 32
);

SWIFT_INLINE_BITFIELD_FULL(TypeAliasType, SugarType, 1+1,
: NumPadBits,

Expand Down Expand Up @@ -2205,33 +2198,37 @@ typedef ArrayRefView<Type,CanType,getAsCanType> CanTypeArrayRef;
/// given type arguments.
class BoundGenericType : public NominalOrBoundGenericNominalType,
public llvm::FoldingSetNode {

/// Retrieve the intrusive pointer storage from the subtype
const Type *getTrailingObjectsPointer() const;
Type *getTrailingObjectsPointer() {
const BoundGenericType *temp = this;
return const_cast<Type *>(temp->getTrailingObjectsPointer());
}
const SubstitutionMap Substitutions;

protected:
BoundGenericType(TypeKind theKind, NominalTypeDecl *theDecl, Type parent,
ArrayRef<Type> genericArgs, const ASTContext *context,
RecursiveTypeProperties properties);
BoundGenericType(TypeKind K, NominalTypeDecl *TheDecl, Type Parent,
SubstitutionMap Substitutions, const ASTContext *C,
RecursiveTypeProperties Properties)
: NominalOrBoundGenericNominalType(TheDecl, Parent, K, C, Properties),
Substitutions(Substitutions) {}

public:
static BoundGenericType* get(NominalTypeDecl *TheDecl, Type Parent,
ArrayRef<Type> GenericArgs);
static BoundGenericType *get(NominalTypeDecl *TheDecl, Type Parent,
SubstitutionMap Substitutions);

/// Retrieve the set of generic arguments provided at this level.
ArrayRef<Type> getGenericArgs() const {
return {getTrailingObjectsPointer(), Bits.BoundGenericType.GenericArgCount};
/// Deprecated version of the above .
static BoundGenericType *get(NominalTypeDecl *TheDecl, Type Parent,
ArrayRef<Type> GenericArgs,
ModuleDecl *M = nullptr);

/// Retrieve the substitution map applied to the declaration's underlying
/// to produce the described type.
SubstitutionMap getSubstitutionMap() const {
return Substitutions;
}

void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getDecl(), getParent(), getGenericArgs());
ArrayRef<Type> getDirectGenericArgs() const;

void Profile(llvm::FoldingSetNodeID &ID) const {
Profile(ID, getDecl(), getParent(), Substitutions);
}
static void Profile(llvm::FoldingSetNodeID &ID, NominalTypeDecl *TheDecl,
Type Parent, ArrayRef<Type> GenericArgs);
Type Parent, SubstitutionMap Substitutions);

// Implement isa/cast/dyncast/etc.
static bool classof(const TypeBase *T) {
Expand All @@ -2240,35 +2237,24 @@ class BoundGenericType : public NominalOrBoundGenericNominalType,
}
};
BEGIN_CAN_TYPE_WRAPPER(BoundGenericType, NominalOrBoundGenericNominalType)
CanTypeArrayRef getGenericArgs() const {
return CanTypeArrayRef(getPointer()->getGenericArgs());
CanTypeArrayRef getDirectGenericArgs() const {
return CanTypeArrayRef(getPointer()->getDirectGenericArgs());
}
END_CAN_TYPE_WRAPPER(BoundGenericType, NominalOrBoundGenericNominalType)


/// BoundGenericClassType - A subclass of BoundGenericType for the case
/// when the nominal type is a generic class type.
class BoundGenericClassType final : public BoundGenericType,
private llvm::TrailingObjects<BoundGenericClassType, Type> {
friend TrailingObjects;

private:
BoundGenericClassType(ClassDecl *theDecl, Type parent,
ArrayRef<Type> genericArgs, const ASTContext *context,
RecursiveTypeProperties properties)
class BoundGenericClassType final : public BoundGenericType {
BoundGenericClassType(ClassDecl *TheDecl, Type Parent,
SubstitutionMap Substitutions, const ASTContext *C,
RecursiveTypeProperties Properties)
: BoundGenericType(TypeKind::BoundGenericClass,
reinterpret_cast<NominalTypeDecl*>(theDecl), parent,
genericArgs, context, properties) {}
reinterpret_cast<NominalTypeDecl*>(TheDecl), Parent,
Substitutions, C, Properties) {}
friend class BoundGenericType;

public:
static BoundGenericClassType* get(ClassDecl *theDecl, Type parent,
ArrayRef<Type> genericArgs) {
return cast<BoundGenericClassType>(
BoundGenericType::get(reinterpret_cast<NominalTypeDecl*>(theDecl),
parent, genericArgs));
}

/// Returns the declaration that declares this type.
ClassDecl *getDecl() const {
return reinterpret_cast<ClassDecl*>(BoundGenericType::getDecl());
Expand All @@ -2282,27 +2268,16 @@ DEFINE_EMPTY_CAN_TYPE_WRAPPER(BoundGenericClassType, BoundGenericType)

/// BoundGenericEnumType - A subclass of BoundGenericType for the case
/// when the nominal type is a generic enum type.
class BoundGenericEnumType final : public BoundGenericType,
private llvm::TrailingObjects<BoundGenericEnumType, Type> {
friend TrailingObjects;

private:
BoundGenericEnumType(EnumDecl *theDecl, Type parent,
ArrayRef<Type> genericArgs, const ASTContext *context,
RecursiveTypeProperties properties)
class BoundGenericEnumType final : public BoundGenericType {
BoundGenericEnumType(EnumDecl *TheDecl, Type Parent,
SubstitutionMap Substitutions, const ASTContext *C,
RecursiveTypeProperties Properties)
: BoundGenericType(TypeKind::BoundGenericEnum,
reinterpret_cast<NominalTypeDecl*>(theDecl), parent,
genericArgs, context, properties) {}
reinterpret_cast<NominalTypeDecl*>(TheDecl), Parent,
Substitutions, C, Properties) {}
friend class BoundGenericType;

public:
static BoundGenericEnumType* get(EnumDecl *theDecl, Type parent,
ArrayRef<Type> genericArgs) {
return cast<BoundGenericEnumType>(
BoundGenericType::get(reinterpret_cast<NominalTypeDecl*>(theDecl),
parent, genericArgs));
}

/// Returns the declaration that declares this type.
EnumDecl *getDecl() const {
return reinterpret_cast<EnumDecl*>(BoundGenericType::getDecl());
Expand All @@ -2316,27 +2291,16 @@ DEFINE_EMPTY_CAN_TYPE_WRAPPER(BoundGenericEnumType, BoundGenericType)

/// BoundGenericStructType - A subclass of BoundGenericType for the case
/// when the nominal type is a generic struct type.
class BoundGenericStructType final : public BoundGenericType,
private llvm::TrailingObjects<BoundGenericStructType, Type> {
friend TrailingObjects;

private:
BoundGenericStructType(StructDecl *theDecl, Type parent,
ArrayRef<Type> genericArgs, const ASTContext *context,
RecursiveTypeProperties properties)
class BoundGenericStructType final : public BoundGenericType {
BoundGenericStructType(StructDecl *TheDecl, Type Parent,
SubstitutionMap Substitutions, const ASTContext *C,
RecursiveTypeProperties Properties)
: BoundGenericType(TypeKind::BoundGenericStruct,
reinterpret_cast<NominalTypeDecl*>(theDecl), parent,
genericArgs, context, properties) {}
reinterpret_cast<NominalTypeDecl*>(TheDecl), Parent,
Substitutions, C, Properties) {}
friend class BoundGenericType;

public:
static BoundGenericStructType* get(StructDecl *theDecl, Type parent,
ArrayRef<Type> genericArgs) {
return cast<BoundGenericStructType>(
BoundGenericType::get(reinterpret_cast<NominalTypeDecl*>(theDecl),
parent, genericArgs));
}

/// Returns the declaration that declares this type.
StructDecl *getDecl() const {
return reinterpret_cast<StructDecl*>(BoundGenericType::getDecl());
Expand Down Expand Up @@ -6413,7 +6377,7 @@ inline Type TupleTypeElt::getVarargBaseTy() const {
return AT->getBaseType();
if (auto *BGT = dyn_cast<BoundGenericType>(T)) {
// It's the stdlib Array<T>.
return BGT->getGenericArgs()[0];
return BGT->getDirectGenericArgs()[0];
}
assert(T->hasError());
return T;
Expand Down Expand Up @@ -6445,16 +6409,6 @@ inline ParameterTypeFlags ParameterTypeFlags::fromParameterType(
return {isVariadic, isAutoClosure, isNonEphemeral, ownership, isNoDerivative};
}

inline const Type *BoundGenericType::getTrailingObjectsPointer() const {
if (auto ty = dyn_cast<BoundGenericStructType>(this))
return ty->getTrailingObjects<Type>();
if (auto ty = dyn_cast<BoundGenericEnumType>(this))
return ty->getTrailingObjects<Type>();
if (auto ty = dyn_cast<BoundGenericClassType>(this))
return ty->getTrailingObjects<Type>();
llvm_unreachable("Unhandled BoundGenericType!");
}

inline ArrayRef<AnyFunctionType::Param> AnyFunctionType::getParams() const {
switch (getKind()) {
case TypeKind::Function:
Expand Down
Loading