@@ -465,6 +465,69 @@ _buildDemanglingForNominalType(const Metadata *type, Demangle::Demangler &Dem) {
465465 return _buildDemanglingForContext (description, demangledGenerics, Dem);
466466}
467467
468+ static Demangle::NodePointer
469+ _replaceGeneralizationArg (Demangle::NodePointer type,
470+ SubstGenericParametersFromMetadata substitutions,
471+ Demangle::Demangler &Dem) {
472+ assert (type->getKind () == Node::Kind::Type);
473+ auto genericParam = type->getChild (0 );
474+
475+ if (genericParam->getKind () != Node::Kind::DependentGenericParamType)
476+ return type;
477+
478+ auto depth = genericParam->getChild (0 )->getIndex ();
479+ auto index = genericParam->getChild (1 )->getIndex ();
480+
481+ auto arg = substitutions.getMetadata (depth, index);
482+ assert (arg.isMetadata ());
483+ return _swift_buildDemanglingForMetadata (arg.getMetadata (), Dem);
484+ }
485+
486+ static Demangle::NodePointer
487+ _buildDemanglingForExtendedExistential (const Metadata *type,
488+ Demangle::Demangler &Dem) {
489+ auto ee = cast<ExtendedExistentialTypeMetadata>(type);
490+
491+ auto demangledExistential = Dem.demangleType (ee->Shape ->ExistentialType .get (),
492+ ResolveToDemanglingForContext (Dem));
493+
494+ if (!ee->Shape ->hasGeneralizationSignature ())
495+ return demangledExistential;
496+
497+ SubstGenericParametersFromMetadata substitutions (ee->Shape ,
498+ ee->getGeneralizationArguments ());
499+
500+ // Dig out the requirement list.
501+ auto constrainedExistential = demangledExistential->getChild (0 );
502+ assert (constrainedExistential->getKind () == Node::Kind::ConstrainedExistential);
503+ auto reqList = constrainedExistential->getChild (1 );
504+ assert (reqList->getKind () == Node::Kind::ConstrainedExistentialRequirementList);
505+
506+ auto newReqList = Dem.createNode (Node::Kind::ConstrainedExistentialRequirementList);
507+
508+ for (auto req : *reqList) {
509+ // Currently, the only requirements that can create generalization arguments
510+ // are same types.
511+ if (req->getKind () != Node::Kind::DependentGenericSameTypeRequirement) {
512+ newReqList->addChild (req, Dem);
513+ continue ;
514+ }
515+
516+ auto lhs = _replaceGeneralizationArg (req->getChild (0 ), substitutions, Dem);
517+ auto rhs = _replaceGeneralizationArg (req->getChild (1 ), substitutions, Dem);
518+
519+ auto newReq = Dem.createNode (Node::Kind::DependentGenericSameTypeRequirement);
520+ newReq->addChild (lhs, Dem);
521+ newReq->addChild (rhs, Dem);
522+
523+ newReqList->addChild (newReq, Dem);
524+ }
525+
526+ constrainedExistential->replaceChild (1 , newReqList);
527+
528+ return demangledExistential;
529+ }
530+
468531// Build a demangled type tree for a type.
469532//
470533// FIXME: This should use MetadataReader.h.
@@ -596,13 +659,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
596659 return proto_list;
597660 }
598661 case MetadataKind::ExtendedExistential: {
599- // FIXME: Implement this by demangling the extended existential and
600- // substituting the generalization arguments into the demangle tree.
601- // For now, unconditional casts will report '<<< invalid type >>>' when
602- // they fail.
603- // TODO: for clients that need to guarantee round-tripping, demangle
604- // to a SymbolicExtendedExistentialType.
605- return nullptr ;
662+ return _buildDemanglingForExtendedExistential (type, Dem);
606663 }
607664 case MetadataKind::ExistentialMetatype: {
608665 auto metatype = static_cast <const ExistentialMetatypeMetadata *>(type);
0 commit comments