3636#include " swift/AST/IndexSubset.h"
3737#include " swift/AST/KnownProtocols.h"
3838#include " swift/AST/LazyResolver.h"
39+ #include " swift/AST/LocalArchetypeRequirementCollector.h"
3940#include " swift/AST/MacroDiscriminatorContext.h"
4041#include " swift/AST/ModuleDependencies.h"
4142#include " swift/AST/ModuleLoader.h"
@@ -5884,11 +5885,8 @@ CanGenericSignature ASTContext::getSingleGenericParameterSignature() const {
58845885
58855886Type OpenedArchetypeType::getSelfInterfaceTypeFromContext (GenericSignature parentSig,
58865887 ASTContext &ctx) {
5887- unsigned depth = 0 ;
5888- if (!parentSig.getGenericParams ().empty ())
5889- depth = parentSig.getGenericParams ().back ()->getDepth () + 1 ;
58905888 return GenericTypeParamType::get (/* isParameterPack=*/ false ,
5891- /* depth= */ depth , /* index=*/ 0 ,
5889+ parentSig. getNextDepth () , /* index=*/ 0 ,
58925890 ctx);
58935891}
58945892
@@ -5900,33 +5898,25 @@ ASTContext::getOpenedExistentialSignature(Type type, GenericSignature parentSig)
59005898 type = existential->getConstraintType ();
59015899
59025900 const CanType constraint = type->getCanonicalType ();
5903- assert (parentSig || !constraint->hasTypeParameter () &&
5904- " Interface type here requires a parent signature" );
59055901
59065902 auto canParentSig = parentSig.getCanonicalSignature ();
59075903 auto key = std::make_pair (constraint, canParentSig.getPointer ());
59085904 auto found = getImpl ().ExistentialSignatures .find (key);
59095905 if (found != getImpl ().ExistentialSignatures .end ())
59105906 return found->second ;
59115907
5912- auto genericParam = OpenedArchetypeType::getSelfInterfaceTypeFromContext (
5913- canParentSig, *this )
5914- ->castTo <GenericTypeParamType>();
5915- Requirement requirement (RequirementKind::Conformance, genericParam,
5916- constraint);
5908+ LocalArchetypeRequirementCollector collector (*this , canParentSig);
5909+ collector.addOpenedExistential (type);
59175910 auto genericSig = buildGenericSignature (
5918- *this , canParentSig,
5919- {genericParam}, {requirement},
5920- /* allowInverses=*/ true );
5921-
5922- CanGenericSignature canGenericSig (genericSig);
5911+ *this , collector.OuterSig , collector.Params , collector.Requirements ,
5912+ /* allowInverses=*/ true ).getCanonicalSignature ();
59235913
59245914 auto result = getImpl ().ExistentialSignatures .insert (
5925- std::make_pair (key, canGenericSig ));
5915+ std::make_pair (key, genericSig ));
59265916 assert (result.second );
59275917 (void ) result;
59285918
5929- return canGenericSig ;
5919+ return genericSig ;
59305920}
59315921
59325922CanGenericSignature
@@ -5938,100 +5928,12 @@ ASTContext::getOpenedElementSignature(CanGenericSignature baseGenericSig,
59385928 if (found != sigs.end ())
59395929 return found->second ;
59405930
5941- // This operation doesn't make sense if the input signature does not contain`
5942- // any pack generic parameters.
5943- #ifndef NDEBUG
5944- {
5945- auto found = std::find_if (baseGenericSig.getGenericParams ().begin (),
5946- baseGenericSig.getGenericParams ().end (),
5947- [](GenericTypeParamType *paramType) {
5948- return paramType->isParameterPack ();
5949- });
5950- assert (found != baseGenericSig.getGenericParams ().end ());
5951- }
5952- #endif
5953-
5954- // The pack element signature includes all type parameters and requirements
5955- // from the outer context, plus a new set of type parameters representing
5956- // open pack elements and their corresponding element requirements.
5957-
5958- llvm::SmallMapVector<GenericTypeParamType *,
5959- GenericTypeParamType *, 2 > packElementParams;
5960- SmallVector<GenericTypeParamType *, 2 > genericParams (
5961- baseGenericSig.getGenericParams ().begin (), baseGenericSig.getGenericParams ().end ());
5962- SmallVector<Requirement, 2 > requirements;
5963-
5964- auto packElementDepth =
5965- baseGenericSig.getInnermostGenericParams ().front ()->getDepth () + 1 ;
5966-
5967- for (auto paramType : baseGenericSig.getGenericParams ()) {
5968- if (!paramType->isParameterPack ())
5969- continue ;
5970-
5971- // Only include opened element parameters for packs in the given
5972- // shape equivalence class.
5973- if (!baseGenericSig->haveSameShape (paramType, shapeClass))
5974- continue ;
5975-
5976- auto *elementParam = GenericTypeParamType::get (/* isParameterPack*/ false ,
5977- packElementDepth,
5978- packElementParams.size (),
5979- *this );
5980- genericParams.push_back (elementParam);
5981- packElementParams[paramType] = elementParam;
5982- }
5983-
5984- auto eraseParameterPackRec = [&](Type type) -> Type {
5985- return type.transformTypeParameterPacks (
5986- [&](SubstitutableType *t) -> std::optional<Type> {
5987- if (auto *paramType = dyn_cast<GenericTypeParamType>(t)) {
5988- if (packElementParams.find (paramType) != packElementParams.end ()) {
5989- return Type (packElementParams[paramType]);
5990- }
5991-
5992- return Type (t);
5993- }
5994- return std::nullopt ;
5995- });
5996- };
5997-
5998- for (auto requirement : baseGenericSig.getRequirements ()) {
5999- requirements.push_back (requirement);
6000-
6001- // If this requirement contains parameter packs, create a new requirement
6002- // for the corresponding pack element.
6003- switch (requirement.getKind ()) {
6004- case RequirementKind::SameShape:
6005- // Drop same-shape requirements from the element signature.
6006- break ;
6007- case RequirementKind::Conformance:
6008- case RequirementKind::Superclass:
6009- case RequirementKind::SameType: {
6010- auto firstType = eraseParameterPackRec (requirement.getFirstType ());
6011- auto secondType = eraseParameterPackRec (requirement.getSecondType ());
6012- if (firstType->isEqual (requirement.getFirstType ()) &&
6013- secondType->isEqual (requirement.getSecondType ()))
6014- break ;
6015-
6016- requirements.emplace_back (requirement.getKind (),
6017- firstType, secondType);
6018- break ;
6019- }
6020- case RequirementKind::Layout: {
6021- auto firstType = eraseParameterPackRec (requirement.getFirstType ());
6022- if (firstType->isEqual (requirement.getFirstType ()))
6023- break ;
6024-
6025- requirements.emplace_back (requirement.getKind (), firstType,
6026- requirement.getLayoutConstraint ());
6027- break ;
6028- }
6029- }
6030- }
6031-
5931+ LocalArchetypeRequirementCollector collector (*this , baseGenericSig);
5932+ collector.addOpenedElement (shapeClass);
60325933 auto elementSig = buildGenericSignature (
6033- *this , GenericSignature (), genericParams, requirements ,
5934+ *this , collector. OuterSig , collector. Params , collector. Requirements ,
60345935 /* allowInverses=*/ false ).getCanonicalSignature ();
5936+
60355937 sigs[key] = elementSig;
60365938 return elementSig;
60375939}
0 commit comments