Skip to content

Commit 32c3863

Browse files
authored
Merge pull request #16740 from slavapestov/simpler-substitution-map
Small SubstitutionMap cleanups
2 parents 19a7fff + bd6281c commit 32c3863

37 files changed

+130
-145
lines changed

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ class GenericSignatureBuilder {
508508
Optional<ProtocolConformanceRef>
509509
operator()(CanType dependentType,
510510
Type conformingReplacementType,
511-
ProtocolType *conformedProtocol) const {
511+
ProtocolDecl *conformedProtocol) const {
512512
return builder->lookupConformance(dependentType,
513513
conformingReplacementType,
514514
conformedProtocol);
@@ -522,7 +522,7 @@ class GenericSignatureBuilder {
522522
/// Lookup a protocol conformance in a module-agnostic manner.
523523
Optional<ProtocolConformanceRef>
524524
lookupConformance(CanType dependentType, Type conformingReplacementType,
525-
ProtocolType *conformedProtocol);
525+
ProtocolDecl *conformedProtocol);
526526

527527

528528
/// Retrieve the lazy resolver, if there is one.

include/swift/AST/RequirementEnvironment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class RequirementEnvironment {
114114

115115
/// Retrieve the substitution map that maps the interface types of the
116116
/// requirement to the interface types of the synthetic environment.
117-
const SubstitutionMap &getRequirementToSyntheticMap() const {
117+
SubstitutionMap getRequirementToSyntheticMap() const {
118118
return reqToSyntheticEnvMap;
119119
}
120120
};

include/swift/AST/SubstitutionMap.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class SubstitutionMap {
164164

165165
/// Apply a substitution to all replacement types in the map. Does not
166166
/// change keys.
167-
SubstitutionMap subst(const SubstitutionMap &subMap) const;
167+
SubstitutionMap subst(SubstitutionMap subMap) const;
168168

169169
/// Apply a substitution to all replacement types in the map. Does not
170170
/// change keys.
@@ -208,8 +208,8 @@ class SubstitutionMap {
208208
///
209209
/// The 'how' parameter determines if we're looking at the depth or index.
210210
static SubstitutionMap
211-
combineSubstitutionMaps(const SubstitutionMap &firstSubMap,
212-
const SubstitutionMap &secondSubMap,
211+
combineSubstitutionMaps(SubstitutionMap firstSubMap,
212+
SubstitutionMap secondSubMap,
213213
CombineSubstitutionMaps how,
214214
unsigned baseDepthOrIndex,
215215
unsigned origDepthOrIndex,
@@ -270,6 +270,28 @@ class SubstitutionMap {
270270
Type lookupSubstitution(CanSubstitutableType type) const;
271271
};
272272

273+
/// A function object suitable for use as a \c TypeSubstitutionFn that
274+
/// queries an underlying \c SubstitutionMap.
275+
struct QuerySubstitutionMap {
276+
SubstitutionMap subMap;
277+
278+
Type operator()(SubstitutableType *type) const;
279+
};
280+
281+
/// Functor class suitable for use as a \c LookupConformanceFn to look up a
282+
/// conformance in a \c SubstitutionMap.
283+
class LookUpConformanceInSubstitutionMap {
284+
SubstitutionMap Subs;
285+
public:
286+
explicit LookUpConformanceInSubstitutionMap(SubstitutionMap Subs)
287+
: Subs(Subs) {}
288+
289+
Optional<ProtocolConformanceRef>
290+
operator()(CanType dependentType,
291+
Type conformingReplacementType,
292+
ProtocolDecl *conformedProtocol) const;
293+
};
294+
273295
} // end namespace swift
274296

275297
namespace llvm {

include/swift/AST/Type.h

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,11 @@ struct QueryTypeSubstitutionMapOrIdentity {
8888
Type operator()(SubstitutableType *type) const;
8989
};
9090

91-
/// A function object suitable for use as a \c TypeSubstitutionFn that
92-
/// queries an underlying \c SubstitutionMap.
93-
struct QuerySubstitutionMap {
94-
const SubstitutionMap &subMap;
95-
96-
Type operator()(SubstitutableType *type) const;
97-
};
98-
9991
/// Function used to resolve conformances.
10092
using GenericFunction = auto(CanType dependentType,
101-
Type conformingReplacementType,
102-
ProtocolType *conformedProtocol)
103-
->Optional<ProtocolConformanceRef>;
93+
Type conformingReplacementType,
94+
ProtocolDecl *conformedProtocol)
95+
-> Optional<ProtocolConformanceRef>;
10496
using LookupConformanceFn = llvm::function_ref<GenericFunction>;
10597

10698
/// Functor class suitable for use as a \c LookupConformanceFn to look up a
@@ -114,21 +106,7 @@ class LookUpConformanceInModule {
114106
Optional<ProtocolConformanceRef>
115107
operator()(CanType dependentType,
116108
Type conformingReplacementType,
117-
ProtocolType *conformedProtocol) const;
118-
};
119-
120-
/// Functor class suitable for use as a \c LookupConformanceFn to look up a
121-
/// conformance in a \c SubstitutionMap.
122-
class LookUpConformanceInSubstitutionMap {
123-
const SubstitutionMap &Subs;
124-
public:
125-
explicit LookUpConformanceInSubstitutionMap(const SubstitutionMap &Subs)
126-
: Subs(Subs) {}
127-
128-
Optional<ProtocolConformanceRef>
129-
operator()(CanType dependentType,
130-
Type conformingReplacementType,
131-
ProtocolType *conformedProtocol) const;
109+
ProtocolDecl *conformedProtocol) const;
132110
};
133111

134112
/// Functor class suitable for use as a \c LookupConformanceFn that provides
@@ -139,7 +117,7 @@ class MakeAbstractConformanceForGenericType {
139117
Optional<ProtocolConformanceRef>
140118
operator()(CanType dependentType,
141119
Type conformingReplacementType,
142-
ProtocolType *conformedProtocol) const;
120+
ProtocolDecl *conformedProtocol) const;
143121
};
144122

145123
/// Functor class suitable for use as a \c LookupConformanceFn that fetches
@@ -153,7 +131,7 @@ class LookUpConformanceInSignature {
153131
Optional<ProtocolConformanceRef>
154132
operator()(CanType dependentType,
155133
Type conformingReplacementType,
156-
ProtocolType *conformedProtocol) const;
134+
ProtocolDecl *conformedProtocol) const;
157135
};
158136

159137
/// Flags that can be passed when substituting into a type.
@@ -314,7 +292,7 @@ class Type {
314292
/// \param options Options that affect the substitutions.
315293
///
316294
/// \returns the substituted type, or a null type if an error occurred.
317-
Type subst(const SubstitutionMap &substitutions,
295+
Type subst(SubstitutionMap substitutions,
318296
SubstOptions options = None) const;
319297

320298
/// Replace references to substitutable types with new, concrete types and

include/swift/AST/Types.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ class NameAliasType final
16051605
friend TrailingObjects;
16061606

16071607
NameAliasType(TypeAliasDecl *typealias, Type parent,
1608-
const SubstitutionMap &substitutions, Type underlying,
1608+
SubstitutionMap substitutions, Type underlying,
16091609
RecursiveTypeProperties properties);
16101610

16111611
size_t numTrailingObjects(OverloadToken<Type>) const {
@@ -1623,7 +1623,7 @@ class NameAliasType final
16231623

16241624
public:
16251625
static NameAliasType *get(TypeAliasDecl *typealias, Type parent,
1626-
const SubstitutionMap &substitutions,
1626+
SubstitutionMap substitutions,
16271627
Type underlying);
16281628

16291629
/// \brief Returns the declaration that declares this type.
@@ -1660,7 +1660,7 @@ class NameAliasType final
16601660
void Profile(llvm::FoldingSetNodeID &id) const;
16611661

16621662
static void Profile(llvm::FoldingSetNodeID &id, TypeAliasDecl *typealias,
1663-
Type parent, const SubstitutionMap &substitutions,
1663+
Type parent, SubstitutionMap substitutions,
16641664
Type underlying);
16651665

16661666
// Implement isa/cast/dyncast/etc.
@@ -3057,7 +3057,7 @@ class GenericFunctionType final : public AnyFunctionType,
30573057

30583058
/// Substitute the given generic arguments into this generic
30593059
/// function type and return the resulting non-generic type.
3060-
FunctionType *substGenericArgs(const SubstitutionMap &subs);
3060+
FunctionType *substGenericArgs(SubstitutionMap subs);
30613061

30623062
/// Substitute the given generic arguments into this generic
30633063
/// function type using the given substitution and conformance lookup
@@ -4033,7 +4033,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
40334033
isABICompatibleWith(CanSILFunctionType other) const;
40344034

40354035
CanSILFunctionType substGenericArgs(SILModule &silModule,
4036-
const SubstitutionMap &subs);
4036+
SubstitutionMap subs);
40374037
CanSILFunctionType substGenericArgs(SILModule &silModule,
40384038
TypeSubstitutionFn subs,
40394039
LookupConformanceFn conformances);

include/swift/SIL/SILType.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ class SILType {
439439
///
440440
/// Only call this with function types!
441441
SILType substGenericArgs(SILModule &M,
442-
const SubstitutionMap &SubMap) const;
442+
SubstitutionMap SubMap) const;
443443

444444
/// If the original type is generic, pass the signature as genericSig.
445445
///
@@ -450,7 +450,7 @@ class SILType {
450450
LookupConformanceFn conformances,
451451
CanGenericSignature genericSig=CanGenericSignature()) const;
452452

453-
SILType subst(SILModule &silModule, const SubstitutionMap &subs) const;
453+
SILType subst(SILModule &silModule, SubstitutionMap subs) const;
454454

455455
/// Return true if this type references a "ref" type that has a single pointer
456456
/// representation. Class existentials do not always qualify.

include/swift/SILOptimizer/Utils/Generics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class ReabstractionInfo {
114114

115115
// Create a new substituted type with the updated signature.
116116
CanSILFunctionType createSubstitutedType(SILFunction *OrigF,
117-
const SubstitutionMap &SubstMap,
117+
SubstitutionMap SubstMap,
118118
bool HasUnboundGenericParams);
119119

120120
void createSubstitutedAndSpecializedTypes();

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,7 +2874,7 @@ StringRef ASTContext::getSwiftName(KnownFoundationEntity kind) {
28742874
//===----------------------------------------------------------------------===//
28752875

28762876
NameAliasType::NameAliasType(TypeAliasDecl *typealias, Type parent,
2877-
const SubstitutionMap &substitutions,
2877+
SubstitutionMap substitutions,
28782878
Type underlying,
28792879
RecursiveTypeProperties properties)
28802880
: SugarType(TypeKind::NameAlias, underlying, properties),
@@ -2897,7 +2897,7 @@ NameAliasType::NameAliasType(TypeAliasDecl *typealias, Type parent,
28972897
}
28982898

28992899
NameAliasType *NameAliasType::get(TypeAliasDecl *typealias, Type parent,
2900-
const SubstitutionMap &substitutions,
2900+
SubstitutionMap substitutions,
29012901
Type underlying) {
29022902
// Compute the recursive properties.
29032903
//
@@ -2950,7 +2950,7 @@ void NameAliasType::Profile(llvm::FoldingSetNodeID &id) const {
29502950
void NameAliasType::Profile(
29512951
llvm::FoldingSetNodeID &id,
29522952
TypeAliasDecl *typealias,
2953-
Type parent, const SubstitutionMap &substitutions,
2953+
Type parent, SubstitutionMap substitutions,
29542954
Type underlying) {
29552955
id.AddPointer(typealias);
29562956
id.AddPointer(parent.getPointer());

lib/AST/GenericSignature.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ bool GenericSignature::isRequirementSatisfied(Requirement requirement) {
556556
return conformsToProtocol(canFirstType, protocol);
557557
else
558558
return (bool)GSB->lookupConformance(/*dependentType=*/CanType(),
559-
canFirstType, protocolType);
559+
canFirstType, protocol);
560560
}
561561

562562
case RequirementKind::SameType: {

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,9 +2639,7 @@ GenericSignatureBuilder::resolveConcreteConformance(ResolvedType type,
26392639
// Lookup the conformance of the concrete type to this protocol.
26402640
auto conformance =
26412641
lookupConformance(type.getDependentType(*this)->getCanonicalType(),
2642-
concrete,
2643-
proto->getDeclaredInterfaceType()
2644-
->castTo<ProtocolType>());
2642+
concrete, proto);
26452643
if (!conformance) {
26462644
if (!concrete->hasError() && concreteSource->getLoc().isValid()) {
26472645
Impl->HadAnyError = true;
@@ -2672,9 +2670,7 @@ const RequirementSource *GenericSignatureBuilder::resolveSuperConformance(
26722670
// Lookup the conformance of the superclass to this protocol.
26732671
auto conformance =
26742672
lookupConformance(type.getDependentType(*this)->getCanonicalType(),
2675-
superclass,
2676-
proto->getDeclaredInterfaceType()
2677-
->castTo<ProtocolType>());
2673+
superclass, proto);
26782674
if (!conformance) return nullptr;
26792675

26802676
// Conformance to this protocol is redundant; update the requirement source
@@ -3825,16 +3821,16 @@ GenericSignatureBuilder::getLookupConformanceFn()
38253821
Optional<ProtocolConformanceRef>
38263822
GenericSignatureBuilder::lookupConformance(CanType dependentType,
38273823
Type conformingReplacementType,
3828-
ProtocolType *conformedProtocol) {
3824+
ProtocolDecl *conformedProtocol) {
38293825
if (conformingReplacementType->isTypeParameter())
3830-
return ProtocolConformanceRef(conformedProtocol->getDecl());
3826+
return ProtocolConformanceRef(conformedProtocol);
38313827

38323828
// Figure out which module to look into.
38333829
// FIXME: When lookupConformance() starts respecting modules, we'll need
38343830
// to do some filtering here.
3835-
ModuleDecl *searchModule = conformedProtocol->getDecl()->getParentModule();
3831+
ModuleDecl *searchModule = conformedProtocol->getParentModule();
38363832
auto result = searchModule->lookupConformance(conformingReplacementType,
3837-
conformedProtocol->getDecl());
3833+
conformedProtocol);
38383834
if (result && getLazyResolver())
38393835
getLazyResolver()->markConformanceUsed(*result, searchModule);
38403836

@@ -4726,7 +4722,7 @@ ConstraintResult GenericSignatureBuilder::addTypeRequirement(
47264722
// getLookupConformanceFns used in here don't use that parameter anyway.
47274723
auto dependentType = CanType();
47284724
auto conformance =
4729-
getLookupConformanceFn()(dependentType, subjectType, proto);
4725+
getLookupConformanceFn()(dependentType, subjectType, proto->getDecl());
47304726

47314727
// FIXME: diagnose if there's no conformance.
47324728
if (conformance) {

lib/AST/ProtocolConformance.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ ProtocolConformanceRef::subst(Type origType,
118118

119119
// Check the conformance map.
120120
if (auto result = conformances(origType->getCanonicalType(),
121-
substType,
122-
proto->getDeclaredType())) {
121+
substType, proto)) {
123122
return *result;
124123
}
125124

lib/AST/RequirementEnvironment.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,8 @@ RequirementEnvironment::RequirementEnvironment(
110110
return substGenericParam;
111111
},
112112
[selfType, substConcreteType, conformance, conformanceDC, &ctx](
113-
CanType type, Type replacement, ProtocolType *protoType)
113+
CanType type, Type replacement, ProtocolDecl *proto)
114114
-> Optional<ProtocolConformanceRef> {
115-
auto proto = protoType->getDecl();
116-
117115
// The protocol 'Self' conforms concretely to the conforming type.
118116
if (type->isEqual(selfType)) {
119117
ProtocolConformance *specialized = conformance;

0 commit comments

Comments
 (0)