Skip to content

Commit b601c29

Browse files
committed
AST: Replace remaining uses of Type::transform() with transformRec()
1 parent d35af38 commit b601c29

22 files changed

+154
-174
lines changed

include/swift/AST/Type.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,6 @@ class Type {
250250
/// its children.
251251
bool findIf(llvm::function_ref<bool(Type)> pred) const;
252252

253-
/// Transform the given type by recursively applying the user-provided
254-
/// function to each node.
255-
///
256-
/// \param fn A function object with the signature \c Type(Type) , which
257-
/// accepts a type and returns either a transformed type or a null type
258-
/// (which will propagate out the null type).
259-
///
260-
/// \returns the result of transforming the type.
261-
Type transform(llvm::function_ref<Type(Type)> fn) const;
262-
263253
/// Transform the given type by recursively applying the user-provided
264254
/// function to each node.
265255
///

lib/AST/ASTMangler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3723,10 +3723,10 @@ ASTMangler::dropProtocolsFromAssociatedTypes(Type type,
37233723
if (!type->hasDependentMember())
37243724
return type;
37253725

3726-
return type.transform([&](Type t) -> Type {
3727-
if (auto *dmt = dyn_cast<DependentMemberType>(t.getPointer()))
3726+
return type.transformRec([&](TypeBase *t) -> std::optional<Type> {
3727+
if (auto *dmt = dyn_cast<DependentMemberType>(t))
37283728
return dropProtocolFromAssociatedType(dmt, sig);
3729-
return t;
3729+
return std::nullopt;
37303730
});
37313731
}
37323732

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6134,11 +6134,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
61346134
// FIXME: We need to replace nested existential metatypes so that
61356135
// we don't print duplicate 'any'. This will be unnecessary once
61366136
// ExistentialMetatypeType is split into ExistentialType(MetatypeType).
6137-
printWithParensIfNotSimple(instanceType.transform([](Type type) -> Type {
6138-
if (auto existential = type->getAs<ExistentialMetatypeType>())
6137+
printWithParensIfNotSimple(instanceType.transformRec([](Type t) -> std::optional<Type> {
6138+
if (auto existential = t->getAs<ExistentialMetatypeType>())
61396139
return MetatypeType::get(existential->getInstanceType());
61406140

6141-
return type;
6141+
return std::nullopt;
61426142
}));
61436143
} else {
61446144
assert(T->is<MetatypeType>());

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,12 +3477,12 @@ static Type mapSignatureFunctionType(ASTContext &ctx, Type type,
34773477

34783478
/// Map a type within the signature of a declaration.
34793479
static Type mapSignatureType(ASTContext &ctx, Type type) {
3480-
return type.transform([&](Type type) -> Type {
3480+
return type.transformRec([&](Type type) -> std::optional<Type> {
34813481
if (type->is<FunctionType>()) {
34823482
return mapSignatureFunctionType(ctx, type, false, false, false, 1);
34833483
}
34843484

3485-
return type;
3485+
return std::nullopt;
34863486
});
34873487
}
34883488

lib/AST/DiagnosticEngine.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,11 @@ static bool isInterestingTypealias(Type type) {
613613
/// declaration and end up presenting the parameter in τ_0_0 format on
614614
/// diagnostic.
615615
static Type getAkaTypeForDisplay(Type type) {
616-
return type.transform([](Type visitTy) -> Type {
617-
if (isa<SugarType>(visitTy.getPointer()) &&
618-
!isa<GenericTypeParamType>(visitTy.getPointer()))
616+
return type.transformRec([&](TypeBase *visitTy) -> std::optional<Type> {
617+
if (isa<SugarType>(visitTy) &&
618+
!isa<GenericTypeParamType>(visitTy))
619619
return getAkaTypeForDisplay(visitTy->getDesugaredType());
620-
return visitTy;
620+
return std::nullopt;
621621
});
622622
}
623623

lib/AST/GenericSignature.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,11 @@ Type GenericSignatureImpl::getSugaredType(Type type) const {
600600
if (!type->hasTypeParameter())
601601
return type;
602602

603-
return type.transform([this](Type Ty) -> Type {
604-
if (auto GP = dyn_cast<GenericTypeParamType>(Ty.getPointer())) {
603+
return type.transformRec([this](TypeBase *Ty) -> std::optional<Type> {
604+
if (auto GP = dyn_cast<GenericTypeParamType>(Ty)) {
605605
return Type(getSugaredType(GP));
606606
}
607-
return Ty;
607+
return std::nullopt;
608608
});
609609
}
610610

@@ -1082,8 +1082,10 @@ static Requirement stripBoundDependentMemberTypes(Requirement req) {
10821082
case RequirementKind::Superclass:
10831083
case RequirementKind::SameType:
10841084
return Requirement(req.getKind(), subjectType,
1085-
req.getSecondType().transform([](Type t) {
1086-
return stripBoundDependentMemberTypes(t);
1085+
req.getSecondType().transformRec([](Type t) -> std::optional<Type> {
1086+
if (t->isTypeParameter())
1087+
return stripBoundDependentMemberTypes(t);
1088+
return std::nullopt;
10871089
}));
10881090

10891091
case RequirementKind::Layout:

lib/AST/SubstitutionMap.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,37 +593,38 @@ SubstitutionMap::combineSubstitutionMaps(SubstitutionMap firstSubMap,
593593
GenericSignature genericSig) {
594594
auto &ctx = genericSig->getASTContext();
595595

596-
auto replaceGenericParameter = [&](Type type) -> Type {
596+
auto replaceGenericParameter = [&](Type type) -> std::optional<Type> {
597597
if (auto gp = type->getAs<GenericTypeParamType>()) {
598598
if (how == CombineSubstitutionMaps::AtDepth) {
599599
if (gp->getDepth() < firstDepthOrIndex)
600600
return Type();
601-
return GenericTypeParamType::get(gp->isParameterPack(),
602-
gp->getDepth() + secondDepthOrIndex -
603-
firstDepthOrIndex,
604-
gp->getIndex(), ctx);
601+
return Type(GenericTypeParamType::get(gp->isParameterPack(),
602+
gp->getDepth() + secondDepthOrIndex -
603+
firstDepthOrIndex,
604+
gp->getIndex(), ctx));
605605
}
606606

607607
assert(how == CombineSubstitutionMaps::AtIndex);
608608
if (gp->getIndex() < firstDepthOrIndex)
609609
return Type();
610-
return GenericTypeParamType::get(
610+
return Type(GenericTypeParamType::get(
611611
gp->isParameterPack(), gp->getDepth(),
612-
gp->getIndex() + secondDepthOrIndex - firstDepthOrIndex, ctx);
612+
gp->getIndex() + secondDepthOrIndex - firstDepthOrIndex, ctx));
613613
}
614614

615-
return type;
615+
return std::nullopt;
616616
};
617617

618618
return get(
619619
genericSig,
620620
[&](SubstitutableType *type) {
621621
if (auto replacement = replaceGenericParameter(type))
622-
return Type(replacement).subst(secondSubMap);
622+
if (*replacement)
623+
return replacement->subst(secondSubMap);
623624
return Type(type).subst(firstSubMap);
624625
},
625626
[&](CanType type, Type substType, ProtocolDecl *proto) {
626-
if (auto replacement = type.transform(replaceGenericParameter))
627+
if (auto replacement = type.transformRec(replaceGenericParameter))
627628
return secondSubMap.lookupConformance(replacement->getCanonicalType(),
628629
proto);
629630
if (auto conformance = firstSubMap.lookupConformance(type, proto))

lib/AST/Type.cpp

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,10 @@ Type TypeBase::getRValueType() {
750750
if (!hasLValueType())
751751
return this;
752752

753-
return Type(this).transform([](Type t) -> Type {
754-
if (auto *lvalueTy = dyn_cast<LValueType>(t.getPointer()))
753+
return Type(this).transformRec([](TypeBase *t) -> std::optional<Type> {
754+
if (auto *lvalueTy = dyn_cast<LValueType>(t))
755755
return lvalueTy->getObjectType();
756-
return t;
756+
return std::nullopt;
757757
});
758758
}
759759

@@ -1863,7 +1863,7 @@ CanType TypeBase::getReducedType(GenericSignature sig) {
18631863
}
18641864

18651865
CanType TypeBase::getMinimalCanonicalType(const DeclContext *useDC) const {
1866-
const auto MinimalTy = getCanonicalType().transform([useDC](Type Ty) -> Type {
1866+
const auto MinimalTy = getCanonicalType().transformRec([useDC](TypeBase *Ty) -> std::optional<Type> {
18671867
const CanType CanTy = CanType(Ty);
18681868

18691869
if (const auto ET = dyn_cast<ExistentialType>(CanTy)) {
@@ -1899,15 +1899,15 @@ CanType TypeBase::getMinimalCanonicalType(const DeclContext *useDC) const {
18991899
return Composition->getMinimalCanonicalType(useDC);
19001900
}
19011901

1902-
return CanTy;
1902+
return std::nullopt;
19031903
});
19041904

19051905
return CanType(MinimalTy);
19061906
}
19071907

19081908
TypeBase *TypeBase::reconstituteSugar(bool Recursive) {
1909-
auto Func = [Recursive](Type Ty) -> Type {
1910-
if (auto boundGeneric = dyn_cast<BoundGenericType>(Ty.getPointer())) {
1909+
auto Func = [Recursive](TypeBase *Ty) -> std::optional<Type> {
1910+
if (auto boundGeneric = dyn_cast<BoundGenericType>(Ty)) {
19111911

19121912
auto getGenericArg = [&](unsigned i) -> Type {
19131913
auto arg = boundGeneric->getGenericArgs()[i];
@@ -1917,27 +1917,30 @@ TypeBase *TypeBase::reconstituteSugar(bool Recursive) {
19171917
};
19181918

19191919
if (boundGeneric->isArray())
1920-
return ArraySliceType::get(getGenericArg(0));
1920+
return Type(ArraySliceType::get(getGenericArg(0)));
19211921
if (boundGeneric->isDictionary())
1922-
return DictionaryType::get(getGenericArg(0), getGenericArg(1));
1922+
return Type(DictionaryType::get(getGenericArg(0), getGenericArg(1)));
19231923
if (boundGeneric->isOptional())
1924-
return OptionalType::get(getGenericArg(0));
1924+
return Type(OptionalType::get(getGenericArg(0)));
19251925
}
1926-
return Ty;
1926+
return std::nullopt;
19271927
};
19281928
if (Recursive)
1929-
return Type(this).transform(Func).getPointer();
1930-
else
1931-
return Func(this).getPointer();
1929+
return Type(this).transformRec(Func).getPointer();
1930+
1931+
if (auto result = Func(this))
1932+
return result->getPointer();
1933+
1934+
return this;
19321935
}
19331936

19341937
TypeBase *TypeBase::getWithoutSyntaxSugar() {
1935-
auto Func = [](Type Ty) -> Type {
1936-
if (auto *syntaxSugarType = dyn_cast<SyntaxSugarType>(Ty.getPointer()))
1938+
auto Func = [](TypeBase *Ty) -> std::optional<Type> {
1939+
if (auto *syntaxSugarType = dyn_cast<SyntaxSugarType>(Ty))
19371940
return syntaxSugarType->getSinglyDesugaredType()->getWithoutSyntaxSugar();
1938-
return Ty;
1941+
return std::nullopt;
19391942
};
1940-
return Type(this).transform(Func).getPointer();
1943+
return Type(this).transformRec(Func).getPointer();
19411944
}
19421945

19431946
#define TYPE(Id, Parent)
@@ -4156,23 +4159,6 @@ static bool transformSILParameter(
41564159
return false;
41574160
}
41584161

4159-
Type Type::transform(llvm::function_ref<Type(Type)> fn) const {
4160-
return transformWithPosition(
4161-
TypePosition::Invariant,
4162-
[fn](TypeBase *type, auto) -> std::optional<Type> {
4163-
Type transformed = fn(Type(type));
4164-
if (!transformed)
4165-
return Type();
4166-
4167-
// If the function didn't change the type at
4168-
// all, let transformRec() recurse.
4169-
if (transformed.getPointer() == type)
4170-
return std::nullopt;
4171-
4172-
return transformed;
4173-
});
4174-
}
4175-
41764162
static PackType *getTransformedPack(Type substType) {
41774163
if (auto pack = substType->getAs<PackType>()) {
41784164
return pack;

lib/IDE/CompletionLookup.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,14 @@ Type CompletionLookup::eraseArchetypes(Type type, GenericSignature genericSig) {
574574
genericFuncType->getExtInfo());
575575
}
576576

577-
return type.transform([&](Type t) -> Type {
577+
return type.transformRec([&](Type t) -> std::optional<Type> {
578578
// FIXME: Code completion should only deal with one or the other,
579579
// and not both.
580580
if (auto *archetypeType = t->getAs<ArchetypeType>()) {
581581
// Don't erase opaque archetype.
582582
if (isa<OpaqueTypeArchetypeType>(archetypeType) &&
583583
archetypeType->isRoot())
584-
return t;
584+
return std::nullopt;
585585

586586
auto genericSig = archetypeType->getGenericEnvironment()->getGenericSignature();
587587
auto upperBound = genericSig->getUpperBound(
@@ -603,7 +603,7 @@ Type CompletionLookup::eraseArchetypes(Type type, GenericSignature genericSig) {
603603
return upperBound;
604604
}
605605

606-
return t;
606+
return std::nullopt;
607607
});
608608
}
609609

@@ -2062,15 +2062,15 @@ void CompletionLookup::onLookupNominalTypeMembers(NominalTypeDecl *NTD,
20622062
}
20632063

20642064
Type CompletionLookup::normalizeTypeForDuplicationCheck(Type Ty) {
2065-
return Ty.transform([](Type T) {
2065+
return Ty.transformRec([](Type T) -> std::optional<Type> {
20662066
if (auto opaque = T->getAs<OpaqueTypeArchetypeType>()) {
20672067
/// Opaque type has a _invisible_ substitution map. Since IDE can't
20682068
/// differentiate them, replace it with empty substitution map.
2069-
return OpaqueTypeArchetypeType::get(opaque->getDecl(),
2070-
opaque->getInterfaceType(),
2071-
/*Substitutions=*/{});
2069+
return Type(OpaqueTypeArchetypeType::get(opaque->getDecl(),
2070+
opaque->getInterfaceType(),
2071+
/*Substitutions=*/{}));
20722072
}
2073-
return T;
2073+
return std::nullopt;
20742074
});
20752075
}
20762076

lib/IRGen/GenReflection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,12 +1473,12 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
14731473

14741474
// Erase pseudogeneric captures down to AnyObject.
14751475
if (OrigCalleeType->isPseudogeneric()) {
1476-
SwiftType = SwiftType.transform([&](Type t) -> Type {
1476+
SwiftType = SwiftType.transformRec([&](Type t) -> std::optional<Type> {
14771477
if (auto *archetype = t->getAs<ArchetypeType>()) {
14781478
assert(archetype->requiresClass() && "don't know what to do");
14791479
return IGM.Context.getAnyObjectType();
14801480
}
1481-
return t;
1481+
return std::nullopt;
14821482
})->getCanonicalType();
14831483
}
14841484

0 commit comments

Comments
 (0)