Skip to content

Push ArchetypeType's API down to subclasses. #21277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace swift {
class ValueDecl;
class Decl;
class DeclRefExpr;
class OpenedArchetypeType;
class Pattern;
class SubscriptDecl;
class Stmt;
Expand Down Expand Up @@ -2694,7 +2695,7 @@ class OpenExistentialExpr : public Expr {
/// Retrieve the existential value that is being opened.
Expr *getExistentialValue() const { return ExistentialValue; }

/// Set the existential value that is being opened.
/// Set the existential val ue that is being opened.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo, unless I missed the lecture about "val ue"s in school ;)

void setExistentialValue(Expr *expr) { ExistentialValue = expr; }

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

/// Retrieve the opened archetype, which can only be referenced
/// within this expression's subexpression.
ArchetypeType *getOpenedArchetype() const;
OpenedArchetypeType *getOpenedArchetype() const;

static bool classof(const Expr *E) {
return E->getKind() == ExprKind::OpenExistential;
Expand Down
162 changes: 53 additions & 109 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace swift {
class GenericSignature;
class Identifier;
class InOutType;
class OpenedArchetypeType;
enum class ReferenceCounting : uint8_t;
enum class ResilienceExpansion : unsigned;
class SILModule;
Expand Down Expand Up @@ -548,7 +549,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {

/// Determine whether the type involves the given opened existential
/// archetype.
bool hasOpenedExistential(ArchetypeType *opened);
bool hasOpenedExistential(OpenedArchetypeType *opened);

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

/// Retrieve the set of opened existential archetypes that occur
/// within this type.
void getOpenedExistentials(SmallVectorImpl<ArchetypeType *> &opened);
void getOpenedExistentials(SmallVectorImpl<OpenedArchetypeType *> &opened);

/// Erase the given opened existential type by replacing it with its
/// existential type throughout the given type.
Type eraseOpenedExistential(ArchetypeType *opened);
Type eraseOpenedExistential(OpenedArchetypeType *opened);

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

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

/// Break an existential down into a set of constraints.
ExistentialLayout getExistentialLayout();
Expand Down Expand Up @@ -4571,6 +4572,8 @@ DEFINE_EMPTY_CAN_TYPE_WRAPPER(SubstitutableType, Type)
template<typename Base, typename...AdditionalTrailingObjects>
using ArchetypeTrailingObjects = llvm::TrailingObjects<Base,
ProtocolDecl *, Type, LayoutConstraint, AdditionalTrailingObjects...>;

class PrimaryArchetypeType;

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

public:
/// getNew - Create a new nested archetype with the given associated type.
///
/// The ConformsTo array will be copied into the ASTContext by this routine.
///
/// TODO: Move to NestedArchetypeType
static CanTypeWrapper<ArchetypeType>
getNew(const ASTContext &Ctx, ArchetypeType *Parent,
DependentMemberType *InterfaceType,
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
Type Superclass, LayoutConstraint Layout);

/// getNew - Create a new primary archetype with the given name.
///
/// The ConformsTo array will be minimized then copied into the ASTContext
/// by this routine.
///
/// TODO: Move to PrimaryArchetypeType
static CanTypeWrapper<ArchetypeType>
getNew(const ASTContext &Ctx,
GenericEnvironment *GenericEnv,
GenericTypeParamType *InterfaceType,
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
Type Superclass, LayoutConstraint Layout);

/// Create a new archetype that represents the opened type
/// of an existential value.
///
/// \param existential The existential type to open.
///
/// \param knownID When non-empty, the known ID of the archetype. When empty,
/// a fresh archetype with a unique ID will be opened.
static CanTypeWrapper<ArchetypeType>
getOpened(Type existential,
Optional<UUID> knownID = None);

/// Create a new archetype that represents the opened type
/// of an existential value.
///
/// \param existential The existential type or existential metatype to open.
static CanType getAnyOpened(Type existential);

/// Retrieve the name of this archetype.
Identifier getName() const;

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

/// Retrieve the parent of this archetype, or null if this is a
/// primary archetype.
ArchetypeType *getParent() const;

/// Retrieve the opened existential type.
/// TODO: Remove and leave on only OpenedArchetypeType
Type getOpenedExistentialType() const;

/// Retrieve the generic environment in which this archetype resides.
///
/// Note: opened archetypes currently don't have generic environments.
///
/// TODO: Remove and leave only on PrimaryArchetypeType
GenericEnvironment *getGenericEnvironment() const;

/// Retrieve the interface type of this associated type, which will either
/// be a GenericTypeParamType or a DependentMemberType.
Type getInterfaceType() const { return InterfaceType; }

/// Retrieve the associated type to which this archetype (if it is a nested
/// archetype) corresponds.
///
/// This associated type will have the same name as the archetype and will
/// be a member of one of the protocols to which the parent archetype
/// conforms.
AssociatedTypeDecl *getAssocType() const;

/// getConformsTo - Retrieve the set of protocols to which this substitutable
/// type shall conform.
ArrayRef<ProtocolDecl *> getConformsTo() const {
Expand Down Expand Up @@ -4759,18 +4698,8 @@ class ArchetypeType : public SubstitutableType,
/// Register a nested type with the given name.
void registerNestedType(Identifier name, Type nested);

/// isPrimary - Determine whether this is the archetype for a 'primary'
/// archetype, e.g., one that is not nested within another archetype and is
/// not an opened existential.
bool isPrimary() const;

/// getPrimary - Return the primary archetype parent of this archetype.
ArchetypeType *getPrimary() const;

/// Retrieve the ID number of this opened existential.
///
/// TODO: Remove and place on OpenedArchetypeType only
UUID getOpenedExistentialID() const;
PrimaryArchetypeType *getPrimary() const;

// Implement isa/cast/dyncast/etc.
static bool classof(const TypeBase *T) {
Expand All @@ -4786,9 +4715,6 @@ class ArchetypeType : public SubstitutableType,
Type Superclass, LayoutConstraint Layout);
};
BEGIN_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)
CanArchetypeType getParent() const {
return CanArchetypeType(getPointer()->getParent());
}
END_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)

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

public:
/// getNew - Create a new primary archetype with the given name.
///
/// The ConformsTo array will be minimized then copied into the ASTContext
/// by this routine.
static CanTypeWrapper<PrimaryArchetypeType>
getNew(const ASTContext &Ctx,
GenericEnvironment *GenericEnv,
GenericTypeParamType *InterfaceType,
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
Type Superclass, LayoutConstraint Layout);

/// Retrieve the generic environment in which this archetype resides.
GenericEnvironment *getGenericEnvironment() const {
return Environment;
Expand Down Expand Up @@ -4830,6 +4767,23 @@ class OpenedArchetypeType final : public ArchetypeType,
TypeBase *Opened;
UUID ID;
public:
/// Create a new archetype that represents the opened type
/// of an existential value.
///
/// \param existential The existential type to open.
///
/// \param knownID When non-empty, the known ID of the archetype. When empty,
/// a fresh archetype with a unique ID will be opened.
static CanTypeWrapper<OpenedArchetypeType>
get(Type existential,
Optional<UUID> knownID = None);

/// Create a new archetype that represents the opened type
/// of an existential value.
///
/// \param existential The existential type or existential metatype to open.
static CanType getAny(Type existential);

/// Retrieve the ID number of this opened existential.
UUID getOpenedExistentialID() const { return ID; }

Expand Down Expand Up @@ -4860,11 +4814,22 @@ class NestedArchetypeType final : public ArchetypeType,
ArchetypeType *Parent;

public:
/// getNew - Create a new nested archetype with the given associated type.
///
/// The ConformsTo array will be copied into the ASTContext by this routine.
static CanTypeWrapper<NestedArchetypeType>
getNew(const ASTContext &Ctx, ArchetypeType *Parent,
DependentMemberType *InterfaceType,
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
Type Superclass, LayoutConstraint Layout);

/// Retrieve the parent of this archetype, or null if this is a
/// primary archetype.
ArchetypeType *getParent() const {
return Parent;
}

AssociatedTypeDecl *getAssocType() const;

static bool classof(const TypeBase *T) {
return T->getKind() == TypeKind::NestedArchetype;
Expand All @@ -4878,29 +4843,11 @@ class NestedArchetypeType final : public ArchetypeType,
Type Superclass, LayoutConstraint Layout);
};
BEGIN_CAN_TYPE_WRAPPER(NestedArchetypeType, ArchetypeType)
CanArchetypeType getParent() const {
return CanArchetypeType(getPointer()->getParent());
}
END_CAN_TYPE_WRAPPER(NestedArchetypeType, ArchetypeType)

inline bool ArchetypeType::isPrimary() const {
return isa<PrimaryArchetypeType>(this);
}

inline UUID ArchetypeType::getOpenedExistentialID() const {
return cast<OpenedArchetypeType>(this)->getOpenedExistentialID();
}

inline Type ArchetypeType::getOpenedExistentialType() const {
if (auto child = dyn_cast<OpenedArchetypeType>(this))
return child->getOpenedExistentialType();
return nullptr;
}

inline ArchetypeType *ArchetypeType::getParent() const {
if (auto child = dyn_cast<NestedArchetypeType>(this))
return child->getParent();

return nullptr;
}

template<typename Type>
const Type *ArchetypeType::getSubclassTrailingObjects() const {
if (auto contextTy = dyn_cast<PrimaryArchetypeType>(this)) {
Expand Down Expand Up @@ -5256,20 +5203,17 @@ inline bool TypeBase::isOpenedExistential() const {
return false;

CanType T = getCanonicalType();
if (auto archetype = dyn_cast<ArchetypeType>(T))
return !archetype->getOpenedExistentialType().isNull();
return false;
return isa<OpenedArchetypeType>(T);
}

inline bool TypeBase::isOpenedExistentialWithError() {
if (!hasOpenedExistential())
return false;

CanType T = getCanonicalType();
if (auto archetype = dyn_cast<ArchetypeType>(T)) {
if (auto archetype = dyn_cast<OpenedArchetypeType>(T)) {
auto openedExistentialType = archetype->getOpenedExistentialType();
return (!openedExistentialType.isNull() &&
openedExistentialType->isExistentialWithError());
return openedExistentialType->isExistentialWithError();
}
return false;
}
Expand Down
16 changes: 8 additions & 8 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
return asImpl().remapASTType(ty);
}

void remapOpenedType(CanArchetypeType archetypeTy) {
void remapOpenedType(CanOpenedArchetypeType archetypeTy) {
auto existentialTy = archetypeTy->getOpenedExistentialType()->getCanonicalType();
auto replacementTy = ArchetypeType::getOpened(getOpASTType(existentialTy));
auto replacementTy = OpenedArchetypeType::get(getOpASTType(existentialTy));
registerOpenedExistentialRemapping(archetypeTy, replacementTy);
}

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

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

getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
recordClonedInstruction(Inst, getBuilder().createOpenExistentialValue(
Expand All @@ -1911,7 +1911,7 @@ visitOpenExistentialMetatypeInst(OpenExistentialMetatypeInst *Inst) {
exType = exMetatype.getInstanceType();
openedType = cast<MetatypeType>(openedType).getInstanceType();
}
remapOpenedType(cast<ArchetypeType>(openedType));
remapOpenedType(cast<OpenedArchetypeType>(openedType));

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

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

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

getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
recordClonedInstruction(Inst, getBuilder().createOpenExistentialBoxValue(
Expand Down
Loading