Skip to content

Commit b701dc4

Browse files
authored
Merge pull request #21277 from jckarter/archetype-subclass-api
Push ArchetypeType's API down to subclasses.
2 parents bb6d945 + 8997913 commit b701dc4

37 files changed

+262
-325
lines changed

include/swift/AST/Expr.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace swift {
4343
class ValueDecl;
4444
class Decl;
4545
class DeclRefExpr;
46+
class OpenedArchetypeType;
4647
class Pattern;
4748
class SubscriptDecl;
4849
class Stmt;
@@ -2694,7 +2695,7 @@ class OpenExistentialExpr : public Expr {
26942695
/// Retrieve the existential value that is being opened.
26952696
Expr *getExistentialValue() const { return ExistentialValue; }
26962697

2697-
/// Set the existential value that is being opened.
2698+
/// Set the existential val ue that is being opened.
26982699
void setExistentialValue(Expr *expr) { ExistentialValue = expr; }
26992700

27002701
/// Retrieve the opaque value representing the value (of archetype
@@ -2703,7 +2704,7 @@ class OpenExistentialExpr : public Expr {
27032704

27042705
/// Retrieve the opened archetype, which can only be referenced
27052706
/// within this expression's subexpression.
2706-
ArchetypeType *getOpenedArchetype() const;
2707+
OpenedArchetypeType *getOpenedArchetype() const;
27072708

27082709
static bool classof(const Expr *E) {
27092710
return E->getKind() == ExprKind::OpenExistential;

include/swift/AST/Types.h

Lines changed: 53 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ namespace swift {
5555
class GenericSignature;
5656
class Identifier;
5757
class InOutType;
58+
class OpenedArchetypeType;
5859
enum class ReferenceCounting : uint8_t;
5960
enum class ResilienceExpansion : unsigned;
6061
class SILModule;
@@ -548,7 +549,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
548549

549550
/// Determine whether the type involves the given opened existential
550551
/// archetype.
551-
bool hasOpenedExistential(ArchetypeType *opened);
552+
bool hasOpenedExistential(OpenedArchetypeType *opened);
552553

553554
/// Determine whether the type is an opened existential type.
554555
///
@@ -561,11 +562,11 @@ class alignas(1 << TypeAlignInBits) TypeBase {
561562

562563
/// Retrieve the set of opened existential archetypes that occur
563564
/// within this type.
564-
void getOpenedExistentials(SmallVectorImpl<ArchetypeType *> &opened);
565+
void getOpenedExistentials(SmallVectorImpl<OpenedArchetypeType *> &opened);
565566

566567
/// Erase the given opened existential type by replacing it with its
567568
/// existential type throughout the given type.
568-
Type eraseOpenedExistential(ArchetypeType *opened);
569+
Type eraseOpenedExistential(OpenedArchetypeType *opened);
569570

570571
/// Erase DynamicSelfType from the given type by replacing it with its
571572
/// underlying type.
@@ -672,7 +673,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
672673
bool isClassExistentialType();
673674

674675
/// Opens an existential instance or meta-type and returns the opened type.
675-
Type openAnyExistentialType(ArchetypeType *&opened);
676+
Type openAnyExistentialType(OpenedArchetypeType *&opened);
676677

677678
/// Break an existential down into a set of constraints.
678679
ExistentialLayout getExistentialLayout();
@@ -4571,6 +4572,8 @@ DEFINE_EMPTY_CAN_TYPE_WRAPPER(SubstitutableType, Type)
45714572
template<typename Base, typename...AdditionalTrailingObjects>
45724573
using ArchetypeTrailingObjects = llvm::TrailingObjects<Base,
45734574
ProtocolDecl *, Type, LayoutConstraint, AdditionalTrailingObjects...>;
4575+
4576+
class PrimaryArchetypeType;
45744577

45754578
/// An archetype is a type that represents a runtime type that is
45764579
/// known to conform to some set of requirements.
@@ -4612,81 +4615,17 @@ class ArchetypeType : public SubstitutableType,
46124615
}
46134616

46144617
public:
4615-
/// getNew - Create a new nested archetype with the given associated type.
4616-
///
4617-
/// The ConformsTo array will be copied into the ASTContext by this routine.
4618-
///
4619-
/// TODO: Move to NestedArchetypeType
4620-
static CanTypeWrapper<ArchetypeType>
4621-
getNew(const ASTContext &Ctx, ArchetypeType *Parent,
4622-
DependentMemberType *InterfaceType,
4623-
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
4624-
Type Superclass, LayoutConstraint Layout);
4625-
4626-
/// getNew - Create a new primary archetype with the given name.
4627-
///
4628-
/// The ConformsTo array will be minimized then copied into the ASTContext
4629-
/// by this routine.
4630-
///
4631-
/// TODO: Move to PrimaryArchetypeType
4632-
static CanTypeWrapper<ArchetypeType>
4633-
getNew(const ASTContext &Ctx,
4634-
GenericEnvironment *GenericEnv,
4635-
GenericTypeParamType *InterfaceType,
4636-
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
4637-
Type Superclass, LayoutConstraint Layout);
4638-
4639-
/// Create a new archetype that represents the opened type
4640-
/// of an existential value.
4641-
///
4642-
/// \param existential The existential type to open.
4643-
///
4644-
/// \param knownID When non-empty, the known ID of the archetype. When empty,
4645-
/// a fresh archetype with a unique ID will be opened.
4646-
static CanTypeWrapper<ArchetypeType>
4647-
getOpened(Type existential,
4648-
Optional<UUID> knownID = None);
4649-
4650-
/// Create a new archetype that represents the opened type
4651-
/// of an existential value.
4652-
///
4653-
/// \param existential The existential type or existential metatype to open.
4654-
static CanType getAnyOpened(Type existential);
4655-
46564618
/// Retrieve the name of this archetype.
46574619
Identifier getName() const;
46584620

46594621
/// Retrieve the fully-dotted name that should be used to display this
46604622
/// archetype.
46614623
std::string getFullName() const;
46624624

4663-
/// Retrieve the parent of this archetype, or null if this is a
4664-
/// primary archetype.
4665-
ArchetypeType *getParent() const;
4666-
4667-
/// Retrieve the opened existential type.
4668-
/// TODO: Remove and leave on only OpenedArchetypeType
4669-
Type getOpenedExistentialType() const;
4670-
4671-
/// Retrieve the generic environment in which this archetype resides.
4672-
///
4673-
/// Note: opened archetypes currently don't have generic environments.
4674-
///
4675-
/// TODO: Remove and leave only on PrimaryArchetypeType
4676-
GenericEnvironment *getGenericEnvironment() const;
4677-
46784625
/// Retrieve the interface type of this associated type, which will either
46794626
/// be a GenericTypeParamType or a DependentMemberType.
46804627
Type getInterfaceType() const { return InterfaceType; }
46814628

4682-
/// Retrieve the associated type to which this archetype (if it is a nested
4683-
/// archetype) corresponds.
4684-
///
4685-
/// This associated type will have the same name as the archetype and will
4686-
/// be a member of one of the protocols to which the parent archetype
4687-
/// conforms.
4688-
AssociatedTypeDecl *getAssocType() const;
4689-
46904629
/// getConformsTo - Retrieve the set of protocols to which this substitutable
46914630
/// type shall conform.
46924631
ArrayRef<ProtocolDecl *> getConformsTo() const {
@@ -4759,18 +4698,8 @@ class ArchetypeType : public SubstitutableType,
47594698
/// Register a nested type with the given name.
47604699
void registerNestedType(Identifier name, Type nested);
47614700

4762-
/// isPrimary - Determine whether this is the archetype for a 'primary'
4763-
/// archetype, e.g., one that is not nested within another archetype and is
4764-
/// not an opened existential.
4765-
bool isPrimary() const;
4766-
47674701
/// getPrimary - Return the primary archetype parent of this archetype.
4768-
ArchetypeType *getPrimary() const;
4769-
4770-
/// Retrieve the ID number of this opened existential.
4771-
///
4772-
/// TODO: Remove and place on OpenedArchetypeType only
4773-
UUID getOpenedExistentialID() const;
4702+
PrimaryArchetypeType *getPrimary() const;
47744703

47754704
// Implement isa/cast/dyncast/etc.
47764705
static bool classof(const TypeBase *T) {
@@ -4786,9 +4715,6 @@ class ArchetypeType : public SubstitutableType,
47864715
Type Superclass, LayoutConstraint Layout);
47874716
};
47884717
BEGIN_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)
4789-
CanArchetypeType getParent() const {
4790-
return CanArchetypeType(getPointer()->getParent());
4791-
}
47924718
END_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)
47934719

47944720
/// An archetype that represents a primary generic argument inside the generic
@@ -4802,6 +4728,17 @@ class PrimaryArchetypeType final : public ArchetypeType,
48024728
GenericEnvironment *Environment;
48034729

48044730
public:
4731+
/// getNew - Create a new primary archetype with the given name.
4732+
///
4733+
/// The ConformsTo array will be minimized then copied into the ASTContext
4734+
/// by this routine.
4735+
static CanTypeWrapper<PrimaryArchetypeType>
4736+
getNew(const ASTContext &Ctx,
4737+
GenericEnvironment *GenericEnv,
4738+
GenericTypeParamType *InterfaceType,
4739+
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
4740+
Type Superclass, LayoutConstraint Layout);
4741+
48054742
/// Retrieve the generic environment in which this archetype resides.
48064743
GenericEnvironment *getGenericEnvironment() const {
48074744
return Environment;
@@ -4830,6 +4767,23 @@ class OpenedArchetypeType final : public ArchetypeType,
48304767
TypeBase *Opened;
48314768
UUID ID;
48324769
public:
4770+
/// Create a new archetype that represents the opened type
4771+
/// of an existential value.
4772+
///
4773+
/// \param existential The existential type to open.
4774+
///
4775+
/// \param knownID When non-empty, the known ID of the archetype. When empty,
4776+
/// a fresh archetype with a unique ID will be opened.
4777+
static CanTypeWrapper<OpenedArchetypeType>
4778+
get(Type existential,
4779+
Optional<UUID> knownID = None);
4780+
4781+
/// Create a new archetype that represents the opened type
4782+
/// of an existential value.
4783+
///
4784+
/// \param existential The existential type or existential metatype to open.
4785+
static CanType getAny(Type existential);
4786+
48334787
/// Retrieve the ID number of this opened existential.
48344788
UUID getOpenedExistentialID() const { return ID; }
48354789

@@ -4860,11 +4814,22 @@ class NestedArchetypeType final : public ArchetypeType,
48604814
ArchetypeType *Parent;
48614815

48624816
public:
4817+
/// getNew - Create a new nested archetype with the given associated type.
4818+
///
4819+
/// The ConformsTo array will be copied into the ASTContext by this routine.
4820+
static CanTypeWrapper<NestedArchetypeType>
4821+
getNew(const ASTContext &Ctx, ArchetypeType *Parent,
4822+
DependentMemberType *InterfaceType,
4823+
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
4824+
Type Superclass, LayoutConstraint Layout);
4825+
48634826
/// Retrieve the parent of this archetype, or null if this is a
48644827
/// primary archetype.
48654828
ArchetypeType *getParent() const {
48664829
return Parent;
48674830
}
4831+
4832+
AssociatedTypeDecl *getAssocType() const;
48684833

48694834
static bool classof(const TypeBase *T) {
48704835
return T->getKind() == TypeKind::NestedArchetype;
@@ -4878,29 +4843,11 @@ class NestedArchetypeType final : public ArchetypeType,
48784843
Type Superclass, LayoutConstraint Layout);
48794844
};
48804845
BEGIN_CAN_TYPE_WRAPPER(NestedArchetypeType, ArchetypeType)
4846+
CanArchetypeType getParent() const {
4847+
return CanArchetypeType(getPointer()->getParent());
4848+
}
48814849
END_CAN_TYPE_WRAPPER(NestedArchetypeType, ArchetypeType)
48824850

4883-
inline bool ArchetypeType::isPrimary() const {
4884-
return isa<PrimaryArchetypeType>(this);
4885-
}
4886-
4887-
inline UUID ArchetypeType::getOpenedExistentialID() const {
4888-
return cast<OpenedArchetypeType>(this)->getOpenedExistentialID();
4889-
}
4890-
4891-
inline Type ArchetypeType::getOpenedExistentialType() const {
4892-
if (auto child = dyn_cast<OpenedArchetypeType>(this))
4893-
return child->getOpenedExistentialType();
4894-
return nullptr;
4895-
}
4896-
4897-
inline ArchetypeType *ArchetypeType::getParent() const {
4898-
if (auto child = dyn_cast<NestedArchetypeType>(this))
4899-
return child->getParent();
4900-
4901-
return nullptr;
4902-
}
4903-
49044851
template<typename Type>
49054852
const Type *ArchetypeType::getSubclassTrailingObjects() const {
49064853
if (auto contextTy = dyn_cast<PrimaryArchetypeType>(this)) {
@@ -5256,20 +5203,17 @@ inline bool TypeBase::isOpenedExistential() const {
52565203
return false;
52575204

52585205
CanType T = getCanonicalType();
5259-
if (auto archetype = dyn_cast<ArchetypeType>(T))
5260-
return !archetype->getOpenedExistentialType().isNull();
5261-
return false;
5206+
return isa<OpenedArchetypeType>(T);
52625207
}
52635208

52645209
inline bool TypeBase::isOpenedExistentialWithError() {
52655210
if (!hasOpenedExistential())
52665211
return false;
52675212

52685213
CanType T = getCanonicalType();
5269-
if (auto archetype = dyn_cast<ArchetypeType>(T)) {
5214+
if (auto archetype = dyn_cast<OpenedArchetypeType>(T)) {
52705215
auto openedExistentialType = archetype->getOpenedExistentialType();
5271-
return (!openedExistentialType.isNull() &&
5272-
openedExistentialType->isExistentialWithError());
5216+
return openedExistentialType->isExistentialWithError();
52735217
}
52745218
return false;
52755219
}

include/swift/SIL/SILCloner.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
253253
return asImpl().remapASTType(ty);
254254
}
255255

256-
void remapOpenedType(CanArchetypeType archetypeTy) {
256+
void remapOpenedType(CanOpenedArchetypeType archetypeTy) {
257257
auto existentialTy = archetypeTy->getOpenedExistentialType()->getCanonicalType();
258-
auto replacementTy = ArchetypeType::getOpened(getOpASTType(existentialTy));
258+
auto replacementTy = OpenedArchetypeType::get(getOpASTType(existentialTy));
259259
registerOpenedExistentialRemapping(archetypeTy, replacementTy);
260260
}
261261

@@ -1878,7 +1878,7 @@ template<typename ImplClass>
18781878
void
18791879
SILCloner<ImplClass>::visitOpenExistentialAddrInst(OpenExistentialAddrInst *Inst) {
18801880
// Create a new archetype for this opened existential type.
1881-
remapOpenedType(Inst->getType().castTo<ArchetypeType>());
1881+
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
18821882

18831883
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
18841884
recordClonedInstruction(
@@ -1891,7 +1891,7 @@ template <typename ImplClass>
18911891
void SILCloner<ImplClass>::visitOpenExistentialValueInst(
18921892
OpenExistentialValueInst *Inst) {
18931893
// Create a new archetype for this opened existential type.
1894-
remapOpenedType(Inst->getType().castTo<ArchetypeType>());
1894+
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
18951895

18961896
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
18971897
recordClonedInstruction(Inst, getBuilder().createOpenExistentialValue(
@@ -1911,7 +1911,7 @@ visitOpenExistentialMetatypeInst(OpenExistentialMetatypeInst *Inst) {
19111911
exType = exMetatype.getInstanceType();
19121912
openedType = cast<MetatypeType>(openedType).getInstanceType();
19131913
}
1914-
remapOpenedType(cast<ArchetypeType>(openedType));
1914+
remapOpenedType(cast<OpenedArchetypeType>(openedType));
19151915

19161916
if (!Inst->getOperand()->getType().canUseExistentialRepresentation(
19171917
Inst->getModule(), ExistentialRepresentation::Class)) {
@@ -1935,7 +1935,7 @@ void
19351935
SILCloner<ImplClass>::
19361936
visitOpenExistentialRefInst(OpenExistentialRefInst *Inst) {
19371937
// Create a new archetype for this opened existential type.
1938-
remapOpenedType(Inst->getType().castTo<ArchetypeType>());
1938+
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
19391939

19401940
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
19411941
recordClonedInstruction(Inst, getBuilder().createOpenExistentialRef(
@@ -1949,7 +1949,7 @@ void
19491949
SILCloner<ImplClass>::
19501950
visitOpenExistentialBoxInst(OpenExistentialBoxInst *Inst) {
19511951
// Create a new archetype for this opened existential type.
1952-
remapOpenedType(Inst->getType().castTo<ArchetypeType>());
1952+
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
19531953

19541954
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
19551955
recordClonedInstruction(Inst, getBuilder().createOpenExistentialBox(
@@ -1963,7 +1963,7 @@ void
19631963
SILCloner<ImplClass>::
19641964
visitOpenExistentialBoxValueInst(OpenExistentialBoxValueInst *Inst) {
19651965
// Create a new archetype for this opened existential type.
1966-
remapOpenedType(Inst->getType().castTo<ArchetypeType>());
1966+
remapOpenedType(Inst->getType().castTo<OpenedArchetypeType>());
19671967

19681968
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
19691969
recordClonedInstruction(Inst, getBuilder().createOpenExistentialBoxValue(

0 commit comments

Comments
 (0)