Skip to content

Commit d3c2875

Browse files
authored
Merge pull request #6315 from jckarter/subst-generic-args
Provide callback-based variants of *FunctionType::substGenericArgs.
2 parents d4baacf + 322cf07 commit d3c2875

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

include/swift/AST/Types.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,6 +2580,12 @@ class GenericFunctionType : public AnyFunctionType,
25802580
///
25812581
/// The order of Substitutions must match the order of generic parameters.
25822582
FunctionType *substGenericArgs(ArrayRef<Substitution> subs);
2583+
2584+
/// Substitute the given generic arguments into this generic
2585+
/// function type using the given substitution and conformance lookup
2586+
/// callbacks.
2587+
FunctionType *substGenericArgs(TypeSubstitutionFn subs,
2588+
LookupConformanceFn conformances);
25832589

25842590
void Profile(llvm::FoldingSetNodeID &ID) {
25852591
Profile(ID, getGenericSignature(), getInput(), getResult(),
@@ -3295,6 +3301,9 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
32953301

32963302
CanSILFunctionType substGenericArgs(SILModule &silModule,
32973303
ArrayRef<Substitution> subs);
3304+
CanSILFunctionType substGenericArgs(SILModule &silModule,
3305+
TypeSubstitutionFn subs,
3306+
LookupConformanceFn conformances);
32983307

32993308
void Profile(llvm::FoldingSetNodeID &ID) {
33003309
Profile(ID, getGenericSignature(), getExtInfo(), getCalleeConvention(),

lib/AST/Type.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,16 +2765,21 @@ Type ProtocolCompositionType::get(const ASTContext &C,
27652765

27662766
FunctionType *
27672767
GenericFunctionType::substGenericArgs(ArrayRef<Substitution> args) {
2768-
auto params = getGenericParams();
2769-
(void)params;
2770-
27712768
auto subs = getGenericSignature()->getSubstitutionMap(args);
27722769

27732770
Type input = getInput().subst(subs);
27742771
Type result = getResult().subst(subs);
27752772
return FunctionType::get(input, result, getExtInfo());
27762773
}
27772774

2775+
FunctionType *
2776+
GenericFunctionType::substGenericArgs(TypeSubstitutionFn subs,
2777+
LookupConformanceFn conformances) {
2778+
Type input = getInput().subst(subs, conformances);
2779+
Type result = getResult().subst(subs, conformances);
2780+
return FunctionType::get(input, result, getExtInfo());
2781+
}
2782+
27782783
static Type getMemberForBaseType(LookupConformanceFn lookupConformances,
27792784
Type origBase,
27802785
Type substBase,

lib/SIL/SILFunctionType.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,13 +2097,29 @@ namespace {
20972097
class SILTypeSubstituter :
20982098
public CanTypeVisitor<SILTypeSubstituter, CanType> {
20992099
SILModule &TheSILModule;
2100-
const SubstitutionMap &Subs;
2100+
// Order dependency - Context must initialize before Subst and Conformances
2101+
Optional<std::pair<QueryTypeSubstitutionMap,
2102+
LookUpConformanceInSubstitutionMap>> Context;
2103+
TypeSubstitutionFn Subst;
2104+
LookupConformanceFn Conformances;
21012105

21022106
ASTContext &getASTContext() { return TheSILModule.getASTContext(); }
21032107

21042108
public:
21052109
SILTypeSubstituter(SILModule &silModule, const SubstitutionMap &subs)
2106-
: TheSILModule(silModule), Subs(subs)
2110+
: TheSILModule(silModule),
2111+
Context({QueryTypeSubstitutionMap{subs.getMap()},
2112+
LookUpConformanceInSubstitutionMap(subs)}),
2113+
Subst(Context->first),
2114+
Conformances(Context->second)
2115+
{}
2116+
2117+
SILTypeSubstituter(SILModule &silModule,
2118+
TypeSubstitutionFn Subst,
2119+
LookupConformanceFn Conformances)
2120+
: TheSILModule(silModule),
2121+
Subst(Subst),
2122+
Conformances(Conformances)
21072123
{}
21082124

21092125
// SIL type lowering only does special things to tuples and functions.
@@ -2242,7 +2258,7 @@ namespace {
22422258
.getSwiftRValueType() == origType);
22432259

22442260
CanType substType =
2245-
origType.subst(Subs, None)->getCanonicalType();
2261+
origType.subst(Subst, Conformances, None)->getCanonicalType();
22462262

22472263
// If the substitution didn't change anything, we know that the
22482264
// original type was a lowered type, so we're good.
@@ -2288,6 +2304,16 @@ SILFunctionType::substGenericArgs(SILModule &silModule,
22882304
/*dropGenerics*/ true);
22892305
}
22902306

2307+
CanSILFunctionType
2308+
SILFunctionType::substGenericArgs(SILModule &silModule,
2309+
TypeSubstitutionFn subs,
2310+
LookupConformanceFn conformances) {
2311+
if (!isPolymorphic()) return CanSILFunctionType(this);
2312+
SILTypeSubstituter substituter(silModule, subs, conformances);
2313+
return substituter.visitSILFunctionType(CanSILFunctionType(this),
2314+
/*dropGenerics*/ true);
2315+
}
2316+
22912317
/// Fast path for bridging types in a function type without uncurrying.
22922318
CanAnyFunctionType
22932319
TypeConverter::getBridgedFunctionType(AbstractionPattern pattern,

0 commit comments

Comments
 (0)