Skip to content

Commit 183cf58

Browse files
committed
Sink the generic environment of an archetype down into ArchetypeType.
All non-nested archetype types already have a generic environment field, and nested archetype types will need one soon anyway on their way to irrelevance, so sinking the field doesn't isn't a net loss. While here, isolate the code that lazily creates and caches the environment, because only one subclass uses it (for opened archetypes) and I had inadvertently broke caching there recently.
1 parent c5c1347 commit 183cf58

File tree

3 files changed

+34
-54
lines changed

3 files changed

+34
-54
lines changed

include/swift/AST/Types.h

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5396,6 +5396,7 @@ class ArchetypeType : public SubstitutableType,
53965396
}
53975397
Type InterfaceType;
53985398
MutableArrayRef<std::pair<Identifier, Type>> NestedTypes;
5399+
mutable GenericEnvironment *Environment = nullptr;
53995400

54005401
void populateNestedTypes() const;
54015402
void resolveNestedType(std::pair<Identifier, Type> &nested) const;
@@ -5494,7 +5495,8 @@ class ArchetypeType : public SubstitutableType,
54945495
RecursiveTypeProperties properties,
54955496
Type InterfaceType,
54965497
ArrayRef<ProtocolDecl *> ConformsTo,
5497-
Type Superclass, LayoutConstraint Layout);
5498+
Type Superclass, LayoutConstraint Layout,
5499+
GenericEnvironment *Environment);
54985500
};
54995501
BEGIN_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)
55005502
END_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)
@@ -5507,8 +5509,6 @@ class PrimaryArchetypeType final : public ArchetypeType,
55075509
friend TrailingObjects;
55085510
friend ArchetypeType;
55095511

5510-
GenericEnvironment *Environment;
5511-
55125512
public:
55135513
/// getNew - Create a new primary archetype with the given name.
55145514
///
@@ -5521,11 +5521,6 @@ class PrimaryArchetypeType final : public ArchetypeType,
55215521
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
55225522
Type Superclass, LayoutConstraint Layout);
55235523

5524-
/// Retrieve the generic environment in which this archetype resides.
5525-
GenericEnvironment *getGenericEnvironment() const {
5526-
return Environment;
5527-
}
5528-
55295524
GenericTypeParamType *getInterfaceType() const {
55305525
return cast<GenericTypeParamType>(InterfaceType.getPointer());
55315526
}
@@ -5551,10 +5546,6 @@ class OpaqueTypeArchetypeType final : public ArchetypeType,
55515546
friend ArchetypeType;
55525547
friend GenericSignatureBuilder;
55535548

5554-
/// A GenericEnvironment with this opaque archetype bound to the interface
5555-
/// type of the output type from the OpaqueDecl.
5556-
GenericEnvironment *Environment = nullptr;
5557-
55585549
friend class GenericEnvironment;
55595550

55605551
static OpaqueTypeArchetypeType *getNew(
@@ -5575,9 +5566,6 @@ class OpaqueTypeArchetypeType final : public ArchetypeType,
55755566
/// Retrieve the set of substitutions applied to the opaque type.
55765567
SubstitutionMap getSubstitutions() const;
55775568

5578-
/// Get a generic environment that has this opaque archetype bound within it.
5579-
GenericEnvironment *getGenericEnvironment() const;
5580-
55815569
/// Compute the canonical interface type within the environment of this
55825570
/// opaque type archetype.
55835571
CanType getCanonicalInterfaceType(Type interfaceType);
@@ -5662,6 +5650,11 @@ class OpenedArchetypeType final : public ArchetypeType,
56625650
mutable GenericEnvironment *Environment = nullptr;
56635651
TypeBase *Opened;
56645652
UUID ID;
5653+
5654+
/// Create a generic environment with this opened type bound to its generic
5655+
/// parameter.
5656+
GenericEnvironment *createGenericEnvironment() const;
5657+
56655658
public:
56665659
/// Create a new archetype that represents the opened type
56675660
/// of an existential value.
@@ -5687,11 +5680,7 @@ class OpenedArchetypeType final : public ArchetypeType,
56875680
Type getOpenedExistentialType() const {
56885681
return Opened;
56895682
}
5690-
5691-
/// Get a generic environment with this opened type bound to its generic
5692-
/// parameter.
5693-
GenericEnvironment *getGenericEnvironment() const;
5694-
5683+
56955684
static bool classof(const TypeBase *T) {
56965685
return T->getKind() == TypeKind::OpenedArchetype;
56975686
}
@@ -5765,8 +5754,6 @@ class SequenceArchetypeType final
57655754
friend TrailingObjects;
57665755
friend ArchetypeType;
57675756

5768-
GenericEnvironment *Environment;
5769-
57705757
public:
57715758
/// getNew - Create a new sequence archetype with the given name.
57725759
///
@@ -5778,9 +5765,6 @@ class SequenceArchetypeType final
57785765
SmallVectorImpl<ProtocolDecl *> &ConformsTo, Type Superclass,
57795766
LayoutConstraint Layout);
57805767

5781-
/// Retrieve the generic environment in which this archetype resides.
5782-
GenericEnvironment *getGenericEnvironment() const { return Environment; }
5783-
57845768
GenericTypeParamType *getInterfaceType() const {
57855769
return cast<GenericTypeParamType>(InterfaceType.getPointer());
57865770
}

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4323,10 +4323,7 @@ CanOpenedArchetypeType OpenedArchetypeType::get(Type existential,
43234323
return CanOpenedArchetypeType(result);
43244324
}
43254325

4326-
GenericEnvironment *OpenedArchetypeType::getGenericEnvironment() const {
4327-
if (Environment)
4328-
return Environment;
4329-
4326+
GenericEnvironment *OpenedArchetypeType::createGenericEnvironment() const {
43304327
auto thisType = const_cast<OpenedArchetypeType*>(this);
43314328
auto &ctx = thisType->getASTContext();
43324329
// Create a generic environment to represent the opened type.

lib/AST/Type.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3185,9 +3185,11 @@ ArchetypeType::ArchetypeType(TypeKind Kind,
31853185
RecursiveTypeProperties properties,
31863186
Type InterfaceType,
31873187
ArrayRef<ProtocolDecl *> ConformsTo,
3188-
Type Superclass, LayoutConstraint Layout)
3188+
Type Superclass, LayoutConstraint Layout,
3189+
GenericEnvironment *Environment)
31893190
: SubstitutableType(Kind, &Ctx, properties),
3190-
InterfaceType(InterfaceType)
3191+
InterfaceType(InterfaceType),
3192+
Environment(Environment)
31913193
{
31923194
// Set up the bits we need for trailing objects to work.
31933195
Bits.ArchetypeType.ExpandedNestedTypes = false;
@@ -3209,20 +3211,21 @@ ArchetypeType::ArchetypeType(TypeKind Kind,
32093211
}
32103212

32113213
GenericEnvironment *ArchetypeType::getGenericEnvironment() const {
3212-
auto root = getRoot();
3213-
if (auto primary = dyn_cast<PrimaryArchetypeType>(root)) {
3214-
return primary->getGenericEnvironment();
3214+
if (Environment) {
3215+
return Environment;
32153216
}
3216-
if (auto opened = dyn_cast<OpenedArchetypeType>(root)) {
3217-
return opened->getGenericEnvironment();
3218-
}
3219-
if (auto opaque = dyn_cast<OpaqueTypeArchetypeType>(root)) {
3220-
return opaque->getGenericEnvironment();
3217+
3218+
// Opened archetypes lazily create their generic environment.
3219+
if (auto opened = dyn_cast<OpenedArchetypeType>(this)) {
3220+
return Environment = opened->createGenericEnvironment();
32213221
}
3222-
if (auto opaque = dyn_cast<SequenceArchetypeType>(root)) {
3223-
return opaque->getGenericEnvironment();
3222+
3223+
// Nested archetypes get their environment from their root.
3224+
if (auto nested = dyn_cast<NestedArchetypeType>(this)) {
3225+
return Environment = nested->getRoot()->getGenericEnvironment();
32243226
}
3225-
llvm_unreachable("unhandled root archetype kind?!");
3227+
3228+
llvm_unreachable("Archetype without a generic environment!");
32263229
}
32273230

32283231
ArchetypeType *ArchetypeType::getRoot() const {
@@ -3261,8 +3264,7 @@ PrimaryArchetypeType::PrimaryArchetypeType(const ASTContext &Ctx,
32613264
Type Superclass, LayoutConstraint Layout)
32623265
: ArchetypeType(TypeKind::PrimaryArchetype, Ctx,
32633266
RecursiveTypeProperties::HasArchetype,
3264-
InterfaceType, ConformsTo, Superclass, Layout),
3265-
Environment(GenericEnv)
3267+
InterfaceType, ConformsTo, Superclass, Layout, GenericEnv)
32663268
{
32673269
}
32683270

@@ -3274,7 +3276,8 @@ OpenedArchetypeType::OpenedArchetypeType(const ASTContext &Ctx,
32743276
: ArchetypeType(TypeKind::OpenedArchetype, Ctx,
32753277
RecursiveTypeProperties::HasArchetype
32763278
| RecursiveTypeProperties::HasOpenedExistential,
3277-
Type(), ConformsTo, Superclass, Layout),
3279+
Type(), ConformsTo, Superclass, Layout,
3280+
/*Environment=*/nullptr),
32783281
Opened(Existential.getPointer()),
32793282
ID(uuid)
32803283
{
@@ -3288,7 +3291,8 @@ NestedArchetypeType::NestedArchetypeType(const ASTContext &Ctx,
32883291
LayoutConstraint Layout)
32893292
: ArchetypeType(TypeKind::NestedArchetype, Ctx,
32903293
Parent->getRecursiveProperties(),
3291-
InterfaceType, ConformsTo, Superclass, Layout),
3294+
InterfaceType, ConformsTo, Superclass, Layout,
3295+
/*Environment=*/nullptr),
32923296
Parent(Parent)
32933297
{
32943298
}
@@ -3300,8 +3304,8 @@ OpaqueTypeArchetypeType::OpaqueTypeArchetypeType(
33003304
ArrayRef<ProtocolDecl*> conformsTo,
33013305
Type superclass, LayoutConstraint layout)
33023306
: ArchetypeType(TypeKind::OpaqueTypeArchetype, interfaceType->getASTContext(),
3303-
properties, interfaceType, conformsTo, superclass, layout),
3304-
Environment(environment)
3307+
properties, interfaceType, conformsTo, superclass, layout,
3308+
environment)
33053309
{
33063310
}
33073311

@@ -3315,8 +3319,7 @@ SequenceArchetypeType::SequenceArchetypeType(
33153319
LayoutConstraint Layout)
33163320
: ArchetypeType(TypeKind::SequenceArchetype, Ctx,
33173321
RecursiveTypeProperties::HasArchetype, InterfaceType,
3318-
ConformsTo, Superclass, Layout),
3319-
Environment(GenericEnv) {
3322+
ConformsTo, Superclass, Layout, GenericEnv) {
33203323
assert(cast<GenericTypeParamType>(InterfaceType.getPointer())->isTypeSequence());
33213324
}
33223325

@@ -3328,10 +3331,6 @@ CanType OpaqueTypeArchetypeType::getCanonicalInterfaceType(Type interfaceType) {
33283331
->getCanonicalType();
33293332
}
33303333

3331-
GenericEnvironment *OpaqueTypeArchetypeType::getGenericEnvironment() const {
3332-
return Environment;
3333-
}
3334-
33353334
OpaqueTypeDecl *OpaqueTypeArchetypeType::getDecl() const {
33363335
return Environment->getOpaqueTypeDecl();
33373336
}

0 commit comments

Comments
 (0)