@@ -328,7 +328,7 @@ struct ASTContext::Implementation {
328
328
CanGenericSignature SingleGenericParameterSignature;
329
329
330
330
// / The existential signature <T : P> for each P.
331
- llvm::DenseMap<std::pair<CanType, const DeclContext *>, CanGenericSignature>
331
+ llvm::DenseMap<std::pair<CanType, const GenericSignatureImpl *>, CanGenericSignature>
332
332
ExistentialSignatures;
333
333
334
334
// / Overridden declarations.
@@ -4441,17 +4441,18 @@ CanTypeWrapper<OpenedArchetypeType> OpenedArchetypeType::getNew(
4441
4441
}
4442
4442
4443
4443
CanTypeWrapper<OpenedArchetypeType>
4444
- OpenedArchetypeType::get (CanType existential, const DeclContext *useDC ,
4444
+ OpenedArchetypeType::get (CanType existential, GenericSignature parentSig ,
4445
4445
Optional<UUID> knownID) {
4446
- return get (existential,
4447
- OpenedArchetypeType::getSelfInterfaceTypeFromContext (useDC),
4448
- useDC , knownID);
4446
+ assert (existential-> isExistentialType ());
4447
+ auto interfaceType = OpenedArchetypeType::getSelfInterfaceTypeFromContext (parentSig, existential-> getASTContext ());
4448
+ return get (existential, interfaceType, parentSig , knownID);
4449
4449
}
4450
4450
4451
4451
CanOpenedArchetypeType OpenedArchetypeType::get (CanType existential,
4452
4452
Type interfaceType,
4453
- const DeclContext *useDC ,
4453
+ GenericSignature parentSig ,
4454
4454
Optional<UUID> knownID) {
4455
+ assert (existential->isExistentialType ());
4455
4456
assert (!interfaceType->hasArchetype () && " must be interface type" );
4456
4457
// FIXME: Opened archetypes can't be transformed because the
4457
4458
// the identity of the archetype has to be preserved. This
@@ -4485,7 +4486,7 @@ CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential,
4485
4486
4486
4487
// / Create a generic environment for this opened archetype.
4487
4488
auto genericEnv =
4488
- GenericEnvironment::forOpenedExistential (existential, useDC , *knownID);
4489
+ GenericEnvironment::forOpenedExistential (existential, parentSig , *knownID);
4489
4490
openedExistentialEnvironments[*knownID] = genericEnv;
4490
4491
4491
4492
// Map the interface type into that environment.
@@ -4495,22 +4496,22 @@ CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential,
4495
4496
}
4496
4497
4497
4498
CanType OpenedArchetypeType::getAny (CanType existential, Type interfaceType,
4498
- const DeclContext *useDC) {
4499
+ GenericSignature parentSig) {
4500
+ assert (existential->isAnyExistentialType ());
4499
4501
if (auto metatypeTy = existential->getAs <ExistentialMetatypeType>()) {
4500
4502
auto instanceTy =
4501
4503
metatypeTy->getExistentialInstanceType ()->getCanonicalType ();
4502
4504
return CanMetatypeType::get (
4503
- OpenedArchetypeType::getAny (instanceTy, interfaceType, useDC ));
4505
+ OpenedArchetypeType::getAny (instanceTy, interfaceType, parentSig ));
4504
4506
}
4505
4507
assert (existential->isExistentialType ());
4506
- return OpenedArchetypeType::get (existential, interfaceType, useDC );
4508
+ return OpenedArchetypeType::get (existential, interfaceType, parentSig );
4507
4509
}
4508
4510
4509
4511
CanType OpenedArchetypeType::getAny (CanType existential,
4510
- const DeclContext *useDC) {
4511
- return getAny (existential,
4512
- OpenedArchetypeType::getSelfInterfaceTypeFromContext (useDC),
4513
- useDC);
4512
+ GenericSignature parentSig) {
4513
+ auto interfaceTy = OpenedArchetypeType::getSelfInterfaceTypeFromContext (parentSig, existential->getASTContext ());
4514
+ return getAny (existential, interfaceTy, parentSig);
4514
4515
}
4515
4516
4516
4517
void SubstitutionMap::Storage::Profile (
@@ -4666,19 +4667,14 @@ GenericEnvironment *GenericEnvironment::getIncomplete(
4666
4667
4667
4668
// / Create a new generic environment for an opened archetype.
4668
4669
GenericEnvironment *
4669
- GenericEnvironment::forOpenedExistential (Type existential,
4670
- const DeclContext *useDC , UUID uuid) {
4670
+ GenericEnvironment::forOpenedExistential (
4671
+ Type existential, GenericSignature parentSig , UUID uuid) {
4671
4672
auto &ctx = existential->getASTContext ();
4672
- auto signature = ctx.getOpenedArchetypeSignature (existential, useDC);
4673
-
4674
- SubstitutionMap subs;
4675
- if (auto *useEnvironment = useDC->getGenericEnvironmentOfContext ()) {
4676
- subs = useEnvironment->getForwardingSubstitutionMap ();
4677
- }
4678
- return GenericEnvironment::forOpenedExistential (existential, signature, uuid);
4673
+ auto signature = ctx.getOpenedArchetypeSignature (existential, parentSig);
4674
+ return GenericEnvironment::forOpenedArchetypeSignature (existential, signature, uuid);
4679
4675
}
4680
4676
4681
- GenericEnvironment *GenericEnvironment::forOpenedExistential (
4677
+ GenericEnvironment *GenericEnvironment::forOpenedArchetypeSignature (
4682
4678
Type existential, GenericSignature signature, UUID uuid) {
4683
4679
// Allocate and construct the new environment.
4684
4680
auto &ctx = existential->getASTContext ();
@@ -5187,22 +5183,19 @@ CanGenericSignature ASTContext::getSingleGenericParameterSignature() const {
5187
5183
return canonicalSig;
5188
5184
}
5189
5185
5190
- Type OpenedArchetypeType::getSelfInterfaceTypeFromContext (const DeclContext *useDC) {
5191
- auto typeContext = useDC->getInnermostTypeContext ();
5192
- if (typeContext && typeContext->getSelfProtocolDecl ()) {
5193
- return typeContext->getProtocolSelfType ();
5194
- } else {
5195
- return GenericTypeParamType::get (
5196
- /* isTypeSequence=*/ false ,
5197
- /* depth*/ useDC->getGenericContextDepth () + 1 , /* index*/ 0 ,
5198
- useDC->getASTContext ());
5199
- }
5186
+ Type OpenedArchetypeType::getSelfInterfaceTypeFromContext (GenericSignature parentSig,
5187
+ ASTContext &ctx) {
5188
+ unsigned depth = 0 ;
5189
+ if (!parentSig.getGenericParams ().empty ())
5190
+ depth = parentSig.getGenericParams ().back ()->getDepth () + 1 ;
5191
+ return GenericTypeParamType::get (/* isTypeSequence=*/ false ,
5192
+ /* depth=*/ depth, /* index=*/ 0 ,
5193
+ ctx);
5200
5194
}
5201
5195
5202
5196
CanGenericSignature
5203
- ASTContext::getOpenedArchetypeSignature (Type type, const DeclContext *useDC ) {
5197
+ ASTContext::getOpenedArchetypeSignature (Type type, GenericSignature parentSig ) {
5204
5198
assert (type->isExistentialType ());
5205
- assert (useDC && " Must have a working declaration context!" );
5206
5199
5207
5200
if (auto existential = type->getAs <ExistentialType>())
5208
5201
type = existential->getConstraintType ();
@@ -5213,7 +5206,7 @@ ASTContext::getOpenedArchetypeSignature(Type type, const DeclContext *useDC) {
5213
5206
// The opened archetype signature for a protocol type is identical
5214
5207
// to the protocol's own canonical generic signature if there aren't any
5215
5208
// outer generic parameters to worry about.
5216
- if (!useDC-> isGenericContext ()) {
5209
+ if (parentSig. isNull ()) {
5217
5210
if (const auto protoTy = dyn_cast<ProtocolType>(constraint)) {
5218
5211
return protoTy->getDecl ()->getGenericSignature ().getCanonicalSignature ();
5219
5212
}
@@ -5222,31 +5215,23 @@ ASTContext::getOpenedArchetypeSignature(Type type, const DeclContext *useDC) {
5222
5215
// Otherwise we need to build a generic signature that captures any outer
5223
5216
// generic parameters. This ensures that we keep e.g. generic superclass
5224
5217
// existentials contained in a well-formed generic context.
5225
- auto found = getImpl ().ExistentialSignatures .find ({constraint, useDC});
5218
+ auto canParentSig = parentSig.getCanonicalSignature ();
5219
+ auto found = getImpl ().ExistentialSignatures .find ({constraint, canParentSig.getPointer ()});
5226
5220
if (found != getImpl ().ExistentialSignatures .end ())
5227
5221
return found->second ;
5228
5222
5229
- GenericSignature outerSignature;
5230
- auto *typeContext = useDC->getInnermostTypeContext ();
5231
- if (typeContext && typeContext->getSelfProtocolDecl ()) {
5232
- outerSignature = GenericSignature ();
5233
- } else {
5234
- outerSignature = useDC->getGenericSignatureOfContext ();
5235
- }
5236
-
5237
- auto genericParam = OpenedArchetypeType::getSelfInterfaceTypeFromContext (useDC)
5238
- ->getCanonicalType ()->getAs <GenericTypeParamType>();
5239
-
5223
+ auto genericParam = OpenedArchetypeType::getSelfInterfaceTypeFromContext (canParentSig, type->getASTContext ())
5224
+ ->castTo <GenericTypeParamType>();
5240
5225
Requirement requirement (RequirementKind::Conformance, genericParam,
5241
5226
constraint);
5242
5227
auto genericSig = buildGenericSignature (
5243
- *this , outerSignature. getCanonicalSignature () ,
5228
+ *this , canParentSig ,
5244
5229
{genericParam}, {requirement});
5245
5230
5246
5231
CanGenericSignature canGenericSig (genericSig);
5247
5232
5248
5233
auto result = getImpl ().ExistentialSignatures .insert (
5249
- std::make_pair (std::make_pair (constraint, useDC ), canGenericSig));
5234
+ std::make_pair (std::make_pair (constraint, canParentSig. getPointer () ), canGenericSig));
5250
5235
assert (result.second );
5251
5236
(void ) result;
5252
5237
0 commit comments