29
29
30
30
namespace swift {
31
31
32
+ struct SubstitutionMapWithLocalArchetypes {
33
+ std::optional<SubstitutionMap> SubsMap;
34
+ TypeSubstitutionMap LocalArchetypeSubs;
35
+
36
+ SubstitutionMapWithLocalArchetypes () {}
37
+ SubstitutionMapWithLocalArchetypes (SubstitutionMap subs) : SubsMap(subs) {}
38
+
39
+ Type operator ()(SubstitutableType *type) {
40
+ if (isa<LocalArchetypeType>(type))
41
+ return QueryTypeSubstitutionMap{LocalArchetypeSubs}(type);
42
+
43
+ if (SubsMap)
44
+ return Type (type).subst (*SubsMap);
45
+
46
+ return Type (type);
47
+ }
48
+
49
+ ProtocolConformanceRef operator ()(CanType origType,
50
+ Type substType,
51
+ ProtocolDecl *proto) {
52
+ if (isa<LocalArchetypeType>(origType))
53
+ return proto->getParentModule ()->lookupConformance (substType, proto);
54
+ if (SubsMap)
55
+ return SubsMap->lookupConformance (origType, proto);
56
+
57
+ return ProtocolConformanceRef (proto);
58
+ }
59
+ };
60
+
32
61
// / SILCloner - Abstract SIL visitor which knows how to clone instructions and
33
62
// / whose behavior can be customized by subclasses via the CRTP. This is meant
34
63
// / to be subclassed to implement inlining, function specialization, and other
@@ -49,7 +78,8 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
49
78
50
79
SILBuilder Builder;
51
80
DominanceInfo *DomTree = nullptr ;
52
- TypeSubstitutionMap LocalArchetypeSubs;
81
+ SubstitutionMapWithLocalArchetypes Functor;
82
+ TypeSubstitutionMap &LocalArchetypeSubs;
53
83
54
84
// The old-to-new value map.
55
85
llvm::DenseMap<SILValue, SILValue> ValueMap;
@@ -76,9 +106,10 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
76
106
using SILInstructionVisitor<ImplClass>::asImpl;
77
107
78
108
explicit SILCloner (SILFunction &F, DominanceInfo *DT = nullptr )
79
- : Builder(F), DomTree(DT) {}
109
+ : Builder(F), DomTree(DT), LocalArchetypeSubs(Functor.LocalArchetypeSubs) {}
80
110
81
- explicit SILCloner (SILGlobalVariable *GlobVar) : Builder(GlobVar) {}
111
+ explicit SILCloner (SILGlobalVariable *GlobVar)
112
+ : Builder(GlobVar), LocalArchetypeSubs(Functor.LocalArchetypeSubs) {}
82
113
83
114
void clearClonerState () {
84
115
ValueMap.clear ();
@@ -189,12 +220,6 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
189
220
}
190
221
191
222
SubstitutionMap getOpSubstitutionMap (SubstitutionMap Subs) {
192
- // If we have local archetypes to substitute, do so now.
193
- if (Subs.hasLocalArchetypes () && !LocalArchetypeSubs.empty ()) {
194
- Subs = Subs.subst (QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs},
195
- MakeAbstractConformanceForGenericType ());
196
- }
197
-
198
223
return asImpl ().remapSubstitutionMap (Subs)
199
224
.getCanonical (/* canonicalizeSignature*/ false );
200
225
}
@@ -441,7 +466,13 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
441
466
SILBasicBlock *remapBasicBlock (SILBasicBlock *BB);
442
467
void postProcess (SILInstruction *Orig, SILInstruction *Cloned);
443
468
444
- SubstitutionMap remapSubstitutionMap (SubstitutionMap Subs) { return Subs; }
469
+ SubstitutionMap remapSubstitutionMap (SubstitutionMap Subs) {
470
+ // If we have local archetypes to substitute, do so now.
471
+ if (Subs.hasLocalArchetypes ())
472
+ Subs = Subs.subst (Functor, Functor);
473
+
474
+ return Subs;
475
+ }
445
476
446
477
// / This is called by either of the top-level visitors, cloneReachableBlocks
447
478
// / or cloneSILFunction, after all other visitors are have been called.
0 commit comments