@@ -72,10 +72,8 @@ class ASTMangler : public Mangler {
7272 // / If enabled, marker protocols can be encoded in the mangled name.
7373 bool AllowMarkerProtocols = true ;
7474
75- // / Whether the mangling predates concurrency, and therefore shouldn't
76- // / include concurrency features such as global actors or @Sendable
77- // / function types.
78- bool Preconcurrency = false ;
75+ // / If enabled, inverses will not be mangled into generic signatures.
76+ bool AllowInverses = true ;
7977
8078 // / If enabled, declarations annotated with @_originallyDefinedIn are mangled
8179 // / as if they're part of their original module. Disabled for debug mangling,
@@ -455,16 +453,54 @@ class ASTMangler : public Mangler {
455453 GenericSignature sig,
456454 const ValueDecl *forDecl);
457455
458- void appendContextOf (const ValueDecl *decl);
456+ // A "base entity" is a function, property, subscript, or any other
457+ // declaration that can appear in an extension.
458+ struct BaseEntitySignature {
459+ private:
460+ GenericSignature sig;
461+ bool innermostTypeDecl;
462+ bool extension;
463+ std::optional<unsigned > mangledDepth; // for inverses
464+ public:
465+ bool reachedInnermostTypeDecl () {
466+ bool answer = innermostTypeDecl;
467+ innermostTypeDecl = false ;
468+ return answer;
469+ }
470+ bool reachedExtension () const { return extension; }
471+ void setReachedExtension () { assert (!extension); extension = true ; }
472+ GenericSignature getSignature () const { return sig; }
473+ // The depth of the inverses mangled so far.
474+ std::optional<unsigned > getDepth () const { return mangledDepth; }
475+ void setDepth (unsigned depth) {
476+ assert (!mangledDepth || *mangledDepth <= depth);
477+ mangledDepth = depth;
478+ }
479+ BaseEntitySignature (const Decl *decl);
480+ };
481+
482+ void appendContextOf (const ValueDecl *decl, BaseEntitySignature &base);
483+ void appendContextualInverses (const GenericTypeDecl *contextDecl,
484+ BaseEntitySignature &base,
485+ const ModuleDecl *module ,
486+ StringRef useModuleName);
459487
460- void appendContext (const DeclContext *ctx, StringRef useModuleName);
488+ void appendContext (const DeclContext *ctx,
489+ BaseEntitySignature &base,
490+ StringRef useModuleName);
461491
462492 void appendModule (const ModuleDecl *module , StringRef useModuleName);
463493
494+ void appendExtension (const ExtensionDecl *ext,
495+ BaseEntitySignature &base,
496+ StringRef useModuleName);
497+
464498 void appendProtocolName (const ProtocolDecl *protocol,
465499 bool allowStandardSubstitution = true );
466500
467501 void appendAnyGenericType (const GenericTypeDecl *decl);
502+ void appendAnyGenericType (const GenericTypeDecl *decl,
503+ BaseEntitySignature &base);
468504
469505 enum FunctionManglingKind {
470506 NoFunctionMangling,
@@ -508,17 +544,42 @@ class ASTMangler : public Mangler {
508544 GenericSignature sig,
509545 const ValueDecl *forDecl = nullptr );
510546
547+ struct GenericSignatureParts {
548+ ArrayRef<CanGenericTypeParamType> params;
549+ unsigned initialParamDepth = 0 ;
550+ SmallVector<Requirement, 2 > requirements;
551+ SmallVector<InverseRequirement, 2 > inverses;
552+ bool isNull () const ; // Is there anything to mangle?
553+ bool hasRequirements () const ; // Are there any requirements to mangle?
554+ void clear ();
555+ };
556+
557+ // / Append a generic signature to the mangling.
558+ // /
559+ // / \param sig The generic signature.
560+ // /
561+ // / \returns \c true if a generic signature was appended, \c false
562+ // / if it was empty.
563+ bool appendGenericSignature (GenericSignature sig);
564+
511565 // / Append a generic signature to the mangling.
512566 // /
513567 // / \param sig The generic signature.
514568 // /
515569 // / \param contextSig The signature of the known context. This function
516570 // / will only mangle the difference between \c sig and \c contextSig.
517571 // /
572+ // / \param base The signature of the base entity whose generic signature we're
573+ // / mangling. This function will only mangle the inverses on generic
574+ // / parameter in \c sig that are not eliminated by conformance requirements in
575+ // / \c base.
576+ // /
577+ // /
518578 // / \returns \c true if a generic signature was appended, \c false
519579 // / if it was empty.
520580 bool appendGenericSignature (GenericSignature sig,
521- GenericSignature contextSig = nullptr );
581+ GenericSignature contextSig,
582+ BaseEntitySignature &base);
522583
523584 // / Append a requirement to the mangling.
524585 // /
@@ -532,10 +593,23 @@ class ASTMangler : public Mangler {
532593 void appendRequirement (const Requirement &reqt, GenericSignature sig,
533594 bool lhsBaseIsProtocolSelf = false );
534595
596+ // / Append an inverse requirement into the mangling.
597+ // /
598+ // / Instead of mangling the presence of an invertible protocol, we mangle
599+ // / their absence, which is what an inverse represents.
600+ // /
601+ // / \param req The inverse requirement to mangle.
602+ void appendInverseRequirement (const InverseRequirement &req,
603+ GenericSignature sig,
604+ bool lhsBaseIsProtocolSelf = false );
605+
606+ void gatherGenericSignatureParts (GenericSignature sig,
607+ GenericSignature contextSig,
608+ BaseEntitySignature &base,
609+ GenericSignatureParts &parts);
610+
535611 void appendGenericSignatureParts (GenericSignature sig,
536- ArrayRef<CanTypeWrapper<GenericTypeParamType>> params,
537- unsigned initialParamDepth,
538- ArrayRef<Requirement> requirements);
612+ GenericSignatureParts const & parts);
539613
540614 DependentMemberType *dropProtocolFromAssociatedType (DependentMemberType *dmt,
541615 GenericSignature sig);
@@ -565,6 +639,7 @@ class ASTMangler : public Mangler {
565639
566640
567641 void appendDeclType (const ValueDecl *decl,
642+ BaseEntitySignature &base,
568643 FunctionManglingKind functionMangling = NoFunctionMangling);
569644
570645 bool tryAppendStandardSubstitution (const GenericTypeDecl *type);
@@ -580,7 +655,10 @@ class ASTMangler : public Mangler {
580655 void appendAccessorEntity (StringRef accessorKindCode,
581656 const AbstractStorageDecl *decl, bool isStatic);
582657
583- void appendEntity (const ValueDecl *decl, StringRef EntityOp, bool isStatic);
658+ void appendEntity (const ValueDecl *decl,
659+ BaseEntitySignature &base,
660+ StringRef EntityOp,
661+ bool isStatic);
584662
585663 void appendEntity (const ValueDecl *decl);
586664
0 commit comments