@@ -49,7 +49,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
49
49
50
50
SILBuilder Builder;
51
51
DominanceInfo *DomTree = nullptr ;
52
- TypeSubstitutionMap OpenedExistentialSubs ;
52
+ TypeSubstitutionMap LocalArchetypeSubs ;
53
53
54
54
// The old-to-new value map.
55
55
llvm::DenseMap<SILValue, SILValue> ValueMap;
@@ -164,10 +164,10 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
164
164
asImpl ().mapValue (origValue, mappedValue);
165
165
}
166
166
167
- // / Register a re-mapping for opened existentials.
168
- void registerOpenedExistentialRemapping (ArchetypeType *From,
169
- ArchetypeType *To) {
170
- auto result = OpenedExistentialSubs .insert (
167
+ // / Register a re-mapping for local archetypes such as opened existentials.
168
+ void registerLocalArchetypeRemapping (ArchetypeType *From,
169
+ ArchetypeType *To) {
170
+ auto result = LocalArchetypeSubs .insert (
171
171
std::make_pair (CanArchetypeType (From), CanType (To)));
172
172
assert (result.second );
173
173
(void )result;
@@ -189,15 +189,15 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
189
189
}
190
190
191
191
SubstitutionMap getOpSubstitutionMap (SubstitutionMap Subs) {
192
- // If we have open existentials to substitute, check whether that's
192
+ // If we have local archetypes to substitute, check whether that's
193
193
// relevant to this particular substitution.
194
- if (!OpenedExistentialSubs .empty ()) {
194
+ if (!LocalArchetypeSubs .empty ()) {
195
195
for (auto ty : Subs.getReplacementTypes ()) {
196
- // If we found a type containing an opened existential , substitute
196
+ // If we found a type containing a local archetype , substitute
197
197
// open existentials throughout the substitution map.
198
- if (ty->hasOpenedExistential ()) {
198
+ if (ty->hasLocalArchetype ()) {
199
199
Subs = Subs.subst (QueryTypeSubstitutionMapOrIdentity{
200
- OpenedExistentialSubs },
200
+ LocalArchetypeSubs },
201
201
MakeAbstractConformanceForGenericType ());
202
202
break ;
203
203
}
@@ -209,19 +209,19 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
209
209
210
210
SILType getTypeInClonedContext (SILType Ty) {
211
211
auto objectTy = Ty.getASTType ();
212
- // Do not substitute opened existential types , if we do not have any.
213
- if (!objectTy->hasOpenedExistential ())
212
+ // Do not substitute local archetypes , if we do not have any.
213
+ if (!objectTy->hasLocalArchetype ())
214
214
return Ty;
215
- // Do not substitute opened existential types , if it is not required.
215
+ // Do not substitute local archetypes , if it is not required.
216
216
// This is often the case when cloning basic blocks inside the same
217
217
// function.
218
- if (OpenedExistentialSubs .empty ())
218
+ if (LocalArchetypeSubs .empty ())
219
219
return Ty;
220
220
221
- // Substitute opened existential types , if we have any.
221
+ // Substitute local archetypes , if we have any.
222
222
return Ty.subst (
223
223
Builder.getModule (),
224
- QueryTypeSubstitutionMapOrIdentity{OpenedExistentialSubs },
224
+ QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs },
225
225
MakeAbstractConformanceForGenericType ());
226
226
}
227
227
SILType getOpType (SILType Ty) {
@@ -230,17 +230,17 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
230
230
}
231
231
232
232
CanType getASTTypeInClonedContext (Type ty) {
233
- // Do not substitute opened existential types , if we do not have any.
234
- if (!ty->hasOpenedExistential ())
233
+ // Do not substitute local archetypes , if we do not have any.
234
+ if (!ty->hasLocalArchetype ())
235
235
return ty->getCanonicalType ();
236
- // Do not substitute opened existential types , if it is not required.
236
+ // Do not substitute local archetypes , if it is not required.
237
237
// This is often the case when cloning basic blocks inside the same
238
238
// function.
239
- if (OpenedExistentialSubs .empty ())
239
+ if (LocalArchetypeSubs .empty ())
240
240
return ty->getCanonicalType ();
241
241
242
242
return ty.subst (
243
- QueryTypeSubstitutionMapOrIdentity{OpenedExistentialSubs },
243
+ QueryTypeSubstitutionMapOrIdentity{LocalArchetypeSubs },
244
244
MakeAbstractConformanceForGenericType ()
245
245
)->getCanonicalType ();
246
246
}
@@ -258,7 +258,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
258
258
->getCanonicalType ();
259
259
auto substExistentialTy = getOpASTType (origExistentialTy);
260
260
auto replacementTy = OpenedArchetypeType::get (substExistentialTy, sig);
261
- registerOpenedExistentialRemapping (archetypeTy, replacementTy);
261
+ registerLocalArchetypeRemapping (archetypeTy, replacementTy);
262
262
}
263
263
264
264
// SILCloner will take care of debug scope on the instruction
@@ -273,12 +273,12 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
273
273
274
274
ProtocolConformanceRef getOpConformance (Type ty,
275
275
ProtocolConformanceRef conformance) {
276
- // If we have open existentials to substitute, do so now.
277
- if (ty->hasOpenedExistential () && !OpenedExistentialSubs .empty ()) {
276
+ // If we have local archetypes to substitute, do so now.
277
+ if (ty->hasLocalArchetype () && !LocalArchetypeSubs .empty ()) {
278
278
conformance =
279
279
conformance.subst (ty,
280
280
QueryTypeSubstitutionMapOrIdentity{
281
- OpenedExistentialSubs },
281
+ LocalArchetypeSubs },
282
282
MakeAbstractConformanceForGenericType ());
283
283
}
284
284
@@ -2392,6 +2392,52 @@ void SILCloner<ImplClass>::visitDeinitExistentialValueInst(
2392
2392
getOpLocation (Inst->getLoc ()), getOpValue (Inst->getOperand ())));
2393
2393
}
2394
2394
2395
+ template <typename ImplClass>
2396
+ void SILCloner<ImplClass>::visitOpenPackElementInst(
2397
+ OpenPackElementInst *Inst) {
2398
+ getBuilder ().setCurrentDebugScope (getOpScope (Inst->getDebugScope ()));
2399
+
2400
+ auto newIndexValue = getOpValue (Inst->getIndexOperand ());
2401
+ auto loc = getOpLocation (Inst->getLoc ());
2402
+
2403
+ // We need to make a new opened-element environment. This is *not*
2404
+ // a refinement of the contextual environment of the new insertion
2405
+ // site; we just substitute the contextual substitutions in the
2406
+ // opened environment and build a new one.
2407
+ auto origEnv = Inst->getOpenedGenericEnvironment ();
2408
+
2409
+ // Substitute the contextual substitutions.
2410
+ auto newContextSubs =
2411
+ getOpSubstitutionMap (origEnv->getPackElementContextSubstitutions ());
2412
+
2413
+ // Substitute the shape class.
2414
+ auto newShapeClass = getOpASTType (origEnv->getOpenedElementShapeClass ());
2415
+
2416
+ // Build the new environment.
2417
+ auto newEnv =
2418
+ GenericEnvironment::forOpenedElement (origEnv->getGenericSignature (),
2419
+ UUID::fromTime (), newShapeClass,
2420
+ newContextSubs);
2421
+
2422
+ // Associate the old opened archetypes with the new ones.
2423
+ SmallVector<ArchetypeType*, 4 > oldOpenedArchetypes;
2424
+ origEnv->forEachPackElementArchetype ([&](ElementArchetypeType *oldType) {
2425
+ oldOpenedArchetypes.push_back (oldType);
2426
+ });
2427
+ {
2428
+ size_t nextOldIndex = 0 ;
2429
+ newEnv->forEachPackElementArchetype ([&](ElementArchetypeType *newType) {
2430
+ ArchetypeType *oldType = oldOpenedArchetypes[nextOldIndex++];
2431
+ registerLocalArchetypeRemapping (oldType, newType);
2432
+ });
2433
+ assert (nextOldIndex == oldOpenedArchetypes.size () &&
2434
+ " different opened archetype count" );
2435
+ }
2436
+
2437
+ recordClonedInstruction (
2438
+ Inst, getBuilder ().createOpenPackElement (loc, newIndexValue, newEnv));
2439
+ }
2440
+
2395
2441
template <typename ImplClass>
2396
2442
void
2397
2443
SILCloner<ImplClass>::visitCopyBlockInst(CopyBlockInst *Inst) {
0 commit comments