@@ -464,7 +464,7 @@ struct ASTContext::Implementation {
464464 llvm::DenseMap<BuiltinIntegerWidth, BuiltinIntegerType*> IntegerTypes;
465465 llvm::FoldingSet<BuiltinVectorType> BuiltinVectorTypes;
466466 llvm::FoldingSet<DeclName::CompoundDeclName> CompoundNames;
467- llvm::DenseMap<UUID, OpenedArchetypeType *> OpenedExistentialArchetypes ;
467+ llvm::DenseMap<UUID, GenericEnvironment *> OpenedExistentialEnvironments ;
468468 llvm::FoldingSet<IndexSubset> IndexSubsets;
469469 llvm::FoldingSet<AutoDiffDerivativeFunctionIdentifier>
470470 AutoDiffDerivativeFunctionIdentifiers;
@@ -2498,7 +2498,7 @@ size_t ASTContext::getTotalMemory() const {
24982498 // getImpl().BuiltinVectorTypes ?
24992499 // getImpl().GenericSignatures ?
25002500 // getImpl().CompoundNames ?
2501- getImpl ().OpenedExistentialArchetypes .getMemorySize () +
2501+ getImpl ().OpenedExistentialEnvironments .getMemorySize () +
25022502 getImpl ().Permanent .getTotalMemory ();
25032503
25042504 Size += getSolverMemory ();
@@ -4268,17 +4268,44 @@ Type OpaqueTypeArchetypeType::get(
42684268 return env->getOrCreateArchetypeFromInterfaceType (interfaceType);
42694269}
42704270
4271+ CanTypeWrapper<OpenedArchetypeType> OpenedArchetypeType::getNew (
4272+ GenericEnvironment *environment, Type interfaceType,
4273+ ArrayRef<ProtocolDecl *> conformsTo, Type superclass,
4274+ LayoutConstraint layout) {
4275+ auto arena = AllocationArena::Permanent;
4276+ ASTContext &ctx = interfaceType->getASTContext ();
4277+ void *mem = ctx.Allocate (
4278+ OpenedArchetypeType::totalSizeToAlloc<ProtocolDecl *,Type,LayoutConstraint>(
4279+ conformsTo.size (),
4280+ superclass ? 1 : 0 ,
4281+ layout ? 1 : 0 ),
4282+ alignof (OpenedArchetypeType), arena);
4283+
4284+ return CanOpenedArchetypeType (::new (mem) OpenedArchetypeType (
4285+ environment, interfaceType, conformsTo, superclass, layout));
4286+ }
4287+
4288+ CanTypeWrapper<OpenedArchetypeType> OpenedArchetypeType::get (
4289+ Type existential, Optional<UUID> knownID) {
4290+ Type interfaceType = GenericTypeParamType::get (
4291+ /* isTypeSequence=*/ false , 0 , 0 , existential->getASTContext ());
4292+ return get (existential, interfaceType, knownID);
4293+ }
4294+
42714295CanOpenedArchetypeType OpenedArchetypeType::get (Type existential,
4296+ Type interfaceType,
42724297 Optional<UUID> knownID) {
42734298 auto &ctx = existential->getASTContext ();
4274- auto &openedExistentialArchetypes = ctx.getImpl ().OpenedExistentialArchetypes ;
4299+ auto &openedExistentialEnvironments =
4300+ ctx.getImpl ().OpenedExistentialEnvironments ;
42754301 // If we know the ID already...
42764302 if (knownID) {
42774303 // ... and we already have an archetype for that ID, return it.
4278- auto found = openedExistentialArchetypes .find (*knownID);
4304+ auto found = openedExistentialEnvironments .find (*knownID);
42794305
4280- if (found != openedExistentialArchetypes.end ()) {
4281- auto result = found->second ;
4306+ if (found != openedExistentialEnvironments.end ()) {
4307+ auto result = found->second ->mapTypeIntoContext (interfaceType)
4308+ ->castTo <OpenedArchetypeType>();
42824309 assert (result->getOpenedExistentialType ()->isEqual (existential) &&
42834310 " Retrieved the wrong opened existential type?" );
42844311 return CanOpenedArchetypeType (result);
@@ -4288,52 +4315,32 @@ CanOpenedArchetypeType OpenedArchetypeType::get(Type existential,
42884315 knownID = UUID::fromTime ();
42894316 }
42904317
4291- auto layout = existential->getExistentialLayout ();
4292-
4293- SmallVector<ProtocolDecl *, 2 > protos;
4294- for (auto proto : layout.getProtocols ())
4295- protos.push_back (proto->getDecl ());
4296-
4297- auto layoutConstraint = layout.getLayoutConstraint ();
4298- if (!layoutConstraint && layout.requiresClass ()) {
4299- layoutConstraint = LayoutConstraint::getLayoutConstraint (
4300- LayoutConstraintKind::Class);
4301- }
4302-
4303- auto layoutSuperclass = layout.getSuperclass ();
4304-
4305- auto arena = AllocationArena::Permanent;
4306- void *mem = ctx.Allocate (
4307- OpenedArchetypeType::totalSizeToAlloc<ProtocolDecl *, Type, LayoutConstraint>(
4308- protos.size (),
4309- layoutSuperclass ? 1 : 0 ,
4310- layoutConstraint ? 1 : 0 ),
4311- alignof (OpenedArchetypeType), arena);
4312-
4313- auto result =
4314- ::new (mem) OpenedArchetypeType (ctx, existential,
4315- protos, layoutSuperclass,
4316- layoutConstraint, *knownID);
4317- result->InterfaceType =
4318- GenericTypeParamType::get (/* type sequence*/ false ,
4319- /* depth*/ 0 , /* index*/ 0 , ctx);
4320-
4321- auto signature = ctx.getOpenedArchetypeSignature (existential);
4322- result->Environment = GenericEnvironment::forOpenedExistential (
4323- signature, result);
4324-
4325- openedExistentialArchetypes[*knownID] = result;
4318+ // / Create a generic environment for this opened archetype.
4319+ auto genericEnv = GenericEnvironment::forOpenedExistential (
4320+ existential, *knownID);
4321+ openedExistentialEnvironments[*knownID] = genericEnv;
43264322
4323+ // Map the interface type into that environment.
4324+ auto result = genericEnv->mapTypeIntoContext (interfaceType)
4325+ ->castTo <OpenedArchetypeType>();
43274326 return CanOpenedArchetypeType (result);
43284327}
43294328
4330- CanType OpenedArchetypeType::getAny (Type existential) {
4329+
4330+ CanType OpenedArchetypeType::getAny (Type existential, Type interfaceType) {
43314331 if (auto metatypeTy = existential->getAs <ExistentialMetatypeType>()) {
43324332 auto instanceTy = metatypeTy->getExistentialInstanceType ();
4333- return CanMetatypeType::get (OpenedArchetypeType::getAny (instanceTy));
4333+ return CanMetatypeType::get (
4334+ OpenedArchetypeType::getAny (instanceTy, interfaceType));
43344335 }
43354336 assert (existential->isExistentialType ());
4336- return OpenedArchetypeType::get (existential);
4337+ return OpenedArchetypeType::get (existential, interfaceType);
4338+ }
4339+
4340+ CanType OpenedArchetypeType::getAny (Type existential) {
4341+ Type interfaceType = GenericTypeParamType::get (
4342+ /* isTypeSequence=*/ false , 0 , 0 , existential->getASTContext ());
4343+ return getAny (existential, interfaceType);
43374344}
43384345
43394346void SubstitutionMap::Storage::Profile (
@@ -4480,27 +4487,26 @@ GenericEnvironment *GenericEnvironment::getIncomplete(
44804487
44814488 // Allocate and construct the new environment.
44824489 unsigned numGenericParams = signature.getGenericParams ().size ();
4483- size_t bytes = totalSizeToAlloc<OpaqueTypeDecl *, SubstitutionMap, Type>(
4484- 0 , 0 , numGenericParams);
4490+ size_t bytes = totalSizeToAlloc<OpaqueTypeDecl *, SubstitutionMap,
4491+ OpenedGenericEnvironmentData, Type>(
4492+ 0 , 0 , 0 , numGenericParams);
44854493 void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment));
4486- return new (mem) GenericEnvironment (signature, Kind::Normal );
4494+ return new (mem) GenericEnvironment (signature);
44874495}
44884496
44894497// / Create a new generic environment for an opened archetype.
44904498GenericEnvironment *GenericEnvironment::forOpenedExistential (
4491- GenericSignature signature, const OpenedArchetypeType *type) {
4492- auto &ctx = signature->getASTContext ();
4499+ Type existential, UUID uuid) {
4500+ auto &ctx = existential->getASTContext ();
4501+ auto signature = ctx.getOpenedArchetypeSignature (existential);
44934502
44944503 // Allocate and construct the new environment.
44954504 unsigned numGenericParams = signature.getGenericParams ().size ();
4496- size_t bytes = totalSizeToAlloc<OpaqueTypeDecl *, SubstitutionMap, Type>(
4497- 0 , 0 , numGenericParams);
4505+ size_t bytes = totalSizeToAlloc<OpaqueTypeDecl *, SubstitutionMap,
4506+ OpenedGenericEnvironmentData, Type>(
4507+ 0 , 0 , 1 , numGenericParams);
44984508 void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment));
4499- auto env = new (mem) GenericEnvironment (signature, Kind::OpenedExistential);
4500- env->addMapping (
4501- signature.getGenericParams ().front (),
4502- Type (const_cast <OpenedArchetypeType *>(type)));
4503- return env;
4509+ return new (mem) GenericEnvironment (signature, existential, uuid);
45044510}
45054511
45064512// / Create a new generic environment for an opaque type with the given set of
@@ -4512,8 +4518,9 @@ GenericEnvironment *GenericEnvironment::forOpaqueType(
45124518 // Allocate and construct the new environment.
45134519 auto signature = opaque->getOpaqueInterfaceGenericSignature ();
45144520 unsigned numGenericParams = signature.getGenericParams ().size ();
4515- size_t bytes = totalSizeToAlloc<OpaqueTypeDecl *, SubstitutionMap, Type>(
4516- 1 , 1 , numGenericParams);
4521+ size_t bytes = totalSizeToAlloc<OpaqueTypeDecl *, SubstitutionMap,
4522+ OpenedGenericEnvironmentData, Type>(
4523+ 1 , 1 , 0 , numGenericParams);
45174524 void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment), arena);
45184525 auto env = new (mem) GenericEnvironment (signature, opaque, subs);
45194526 return env;
0 commit comments