Rework archetype mangling to follow parent hierarchy.

Use the ordinal archetype manglings only for the primary archetypes of a generic context, and define a mangling for associated types relative to their parent archetype. This will make the archetype mangling resilient in the face of our planned improvements to associated type and protocol conformance ABI. It also correctly mangles self and associated types of protocols, which my previous attempt utterly failed to accomplish.

Swift SVN r8174
This commit is contained in:
Joe Groff
2013-09-13 01:16:27 +00:00
parent fe63b96109
commit c65f4c3f43
3 changed files with 94 additions and 63 deletions

View File

@@ -254,8 +254,7 @@ types where the metadata itself has unknown layout.)
type ::= 'G' type <type>+ '_' // generic type application
type ::= 'M' type // metatype
type ::= 'P' protocol-list '_' // protocol type
type ::= 'Q' index // archetype with depth=0
type ::= 'Qd' index index // archetype with depth=M+1
type ::= archetype
type ::= 'R' type // byref
type ::= 'T' tuple-element* '_' // tuple
type ::= 't' tuple-element* '_' // variadic tuple
@@ -268,9 +267,12 @@ types where the metadata itself has unknown layout.)
nominal-type-kind ::= 'C' // class
nominal-type-kind ::= 'O' // union
nominal-type-kind ::= 'V' // struct
archetype ::= 'Q' index // archetype with depth=0
archetype ::= 'Qd' index index // archetype with depth=M+1
archetype ::= associated-type
associated-type ::= substitution
associated-type ::= 'QP' protocol identifier // protocol associated type
associated-type ::= 'QQ' protocol // protocol Self type
associated-type ::= 'Q' protocol-context // self type of protocol
associated-type ::= 'Q' archetype identifier // associated type
protocol-context ::= 'P' protocol
tuple-element ::= identifier? type

View File

@@ -1099,6 +1099,59 @@ private:
archetypeName(index));
}
NodePointer demangleArchetypeType() {
auto makeSelfType = [&](NodePointer proto) -> NodePointer {
NodePointer selfType
= Node::makeNodePointer(Node::Kind::SelfTypeRef);
selfType->push_back_child(proto);
Substitutions.push_back({ selfType, IsProtocol::no });
return selfType;
};
auto makeAssociatedType = [&](NodePointer root) -> NodePointer {
NodePointer name = demangleIdentifier();
if (!name) return nullptr;
NodePointer assocType
= Node::makeNodePointer(Node::Kind::AssociatedTypeRef);
assocType->push_back_child(root);
assocType->push_back_child(name);
Substitutions.push_back({ assocType, IsProtocol::no });
return assocType;
};
if (Mangled.nextIf('P')) {
NodePointer proto = demangleProtocolName();
if (!proto) return nullptr;
return makeSelfType(proto);
}
if (Mangled.nextIf('Q')) {
NodePointer root = demangleArchetypeType();
if (!root) return nullptr;
return makeAssociatedType(root);
}
if (Mangled.nextIf('S')) {
Substitution sub = demangleSubstitutionIndexWithProtocol();
if (!sub.first) return nullptr;
if (sub.second == IsProtocol::yes)
return makeSelfType(sub.first);
else
return makeAssociatedType(sub.first);
}
if (Mangled.nextIf('d')) {
size_t depth, index;
if (!demangleIndex(depth))
return nullptr;
if (!demangleIndex(index))
return nullptr;
return demangleArchetypeRef(depth + 1, index);
}
size_t index;
if (!demangleIndex(index))
return nullptr;
return demangleArchetypeRef(0, index);
}
NodePointer demangleTuple(IsVariadic isV) {
NodePointer tuple = Node::makeNodePointer(
isV == IsVariadic::yes ? Node::Kind::VariadicTuple
@@ -1308,38 +1361,7 @@ private:
return demangleProtocolList();
}
if (c == 'Q') {
if (Mangled.nextIf('P')) {
NodePointer proto = demangleProtocolName();
if (!proto) return nullptr;
NodePointer name = demangleIdentifier();
if (!name) return nullptr;
NodePointer assocType
= Node::makeNodePointer(Node::Kind::AssociatedTypeRef);
assocType->push_back_child(proto);
assocType->push_back_child(name);
Substitutions.push_back({ assocType, IsProtocol::no });
return assocType;
}
if (Mangled.nextIf('Q')) {
NodePointer proto = demangleProtocolName();
if (!proto) return nullptr;
NodePointer selfType = Node::makeNodePointer(Node::Kind::SelfTypeRef);
selfType->push_back_child(proto);
Substitutions.push_back({ selfType, IsProtocol::no });
return selfType;
}
if (Mangled.nextIf('d')) {
size_t depth, index;
if (!demangleIndex(depth))
return nullptr;
if (!demangleIndex(index))
return nullptr;
return demangleArchetypeRef(depth + 1, index);
}
size_t index;
if (!demangleIndex(index))
return nullptr;
return demangleArchetypeRef(0, index);
return demangleArchetypeType();
}
if (c == 'R') {
NodePointer byref = Node::makeNodePointer(Node::Kind::ByRef);

View File

@@ -290,7 +290,7 @@ void Mangler::bindGenericParameters(const GenericParamList *genericParams,
// the outer context.
ArchetypesDepth = genericParams->getDepth() + 1;
unsigned index = 0;
for (auto archetype : genericParams->getAllArchetypes()) {
for (auto archetype : genericParams->getPrimaryArchetypes()) {
// Remember the current depth and level.
ArchetypeInfo info;
info.Depth = ArchetypesDepth;
@@ -429,6 +429,7 @@ void Mangler::mangleDeclType(ValueDecl *decl, ExplosionKind explosion,
/// <type> ::= P <protocol-list> _ # protocol composition
/// <type> ::= Q <index> # archetype with depth=0, index=N
/// <type> ::= Qd <index> <index> # archetype with depth=M+1, index=N
/// <
/// <type> ::= R <type> # lvalue
/// <type> ::= T <tuple-element>* _ # tuple
/// <type> ::= U <generic-parameter>+ _ <type>
@@ -576,35 +577,42 @@ void Mangler::mangleType(CanType type, ExplosionKind explosion,
return;
}
// type ::= archetype
case TypeKind::Archetype: {
auto archetype = cast<ArchetypeType>(type);
// type ::= associated-type
if (!archetype->getParent())
if (auto assocType = archetype->getAssocType()) {
// archetype ::= associated-type
// associated-type ::= substitution
if (tryMangleSubstitution(archetype.getPointer()))
return;
addSubstitution(archetype.getPointer());
Buffer << 'Q';
// associated-type ::= QQ <protocol>
if (assocType->isSelf()) {
Buffer << "QQ";
mangleProtocolName(assocType->getProtocol());
// associated-type ::= 'Q' archetype identifier
// Mangle the associated type of a parent archetype.
if (auto parent = archetype->getParent()) {
assert(archetype->getAssocType()
&& "child archetype has no associated type?!");
return;
}
// associated-type ::= QP <protocol> <identifier>
Buffer << "QP";
mangleProtocolName(assocType->getProtocol());
mangleType(CanType(parent), explosion, 0);
mangleIdentifier(archetype->getName());
addSubstitution(archetype.getPointer());
return;
}
// <type> ::= Q <index> # archetype with depth=0, index=N
// <type> ::= Qd <index> <index> # archetype with depth=M+1, index=N
// associated-type ::= 'Q' protocol-context
// Mangle the Self archetype of a protocol.
if (archetype->getAssocType() && archetype->getAssocType()->isSelf()) {
Buffer << 'P';
mangleProtocolName(archetype->getAssocType()->getProtocol());
addSubstitution(archetype.getPointer());
return;
}
// archetype ::= 'Q' <index> # archetype with depth=0, index=N
// archetype ::= 'Qd' <index> <index> # archetype with depth=M+1, index=N
// Mangle generic parameter archetypes.
// Find the archetype information. It may be possible for this to
// fail for local declarations --- that might be okay; it means we
@@ -615,7 +623,6 @@ void Mangler::mangleType(CanType type, ExplosionKind explosion,
auto &info = it->second;
assert(ArchetypesDepth >= info.Depth);
Buffer << 'Q';
unsigned relativeDepth = ArchetypesDepth - info.Depth;
if (relativeDepth != 0) {
Buffer << 'd' << Index(relativeDepth - 1);