mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Make protocol methods generic over <Self>.
Pull the implicit 'Self' associated type out of the protocol and into
an implicitly-declared generic parameter list for the protocol. This
makes all of the methods of a protocol polymorphic, e.g., given
protocol P {
typealias Assoc
func getAssoc() -> Assoc
}
the type of P.getAssoc is:
<Self : P> (self : @inout P) -> () -> Self.Assoc
This directly expresses the notion that protocol methods are
polymorphic, even though 'Self' is always implicitly bound. It can be
used to simplify IRgen and some parts of the type checker, as well as
laying more of the groundwork for default definitions within
protocols as well as sundry other improvements to the generics
system.
There are a number of moving parts that needed to be updated in tandem
for this. In no particular order:
- Protocols always get an implicit generic parameter list, with a
single generic parameter 'Self' that conforms to the protocol itself.
- The 'Self' archetype type now knows which protocol it is
associated with (since we can no longer point it at the Self
associated type declaration).
- Protocol methods now get interface types (i.e., canonicalizable
dependent function types).
- The "all archetypes" list for a polymorphic function type does not
include the Self archetype nor its nested types, because they are
handled implicitly. This avoids the need to rework IRGen's handling
of archetypes for now.
- When (de-)serializing a XREF for a function type that has an
interface type, use the canonicalized interface type, which can be
meaningfully compared during deserialization (unlike the
PolymorphicFunctionType we'd otherwise be dealing with).
- Added a SIL-specific type attribute @sil_self, which extracts the
'Self' archetype of a protocol, because we can no longer refer to
the associated type "P.Self".
Swift SVN r9066
This commit is contained in:
@@ -133,6 +133,7 @@ DeclID Serializer::addDeclRef(const Decl *D) {
|
||||
case DeclKind::Class:
|
||||
case DeclKind::Struct:
|
||||
case DeclKind::Enum:
|
||||
case DeclKind::Protocol:
|
||||
paramList = cast<NominalTypeDecl>(D)->getGenericParams();
|
||||
break;
|
||||
default:
|
||||
@@ -862,8 +863,18 @@ void Serializer::writeCrossReference(const Decl *D) {
|
||||
// If the value is a type, it's uniquely identified by its name.
|
||||
// Otherwise, use the value's type for disambiguation.
|
||||
Type ty;
|
||||
if (!isa<TypeDecl>(value))
|
||||
ty = value->getType();
|
||||
if (!isa<TypeDecl>(value)) {
|
||||
// If this function has an interface type, use its canonicalized form.
|
||||
if (auto func = dyn_cast<AbstractFunctionDecl>(value)) {
|
||||
ty = func->getInterfaceType();
|
||||
if (ty)
|
||||
ty = ty->getCanonicalType();
|
||||
}
|
||||
|
||||
if (!ty)
|
||||
ty = value->getType();
|
||||
}
|
||||
|
||||
typeID = addTypeRef(ty);
|
||||
}
|
||||
|
||||
@@ -1040,7 +1051,6 @@ void Serializer::writeDecl(const Decl *D) {
|
||||
|
||||
case DeclKind::GenericTypeParam: {
|
||||
auto genericParam = cast<GenericTypeParamDecl>(D);
|
||||
assert(!genericParam->isImplicit() && "Implicit generic parameter?");
|
||||
|
||||
// FIXME: Handle attributes.
|
||||
// FIXME: Do generic params have any interesting attributes?
|
||||
@@ -1052,6 +1062,7 @@ void Serializer::writeDecl(const Decl *D) {
|
||||
GenericTypeParamDeclLayout::emitRecord(Out, ScratchRecord, abbrCode,
|
||||
addIdentifierRef(genericParam->getName()),
|
||||
addDeclRef(DC),
|
||||
genericParam->isImplicit(),
|
||||
genericParam->getDepth(),
|
||||
genericParam->getIndex(),
|
||||
addTypeRef(genericParam->getSuperclass()),
|
||||
@@ -1167,7 +1178,6 @@ void Serializer::writeDecl(const Decl *D) {
|
||||
|
||||
assert(remainingAttrs.empty() && "unhandled protocol attrs");
|
||||
|
||||
assert(!proto->getGenericParams() && "protocols can't be generic");
|
||||
const Decl *DC = getDeclForContext(proto->getDeclContext());
|
||||
|
||||
SmallVector<DeclID, 4> protocols;
|
||||
@@ -1183,6 +1193,8 @@ void Serializer::writeDecl(const Decl *D) {
|
||||
proto->isObjC(),
|
||||
protocols);
|
||||
|
||||
writeGenericParams(proto->getGenericParams());
|
||||
writeRequirements(proto->getGenericRequirements());
|
||||
writeMembers(proto->getMembers(), true);
|
||||
break;
|
||||
}
|
||||
@@ -1509,12 +1521,18 @@ void Serializer::writeType(Type ty) {
|
||||
for (auto proto : archetypeTy->getConformsTo())
|
||||
conformances.push_back(addDeclRef(proto));
|
||||
|
||||
DeclID assocTypeOrProtoID;
|
||||
if (auto assocType = archetypeTy->getAssocType())
|
||||
assocTypeOrProtoID = addDeclRef(assocType);
|
||||
else
|
||||
assocTypeOrProtoID = addDeclRef(archetypeTy->getSelfProtocol());
|
||||
|
||||
unsigned abbrCode = DeclTypeAbbrCodes[ArchetypeTypeLayout::Code];
|
||||
ArchetypeTypeLayout::emitRecord(Out, ScratchRecord, abbrCode,
|
||||
addIdentifierRef(archetypeTy->getName()),
|
||||
archetypeTy->isPrimary(),
|
||||
indexOrParentID,
|
||||
addDeclRef(archetypeTy->getAssocType()),
|
||||
assocTypeOrProtoID,
|
||||
addTypeRef(archetypeTy->getSuperclass()),
|
||||
conformances);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user