@@ -464,7 +464,7 @@ struct ASTContext::Implementation {
464
464
llvm::DenseMap<BuiltinIntegerWidth, BuiltinIntegerType*> IntegerTypes;
465
465
llvm::FoldingSet<BuiltinVectorType> BuiltinVectorTypes;
466
466
llvm::FoldingSet<DeclName::CompoundDeclName> CompoundNames;
467
- llvm::DenseMap<UUID, OpenedArchetypeType *> OpenedExistentialArchetypes ;
467
+ llvm::DenseMap<UUID, GenericEnvironment *> OpenedExistentialEnvironments ;
468
468
llvm::FoldingSet<IndexSubset> IndexSubsets;
469
469
llvm::FoldingSet<AutoDiffDerivativeFunctionIdentifier>
470
470
AutoDiffDerivativeFunctionIdentifiers;
@@ -2498,7 +2498,7 @@ size_t ASTContext::getTotalMemory() const {
2498
2498
// getImpl().BuiltinVectorTypes ?
2499
2499
// getImpl().GenericSignatures ?
2500
2500
// getImpl().CompoundNames ?
2501
- getImpl ().OpenedExistentialArchetypes .getMemorySize () +
2501
+ getImpl ().OpenedExistentialEnvironments .getMemorySize () +
2502
2502
getImpl ().Permanent .getTotalMemory ();
2503
2503
2504
2504
Size += getSolverMemory ();
@@ -4268,17 +4268,44 @@ Type OpaqueTypeArchetypeType::get(
4268
4268
return env->getOrCreateArchetypeFromInterfaceType (interfaceType);
4269
4269
}
4270
4270
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
+
4271
4295
CanOpenedArchetypeType OpenedArchetypeType::get (Type existential,
4296
+ Type interfaceType,
4272
4297
Optional<UUID> knownID) {
4273
4298
auto &ctx = existential->getASTContext ();
4274
- auto &openedExistentialArchetypes = ctx.getImpl ().OpenedExistentialArchetypes ;
4299
+ auto &openedExistentialEnvironments =
4300
+ ctx.getImpl ().OpenedExistentialEnvironments ;
4275
4301
// If we know the ID already...
4276
4302
if (knownID) {
4277
4303
// ... and we already have an archetype for that ID, return it.
4278
- auto found = openedExistentialArchetypes .find (*knownID);
4304
+ auto found = openedExistentialEnvironments .find (*knownID);
4279
4305
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>();
4282
4309
assert (result->getOpenedExistentialType ()->isEqual (existential) &&
4283
4310
" Retrieved the wrong opened existential type?" );
4284
4311
return CanOpenedArchetypeType (result);
@@ -4288,52 +4315,32 @@ CanOpenedArchetypeType OpenedArchetypeType::get(Type existential,
4288
4315
knownID = UUID::fromTime ();
4289
4316
}
4290
4317
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;
4326
4322
4323
+ // Map the interface type into that environment.
4324
+ auto result = genericEnv->mapTypeIntoContext (interfaceType)
4325
+ ->castTo <OpenedArchetypeType>();
4327
4326
return CanOpenedArchetypeType (result);
4328
4327
}
4329
4328
4330
- CanType OpenedArchetypeType::getAny (Type existential) {
4329
+
4330
+ CanType OpenedArchetypeType::getAny (Type existential, Type interfaceType) {
4331
4331
if (auto metatypeTy = existential->getAs <ExistentialMetatypeType>()) {
4332
4332
auto instanceTy = metatypeTy->getExistentialInstanceType ();
4333
- return CanMetatypeType::get (OpenedArchetypeType::getAny (instanceTy));
4333
+ return CanMetatypeType::get (
4334
+ OpenedArchetypeType::getAny (instanceTy, interfaceType));
4334
4335
}
4335
4336
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);
4337
4344
}
4338
4345
4339
4346
void SubstitutionMap::Storage::Profile (
@@ -4480,27 +4487,26 @@ GenericEnvironment *GenericEnvironment::getIncomplete(
4480
4487
4481
4488
// Allocate and construct the new environment.
4482
4489
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);
4485
4493
void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment));
4486
- return new (mem) GenericEnvironment (signature, Kind::Normal );
4494
+ return new (mem) GenericEnvironment (signature);
4487
4495
}
4488
4496
4489
4497
// / Create a new generic environment for an opened archetype.
4490
4498
GenericEnvironment *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);
4493
4502
4494
4503
// Allocate and construct the new environment.
4495
4504
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);
4498
4508
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);
4504
4510
}
4505
4511
4506
4512
// / Create a new generic environment for an opaque type with the given set of
@@ -4512,8 +4518,9 @@ GenericEnvironment *GenericEnvironment::forOpaqueType(
4512
4518
// Allocate and construct the new environment.
4513
4519
auto signature = opaque->getOpaqueInterfaceGenericSignature ();
4514
4520
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);
4517
4524
void *mem = ctx.Allocate (bytes, alignof (GenericEnvironment), arena);
4518
4525
auto env = new (mem) GenericEnvironment (signature, opaque, subs);
4519
4526
return env;
0 commit comments