@@ -467,8 +467,92 @@ An ``extension`` mangling is used whenever an entity's declaration context is
467
467
an extension *and * the entity being extended is in a different module. In this
468
468
case the extension's module is mangled first, followed by the entity being
469
469
extended. If the extension and the extended entity are in the same module, the
470
- plain ``entity `` mangling is preferred. If the extension is constrained, the
471
- constraints on the extension are mangled in its generic signature.
470
+ plain ``entity `` mangling is preferred, but not always used. An extension is
471
+ considered "constrained" if it:
472
+
473
+ - Has any requirements not already satisfied by the extended nominal,
474
+ excluding conformance requirements for invertible protocols.
475
+ - Has any generic parameters with an inverse requirement.
476
+
477
+ Those requirements included in any of the above are included in the extension's
478
+ generic signature. The reason for this additional complexity is that we do not
479
+ mangle conformance req's for invertible protocols, only their absence.
480
+
481
+ ::
482
+
483
+ struct S<A: ~Copyable, B: ~Copyable> {}
484
+
485
+ // An unconstrained extension.
486
+ extension S {}
487
+
488
+ // Also an unconstrained extension, because there are no inverses to mangle.
489
+ // This extension is exactly the same as the previous.
490
+ extension S where A: Copyable, B: Copyable {}
491
+
492
+ // A constrained extension, because of the added requirement `B: P` that is
493
+ // not already present in S.
494
+ extension S where B: P {}
495
+
496
+ // A constrained extension, because of the absence of `A: Copyable`.
497
+ // Despite also being absent in `S`, absences of invertible protocols
498
+ // are always mangled.
499
+ extension S where A: ~Copyable {}
500
+
501
+ Some entities, like computed properties, rely on the generic signature in their
502
+ `context `, so in order to disambiguate between those properties and
503
+ those in a context where a generic type requires Copyable, which is not mangled,
504
+ we have the following rule:
505
+
506
+ If the innermost type declaration for an entity has any inverses in its generic
507
+ signature, then extension mangling is used. This strategy is used to ensure
508
+ that moving a declaration between a nominal type and one of its extensions does
509
+ not cause an ABI break if the generic signature of the entity is equivalent in
510
+ both circumstances. For example:
511
+
512
+ ::
513
+
514
+ struct R<A: ~Copyable> {
515
+ func f1() {} // uses extension mangling, just like `f3`
516
+
517
+ func f2() where A: Copyable {}
518
+ }
519
+
520
+ extension R where A: ~Copyable {
521
+ func f3() {}
522
+
523
+ func f4() where A: Copyable {} // uses entity mangling, just like `f2`
524
+ }
525
+
526
+ extension R where A: Copyable {
527
+ // 'f5' is mangled equivalent to 'f2' and 'f4' modulo its identifier.
528
+ func f5() {}
529
+ }
530
+
531
+ For intermediate nested types, i.e., those between the top level and the entity,
532
+ any inverses that remain in at the signature of the entity are mangled into
533
+ that entity's generic signature:
534
+
535
+ ::
536
+
537
+ struct X<A: ~Copyable> {
538
+ struct Y<B: ~Copyable> {
539
+ // 'g1' uses 'entity' context mangling with and has no mangled signatures.
540
+ func g1() where A: Copyable, B: Copyable {}
541
+
542
+ // 'g2' uses 'entity' context mangling. The requirement `B: ~Copyable` is
543
+ //mangled into the generic signature for 'g2'.
544
+ func g2() where A: Copyable {}
545
+
546
+ // 'g3' uses extension mangling with generic signature 'A: ~Copyable'.
547
+ // The mangled generic signature of 'g3' is empty.
548
+ func g3() where B: Copyable {}
549
+
550
+ // 'g4' uses extension mangling with generic signature 'A: ~Copyable'.
551
+ // The mangled generic signature of 'g4' contains 'B: ~Copyable'.
552
+ func g4() {}
553
+ }
554
+ }
555
+
472
556
473
557
When mangling the context of a local entity within a constructor or
474
558
destructor, the non-allocating or non-deallocating variant is used.
@@ -680,12 +764,14 @@ Types
680
764
METATYPE-REPR ::= 'T' // Thick metatype representation
681
765
METATYPE-REPR ::= 'o' // ObjC metatype representation
682
766
767
+ existential-layout ::= protocol-list 'p' // existential layout
768
+ existential-layout ::= protocol-list superclass 'Xc' // existential layout with superclass
769
+ existential-layout ::= protocol-list 'Xl' // existential layout with AnyObject
770
+
683
771
type ::= associated-type
684
772
type ::= any-generic-type
685
- type ::= protocol-list 'p' // existential type
686
- type ::= protocol-list superclass 'Xc' // existential type with superclass
687
- type ::= protocol-list 'Xl' // existential type with AnyObject
688
- type ::= protocol-list requirement* '_' 'XP' // constrained existential type
773
+ type ::= existential-layout // existential type
774
+ type ::= existential-layout requirement '_' requirement* 'XP' // constrained existential type
689
775
type ::= type-list 't' // tuple
690
776
type ::= type generic-signature 'u' // generic type
691
777
type ::= 'x' // generic param, depth=0, idx=0
@@ -925,13 +1011,19 @@ now codified into the ABI; the index 0 is therefore reserved.
925
1011
926
1012
generic-param-pack-marker ::= 'Rv' GENERIC_PARAM-INDEX // generic parameter pack marker
927
1013
1014
+ INVERTIBLE-KIND ::= 'c' // Copyable
1015
+ INVERTIBLE-KIND ::= 'e' // Escapable
1016
+
928
1017
GENERIC-PARAM-COUNT ::= 'z' // zero parameters
929
1018
GENERIC-PARAM-COUNT ::= INDEX // N+1 parameters
930
1019
931
1020
requirement ::= protocol 'R' GENERIC-PARAM-INDEX // protocol requirement
932
1021
requirement ::= protocol assoc-type-name 'Rp' GENERIC-PARAM-INDEX // protocol requirement on associated type
933
1022
requirement ::= protocol assoc-type-list 'RP' GENERIC-PARAM-INDEX // protocol requirement on associated type at depth
934
1023
requirement ::= protocol substitution 'RQ' // protocol requirement with substitution
1024
+ #if SWIFT_RUNTIME_VERSION >= 6.0
1025
+ requirement ::= 'Ri' INVERTIBLE-KIND GENERIC-PARAM-INDEX // inverse requirement
1026
+ #endif
935
1027
requirement ::= type 'Rb' GENERIC-PARAM-INDEX // base class requirement
936
1028
requirement ::= type assoc-type-name 'Rc' GENERIC-PARAM-INDEX // base class requirement on associated type
937
1029
requirement ::= type assoc-type-list 'RC' GENERIC-PARAM-INDEX // base class requirement on associated type at depth
0 commit comments