@@ -449,29 +449,90 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
449449 SILLocation remapLocation (SILLocation Loc) { return Loc; }
450450 const SILDebugScope *remapScope (const SILDebugScope *DS) { return DS; }
451451
452+ bool shouldSubstOpaqueArchetypes () const { return false ; }
453+
452454 SILType remapType (SILType Ty) {
453455 if (Functor.SubsMap || Ty.hasLocalArchetype ()) {
454456 Ty = Ty.subst (Builder.getModule (), Functor, Functor,
455457 CanGenericSignature ());
456458 }
457459
460+ if (asImpl ().shouldSubstOpaqueArchetypes ()) {
461+ auto context = getBuilder ().getTypeExpansionContext ();
462+
463+ if (!Ty.hasOpaqueArchetype () ||
464+ !context.shouldLookThroughOpaqueTypeArchetypes ())
465+ return Ty;
466+
467+ // Remap types containing opaque result types in the current context.
468+ Ty = getBuilder ().getTypeLowering (Ty).getLoweredType ().getCategoryType (
469+ Ty.getCategory ());
470+ }
471+
458472 return Ty;
459473 }
460474
461475 CanType remapASTType (CanType ty) {
462476 if (Functor.SubsMap || ty->hasLocalArchetype ())
463477 ty = ty.subst (Functor, Functor)->getCanonicalType ();
464478
479+ if (asImpl ().shouldSubstOpaqueArchetypes ()) {
480+ auto context = getBuilder ().getTypeExpansionContext ();
481+
482+ if (!ty->hasOpaqueArchetype () ||
483+ !context.shouldLookThroughOpaqueTypeArchetypes ())
484+ return ty;
485+
486+ // Remap types containing opaque result types in the current context.
487+ return substOpaqueTypesWithUnderlyingTypes (ty, context,
488+ /* allowLoweredTypes=*/ false );
489+ }
490+
465491 return ty;
466492 }
467493
468494 ProtocolConformanceRef remapConformance (Type Ty, ProtocolConformanceRef C) {
469- if (Functor.SubsMap || Ty->hasLocalArchetype ())
495+ if (Functor.SubsMap || Ty->hasLocalArchetype ()) {
470496 C = C.subst (Ty, Functor, Functor);
497+ if (asImpl ().shouldSubstOpaqueArchetypes ())
498+ Ty = Ty.subst (Functor, Functor);
499+ }
500+
501+ if (asImpl ().shouldSubstOpaqueArchetypes ()) {
502+ auto context = getBuilder ().getTypeExpansionContext ();
503+
504+ if (!Ty->hasOpaqueArchetype () ||
505+ !context.shouldLookThroughOpaqueTypeArchetypes ())
506+ return C;
507+
508+ return substOpaqueTypesWithUnderlyingTypes (C, Ty, context);
509+ }
471510
472511 return C;
473512 }
474513
514+ SubstitutionMap remapSubstitutionMap (SubstitutionMap Subs) {
515+ // If we have local archetypes to substitute, do so now.
516+ if (Subs.hasLocalArchetypes () || Functor.SubsMap )
517+ Subs = Subs.subst (Functor, Functor);
518+
519+ if (asImpl ().shouldSubstOpaqueArchetypes ()) {
520+ auto context = getBuilder ().getTypeExpansionContext ();
521+
522+ if (!Subs.hasOpaqueArchetypes () ||
523+ !context.shouldLookThroughOpaqueTypeArchetypes ())
524+ return Subs;
525+
526+ ReplaceOpaqueTypesWithUnderlyingTypes replacer (
527+ context.getContext (), context.getResilienceExpansion (),
528+ context.isWholeModuleContext ());
529+ return Subs.subst (replacer, replacer,
530+ SubstFlags::SubstituteOpaqueArchetypes);
531+ }
532+
533+ return Subs;
534+ }
535+
475536 // / Get the value that takes the place of the given `Value` within the cloned
476537 // / region. The given value must already have been mapped by this cloner.
477538 SILValue getMappedValue (SILValue Value);
@@ -481,14 +542,6 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
481542 SILBasicBlock *remapBasicBlock (SILBasicBlock *BB);
482543 void postProcess (SILInstruction *Orig, SILInstruction *Cloned);
483544
484- SubstitutionMap remapSubstitutionMap (SubstitutionMap Subs) {
485- // If we have local archetypes to substitute, do so now.
486- if (Subs.hasLocalArchetypes () || Functor.SubsMap )
487- Subs = Subs.subst (Functor, Functor);
488-
489- return Subs;
490- }
491-
492545 // / This is called by either of the top-level visitors, cloneReachableBlocks
493546 // / or cloneSILFunction, after all other visitors are have been called.
494547
0 commit comments