Skip to content

Commit 3b3ff6a

Browse files
committed
Define Mangling for ParameterizedProtocol
1 parent e2f7d51 commit 3b3ff6a

File tree

19 files changed

+228
-8
lines changed

19 files changed

+228
-8
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ Types
635635
type ::= protocol-list 'p' // existential type
636636
type ::= protocol-list superclass 'Xc' // existential type with superclass
637637
type ::= protocol-list 'Xl' // existential type with AnyObject
638+
type ::= protocol-list 'y' (type* '_')* type* retroactive-conformance* 'Xp' // parameterized protocol type
638639
type ::= type-list 't' // tuple
639640
type ::= type generic-signature 'u' // generic type
640641
type ::= 'x' // generic param, depth=0, idx=0

include/swift/AST/ASTDemangler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class ASTBuilder {
112112
bool isClassBound,
113113
bool forRequirement = true);
114114

115+
Type createParameterizedProtocolType(Type base, ArrayRef<Type> args);
116+
115117
Type createExistentialMetatypeType(Type instance,
116118
Optional<Demangle::ImplMetatypeRepresentation> repr=None);
117119

include/swift/AST/Types.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6417,9 +6417,10 @@ inline bool TypeBase::isAnyExistentialType() {
64176417
}
64186418

64196419
inline bool CanType::isExistentialTypeImpl(CanType type) {
6420-
return (isa<ProtocolType>(type) ||
6421-
isa<ProtocolCompositionType>(type) ||
6422-
isa<ExistentialType>(type));
6420+
return isa<ProtocolType>(type) ||
6421+
isa<ProtocolCompositionType>(type) ||
6422+
isa<ExistentialType>(type) ||
6423+
isa<ParameterizedProtocolType>(type);
64236424
}
64246425

64256426
inline bool CanType::isAnyExistentialTypeImpl(CanType type) {
@@ -6515,6 +6516,8 @@ inline NominalTypeDecl *CanType::getNominalOrBoundGenericNominal() const {
65156516
return Ty->getDecl();
65166517
if (auto Ty = dyn_cast<ExistentialType>(*this))
65176518
return Ty->getConstraintType()->getNominalOrBoundGenericNominal();
6519+
if (auto Ty = dyn_cast<ParameterizedProtocolType>(*this))
6520+
return Ty->getBaseType()->getNominalOrBoundGenericNominal();
65186521
return nullptr;
65196522
}
65206523

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ NODE(EscapingObjCBlock)
170170
CONTEXT_NODE(OtherNominalType)
171171
CONTEXT_NODE(OwningAddressor)
172172
CONTEXT_NODE(OwningMutableAddressor)
173+
NODE(ParameterizedProtocol)
173174
NODE(PartialApplyForwarder)
174175
NODE(PartialApplyObjCForwarder)
175176
NODE(PostfixOperator)

include/swift/Demangling/TypeDecoder.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,34 @@ class TypeDecoder {
710710
forRequirement);
711711
}
712712

713+
case NodeKind::ParameterizedProtocol: {
714+
if (Node->getNumChildren() < 2)
715+
return MAKE_NODE_TYPE_ERROR(Node,
716+
"fewer children (%zu) than required (2)",
717+
Node->getNumChildren());
718+
719+
auto protocolType = decodeMangledType(Node->getChild(0), depth + 1);
720+
if (protocolType.isError())
721+
return protocolType;
722+
723+
llvm::SmallVector<BuiltType, 8> args;
724+
725+
const auto &genericArgs = Node->getChild(1);
726+
if (genericArgs->getKind() != NodeKind::TypeList)
727+
return MAKE_NODE_TYPE_ERROR0(genericArgs, "is not TypeList");
728+
729+
for (auto genericArg : *genericArgs) {
730+
auto paramType = decodeMangledType(genericArg, depth + 1,
731+
/*forRequirement=*/false);
732+
if (paramType.isError())
733+
return paramType;
734+
args.push_back(paramType.getType());
735+
}
736+
737+
return Builder.createParameterizedProtocolType(protocolType.getType(),
738+
args);
739+
}
740+
713741
case NodeKind::Protocol:
714742
case NodeKind::ProtocolSymbolicReference: {
715743
if (auto Proto = decodeMangledProtocolType(Node, depth + 1)) {

include/swift/Reflection/TypeRef.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,42 @@ class ProtocolCompositionTypeRef final : public TypeRef {
563563
}
564564
};
565565

566+
class ParameterizedProtocolTypeRef final : public TypeRef {
567+
const ProtocolCompositionTypeRef *Base;
568+
std::vector<const TypeRef *> Args;
569+
570+
static TypeRefID Profile(const ProtocolCompositionTypeRef *Protocol,
571+
std::vector<const TypeRef *> Args) {
572+
TypeRefID ID;
573+
ID.addPointer(Protocol);
574+
for (auto Arg : Args) {
575+
ID.addPointer(Arg);
576+
}
577+
return ID;
578+
}
579+
580+
public:
581+
ParameterizedProtocolTypeRef(const ProtocolCompositionTypeRef *Protocol,
582+
std::vector<const TypeRef *> Args)
583+
: TypeRef(TypeRefKind::ParameterizedProtocol), Base(Protocol),
584+
Args(Args) {}
585+
586+
template <typename Allocator>
587+
static const ParameterizedProtocolTypeRef *
588+
create(Allocator &A, const ProtocolCompositionTypeRef *Protocol,
589+
std::vector<const TypeRef *> Args) {
590+
FIND_OR_CREATE_TYPEREF(A, ParameterizedProtocolTypeRef, Protocol, Args);
591+
}
592+
593+
const ProtocolCompositionTypeRef *getBase() const { return Base; }
594+
595+
const std::vector<const TypeRef *> &getArgs() const { return Args; }
596+
597+
static bool classof(const TypeRef *TR) {
598+
return TR->getKind() == TypeRefKind::ParameterizedProtocol;
599+
}
600+
};
601+
566602
class MetatypeTypeRef final : public TypeRef {
567603
const TypeRef *InstanceType;
568604
bool WasAbstract;

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,15 @@ class TypeRefBuilder {
614614
isClassBound);
615615
}
616616

617+
const ParameterizedProtocolTypeRef *
618+
createParameterizedProtocolType(const TypeRef *base,
619+
llvm::ArrayRef<const TypeRef *> args) {
620+
auto *baseProto = llvm::dyn_cast<ProtocolCompositionTypeRef>(base);
621+
if (!baseProto)
622+
return nullptr;
623+
return ParameterizedProtocolTypeRef::create(*this, baseProto, args);
624+
}
625+
617626
const ExistentialMetatypeTypeRef *createExistentialMetatypeType(
618627
const TypeRef *instance,
619628
llvm::Optional<Demangle::ImplMetatypeRepresentation> repr = None) {

include/swift/Reflection/TypeRefs.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ TYPEREF(BoundGeneric, TypeRef)
2222
TYPEREF(Tuple, TypeRef)
2323
TYPEREF(Function, TypeRef)
2424
TYPEREF(ProtocolComposition, TypeRef)
25+
TYPEREF(ParameterizedProtocol, TypeRef)
2526
TYPEREF(Metatype, TypeRef)
2627
TYPEREF(ExistentialMetatype, TypeRef)
2728
TYPEREF(GenericTypeParameter, TypeRef)

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4444,7 +4444,8 @@ CanTypeWrapper<OpenedArchetypeType>
44444444
OpenedArchetypeType::get(CanType existential, GenericSignature parentSig,
44454445
Optional<UUID> knownID) {
44464446
assert(existential->isExistentialType());
4447-
auto interfaceType = OpenedArchetypeType::getSelfInterfaceTypeFromContext(parentSig, existential->getASTContext());
4447+
auto interfaceType = OpenedArchetypeType::getSelfInterfaceTypeFromContext(
4448+
parentSig, existential->getASTContext());
44484449
return get(existential, interfaceType, parentSig, knownID);
44494450
}
44504451

@@ -4510,7 +4511,8 @@ CanType OpenedArchetypeType::getAny(CanType existential, Type interfaceType,
45104511

45114512
CanType OpenedArchetypeType::getAny(CanType existential,
45124513
GenericSignature parentSig) {
4513-
auto interfaceTy = OpenedArchetypeType::getSelfInterfaceTypeFromContext(parentSig, existential->getASTContext());
4514+
auto interfaceTy = OpenedArchetypeType::getSelfInterfaceTypeFromContext(
4515+
parentSig, existential->getASTContext());
45144516
return getAny(existential, interfaceTy, parentSig);
45154517
}
45164518

lib/AST/ASTDemangler.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,14 @@ Type ASTBuilder::createExistentialMetatypeType(Type instance,
621621
getMetatypeRepresentation(*repr));
622622
}
623623

624+
Type ASTBuilder::createParameterizedProtocolType(Type base,
625+
ArrayRef<Type> args) {
626+
if (!base->getAs<ProtocolType>())
627+
return Type();
628+
return ParameterizedProtocolType::get(base->getASTContext(),
629+
base->castTo<ProtocolType>(), args);
630+
}
631+
624632
Type ASTBuilder::createMetatypeType(Type instance,
625633
Optional<Demangle::ImplMetatypeRepresentation> repr) {
626634
if (!repr)

0 commit comments

Comments
 (0)