@@ -72,6 +72,9 @@ class ASTMangler : public Mangler {
7272 // / If enabled, marker protocols can be encoded in the mangled name.
7373 bool AllowMarkerProtocols = true ;
7474
75+ // / If enabled, inverses will not be mangled into generic signatures.
76+ bool AllowInverses = true ;
77+
7578 // / If enabled, declarations annotated with @_originallyDefinedIn are mangled
7679 // / as if they're part of their original module. Disabled for debug mangling,
7780 // / because lldb wants to find declarations in the modules they're currently
@@ -450,16 +453,54 @@ class ASTMangler : public Mangler {
450453 GenericSignature sig,
451454 const ValueDecl *forDecl);
452455
453- 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);
454487
455- void appendContext (const DeclContext *ctx, StringRef useModuleName);
488+ void appendContext (const DeclContext *ctx,
489+ BaseEntitySignature &base,
490+ StringRef useModuleName);
456491
457492 void appendModule (const ModuleDecl *module , StringRef useModuleName);
458493
494+ void appendExtension (const ExtensionDecl *ext,
495+ BaseEntitySignature &base,
496+ StringRef useModuleName);
497+
459498 void appendProtocolName (const ProtocolDecl *protocol,
460499 bool allowStandardSubstitution = true );
461500
462501 void appendAnyGenericType (const GenericTypeDecl *decl);
502+ void appendAnyGenericType (const GenericTypeDecl *decl,
503+ BaseEntitySignature &base);
463504
464505 enum FunctionManglingKind {
465506 NoFunctionMangling,
@@ -503,17 +544,42 @@ class ASTMangler : public Mangler {
503544 GenericSignature sig,
504545 const ValueDecl *forDecl = nullptr );
505546
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+
506565 // / Append a generic signature to the mangling.
507566 // /
508567 // / \param sig The generic signature.
509568 // /
510569 // / \param contextSig The signature of the known context. This function
511570 // / will only mangle the difference between \c sig and \c contextSig.
512571 // /
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+ // /
513578 // / \returns \c true if a generic signature was appended, \c false
514579 // / if it was empty.
515580 bool appendGenericSignature (GenericSignature sig,
516- GenericSignature contextSig = nullptr );
581+ GenericSignature contextSig,
582+ BaseEntitySignature &base);
517583
518584 // / Append a requirement to the mangling.
519585 // /
@@ -527,10 +593,23 @@ class ASTMangler : public Mangler {
527593 void appendRequirement (const Requirement &reqt, GenericSignature sig,
528594 bool lhsBaseIsProtocolSelf = false );
529595
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+
530611 void appendGenericSignatureParts (GenericSignature sig,
531- ArrayRef<CanTypeWrapper<GenericTypeParamType>> params,
532- unsigned initialParamDepth,
533- ArrayRef<Requirement> requirements);
612+ GenericSignatureParts const & parts);
534613
535614 DependentMemberType *dropProtocolFromAssociatedType (DependentMemberType *dmt,
536615 GenericSignature sig);
@@ -560,6 +639,7 @@ class ASTMangler : public Mangler {
560639
561640
562641 void appendDeclType (const ValueDecl *decl,
642+ BaseEntitySignature &base,
563643 FunctionManglingKind functionMangling = NoFunctionMangling);
564644
565645 bool tryAppendStandardSubstitution (const GenericTypeDecl *type);
@@ -575,7 +655,10 @@ class ASTMangler : public Mangler {
575655 void appendAccessorEntity (StringRef accessorKindCode,
576656 const AbstractStorageDecl *decl, bool isStatic);
577657
578- void appendEntity (const ValueDecl *decl, StringRef EntityOp, bool isStatic);
658+ void appendEntity (const ValueDecl *decl,
659+ BaseEntitySignature &base,
660+ StringRef EntityOp,
661+ bool isStatic);
579662
580663 void appendEntity (const ValueDecl *decl);
581664
0 commit comments