2929
3030namespace swift {
3131
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+
3261// / SILCloner - Abstract SIL visitor which knows how to clone instructions and
3362// / whose behavior can be customized by subclasses via the CRTP. This is meant
3463// / to be subclassed to implement inlining, function specialization, and other
@@ -49,7 +78,8 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
4978
5079 SILBuilder Builder;
5180 DominanceInfo *DomTree = nullptr ;
52- TypeSubstitutionMap LocalArchetypeSubs;
81+ SubstitutionMapWithLocalArchetypes Functor;
82+ TypeSubstitutionMap &LocalArchetypeSubs;
5383
5484 // The old-to-new value map.
5585 llvm::DenseMap<SILValue, SILValue> ValueMap;
@@ -76,9 +106,10 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
76106 using SILInstructionVisitor<ImplClass>::asImpl;
77107
78108 explicit SILCloner (SILFunction &F, DominanceInfo *DT = nullptr )
79- : Builder(F), DomTree(DT) {}
109+ : Builder(F), DomTree(DT), LocalArchetypeSubs(Functor.LocalArchetypeSubs) {}
80110
81- explicit SILCloner (SILGlobalVariable *GlobVar) : Builder(GlobVar) {}
111+ explicit SILCloner (SILGlobalVariable *GlobVar)
112+ : Builder(GlobVar), LocalArchetypeSubs(Functor.LocalArchetypeSubs) {}
82113
83114 void clearClonerState () {
84115 ValueMap.clear ();
@@ -189,12 +220,6 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
189220 }
190221
191222 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-
198223 return asImpl ().remapSubstitutionMap (Subs)
199224 .getCanonical (/* canonicalizeSignature*/ false );
200225 }
@@ -441,7 +466,13 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
441466 SILBasicBlock *remapBasicBlock (SILBasicBlock *BB);
442467 void postProcess (SILInstruction *Orig, SILInstruction *Cloned);
443468
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+ }
445476
446477 // / This is called by either of the top-level visitors, cloneReachableBlocks
447478 // / or cloneSILFunction, after all other visitors are have been called.
0 commit comments