Skip to content

Commit f3b2a79

Browse files
committed
AST: Store the ASTContext inside the TypeTransform
1 parent 4a82d38 commit f3b2a79

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

include/swift/AST/TypeTransform.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ namespace swift {
2525

2626
template<typename Derived>
2727
class TypeTransform {
28+
public:
29+
ASTContext &ctx;
30+
31+
TypeTransform(ASTContext &ctx) : ctx(ctx) {}
32+
33+
private:
2834
Derived &asDerived() { return *static_cast<Derived *>(this); }
2935

3036
/// \param pos The variance position of the result type.
@@ -98,7 +104,6 @@ class TypeTransform {
98104

99105
// Recur into children of this type.
100106
TypeBase *base = t.getPointer();
101-
auto &ctx = base->getASTContext();
102107

103108
switch (base->getKind()) {
104109
#define BUILTIN_TYPE(Id, Parent) \
@@ -684,14 +689,16 @@ case TypeKind::Id:
684689
if (!anyChanged)
685690
return t;
686691

687-
// Handle vanishing tuples -- If the transform would yield a singleton
688-
// tuple, and we didn't start with one, flatten to produce the
689-
// element type.
690-
if (elements.size() == 1 &&
691-
!elements[0].getType()->is<PackExpansionType>() &&
692-
!(tuple->getNumElements() == 1 &&
693-
!tuple->getElementType(0)->is<PackExpansionType>())) {
694-
return elements[0].getType();
692+
if (asDerived().shouldUnwrapVanishingTuples()) {
693+
// Handle vanishing tuples -- If the transform would yield a singleton
694+
// tuple, and we didn't start with one, flatten to produce the
695+
// element type.
696+
if (elements.size() == 1 &&
697+
!elements[0].getType()->is<PackExpansionType>() &&
698+
!(tuple->getNumElements() == 1 &&
699+
!tuple->getElementType(0)->is<PackExpansionType>())) {
700+
return elements[0].getType();
701+
}
695702
}
696703

697704
return TupleType::get(elements, ctx);
@@ -1002,6 +1009,8 @@ case TypeKind::Id:
10021009
return SubstitutionMap::get(sig, newSubs, LookUpConformanceInModule());
10031010
}
10041011

1012+
bool shouldUnwrapVanishingTuples() const { return true; }
1013+
10051014
CanType transformSILField(CanType fieldTy, TypePosition pos) {
10061015
return doIt(fieldTy, pos)->getCanonicalType();
10071016
}

lib/AST/GenericEnvironment.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,8 @@ namespace {
524524
struct MapTypeIntoContext: TypeTransform<MapTypeIntoContext> {
525525
GenericEnvironment *env;
526526

527-
explicit MapTypeIntoContext(GenericEnvironment *env) : env(env) {}
527+
explicit MapTypeIntoContext(GenericEnvironment *env, ASTContext &ctx)
528+
: TypeTransform(ctx), env(env) {}
528529

529530
std::optional<Type> transform(TypeBase *type, TypePosition pos) {
530531
if (!type->hasTypeParameter())
@@ -552,7 +553,10 @@ struct MapTypeIntoContext: TypeTransform<MapTypeIntoContext> {
552553

553554
Type GenericEnvironment::mapTypeIntoContext(Type type) const {
554555
assert(!type->hasPrimaryArchetype() && "already have a contextual type");
555-
return MapTypeIntoContext(const_cast<GenericEnvironment *>(this))
556+
if (!type->hasTypeParameter())
557+
return type;
558+
return MapTypeIntoContext(const_cast<GenericEnvironment *>(this),
559+
type->getASTContext())
556560
.doIt(type, TypePosition::Invariant);
557561
}
558562

lib/AST/Type.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4092,14 +4092,15 @@ Type Type::transformRec(
40924092
class Transform : public TypeTransform<Transform> {
40934093
llvm::function_ref<std::optional<Type>(TypeBase *)> fn;
40944094
public:
4095-
explicit Transform(llvm::function_ref<std::optional<Type>(TypeBase *)> fn) : fn(fn) {}
4095+
explicit Transform(llvm::function_ref<std::optional<Type>(TypeBase *)> fn,
4096+
ASTContext &ctx) : TypeTransform(ctx), fn(fn) {}
40964097

40974098
std::optional<Type> transform(TypeBase *type, TypePosition position) {
40984099
return fn(type);
40994100
}
41004101
};
41014102

4102-
return Transform(fn).doIt(*this, TypePosition::Invariant);
4103+
return Transform(fn, (*this)->getASTContext()).doIt(*this, TypePosition::Invariant);
41034104
}
41044105

41054106
Type Type::transformWithPosition(
@@ -4109,14 +4110,15 @@ Type Type::transformWithPosition(
41094110
class Transform : public TypeTransform<Transform> {
41104111
llvm::function_ref<std::optional<Type>(TypeBase *, TypePosition)> fn;
41114112
public:
4112-
explicit Transform(llvm::function_ref<std::optional<Type>(TypeBase *, TypePosition)> fn) : fn(fn) {}
4113+
explicit Transform(llvm::function_ref<std::optional<Type>(TypeBase *, TypePosition)> fn,
4114+
ASTContext &ctx) : TypeTransform(ctx), fn(fn) {}
41134115

41144116
std::optional<Type> transform(TypeBase *type, TypePosition position) {
41154117
return fn(type, position);
41164118
}
41174119
};
41184120

4119-
return Transform(fn).doIt(*this, pos);
4121+
return Transform(fn, (*this)->getASTContext()).doIt(*this, pos);
41204122
}
41214123

41224124
bool Type::findIf(llvm::function_ref<bool(Type)> pred) const {

lib/AST/TypeSubstitution.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ class TypeSubstituter : public TypeTransform<TypeSubstituter> {
388388
InFlightSubstitution &IFS;
389389

390390
public:
391-
TypeSubstituter(unsigned level, InFlightSubstitution &IFS)
392-
: level(level), IFS(IFS) {}
391+
TypeSubstituter(ASTContext &ctx, unsigned level, InFlightSubstitution &IFS)
392+
: TypeTransform(ctx), level(level), IFS(IFS) {}
393393

394394
std::optional<Type> transform(TypeBase *type, TypePosition pos);
395395

@@ -566,7 +566,7 @@ Type Type::subst(InFlightSubstitution &IFS) const {
566566
if (IFS.isInvariant(*this))
567567
return *this;
568568

569-
TypeSubstituter transform(/*level=*/0, IFS);
569+
TypeSubstituter transform((*this)->getASTContext(), /*level=*/0, IFS);
570570
return transform.doIt(*this, TypePosition::Invariant);
571571
}
572572

0 commit comments

Comments
 (0)