@@ -72,10 +72,8 @@ class ASTMangler : public Mangler {
72
72
// / If enabled, marker protocols can be encoded in the mangled name.
73
73
bool AllowMarkerProtocols = true ;
74
74
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 ;
79
77
80
78
// / If enabled, declarations annotated with @_originallyDefinedIn are mangled
81
79
// / as if they're part of their original module. Disabled for debug mangling,
@@ -455,16 +453,54 @@ class ASTMangler : public Mangler {
455
453
GenericSignature sig,
456
454
const ValueDecl *forDecl);
457
455
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);
459
487
460
- void appendContext (const DeclContext *ctx, StringRef useModuleName);
488
+ void appendContext (const DeclContext *ctx,
489
+ BaseEntitySignature &base,
490
+ StringRef useModuleName);
461
491
462
492
void appendModule (const ModuleDecl *module, StringRef useModuleName);
463
493
494
+ void appendExtension (const ExtensionDecl *ext,
495
+ BaseEntitySignature &base,
496
+ StringRef useModuleName);
497
+
464
498
void appendProtocolName (const ProtocolDecl *protocol,
465
499
bool allowStandardSubstitution = true );
466
500
467
501
void appendAnyGenericType (const GenericTypeDecl *decl);
502
+ void appendAnyGenericType (const GenericTypeDecl *decl,
503
+ BaseEntitySignature &base);
468
504
469
505
enum FunctionManglingKind {
470
506
NoFunctionMangling,
@@ -508,17 +544,42 @@ class ASTMangler : public Mangler {
508
544
GenericSignature sig,
509
545
const ValueDecl *forDecl = nullptr );
510
546
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
+
511
565
// / Append a generic signature to the mangling.
512
566
// /
513
567
// / \param sig The generic signature.
514
568
// /
515
569
// / \param contextSig The signature of the known context. This function
516
570
// / will only mangle the difference between \c sig and \c contextSig.
517
571
// /
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
+ // /
518
578
// / \returns \c true if a generic signature was appended, \c false
519
579
// / if it was empty.
520
580
bool appendGenericSignature (GenericSignature sig,
521
- GenericSignature contextSig = nullptr );
581
+ GenericSignature contextSig,
582
+ BaseEntitySignature &base);
522
583
523
584
// / Append a requirement to the mangling.
524
585
// /
@@ -532,10 +593,23 @@ class ASTMangler : public Mangler {
532
593
void appendRequirement (const Requirement &reqt, GenericSignature sig,
533
594
bool lhsBaseIsProtocolSelf = false );
534
595
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
+
535
611
void appendGenericSignatureParts (GenericSignature sig,
536
- ArrayRef<CanTypeWrapper<GenericTypeParamType>> params,
537
- unsigned initialParamDepth,
538
- ArrayRef<Requirement> requirements);
612
+ GenericSignatureParts const & parts);
539
613
540
614
DependentMemberType *dropProtocolFromAssociatedType (DependentMemberType *dmt,
541
615
GenericSignature sig);
@@ -565,6 +639,7 @@ class ASTMangler : public Mangler {
565
639
566
640
567
641
void appendDeclType (const ValueDecl *decl,
642
+ BaseEntitySignature &base,
568
643
FunctionManglingKind functionMangling = NoFunctionMangling);
569
644
570
645
bool tryAppendStandardSubstitution (const GenericTypeDecl *type);
@@ -580,7 +655,10 @@ class ASTMangler : public Mangler {
580
655
void appendAccessorEntity (StringRef accessorKindCode,
581
656
const AbstractStorageDecl *decl, bool isStatic);
582
657
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);
584
662
585
663
void appendEntity (const ValueDecl *decl);
586
664
0 commit comments