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)

lib/AST/ASTMangler.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,8 +1269,11 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
12691269
}
12701270

12711271
case TypeKind::ParameterizedProtocol: {
1272-
llvm::errs() << "Not implemented\n";
1273-
abort();
1272+
auto layout = type->getExistentialLayout();
1273+
appendExistentialLayout(layout, sig, forDecl);
1274+
bool isFirstArgList = true;
1275+
appendBoundGenericArgs(type, sig, isFirstArgList, forDecl);
1276+
return appendOperator("XP");
12741277
}
12751278

12761279
case TypeKind::Existential: {
@@ -1574,6 +1577,9 @@ void ASTMangler::appendBoundGenericArgs(Type type, GenericSignature sig,
15741577
if (Type parent = nominalType->getParent())
15751578
appendBoundGenericArgs(parent->getDesugaredType(), sig, isFirstArgList,
15761579
forDecl);
1580+
} else if (auto *ppt = dyn_cast<ParameterizedProtocolType>(typePtr)) {
1581+
assert(!ppt->getBaseType()->getParent());
1582+
genericArgs = ppt->getArgs();
15771583
} else {
15781584
auto boundType = cast<BoundGenericType>(typePtr);
15791585
genericArgs = boundType->getGenericArgs();

lib/Demangling/Demangler.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,12 @@ NodePointer Demangler::demangleBoundGenericArgs(NodePointer Nominal,
18151815
case Node::Kind::Constructor:
18161816
// Well, not really a nominal type.
18171817
return createWithChildren(Node::Kind::BoundGenericFunction, Nominal, args);
1818+
case Node::Kind::Type:
1819+
if (!Nominal->hasChildren())
1820+
return nullptr;
1821+
if (Nominal->getFirstChild()->getKind() != Node::Kind::ProtocolList)
1822+
return nullptr;
1823+
return createWithChildren(Node::Kind::ParameterizedProtocol, Nominal, args);
18181824
default:
18191825
return nullptr;
18201826
}
@@ -3253,6 +3259,22 @@ NodePointer Demangler::demangleSpecialType() {
32533259
return createType(createWithChildren(Node::Kind::ExistentialMetatype,
32543260
MTR, Type));
32553261
}
3262+
case 'P': {
3263+
NodePointer RetroactiveConformances;
3264+
Vector<NodePointer> TypeListList(*this, 4);
3265+
3266+
if (!demangleBoundGenerics(TypeListList, RetroactiveConformances))
3267+
return nullptr;
3268+
3269+
NodePointer Type = popNode(Node::Kind::Type);
3270+
if (!Type)
3271+
return nullptr;
3272+
3273+
NodePointer BoundNode = demangleBoundGenericArgs(Type, TypeListList, 0);
3274+
NodePointer NTy = createType(BoundNode);
3275+
addSubstitution(NTy);
3276+
return NTy;
3277+
}
32563278
case 'p':
32573279
return createType(createWithChild(Node::Kind::ExistentialMetatype,
32583280
popNode(Node::Kind::Type)));

lib/Demangling/NodePrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ class NodePrinter {
306306
case Node::Kind::MetatypeRepresentation:
307307
case Node::Kind::Module:
308308
case Node::Kind::Tuple:
309+
case Node::Kind::ParameterizedProtocol:
309310
case Node::Kind::Protocol:
310311
case Node::Kind::ProtocolSymbolicReference:
311312
case Node::Kind::ReturnType:
@@ -2270,6 +2271,10 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
22702271
}
22712272
return nullptr;
22722273
}
2274+
case Node::Kind::ParameterizedProtocol: {
2275+
Printer << "parameterized protocol";
2276+
return nullptr;
2277+
}
22732278
case Node::Kind::ExistentialMetatype: {
22742279
unsigned Idx = 0;
22752280
if (Node->getNumChildren() == 2) {

lib/Demangling/OldRemangler.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2714,7 +2714,10 @@ ManglingError Remangler::mangleObjCAsyncCompletionHandlerImpl(Node *node,
27142714
unsigned depth) {
27152715
return MANGLING_ERROR(ManglingError::UnsupportedNodeKind, node);
27162716
}
2717-
2717+
ManglingError Remangler::mangleParameterizedProtocol(Node *node,
2718+
unsigned int depth) {
2719+
return MANGLING_ERROR(ManglingError::UnsupportedNodeKind, node);
2720+
}
27182721
ManglingError
27192722
Remangler::mangleCanonicalSpecializedGenericMetaclass(Node *node,
27202723
unsigned depth) {

lib/Demangling/Remangler.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,13 @@ ManglingError Remangler::mangleGenericArgs(Node *node, char &Separator,
601601
break;
602602
}
603603

604+
case Node::Kind::ParameterizedProtocol: {
605+
Buffer << Separator;
606+
Separator = '_';
607+
RETURN_IF_ERROR(mangleChildNodes(node->getChild(1), depth + 1));
608+
break;
609+
}
610+
604611
case Node::Kind::BoundGenericFunction: {
605612
fullSubstitutionMap = true;
606613

@@ -1218,6 +1225,15 @@ ManglingError Remangler::mangleErrorType(Node *node, unsigned depth) {
12181225
return ManglingError::Success;
12191226
}
12201227

1228+
ManglingError Remangler::mangleParameterizedProtocol(Node *node,
1229+
unsigned int depth) {
1230+
RETURN_IF_ERROR(mangleType(node->getChild(0), depth + 1));
1231+
char separator = 'y';
1232+
RETURN_IF_ERROR(mangleGenericArgs(node, separator, depth + 1));
1233+
Buffer << "XP";
1234+
return ManglingError::Success;
1235+
}
1236+
12211237
ManglingError Remangler::mangleExistentialMetatype(Node *node, unsigned depth) {
12221238
if (node->getFirstChild()->getKind() == Node::Kind::MetatypeRepresentation) {
12231239
RETURN_IF_ERROR(mangleChildNode(node, 1, depth + 1));
@@ -3475,6 +3491,7 @@ bool Demangle::isSpecialized(Node *node) {
34753491
case Node::Kind::BoundGenericTypeAlias:
34763492
case Node::Kind::BoundGenericProtocol:
34773493
case Node::Kind::BoundGenericFunction:
3494+
case Node::Kind::ParameterizedProtocol:
34783495
return true;
34793496

34803497
case Node::Kind::Structure:
@@ -3577,6 +3594,12 @@ ManglingErrorOr<NodePointer> Demangle::getUnspecialized(Node *node,
35773594
return nominalType;
35783595
}
35793596

3597+
case Node::Kind::ParameterizedProtocol: {
3598+
NodePointer unboundType = node->getChild(0);
3599+
DEMANGLER_ASSERT(unboundType->getKind() == Node::Kind::Type, unboundType);
3600+
return unboundType;
3601+
}
3602+
35803603
case Node::Kind::BoundGenericFunction: {
35813604
NodePointer unboundFunction = node->getChild(0);
35823605
DEMANGLER_ASSERT(unboundFunction->getKind() == Node::Kind::Function ||

stdlib/public/Reflection/TypeLowering.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,11 @@ class HasFixedSize
16981698
return true;
16991699
}
17001700

1701+
bool
1702+
visitParameterizedProtocolTypeRef(const ParameterizedProtocolTypeRef *PPT) {
1703+
return true;
1704+
}
1705+
17011706
bool
17021707
visitSILBoxTypeRef(const SILBoxTypeRef *SB) {
17031708
return true;
@@ -1814,6 +1819,11 @@ class HasSingletonMetatype
18141819
return MetatypeRepresentation::Thin;
18151820
}
18161821

1822+
MetatypeRepresentation
1823+
visitParameterizedProtocolTypeRef(const ParameterizedProtocolTypeRef *PPT) {
1824+
return MetatypeRepresentation::Thin;
1825+
}
1826+
18171827
MetatypeRepresentation visitMetatypeTypeRef(const MetatypeTypeRef *M) {
18181828
if (M->wasAbstract())
18191829
return MetatypeRepresentation::Thick;
@@ -2260,6 +2270,11 @@ class LowerType
22602270
return builder.build(ExternalTypeInfo);
22612271
}
22622272

2273+
const TypeInfo *
2274+
visitParameterizedProtocolTypeRef(const ParameterizedProtocolTypeRef *PPT) {
2275+
return visitProtocolCompositionTypeRef(PPT->getBase());
2276+
}
2277+
22632278
const TypeInfo *visitMetatypeTypeRef(const MetatypeTypeRef *M) {
22642279
switch (HasSingletonMetatype().visit(M)) {
22652280
case MetatypeRepresentation::Unknown:

0 commit comments

Comments
 (0)