@@ -495,6 +495,7 @@ struct ASTContext::Implementation {
495
495
llvm::FoldingSet<BuiltinVectorType> BuiltinVectorTypes;
496
496
llvm::FoldingSet<DeclName::CompoundDeclName> CompoundNames;
497
497
llvm::DenseMap<UUID, GenericEnvironment *> OpenedExistentialEnvironments;
498
+ llvm::DenseMap<UUID, GenericEnvironment *> OpenedElementEnvironments;
498
499
llvm::FoldingSet<IndexSubset> IndexSubsets;
499
500
llvm::FoldingSet<AutoDiffDerivativeFunctionIdentifier>
500
501
AutoDiffDerivativeFunctionIdentifiers;
@@ -4693,26 +4694,7 @@ OpaqueTypeArchetypeType *OpaqueTypeArchetypeType::getNew(
4693
4694
4694
4695
Type OpaqueTypeArchetypeType::get (
4695
4696
OpaqueTypeDecl *Decl, Type interfaceType, SubstitutionMap Substitutions) {
4696
- // TODO: We could attempt to preserve type sugar in the substitution map.
4697
- // Currently archetypes are assumed to be always canonical in many places,
4698
- // though, so doing so would require fixing those places.
4699
- Substitutions = Substitutions.getCanonical ();
4700
-
4701
- auto &ctx = Decl->getASTContext ();
4702
-
4703
- // Look for an opaque archetype environment in the appropriate arena.
4704
- auto properties = getOpaqueTypeArchetypeProperties (Substitutions);
4705
- auto arena = getArena (properties);
4706
- auto &environments
4707
- = ctx.getImpl ().getArena (arena).OpaqueArchetypeEnvironments ;
4708
- GenericEnvironment *env = environments[{Decl, Substitutions}];
4709
-
4710
- // Create the environment if it's missing.
4711
- if (!env) {
4712
- env = GenericEnvironment::forOpaqueType (Decl, Substitutions, arena);
4713
- environments[{Decl, Substitutions}] = env;
4714
- }
4715
-
4697
+ auto *env = GenericEnvironment::forOpaqueType (Decl, Substitutions);
4716
4698
return env->getOrCreateArchetypeFromInterfaceType (interfaceType);
4717
4699
}
4718
4700
@@ -4928,13 +4910,48 @@ GenericEnvironment *GenericEnvironment::forPrimary(GenericSignature signature) {
4928
4910
4929
4911
// Allocate and construct the new environment.
4930
4912
unsigned numGenericParams = signature.getGenericParams ().size ();
4931
- size_t bytes = totalSizeToAlloc<OpaqueTypeDecl *, SubstitutionMap,
4932
- OpenedGenericEnvironmentData, Type>(
4913
+ size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
4914
+ OpenedExistentialEnvironmentData,
4915
+ OpenedElementEnvironmentData, Type>(
4933
4916
0 , 0 , 0 , numGenericParams);
4934
4917
void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment));
4935
4918
return new (mem) GenericEnvironment (signature);
4936
4919
}
4937
4920
4921
+ // / Create a new generic environment for an opaque type with the given set of
4922
+ // / outer substitutions.
4923
+ GenericEnvironment *GenericEnvironment::forOpaqueType (
4924
+ OpaqueTypeDecl *opaque, SubstitutionMap subs) {
4925
+ // TODO: We could attempt to preserve type sugar in the substitution map.
4926
+ // Currently archetypes are assumed to be always canonical in many places,
4927
+ // though, so doing so would require fixing those places.
4928
+ subs = subs.getCanonical ();
4929
+
4930
+ auto &ctx = opaque->getASTContext ();
4931
+
4932
+ auto properties = getOpaqueTypeArchetypeProperties (subs);
4933
+ auto arena = getArena (properties);
4934
+ auto &environments
4935
+ = ctx.getImpl ().getArena (arena).OpaqueArchetypeEnvironments ;
4936
+ GenericEnvironment *env = environments[{opaque, subs}];
4937
+
4938
+ if (!env) {
4939
+ // Allocate and construct the new environment.
4940
+ auto signature = opaque->getOpaqueInterfaceGenericSignature ();
4941
+ unsigned numGenericParams = signature.getGenericParams ().size ();
4942
+ size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
4943
+ OpenedExistentialEnvironmentData,
4944
+ OpenedElementEnvironmentData, Type>(
4945
+ 1 , 0 , 0 , numGenericParams);
4946
+ void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment), arena);
4947
+ env = new (mem) GenericEnvironment (signature, opaque, subs);
4948
+
4949
+ environments[{opaque, subs}] = env;
4950
+ }
4951
+
4952
+ return env;
4953
+ }
4954
+
4938
4955
// / Create a new generic environment for an opened archetype.
4939
4956
GenericEnvironment *
4940
4957
GenericEnvironment::forOpenedExistential (
@@ -4969,9 +4986,10 @@ GenericEnvironment::forOpenedExistential(
4969
4986
4970
4987
// Allocate and construct the new environment.
4971
4988
unsigned numGenericParams = signature.getGenericParams ().size ();
4972
- size_t bytes = totalSizeToAlloc<OpaqueTypeDecl *, SubstitutionMap,
4973
- OpenedGenericEnvironmentData, Type>(
4974
- 0 , 0 , 1 , numGenericParams);
4989
+ size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
4990
+ OpenedExistentialEnvironmentData,
4991
+ OpenedElementEnvironmentData, Type>(
4992
+ 0 , 1 , 0 , numGenericParams);
4975
4993
void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment));
4976
4994
auto *genericEnv =
4977
4995
new (mem) GenericEnvironment (signature, existential, parentSig, uuid);
@@ -4981,21 +4999,35 @@ GenericEnvironment::forOpenedExistential(
4981
4999
return genericEnv;
4982
5000
}
4983
5001
4984
- // / Create a new generic environment for an opaque type with the given set of
4985
- // / outer substitutions.
4986
- GenericEnvironment *GenericEnvironment::forOpaqueType (
4987
- OpaqueTypeDecl *opaque, SubstitutionMap subs, AllocationArena arena) {
4988
- auto &ctx = opaque->getASTContext ();
5002
+ // / Create a new generic environment for an element archetype.
5003
+ GenericEnvironment *
5004
+ GenericEnvironment::forOpenedElement (GenericSignature signature, UUID uuid) {
5005
+ auto &ctx = signature->getASTContext ();
5006
+
5007
+ auto &openedElementEnvironments =
5008
+ ctx.getImpl ().OpenedElementEnvironments ;
5009
+ auto found = openedElementEnvironments.find (uuid);
5010
+
5011
+ if (found != openedElementEnvironments.end ()) {
5012
+ auto *existingEnv = found->second ;
5013
+ assert (existingEnv->getGenericSignature ().getPointer () == signature.getPointer ());
5014
+ assert (existingEnv->getOpenedElementUUID () == uuid);
5015
+
5016
+ return existingEnv;
5017
+ }
4989
5018
4990
5019
// Allocate and construct the new environment.
4991
- auto signature = opaque->getOpaqueInterfaceGenericSignature ();
4992
5020
unsigned numGenericParams = signature.getGenericParams ().size ();
4993
- size_t bytes = totalSizeToAlloc<OpaqueTypeDecl *, SubstitutionMap,
4994
- OpenedGenericEnvironmentData, Type>(
4995
- 1 , 1 , 0 , numGenericParams);
4996
- void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment), arena);
4997
- auto env = new (mem) GenericEnvironment (signature, opaque, subs);
4998
- return env;
5021
+ size_t bytes = totalSizeToAlloc<OpaqueEnvironmentData,
5022
+ OpenedExistentialEnvironmentData,
5023
+ OpenedElementEnvironmentData, Type>(
5024
+ 0 , 0 , 1 , numGenericParams);
5025
+ void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment));
5026
+ auto *genericEnv = new (mem) GenericEnvironment (signature, uuid);
5027
+
5028
+ openedElementEnvironments[uuid] = genericEnv;
5029
+
5030
+ return genericEnv;
4999
5031
}
5000
5032
5001
5033
void DeclName::CompoundDeclName::Profile (llvm::FoldingSetNodeID &id,
0 commit comments