File: Mangling.rst

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (1436 lines) | stat: -rw-r--r-- 74,428 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
:orphan:

.. _ABI:

.. highlight:: none

Mangling
--------
::

  mangled-name ::= '$s' global  // Swift stable mangling
  mangled-name ::= '@__swiftmacro_' global // Swift mangling for filenames
  mangled-name ::= '_T0' global // Swift 4.0
  mangled-name ::= '$S' global  // Swift 4.2

All Swift-mangled names begin with a common prefix. Since Swift 4.0, the
compiler has used variations of the mangling described in this document, though
pre-stable versions may not exactly conform to this description. By using
distinct prefixes, tools can attempt to accommodate bugs and version variations
in pre-stable versions of Swift.

The basic mangling scheme is a list of 'operators' where the operators are
structured in a post-fix order. For example the mangling may start with an
identifier but only later in the mangling a type-like operator defines how this
identifier has to be interpreted::

  4Test3FooC   // The trailing 'C' says that 'Foo' is a class in module 'Test'

Operators are either identifiers or a sequence of one or more characters,
like ``C`` for class.
All operators share the same name-space. Important operators are a single
character, which means that no other operator may start with the same
character.

Some less important operators are longer and may also contain one or more
natural numbers. But it's always important that the demangler can identify the
end (the last character) of an operator. For example, it's not possible to
determine the last character if there are two operators ``M`` and ``Ma``:
``a`` could belong to ``M`` or it could be the first character of the next
operator.

The intention of the post-fix order is to optimize for common pre-fixes.
Regardless, if it's the mangling for a metatype or a function in a module, the
mangled name will start with the module name (after the ``_S``).

In the following, productions which are only _part_ of an operator, are
named with uppercase letters.

Symbolic references
~~~~~~~~~~~~~~~~~~~

The Swift compiler emits mangled names into binary images to encode
references to types for runtime instantiation and reflection. In a binary,
these mangled names may embed pointers to runtime data
structures in order to more efficiently represent locally-defined types.
We call these pointers **symbolic references**.
These references will be introduced by a control character in the range
`\x01` ... `\x1F`, which indicates the kind of symbolic reference, followed by
some number of arbitrary bytes *which may include null bytes*. Code that
processes mangled names out of Swift binaries needs to be aware of symbolic
references in order to properly terminate strings; a null terminator may be
part of a symbolic reference.

::

  symbolic-reference ::= [\x01-\x17] .{4} // Relative symbolic reference
   #if sizeof(void*) == 8
     symbolic-reference ::= [\x18-\x1F] .{8} // Absolute symbolic reference
   #elif sizeof(void*) == 4
     symbolic-reference ::= [\x18-\x1F] .{4} // Absolute symbolic reference
   #endif

Symbolic references are only valid in compiler-emitted metadata structures
and must only appear in read-only parts of a binary image. APIs and tools
that interpret Swift mangled names from potentially uncontrolled inputs must
refuse to interpret symbolic references.

The following symbolic reference kinds are currently implemented:

::

   #if SWIFT_RUNTIME_VERSION < 5.1
     {any-generic-type, protocol} ::= '\x01' .{4} // Reference points directly to context descriptor
     {any-generic-type, protocol} ::= '\x02' .{4} // Reference points indirectly to context descriptor
   #else
     {any-generic-type, protocol, opaque-type-decl-name} ::= '\x01' .{4} // Reference points directly to context descriptor
     {any-generic-type, protocol, opaque-type-decl-name} ::= '\x02' .{4} // Reference points indirectly to context descriptor
   #endif
   // The grammatical role of the symbolic reference is determined by the
   // kind of context descriptor referenced

   protocol-conformance-ref ::= '\x03' .{4}  // Reference points directly to protocol conformance descriptor (NOT IMPLEMENTED)
   protocol-conformance-ref ::= '\x04' .{4}  // Reference points indirectly to protocol conformance descriptor (NOT IMPLEMENTED)

   dependent-associated-conformance ::= '\x05' .{4}  // Reference points directly to associated conformance descriptor (NOT IMPLEMENTED)
   dependent-associated-conformance ::= '\x06' .{4}  // Reference points indirectly to associated conformance descriptor (NOT IMPLEMENTED)

   associated-conformance-access-function ::= '\x07' .{4}  // Reference points directly to associated conformance access function relative to the protocol
   associated-conformance-access-function ::= '\x08' .{4}  // Reference points directly to associated conformance access function relative to the conforming type

   // keypaths only in Swift 5.0, generalized in Swift 5.1
   #if SWIFT_RUNTIME_VERSION >= 5.1
     metadata-access-function ::= '\x09' .{4}  // Reference points directly to metadata access function that can be invoked to produce referenced object
   #endif

   #if SWIFT_RUNTIME_VERISON >= 5.7
     symbolic-extended-existential-type-shape ::= '\x0A' .{4} // Reference points directly to an ExtendedExistentialTypeShape
     symbolic-extended-existential-type-shape ::= '\x0B' .{4} // Reference points directly to a NonUniqueExtendedExistentialTypeShape
   #endif

   #if SWIFT_RUNTIME_VERSION >= 5.TBD
    objective-c-protocol-relative-reference  ::=  `\x0C`  .{4} // Reference points directly to a objective-c protcol reference
   #endif

A mangled name may also include ``\xFF`` bytes, which are only used for
alignment padding. They do not affect what the mangled name references and can
be skipped over and ignored.

Globals
~~~~~~~

::

  global ::= type 'N'                    // type metadata (address point)
                                         // -- type starts with [BCOSTV]
  global ::= type 'Mf'                   // 'full' type metadata (start of object)
  global ::= type 'MP'                   // type metadata pattern
  global ::= type 'Ma'                   // type metadata access function
  global ::= type 'ML'                   // type metadata lazy cache variable
  global ::= nominal-type 'Mr'           // generic type completion function
  global ::= nominal-type 'Mi'           // generic type instantiation function
  global ::= nominal-type 'MI'           // generic type instantiation cache
  global ::= nominal-type 'Ml'           // in-place type initialization cache
  global ::= nominal-type 'Mm'           // class metaclass
  global ::= nominal-type 'Mn'           // nominal type descriptor
  #if SWIFT_RUNTIME_VERSION >= 5.1
    global ::= opaque-type-decl-name 'MQ'  // opaque type descriptor -- added in Swift 5.1
  #endif
  global ::= nominal-type 'Mu'           // class method lookup function
  global ::= nominal-type 'MU'           // ObjC metadata update callback function
  global ::= nominal-type 'Ms'           // ObjC resilient class stub
  global ::= nominal-type 'Mt'           // Full ObjC resilient class stub (private)
  global ::= module 'MXM'                // module descriptor
  global ::= context 'MXE'               // extension descriptor
  global ::= context 'MXX'               // anonymous context descriptor
  global ::= context identifier 'MXY'    // anonymous context descriptor
  global ::= type assoc-type-list 'MXA'  // generic parameter ref (HISTORICAL)
  global ::= protocol 'Mp'               // protocol descriptor

  global ::= protocol 'Hr'               // protocol descriptor runtime record
  global ::= nominal-type 'Hn'           // nominal type descriptor runtime record
  #if SWIFT_RUNTIME_VERSION >= 5.1
    global ::= opaque-type 'Ho'          // opaque type descriptor runtime record
  #endif
  global ::= protocol-conformance 'Hc'   // protocol conformance runtime record
  global ::= global 'HF'                 // accessible function runtime record

  global ::= nominal-type 'Mo'           // class metadata immediate member base offset

  global ::= type 'MF'                   // metadata for remote mirrors: field descriptor
  global ::= type 'MB'                   // metadata for remote mirrors: builtin type descriptor
  global ::= protocol-conformance 'MA'   // metadata for remote mirrors: associated type descriptor
  global ::= nominal-type 'MC'           // metadata for remote mirrors: superclass descriptor

  global ::= mangled-name 'TA'                     // partial application forwarder
  global ::= mangled-name 'Ta'                     // ObjC partial application forwarder
  global ::= mangled-name 'TQ' index               // Async await continuation partial function
  global ::= mangled-name 'TY' index               // Async suspend continuation partial function
  global ::= mangled-name 'TwS'                    // #_hasSymbol query function

  global ::= type 'w' VALUE-WITNESS-KIND // value witness

  global ::= protocol 'MS'               // protocol self-conformance descriptor
  global ::= protocol 'WS'               // protocol self-conformance witness table
  global ::= protocol-conformance 'Mc'   // protocol conformance descriptor
  global ::= protocol-conformance 'WP'   // protocol witness table
  global ::= protocol-conformance 'Wa'   // protocol witness table accessor (HISTORICAL)

  global ::= protocol-conformance 'WG'   // generic protocol witness table (HISTORICAL)
  global ::= protocol-conformance 'Wp'   // protocol witness table pattern
  global ::= protocol-conformance 'Wr'   // resilient witness table (HISTORICAL)
  global ::= protocol-conformance 'WI'   // generic protocol witness table instantiation function
  global ::= type protocol-conformance 'WL'   // lazy protocol witness table cache variable

  global ::= protocol-conformance identifier 'Wt' // associated type metadata accessor (HISTORICAL)
  global ::= protocol-conformance assoc-type-list protocol 'WT' // associated type witness table accessor
  global ::= protocol-conformance protocol 'Wb' // base protocol witness table accessor
  global ::= type protocol-conformance 'Wl' // lazy protocol witness table accessor

  global ::= global generic-signature? 'WJ' DIFFERENTIABILITY-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // differentiability witness

  global ::= type 'WV'                   // value witness table
  global ::= entity 'Wvd'                // field offset
  global ::= entity 'WC'                 // resilient enum tag index

  global ::= global 'MK'                 // instantiation cache associated with global

  global ::= global 'MJ'                 // noncanonical specialized generic type metadata instantiation cache associated with global
  global ::= global 'MN'                 // noncanonical specialized generic type metadata for global
  global ::= global 'Mz'                 // canonical specialized generic type metadata caching token

  global ::= global 'Mq'                 // global with a uniquing prefix

  #if SWIFT_RUNTIME_VERSION >= 5.4
    global ::= context (decl-name '_')+ 'WZ' // global variable one-time initialization function
    global ::= context (decl-name '_')+ 'Wz' // global variable one-time initialization token
  #endif

A direct symbol resolves directly to the address of an object.  An
indirect symbol resolves to the address of a pointer to the object.
They are distinct manglings to make a certain class of bugs
immediately obvious.

The terminology is slightly overloaded when discussing offsets.  A
direct offset resolves to a variable holding the true offset.  An
indirect offset resolves to a variable holding an offset to be applied
to type metadata to get the address of the true offset.  (Offset
variables are required when the object being accessed lies within a
resilient structure.  When the layout of the object may depend on
generic arguments, these offsets must be kept in metadata.  Indirect
field offsets are therefore required when accessing fields in generic
types where the metadata itself has unknown layout.)

::

  global ::= global 'Tj'                 // resilient method dispatch thunk
  global ::= global 'Tq'                 // method descriptor

  global ::= global 'TO'                 // ObjC-as-swift thunk
  global ::= global 'To'                 // swift-as-ObjC thunk
  global ::= global 'TD'                 // dynamic dispatch thunk
  global ::= global 'Td'                 // direct method reference thunk
  global ::= global 'TE'                 // distributed actor thunk
  global ::= global 'TF'                 // distributed method accessor
  global ::= global 'TI'                 // implementation of a dynamic_replaceable function
  global ::= global 'Tu'                 // async function pointer of a function
  global ::= global 'TX'                 // function pointer of a dynamic_replaceable function
  global ::= global 'Twb'                // back deployment thunk
  global ::= global 'TwB'                // back deployment fallback function
  global ::= entity entity 'TV'          // vtable override thunk, derived followed by base
  global ::= type label-list? 'D'        // type mangling for the debugger with label list for function types.
  global ::= type 'TC'                   // continuation prototype (not actually used for real symbols)
  global ::= protocol-conformance entity 'TW' // protocol witness thunk
  global ::= entity 'TS'                 // protocol self-conformance witness thunk
  global ::= context identifier identifier 'TB' // property behavior initializer thunk (not used currently)
  global ::= context identifier identifier 'Tb' // property behavior setter thunk (not used currently)
  global ::= global specialization       // function specialization
  global ::= global 'Tm'                 // merged function
  global ::= entity                      // some identifiable thing
  global ::= from-type to-type generic-signature? 'TR'  // reabstraction thunk
  global ::= impl-function-type type 'Tz' index? // objc-to-swift-async completion handler block implementation
  global ::= impl-function-type type 'TZ' index? // objc-to-swift-async completion handler block implementation (predefined by runtime)
  global ::= from-type to-type generic-signature? 'TR'  // reabstraction thunk
  global ::= impl-function-type type generic-signature? 'Tz'     // objc-to-swift-async completion handler block implementation
  global ::= impl-function-type type generic-signature? 'TZ'     // objc-to-swift-async completion handler block implementation (predefined by runtime)
  global ::= from-type to-type self-type generic-signature? 'Ty'  // reabstraction thunk with dynamic 'Self' capture
  global ::= from-type to-type generic-signature? 'Tr'  // obsolete mangling for reabstraction thunk
  global ::= entity generic-signature? type type* 'TK' // key path getter
  global ::= entity generic-signature? type type* 'Tk' // key path setter
  global ::= type generic-signature 'TH' // key path equality
  global ::= type generic-signature 'Th' // key path hasher
  global ::= global generic-signature? 'TJ' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // autodiff function
  global ::= global generic-signature? 'TJV' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // autodiff derivative vtable thunk
  global ::= from-type to-type 'TJO' AUTODIFF-FUNCTION-KIND // autodiff self-reordering reabstraction thunk
  global ::= from-type 'TJS' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' INDEX-SUBSET 'P' // autodiff linear map subset parameters thunk
  global ::= global to-type 'TJS' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' INDEX-SUBSET 'P' // autodiff derivative function subset parameters thunk

  global ::= protocol 'TL'               // protocol requirements base descriptor
  global ::= assoc-type-name 'Tl'        // associated type descriptor
  global ::= assoc-type-name 'TM'        // default associated type witness accessor (HISTORICAL)
  global ::= type assoc-type-list protocol 'Tn' // associated conformance descriptor
  global ::= type assoc-type-list protocol 'TN' // default associated conformance witness accessor
  global ::= type protocol 'Tb'          // base conformance descriptor

  REABSTRACT-THUNK-TYPE ::= 'R'          // reabstraction thunk
  REABSTRACT-THUNK-TYPE ::= 'r'          // reabstraction thunk (obsolete)

  global ::= reabstraction-thunk type 'TU' // reabstraction thunk with global actor constraint

The `from-type` and `to-type` in a reabstraction thunk helper function
are always non-polymorphic ``<impl-function-type>`` types.

::

  VALUE-WITNESS-KIND ::= 'al'           // allocateBuffer
  VALUE-WITNESS-KIND ::= 'ca'           // assignWithCopy
  VALUE-WITNESS-KIND ::= 'ta'           // assignWithTake
  VALUE-WITNESS-KIND ::= 'de'           // deallocateBuffer
  VALUE-WITNESS-KIND ::= 'xx'           // destroy
  VALUE-WITNESS-KIND ::= 'XX'           // destroyBuffer
  VALUE-WITNESS-KIND ::= 'Xx'           // destroyArray
  VALUE-WITNESS-KIND ::= 'CP'           // initializeBufferWithCopyOfBuffer
  VALUE-WITNESS-KIND ::= 'Cp'           // initializeBufferWithCopy
  VALUE-WITNESS-KIND ::= 'cp'           // initializeWithCopy
  VALUE-WITNESS-KIND ::= 'TK'           // initializeBufferWithTakeOfBuffer
  VALUE-WITNESS-KIND ::= 'Tk'           // initializeBufferWithTake
  VALUE-WITNESS-KIND ::= 'tk'           // initializeWithTake
  VALUE-WITNESS-KIND ::= 'pr'           // projectBuffer
  VALUE-WITNESS-KIND ::= 'xs'           // storeExtraInhabitant
  VALUE-WITNESS-KIND ::= 'xg'           // getExtraInhabitantIndex
  VALUE-WITNESS-KIND ::= 'Cc'           // initializeArrayWithCopy
  VALUE-WITNESS-KIND ::= 'Tt'           // initializeArrayWithTakeFrontToBack
  VALUE-WITNESS-KIND ::= 'tT'           // initializeArrayWithTakeBackToFront
  VALUE-WITNESS-KIND ::= 'ug'           // getEnumTag
  VALUE-WITNESS-KIND ::= 'up'           // destructiveProjectEnumData
  VALUE-WITNESS-KIND ::= 'ui'           // destructiveInjectEnumTag
  VALUE-WITNESS-KIND ::= 'et'           // getEnumTagSinglePayload
  VALUE-WITNESS-KIND ::= 'st'           // storeEnumTagSinglePayload

``<VALUE-WITNESS-KIND>`` differentiates the kinds of value
witness functions for a type.

::

  AUTODIFF-FUNCTION-KIND ::= 'f'        // JVP (forward-mode derivative)
  AUTODIFF-FUNCTION-KIND ::= 'r'        // VJP (reverse-mode derivative)
  AUTODIFF-FUNCTION-KIND ::= 'd'        // differential
  AUTODIFF-FUNCTION-KIND ::= 'p'        // pullback

``<AUTODIFF-FUNCTION-KIND>`` differentiates the kinds of functions associated
with a differentiable function used for differentiable programming.

::

  global ::= generic-signature? type 'WOy' // Outlined copy
  global ::= generic-signature? type 'WOe' // Outlined consume
  global ::= generic-signature? type 'WOr' // Outlined retain
  global ::= generic-signature? type 'WOs' // Outlined release
  global ::= generic-signature? type 'WOb' // Outlined initializeWithTake
  global ::= generic-signature? type 'WOc' // Outlined initializeWithCopy
  global ::= generic-signature? type 'WOC' // Outlined initializeWithCopy, not using value witness
  global ::= generic-signature? type 'WOd' // Outlined assignWithTake
  global ::= generic-signature? type 'WOD' // Outlined assignWithTake, not using value witness
  global ::= generic-signature? type 'WOf' // Outlined assignWithCopy
  global ::= generic-signature? type 'WOF' // Outlined assignWithCopy, not using value witness
  global ::= generic-signature? type 'WOh' // Outlined destroy
  global ::= generic-signature? type 'WOH' // Outlined destroy, not using value witness
  global ::= generic-signature? type 'WOi` // Outlined store enum tag
  global ::= generic-signature? type 'WOj` // Outlined enum destructive project
  global ::= generic-signature? type 'WOg` // Outlined enum get tag

Entities
~~~~~~~~

::

  entity ::= nominal-type                    // named type declaration
  entity ::= context entity-spec static? curry-thunk?

  static ::= 'Z'
  curry-thunk ::= 'Tc'

  label-list ::= empty-list            // represents complete absence of parameter labels
  label-list ::= ('_' | identifier)*   // '_' is inserted as placeholder for empty label,
                                       // since the number of labels should match the number of parameters

  // The leading type is the function type
  entity-spec ::= label-list type file-discriminator? 'fC'      // allocating constructor
  entity-spec ::= label-list type file-discriminator? 'fc'      // non-allocating constructor
  entity-spec ::= type 'fU' INDEX            // explicit anonymous closure expression
  entity-spec ::= type 'fu' INDEX            // implicit anonymous closure
  entity-spec ::= 'fA' INDEX                 // default argument N+1 generator
  entity-spec ::= entity 'fa'                // runtime discoverable attribute generator
  entity-spec ::= 'fi'                       // non-local variable initializer
  entity-spec ::= 'fP'                       // property wrapper backing initializer
  entity-spec ::= 'fW'                       // property wrapper init from projected value
  entity-spec ::= 'fD'                       // deallocating destructor; untyped
  entity-spec ::= 'fd'                       // non-deallocating destructor; untyped
  entity-spec ::= 'fE'                       // ivar destroyer; untyped
  entity-spec ::= 'fe'                       // ivar initializer; untyped
  entity-spec ::= 'Tv' NATURAL               // outlined global variable (from context function)
  entity-spec ::= 'Tv' NATURAL 'r'           // outlined global read-only object
  entity-spec ::= 'Te' bridge-spec           // outlined objective c method call

  entity-spec ::= decl-name label-list function-signature generic-signature? 'F'    // function
  entity-spec ::= label-list type file-discriminator? 'i' ACCESSOR                  // subscript
  entity-spec ::= decl-name label-list? type 'v' ACCESSOR                           // variable
  entity-spec ::= decl-name type 'fp'                                               // generic type parameter
  entity-spec ::= decl-name type 'fo'                                               // enum element (currently not used)
  entity-spec ::= decl-name label-list? type generic-signature? 'fm'   // macro
  entity-spec ::= context macro-discriminator-list  // macro expansion
  entity-spec ::= identifier 'Qa'                                                   // associated type declaration

  ACCESSOR ::= 'm'                           // materializeForSet
  ACCESSOR ::= 's'                           // setter
  ACCESSOR ::= 'g'                           // getter
  ACCESSOR ::= 'G'                           // global getter
  ACCESSOR ::= 'w'                           // willSet
  ACCESSOR ::= 'W'                           // didSet
  ACCESSOR ::= 'r'                           // read
  ACCESSOR ::= 'M'                           // modify (temporary)
  ACCESSOR ::= 'a' ADDRESSOR-KIND            // mutable addressor
  ACCESSOR ::= 'l' ADDRESSOR-KIND            // non-mutable addressor
  ACCESSOR ::= 'p'                           // pseudo accessor referring to the storage itself

  ADDRESSOR-KIND ::= 'u'                     // unsafe addressor (no owner)
  ADDRESSOR-KIND ::= 'O'                     // owning addressor (non-native owner), not used anymore
  ADDRESSOR-KIND ::= 'o'                     // owning addressor (native owner), not used anymore
  ADDRESSOR-KIND ::= 'p'                     // pinning addressor (native owner), not used anymore

  decl-name ::= identifier
  decl-name ::= identifier 'L' INDEX                  // locally-discriminated declaration
  decl-name ::= identifier identifier 'LL'            // file-discriminated declaration
  decl-name ::= identifier 'L' RELATED-DISCRIMINATOR  // related declaration

  RELATED-DISCRIMINATOR ::= [a-j]
  RELATED-DISCRIMINATOR ::= [A-J]

  macro-discriminator-list ::= macro-discriminator-list? file-discriminator? macro-expansion-operator INDEX

  macro-expansion-operator ::= decl-name identifier 'fMa' // attached accessor macro
  macro-expansion-operator ::= decl-name identifier 'fMr' // attached member-attribute macro
  macro-expansion-operator ::= identifier 'fMf' // freestanding macro
  macro-expansion-operator ::= decl-name identifier 'fMm' // attached member macro
  macro-expansion-operator ::= decl-name identifier 'fMp' // attached peer macro
  macro-expansion-operator ::= decl-name identifier 'fMc' // attached conformance macro
  macro-expansion-operator ::= decl-name identifier 'fMe' // attached extension macro
  macro-expansion-operator ::= decl-name identifier 'fMq' // attached preamble macro
  macro-expansion-operator ::= decl-name identifier 'fMb' // attached body macro
  macro-expansion-operator ::= decl-name identifier 'fMu' // uniquely-named entity

  file-discriminator ::= identifier 'Ll'     // anonymous file-discriminated declaration

The identifier in a ``<file-discriminator>`` and the second identifier in a
file-discriminated ``<decl-name>`` is a string that represents the file the
original declaration came from. It should be considered unique within the
enclosing module. The first identifier is the name of the entity. Not all
declarations marked ``private`` declarations will use this mangling; if the
entity's context is enough to uniquely identify the entity, the simple
``identifier`` form is preferred.

Twenty operators of the form 'LA', 'LB', etc. are reserved to described
entities related to the entity whose name is provided. For example, 'LE' and
'Le' in the "SC" module are used to represent the structs synthesized by the
Clang importer for various "error code" enums.

Outlined bridged Objective C method call mangling includes which parameters and
return value are bridged and the type of pattern outlined.

::

  bridge-spec ::= bridged-kind bridged-param* bridged-return '_'

  bridged-param ::= 'n' // not bridged parameter
  bridged-param ::= 'b' // bridged parameter

  bridged-return ::= 'n' // not bridged return
  bridged-return ::= 'b' // bridged return

  bridged-kind ::= 'm' // bridged method
  bridged-kind ::= 'a' // bridged property (by address)
  bridged-kind ::= 'p' // bridged property (by value)

Declaration Contexts
~~~~~~~~~~~~~~~~~~~~

These manglings identify the enclosing context in which an entity was declared,
such as its enclosing module, function, or nominal type.

::

  context ::= module
  context ::= entity
  context ::= entity module generic-signature? 'E'

An ``extension`` mangling is used whenever an entity's declaration context is
an extension *and* the entity being extended is in a different module. In this
case the extension's module is mangled first, followed by the entity being
extended. If the extension and the extended entity are in the same module, the
plain ``entity`` mangling is preferred, but not always used. An extension is
considered "constrained" if it:

  - Has any requirements not already satisfied by the extended nominal,
    excluding conformance requirements for invertible protocols.
  - Has any generic parameters with an inverse requirement.

Those requirements included in any of the above are included in the extension's
generic signature. The reason for this additional complexity is that we do not
mangle conformance req's for invertible protocols, only their absence.

::

  struct S<A: ~Copyable, B: ~Copyable> {}

  // An unconstrained extension.
  extension S {}

  // Also an unconstrained extension, because there are no inverses to mangle.
  // This extension is exactly the same as the previous.
  extension S where A: Copyable, B: Copyable {}

  // A constrained extension, because of the added requirement `B: P` that is
  // not already present in S.
  extension S where B: P {}

  // A constrained extension, because of the absence of `A: Copyable`.
  // Despite also being absent in `S`, absences of invertible protocols
  // are always mangled.
  extension S where A: ~Copyable {}

Some entities, like computed properties, rely on the generic signature in their
`context`, so in order to disambiguate between those properties and
those in a context where a generic type requires Copyable, which is not mangled,
we have the following rule:

If the innermost type declaration for an entity has any inverses in its generic
signature, then extension mangling is used. This strategy is used to ensure
that moving a declaration between a nominal type and one of its extensions does
not cause an ABI break if the generic signature of the entity is equivalent in
both circumstances. For example:

::

  struct R<A: ~Copyable> {
    func f1() {} // uses extension mangling, just like `f3`

    func f2() where A: Copyable {}
  }

  extension R where A: ~Copyable {
    func f3() {}

    func f4() where A: Copyable {} // uses entity mangling, just like `f2`
  }

  extension R where A: Copyable {
    // 'f5' is mangled equivalent to 'f2' and 'f4' modulo its identifier.
    func f5() {}
  }

For intermediate nested types, i.e., those between the top level and the entity,
any inverses that remain in at the signature of the entity are mangled into
that entity's generic signature:

::

  struct X<A: ~Copyable> {
    struct Y<B: ~Copyable> {
      // 'g1' uses 'entity' context mangling with and has no mangled signatures.
      func g1() where A: Copyable, B: Copyable {}

      // 'g2' uses 'entity' context mangling. The requirement `B: ~Copyable` is
      //mangled into the generic signature for 'g2'.
      func g2() where A: Copyable {}

      // 'g3' uses extension mangling with generic signature 'A: ~Copyable'.
      // The mangled generic signature of 'g3' is empty.
      func g3() where B: Copyable {}

      // 'g4' uses extension mangling with generic signature 'A: ~Copyable'.
      // The mangled generic signature of 'g4' contains 'B: ~Copyable'.
      func g4() {}
    }
  }


When mangling the context of a local entity within a constructor or
destructor, the non-allocating or non-deallocating variant is used.

::

  module ::= identifier                      // module name
  module ::= known-module                    // abbreviation

  context ::= entity identifier type-list 'XZ' // unknown runtime context

The runtime produces manglings of unknown runtime contexts when a declaration
context has no preserved runtime information, or when a declaration is encoded
in runtime in a way that the current runtime does not understand. These
manglings are unstable and may change between runs of the process.

::

  known-module ::= 's'                       // Swift
  known-module ::= 'SC'                      // Clang-importer-synthesized
  known-module ::= 'So'                      // C and Objective-C

The Objective-C module is used as the context for mangling Objective-C
classes as ``<type>``\ s.


Types
~~~~~

::

  any-generic-type ::= substitution
  any-generic-type ::= context decl-name 'C'     // nominal class type
  any-generic-type ::= context decl-name 'O'     // nominal enum type
  any-generic-type ::= context decl-name 'V'     // nominal struct type
  any-generic-type ::= context decl-name 'XY'    // unknown nominal type
  any-generic-type ::= protocol 'P'              // nominal protocol type

  any-generic-type ::= standard-substitutions

  standard-substitutions ::= 'S' KNOWN-TYPE-KIND       // known nominal type substitution
  standard-substitutions ::= 'S' NATURAL KNOWN-TYPE-KIND    // repeated known type substitutions of the same kind

  KNOWN-TYPE-KIND ::= 'A'                    // Swift.AutoreleasingUnsafeMutablePointer
  KNOWN-TYPE-KIND ::= 'a'                    // Swift.Array
  KNOWN-TYPE-KIND ::= 'B'                    // Swift.BinaryFloatingPoint
  KNOWN-TYPE-KIND ::= 'b'                    // Swift.Bool
  KNOWN-TYPE-KIND ::= 'c' KNOWN-TYPE-KIND-2  // Second set of standard types
  KNOWN-TYPE-KIND ::= 'D'                    // Swift.Dictionary
  KNOWN-TYPE-KIND ::= 'd'                    // Swift.Float64
  KNOWN-TYPE-KIND ::= 'E'                    // Swift.Encodable
  KNOWN-TYPE-KIND ::= 'e'                    // Swift.Decodable
  KNOWN-TYPE-KIND ::= 'F'                    // Swift.FloatingPoint
  KNOWN-TYPE-KIND ::= 'f'                    // Swift.Float32
  KNOWN-TYPE-KIND ::= 'G'                    // Swift.RandomNumberGenerator
  KNOWN-TYPE-KIND ::= 'H'                    // Swift.Hashable
  KNOWN-TYPE-KIND ::= 'h'                    // Swift.Set
  KNOWN-TYPE-KIND ::= 'I'                    // Swift.DefaultIndices
  KNOWN-TYPE-KIND ::= 'i'                    // Swift.Int
  KNOWN-TYPE-KIND ::= 'J'                    // Swift.Character
  KNOWN-TYPE-KIND ::= 'j'                    // Swift.Numeric
  KNOWN-TYPE-KIND ::= 'K'                    // Swift.BidirectionalCollection
  KNOWN-TYPE-KIND ::= 'k'                    // Swift.RandomAccessCollection
  KNOWN-TYPE-KIND ::= 'L'                    // Swift.Comparable
  KNOWN-TYPE-KIND ::= 'l'                    // Swift.Collection
  KNOWN-TYPE-KIND ::= 'M'                    // Swift.MutableCollection
  KNOWN-TYPE-KIND ::= 'm'                    // Swift.RangeReplaceableCollection
  KNOWN-TYPE-KIND ::= 'N'                    // Swift.ClosedRange
  KNOWN-TYPE-KIND ::= 'n'                    // Swift.Range
  KNOWN-TYPE-KIND ::= 'O'                    // Swift.ObjectIdentifier
  KNOWN-TYPE-KIND ::= 'P'                    // Swift.UnsafePointer
  KNOWN-TYPE-KIND ::= 'p'                    // Swift.UnsafeMutablePointer
  KNOWN-TYPE-KIND ::= 'Q'                    // Swift.Equatable
  KNOWN-TYPE-KIND ::= 'q'                    // Swift.Optional
  KNOWN-TYPE-KIND ::= 'R'                    // Swift.UnsafeBufferPointer
  KNOWN-TYPE-KIND ::= 'r'                    // Swift.UnsafeMutableBufferPointer
  KNOWN-TYPE-KIND ::= 'S'                    // Swift.String
  KNOWN-TYPE-KIND ::= 's'                    // Swift.Substring
  KNOWN-TYPE-KIND ::= 'T'                    // Swift.Sequence
  KNOWN-TYPE-KIND ::= 't'                    // Swift.IteratorProtocol
  KNOWN-TYPE-KIND ::= 'U'                    // Swift.UnsignedInteger
  KNOWN-TYPE-KIND ::= 'u'                    // Swift.UInt
  KNOWN-TYPE-KIND ::= 'V'                    // Swift.UnsafeRawPointer
  KNOWN-TYPE-KIND ::= 'v'                    // Swift.UnsafeMutableRawPointer
  KNOWN-TYPE-KIND ::= 'W'                    // Swift.UnsafeRawBufferPointer
  KNOWN-TYPE-KIND ::= 'w'                    // Swift.UnsafeMutableRawBufferPointer
  KNOWN-TYPE-KIND ::= 'X'                    // Swift.RangeExpression
  KNOWN-TYPE-KIND ::= 'x'                    // Swift.Strideable
  KNOWN-TYPE-KIND ::= 'Y'                    // Swift.RawRepresentable
  KNOWN-TYPE-KIND ::= 'y'                    // Swift.StringProtocol
  KNOWN-TYPE-KIND ::= 'Z'                    // Swift.SignedInteger
  KNOWN-TYPE-KIND ::= 'z'                    // Swift.BinaryInteger

  KNOWN-TYPE-KIND-2 ::= 'A'        // Swift.Actor
  KNOWN-TYPE-KIND-2 ::= 'C'        // Swift.CheckedContinuation
  KNOWN-TYPE-KIND-2 ::= 'c'        // Swift.UnsafeContinuation
  KNOWN-TYPE-KIND-2 ::= 'E'        // Swift.CancellationError
  KNOWN-TYPE-KIND-2 ::= 'e'        // Swift.UnownedSerialExecutor
  KNOWN-TYPE-KIND-2 ::= 'F'        // Swift.Executor
  KNOWN-TYPE-KIND-2 ::= 'f'        // Swift.SerialExecutor
  KNOWN-TYPE-KIND-2 ::= 'G'        // Swift.TaskGroup
  KNOWN-TYPE-KIND-2 ::= 'g'        // Swift.ThrowingTaskGroup
  KNOWN-TYPE-KIND-2 ::= 'I'        // Swift.AsyncIteratorProtocol
  KNOWN-TYPE-KIND-2 ::= 'i'        // Swift.AsyncSequence
  KNOWN-TYPE-KIND-2 ::= 'J'        // Swift.UnownedJob
  KNOWN-TYPE-KIND-2 ::= 'M'        // Swift.MainActor
  KNOWN-TYPE-KIND-2 ::= 'P'        // Swift.TaskPriority
  KNOWN-TYPE-KIND-2 ::= 'S'        // Swift.AsyncStream
  KNOWN-TYPE-KIND-2 ::= 's'        // Swift.AsyncThrowingStream
  KNOWN-TYPE-KIND-2 ::= 'T'        // Swift.Task
  KNOWN-TYPE-KIND-2 ::= 't'        // Swift.UnsafeCurrentTask

  protocol ::= context decl-name
  protocol ::= standard-substitutions

  type ::= 'Bb'                              // Builtin.BridgeObject
  type ::= 'BB'                              // Builtin.UnsafeValueBuffer
  #if SWIFT_RUNTIME_VERSION >= 5.5
    type ::= 'Bc'                              // Builtin.RawUnsafeContinuation
    type ::= 'BD'                              // Builtin.DefaultActorStorage
    type ::= 'Be'                              // Builtin.Executor
  #endif
  #if SWIFT_RUNTIME_VERSION >= 5.9
    type ::= 'Bd'                              // Builtin.NonDefaultDistributedActorStorage
  #endif
  type ::= 'Bf' NATURAL '_'                  // Builtin.Float<n>
  type ::= 'Bi' NATURAL '_'                  // Builtin.Int<n>
  type ::= 'BI'                              // Builtin.IntLiteral
  #if SWIFT_RUNTIME_VERSION >= 5.5
    type ::= 'Bj'                              // Builtin.Job
  #endif
  type ::= 'BP'                              // Builtin.PackIndex
  type ::= 'BO'                              // Builtin.UnknownObject (no longer a distinct type, but still used for AnyObject)
  type ::= 'Bo'                              // Builtin.NativeObject
  type ::= 'Bp'                              // Builtin.RawPointer
  type ::= 'Bt'                              // Builtin.SILToken
  type ::= type 'Bv' NATURAL '_'             // Builtin.Vec<n>x<type>
  type ::= 'Bw'                              // Builtin.Word
  type ::= function-signature 'c'            // function type (escaping)
  type ::= function-signature 'X' FUNCTION-KIND // special function type
  type ::= bound-generic-type
  type ::= type 'Sg'                         // optional type, shortcut for: type 'ySqG'
  type ::= type 'Xo'                         // @unowned type
  type ::= type 'Xu'                         // @unowned(unsafe) type
  type ::= type 'Xw'                         // @weak type
  type ::= impl-function-type 'XF'           // function implementation type (currently unused)
  type ::= type 'Xb'                         // SIL @box type (deprecated)
  type ::= type-list 'Xx'                    // SIL box type
  type ::= type-list type-list generic-signature 'XX'
                                             // Generic SIL box type
  type ::= type 'XD'                         // dynamic self type
  type ::= type 'm'                          // metatype without representation
  type ::= type 'XM' METATYPE-REPR           // metatype with representation
  type ::= type 'Xp'                         // existential metatype without representation
  type ::= type 'Xm' METATYPE-REPR           // existential metatype with representation
  type ::= 'Xe'                              // error or unresolved type

  bound-generic-type ::= type 'y' (type* '_')* type* retroactive-conformance* 'G'   // one type-list per nesting level of type
  bound-generic-type ::= substitution

  FUNCTION-KIND ::= 'f'                      // @thin function type
  FUNCTION-KIND ::= 'U'                      // uncurried function type (currently not used)
  FUNCTION-KIND ::= 'K'                      // @auto_closure function type (noescape)
  FUNCTION-KIND ::= 'B'                      // objc block function type
  FUNCTION-KIND ::= 'zB' C-TYPE              // objc block type with non-canonical C type
  FUNCTION-KIND ::= 'L'                      // objc block function type with canonical C type (escaping) (DWARF only; otherwise use 'B' or 'zB' C-TYPE)
  FUNCTION-KIND ::= 'C'                      // C function pointer / C++ method type
  FUNCTION-KIND ::= 'zC' C-TYPE              // C function pointer / C++ method type with non-canonical C type
  FUNCTION-KIND ::= 'A'                      // @auto_closure function type (escaping)
  FUNCTION-KIND ::= 'E'                      // function type (noescape)

  C-TYPE is mangled according to the Itanium ABI, and prefixed with the length.
  Non-ASCII identifiers are preserved as-is; we do not use Punycode.

  function-signature ::= params-type params-type async? sendable? throws? differentiable? function-isolation? self-lifetime-dependence? // results and parameters

  params-type ::= type 'z'? 'h'?             // tuple in case of multiple parameters or a single parameter with a single tuple type
                                             // with optional inout convention, shared convention. parameters don't have labels,
                                             // they are mangled separately as part of the entity.
  params-type ::= empty-list                 // shortcut for no parameters

  #if SWIFT_RUNTIME_VERSION >= 5.5
    async ::= 'Ya'                             // 'async' annotation on function types
    sendable ::= 'Yb'                          // @Sendable on function types
    function-isolation ::= type 'Yc'          // Global actor on function type
  #endif
  throws ::= 'K'                             // 'throws' annotation on function types
  #if SWIFT_RUNTIME_VERSION >= 6.0
    throws ::= type 'YK'                     // 'throws(type)' annotation on function types
    function-isolation ::= type 'YA'         // @isolated(any) on function type
  #endif
  differentiable ::= 'Yjf'                   // @differentiable(_forward) on function type
  differentiable ::= 'Yjr'                   // @differentiable(reverse) on function type
  differentiable ::= 'Yjd'                   // @differentiable on function type
  differentiable ::= 'Yjl'                   // @differentiable(_linear) on function type
 #if SWIFT_RUNTIME_VERSION >= 5.TBD
  lifetime-dependence ::= 'Yli'              // inherit lifetime dependence on param
  lifetime-dependence ::= 'Yls'              // scoped lifetime dependence on param
  self-lifetime-dependence ::= 'YLi'         // inherit lifetime dependence on self
  self-lifetime-dependence ::= 'YLs'         // scoped lifetime dependence on self
#endif
  type-list ::= list-type '_' list-type*     // list of types
  type-list ::= empty-list

                                                  // FIXME: Consider replacing 'h' with a two-char code
  list-type ::= type identifier? 'Yk'? 'z'? 'h'? 'n'? 'Yi'? 'd'? 'Yt'?  // type with optional label, '@noDerivative', inout convention, shared convention, owned convention, actor 'isolated', variadic specifier, and compile-time constant

  METATYPE-REPR ::= 't'                      // Thin metatype representation
  METATYPE-REPR ::= 'T'                      // Thick metatype representation
  METATYPE-REPR ::= 'o'                      // ObjC metatype representation

  existential-layout ::= protocol-list 'p'                 // existential layout
  existential-layout ::= protocol-list superclass 'Xc'     // existential layout with superclass
  existential-layout ::= protocol-list 'Xl'                // existential layout with AnyObject

  type ::= associated-type
  type ::= any-generic-type
  type ::= existential-layout                         // existential type
  type ::= existential-layout requirement '_' requirement* 'XP'   // constrained existential type
  type ::= type-list 't'                     // tuple
  type ::= type generic-signature 'u'        // generic type
  type ::= 'x'                               // generic param, depth=0, idx=0
  type ::= 'q' GENERIC-PARAM-INDEX           // dependent generic parameter
  type ::= type assoc-type-name 'qa'         // associated type of non-generic param
  type ::= assoc-type-name 'Qy' GENERIC-PARAM-INDEX  // associated type
  type ::= assoc-type-name 'Qz'                      // shortcut for 'Qyz'
  type ::= assoc-type-list 'QY' GENERIC-PARAM-INDEX  // associated type at depth
  type ::= assoc-type-list 'QZ'                      // shortcut for 'QYz'
  type ::= opaque-type-decl-name bound-generic-args 'Qo' INDEX // opaque type
  
  type ::= pack-type 'Qe' INDEX              // pack element type
  
  type ::= pattern-type count-type 'Qp'      // pack expansion type
  type ::= pack-element-list 'QP'            // pack type
  type ::= pack-element-list 'QS' DIRECTNESS // SIL pack type

  pack-element-list ::= type '_' type*
  pack-element-list ::= empty-list
  
  #if SWIFT_RUNTIME_VERSION >= 5.2
    type ::= type assoc-type-name 'Qx' // associated type relative to base `type`
    type ::= type assoc-type-list 'QX' // associated type relative to base `type`
  #endif

  #if SWIFT_RUNTIME_VERSION >= 5.7
    type ::= symbolic-extended-existential-type-shape type* retroactive-conformance* 'Xj'
  #endif

  protocol-list ::= protocol '_' protocol*
  protocol-list ::= empty-list

  assoc-type-list ::= assoc-type-name '_' assoc-type-name*

  associated-type ::= substitution
  associated-type ::= type identifier 'Qa' // associated type

  assoc-type-name ::= identifier                // associated type name without protocol
  assoc-type-name ::= identifier protocol 'P'   //

  empty-list ::= 'y'

Associated types use an abbreviated mangling when the base generic parameter
or associated type is constrained by a single protocol requirement. The
associated type in this case can be referenced unambiguously by name alone.
If the base has multiple conformance constraints, then the protocol name is
mangled in to disambiguate.

::

  impl-function-type ::= type* 'I' FUNC-ATTRIBUTES '_'
  impl-function-type ::= type* generic-signature 'I' FUNC-ATTRIBUTES '_'

  FUNC-ATTRIBUTES ::= PATTERN-SUBS? INVOCATION-SUBS? PSEUDO-GENERIC? CALLEE-ESCAPE? ISOLATION? DIFFERENTIABILITY-KIND? CALLEE-CONVENTION FUNC-REPRESENTATION? COROUTINE-KIND? SENDABLE? ASYNC? (PARAM-CONVENTION PARAM-DIFFERENTIABILITY?)* RESULT-CONVENTION* ('Y' PARAM-CONVENTION)* ('z' RESULT-CONVENTION RESULT-DIFFERENTIABILITY?)?

  PATTERN-SUBS ::= 's'                       // has pattern substitutions
  INVOCATION-SUB ::= 'I'                     // has invocation substitutions
  PSEUDO-GENERIC ::= 'P'

  CALLEE-ESCAPE ::= 'e'                      // @escaping (inverse of SIL @noescape)

  ISOLATION ::= 'A'                          // @isolated(any)

  DIFFERENTIABILITY-KIND ::= 'd'             // @differentiable
  DIFFERENTIABILITY-KIND ::= 'l'             // @differentiable(_linear)
  DIFFERENTIABILITY-KIND ::= 'f'             // @differentiable(_forward)
  DIFFERENTIABILITY-KIND ::= 'r'             // @differentiable(reverse)

  CALLEE-CONVENTION ::= 'y'                  // @callee_unowned
  CALLEE-CONVENTION ::= 'g'                  // @callee_guaranteed
  CALLEE-CONVENTION ::= 'x'                  // @callee_owned
  CALLEE-CONVENTION ::= 't'                  // thin

  FUNC-REPRESENTATION ::= 'B'                // C block invocation function
  FUNC-REPRESENTATION ::= 'zB' C-TYPE        // C block invocation function with non-canonical C type
  FUNC-REPRESENTATION ::= 'C'                // C global function
  FUNC-REPRESENTATION ::= 'zC' C-TYPE        // C global function with non-canonical C type
  FUNC-REPRESENTATION ::= 'M'                // Swift method
  FUNC-REPRESENTATION ::= 'J'                // ObjC method
  FUNC-REPRESENTATION ::= 'K'                // closure
  FUNC-REPRESENTATION ::= 'W'                // protocol witness

  COROUTINE-KIND ::= 'A'                     // yield-once coroutine
  COROUTINE-KIND ::= 'G'                     // yield-many coroutine

  #if SWIFT_RUNTIME_VERSION >= 5.5
    SENDABLE ::= 'h'                           // @Sendable
    ASYNC ::= 'H'                              // @async
  #endif

  PARAM-CONVENTION ::= 'i'                   // indirect in
  PARAM-CONVENTION ::= 'c'                   // indirect in constant
  PARAM-CONVENTION ::= 'l'                   // indirect inout
  PARAM-CONVENTION ::= 'b'                   // indirect inout aliasable
  PARAM-CONVENTION ::= 'n'                   // indirect in guaranteed
  PARAM-CONVENTION ::= 'x'                   // direct owned
  PARAM-CONVENTION ::= 'y'                   // direct unowned
  PARAM-CONVENTION ::= 'g'                   // direct guaranteed
  PARAM-CONVENTION ::= 'e'                   // direct deallocating
  PARAM-CONVENTION ::= 'v'                   // pack owned
  PARAM-CONVENTION ::= 'p'                   // pack guaranteed
  PARAM-CONVENTION ::= 'm'                   // pack inout

  PARAM-DIFFERENTIABILITY ::= 'w'            // @noDerivative

  RESULT-CONVENTION ::= 'r'                  // indirect
  RESULT-CONVENTION ::= 'o'                  // owned
  RESULT-CONVENTION ::= 'd'                  // unowned
  RESULT-CONVENTION ::= 'u'                  // unowned inner pointer
  RESULT-CONVENTION ::= 'a'                  // auto-released
  RESULT-CONVENTION ::= 'k'                  // pack

  RESULT-DIFFERENTIABILITY ::= 'w'            // @noDerivative

  DIRECTNESS ::= 'i'                         // indirect
  DIRECTNESS ::= 'd'                         // direct

For the most part, manglings follow the structure of formal language
types.  However, in some cases it is more useful to encode the exact
implementation details of a function type.

::

  #if SWIFT_VERSION >= 5.1
    type ::= 'Qr'                         // opaque result type (of current decl, used for the first opaque type parameter only)
    type ::= 'QR' INDEX                   // same as above, for subsequent opaque type parameters, INDEX is the ordinal -1
    type ::= opaque-type-decl-name bound-generic-args 'Qo' INDEX // opaque type

    opaque-type-decl-name ::= entity 'QO' // opaque result type of specified decl
  #endif

  #if SWIFT_VERSION >= 5.4
    type ::= 'Qu'                         // opaque result type (of current decl, first param)
                                          // used for ObjC class runtime name purposes.
    type ::= 'QU' INDEX
  #endif

Opaque return types have a special short representation in the mangling of
their defining entity. In structural position, opaque types are fully qualified
by mangling the defining entity for the opaque declaration and the substitutions
into the defining entity's generic environment.

The ``type*`` list contains parameter and return types (including the error
result), in that order.
The number of parameters and results must match with the number of
``<PARAM-CONVENTION>`` and ``<RESULT-CONVENTION>`` characters after the
``<FUNC-REPRESENTATION>``.
The ``<generic-signature>`` is used if the function is polymorphic.

DWARF debug info and USRs also mangle sugared types, adding the following
productions:

::

  any-generic-type ::= context decl-name 'a'     // typealias type
  type ::= base-type "XSq"                       // sugared Optional type
  type ::= base-type "XSa"                       // sugared Array type
  type ::= key-type value-type "XSD"             // sugared Dictionary type
  type ::= base-type "XSp"                       // sugared Paren type

Generics
~~~~~~~~

::

  protocol-conformance-context ::= protocol module generic-signature?

  protocol-conformance ::= type protocol-conformance-context

``<protocol-conformance>`` refers to a type's conformance to a protocol. The
named module is the one containing the extension or type declaration that
declared the conformance.

::

  protocol-conformance ::= type protocol

If ``type`` is a generic parameter or associated type of one, then no module
is mangled, because the conformance must be resolved from the generic
environment.

  protocol-conformance ::= context identifier protocol identifier generic-signature?  // Property behavior conformance

Property behaviors are implemented using private protocol conformances.

::

  concrete-protocol-conformance ::= type protocol-conformance-ref any-protocol-conformance-list 'HC'
  protocol-conformance-ref ::= protocol 'HP'   // same module as conforming type
  protocol-conformance-ref ::= protocol 'Hp'   // same module as protocol
  protocol-conformance-ref ::= protocol module // "retroactive"

  any-protocol-conformance ::= concrete-protocol-conformance
  any-protocol-conformance ::= dependent-protocol-conformance
  any-protocol-conformance ::= pack-protocol-conformance

  any-protocol-conformance-list ::= any-protocol-conformance '_' any-protocol-conformance-list
  any-protocol-conformance-list ::= empty-list

  DEPENDENT-CONFORMANCE-INDEX ::= INDEX

  dependent-protocol-conformance ::= type protocol 'HD' DEPENDENT-CONFORMANCE-INDEX
  dependent-protocol-conformance ::= dependent-protocol-conformance protocol 'HI' DEPENDENT-CONFORMANCE-INDEX
  dependent-protocol-conformance ::= dependent-protocol-conformance
      dependent-associated-conformance 'HA' DEPENDENT-CONFORMANCE-INDEX

  dependent-associated-conformance ::= type protocol
  dependent-protocol-conformance ::= dependent-protocol-conformance opaque-type 'HO'

  pack-protocol-conformance ::= any-protocol-conformance-list 'HX'

A compact representation used to represent mangled protocol conformance witness
arguments at runtime. The ``module`` is only specified for conformances that
are "retroactive", meaning that the context in which the conformance is defined
is in neither the protocol or type module. For a non-retroactive conformance
where both the type *and* the protocol are in the same module, or for
synthesized conformances that have no owning module, the "HP" operator is
preferred. The concrete protocol conformances that follow are for the
conditional conformance requirements.

Dependent protocol conformances mangle the access path required to extract a
protocol conformance from some conformance passed into the environment. The
first case (operator "HD") is the leaf requirement, containing a dependent type
and the protocol it conforms to. The remaining dependent protocol conformance
manglings describe lookups performed on their child dependent protocol
conformances. The "HI" operator retrieves the named inherited protocol from the
witness table produced by the child. The "HA" operator refers to an associated
conformance within the witness table, identified by the dependent type and
protocol. In all cases, the DEPENDENT-CONFORMANCE-INDEX is an INDEX value
indicating the position of the appropriate value within the generic environment
(for "HD") or witness table (for "HI" and "HA") when it is known to be at a
fixed position. An index of 1 ("0\_") is used to indicate "unknown"; all other
values are adjusted by 2. That these indexes are not 0-based is a bug that's
now codified into the ABI; the index 0 is therefore reserved.

::

  generic-signature ::= requirement* generic-param-pack-marker* 'l'     // one generic parameter
  generic-signature ::= requirement* generic-param-pack-marker* 'r' GENERIC-PARAM-COUNT* 'l'

  generic-param-pack-marker ::= 'Rv' GENERIC_PARAM-INDEX   // generic parameter pack marker

  GENERIC-PARAM-COUNT ::= 'z'                // zero parameters
  GENERIC-PARAM-COUNT ::= INDEX              // N+1 parameters

  requirement ::= protocol 'R' GENERIC-PARAM-INDEX                  // protocol requirement
  requirement ::= protocol assoc-type-name 'Rp' GENERIC-PARAM-INDEX // protocol requirement on associated type
  requirement ::= protocol assoc-type-list 'RP' GENERIC-PARAM-INDEX // protocol requirement on associated type at depth
  requirement ::= protocol substitution 'RQ'                        // protocol requirement with substitution
#if SWIFT_RUNTIME_VERSION >= 6.0
  requirement ::= 'Ri' INDEX GENERIC-PARAM-INDEX                    // inverse requirement on generic parameter where INDEX is the bit number
  requirement ::= substitution 'RI' INDEX                           // inverse requirement with substitution
  requirement ::= assoc-type-name 'Rj' INDEX GENERIC-PARAM-INDEX    // inverse requirement on associated type
  requirement ::= assoc-type-list 'RJ' INDEX GENERIC-PARAM-INDEX    // inverse requirement on associated type at depth
#endif
  requirement ::= type 'Rb' GENERIC-PARAM-INDEX                     // base class requirement
  requirement ::= type assoc-type-name 'Rc' GENERIC-PARAM-INDEX     // base class requirement on associated type
  requirement ::= type assoc-type-list 'RC' GENERIC-PARAM-INDEX     // base class requirement on associated type at depth
  requirement ::= type substitution 'RB'                            // base class requirement with substitution
  requirement ::= type 'Rs' GENERIC-PARAM-INDEX                     // same-type requirement
  requirement ::= type assoc-type-name 'Rt' GENERIC-PARAM-INDEX     // same-type requirement on associated type
  requirement ::= type assoc-type-list 'RT' GENERIC-PARAM-INDEX     // same-type requirement on associated type at depth
  requirement ::= type substitution 'RS'                            // same-type requirement with substitution
  requirement ::= type 'Rl' GENERIC-PARAM-INDEX LAYOUT-CONSTRAINT   // layout requirement
  requirement ::= type assoc-type-name 'Rm' GENERIC-PARAM-INDEX LAYOUT-CONSTRAINT    // layout requirement on associated type
  requirement ::= type assoc-type-list 'RM' GENERIC-PARAM-INDEX LAYOUT-CONSTRAINT    // layout requirement on associated type at depth
  requirement ::= type substitution 'RM' LAYOUT-CONSTRAINT                           // layout requirement with substitution

  requirement ::= type 'Rh' GENERIC-PARAM-INDEX                     // same-shape requirement (only supported on a generic parameter)

  GENERIC-PARAM-INDEX ::= 'z'                // depth = 0,   idx = 0
  GENERIC-PARAM-INDEX ::= INDEX              // depth = 0,   idx = N+1
  GENERIC-PARAM-INDEX ::= 'd' INDEX INDEX    // depth = M+1, idx = N
  GENERIC-PARAM-INDEX ::= 's'                // depth = 0,   idx = 0; Constrained existential 'Self' type

  LAYOUT-CONSTRAINT ::= 'N'  // NativeRefCountedObject
  LAYOUT-CONSTRAINT ::= 'R'  // RefCountedObject
  LAYOUT-CONSTRAINT ::= 'T'  // Trivial
  LAYOUT-CONSTRAINT ::= 'C'  // Class
  LAYOUT-CONSTRAINT ::= 'D'  // NativeClass
  LAYOUT-CONSTRAINT ::= 'E' LAYOUT-SIZE-AND-ALIGNMENT  // Trivial of exact size
  LAYOUT-CONSTRAINT ::= 'e' LAYOUT-SIZE  // Trivial of exact size
  LAYOUT-CONSTRAINT ::= 'M' LAYOUT-SIZE-AND-ALIGNMENT  // Trivial of size at most N bits
  LAYOUT-CONSTRAINT ::= 'm' LAYOUT-SIZE  // Trivial of size at most N bits
  LAYOUT-CONSTRAINT ::= 'U'  // Unknown layout
  LAYOUT-CONSTRAINT ::= 'B' // BridgeObject
  LAYOUT-CONSTRAINT ::= 'S' // TrivialStride

  LAYOUT-SIZE ::= INDEX // Size only
  LAYOUT-SIZE-AND-ALIGNMENT ::= INDEX INDEX // Size followed by alignment

A generic signature begins with an optional list of requirements.

This is followed by an optional list of generic-param-pack-markers to record
which generic parameters are packs (variadic).

The ``<GENERIC-PARAM-COUNT>`` describes the number of generic parameters at
each depth of the signature. As a special case, no ``<GENERIC-PARAM-COUNT>``
values indicates a single generic parameter at the outermost depth::

  x_xCru                           // <T_0_0> T_0_0 -> T_0_0
  d_0__xCr_0_u                     // <T_0_0><T_1_0, T_1_1> T_0_0 -> T_1_1

A generic signature must only precede an operator character which is different
from any character in a ``<GENERIC-PARAM-COUNT>``.

::

  retroactive-conformance ::= any-protocol-conformance 'g' INDEX

When a protocol conformance used to satisfy one of a bound generic type's
generic requirements is retroactive (i.e., it is specified in a module other
than the module of the conforming type or the conformed-to protocol), it is
mangled with its offset into the set of conformance requirements, the
root protocol conformance, and the suffix 'g'.

::

  // No generalization signature.
  extended-existential-shape ::= type 'Xg' // no generalization signature
  extended-existential-shape ::= generic-signature type 'XG'

Identifiers
~~~~~~~~~~~

::

  identifier ::= substitution
  identifier ::= NATURAL IDENTIFIER-STRING   // identifier without word substitutions
  identifier ::= '0' IDENTIFIER-PART         // identifier with word substitutions

  IDENTIFIER-PART ::= NATURAL IDENTIFIER-STRING
  IDENTIFIER-PART ::= [a-z]                  // word substitution (except the last one)
  IDENTIFIER-PART ::= [A-Z]                  // last word substitution in identifier

  IDENTIFIER-STRING ::= IDENTIFIER-START-CHAR IDENTIFIER-CHAR*
  IDENTIFIER-START-CHAR ::= [_a-zA-Z]
  IDENTIFIER-CHAR ::= [_$a-zA-Z0-9]

``<identifier>`` is run-length encoded: the natural indicates how many
characters follow. Operator characters are mapped to letter characters as
given. In neither case can an identifier start with a digit, so
there's no ambiguity with the run-length.

If the run-length start with a ``0`` the identifier string contains
word substitutions. A word is a sub-string of an identifier which contains
letters and digits ``[A-Za-z0-9]``. Words are separated by underscores
``_``. In addition a new word begins with an uppercase letter ``[A-Z]``
if the previous character is not an uppercase letter::

  Abc1DefG2HI          // contains four words 'Abc1', 'Def' and 'G2' and 'HI'
  _abc1_def_G2hi       // contains three words 'abc1', 'def' and G2hi

The words of all identifiers, which are encoded in the current mangling are
enumerated and assigned to a letter: a = first word, b = second word, etc.

An identifier containing word substitutions is a sequence of run-length encoded
sub-strings and references to previously mangled words.
All but the last word-references are lowercase letters and the last one is an
uppercase letter. If there is no literal sub-string after the last
word-reference, the last word-reference is followed by a ``0``.

Let's assume the current mangling already encoded the identifier ``AbcDefGHI``::

  02Myac1_B    // expands to: MyAbcGHI_Def

A maximum of 26 words in a mangling can be used for substitutions.

::

  identifier ::= '00' natural '_'? IDENTIFIER-CHAR+  // '_' is inserted if the identifier starts with a digit or '_'.

Identifiers that contain non-ASCII characters are encoded using the Punycode
algorithm specified in RFC 3492, with the modifications that ``_`` is used
as the encoding delimiter, and uppercase letters A through J are used in place
of digits 0 through 9 in the encoding character set. The mangling then
consists of an ``00`` followed by the run length of the encoded string and the
encoded string itself. For example, the identifier ``vergüenza`` is mangled
to ``0012vergenza_JFa``. (The encoding in standard Punycode would be
``vergenza-95a``)

If the encoded string starts with a digit or an ``_``, an additional ``_`` is
inserted between the run length and the encoded string.

::

  identifier ::= identifier 'o' OPERATOR-FIXITY

  OPERATOR-FIXITY ::= 'p'                    // prefix operator
  OPERATOR-FIXITY ::= 'P'                    // postfix operator
  OPERATOR-FIXITY ::= 'i'                    // infix operator

  OPERATOR-CHAR ::= 'a'                      // & 'and'
  OPERATOR-CHAR ::= 'c'                      // @ 'commercial at'
  OPERATOR-CHAR ::= 'd'                      // / 'divide'
  OPERATOR-CHAR ::= 'e'                      // = 'equals'
  OPERATOR-CHAR ::= 'g'                      // > 'greater'
  OPERATOR-CHAR ::= 'l'                      // < 'less'
  OPERATOR-CHAR ::= 'm'                      // * 'multiply'
  OPERATOR-CHAR ::= 'n'                      // ! 'not'
  OPERATOR-CHAR ::= 'o'                      // | 'or'
  OPERATOR-CHAR ::= 'p'                      // + 'plus'
  OPERATOR-CHAR ::= 'q'                      // ? 'question'
  OPERATOR-CHAR ::= 'r'                      // % 'remainder'
  OPERATOR-CHAR ::= 's'                      // - 'subtract'
  OPERATOR-CHAR ::= 't'                      // ~ 'tilde'
  OPERATOR-CHAR ::= 'x'                      // ^ 'xor'
  OPERATOR-CHAR ::= 'z'                      // . 'zperiod'

If an identifier is followed by an ``o`` its text is interpreted as an
operator. Each lowercase character maps to an operator character
(``OPERATOR-CHAR``).

Operators that contain non-ASCII characters are mangled by first mapping the
ASCII operator characters to letters as for pure ASCII operator names, then
Punycode-encoding the substituted string.
For example, the infix operator ``«+»`` is mangled to
``007p_qcaDcoi`` (``p_qcaDc`` being the encoding of the substituted
string ``«p»``).

Substitutions
~~~~~~~~~~~~~

::

  substitution ::= 'A' INDEX                  // substitution of N+26
  substitution ::= 'A' SUBST_IDX* LAST-SUBST-IDX    // One or more consecutive substitutions of N < 26
  SUBST-IDX ::= [a-z]
  SUBST-IDX ::= NATURAL [a-z]
  LAST-SUBST-IDX ::= [A-Z]
  LAST-SUBST-IDX ::= NATURAL [A-Z]


``<substitution>`` is a back-reference to a previously mangled entity. The mangling
algorithm maintains a mapping of entities to substitution indices as it runs.
When an entity that can be represented by a substitution (a module, nominal
type, or protocol) is mangled, a substitution is first looked for in the
substitution map, and if it is present, the entity is mangled using the
associated substitution index. Otherwise, the entity is mangled normally, and
it is then added to the substitution map and associated with the next
available substitution index.

For example, in mangling a function type
``(zim.zang.zung, zim.zang.zung, zim.zippity) -> zim.zang.zoo`` (with module
``zim`` and class ``zim.zang``),
the recurring contexts ``zim``, ``zim.zang``, and ``zim.zang.zung``
will be mangled using substitutions after being mangled
for the first time. The first argument type will mangle in long form,
``3zim4zang4zung``, and in doing so, ``zim`` will acquire substitution ``AA``,
``zim.zang`` will acquire substitution ``AB``, and ``zim.zang.zung`` will
acquire ``AC``. The second argument is the same as the first and will mangle
using its substitution, ``AC``. The
third argument type will mangle using the substitution for ``zim``,
``AA7zippity``. (It also acquires substitution ``AD`` which would be used
if it mangled again.) The result type will mangle using the substitution for
``zim.zang``, ``AB3zoo`` (and acquire substitution ``AE``).

There are some pre-defined substitutions, see ``KNOWN-TYPE-KIND``.

If the mangling contains two or more consecutive substitutions, it can be
abbreviated with the ``A`` substitution. Similar to word-substitutions the
index is encoded as letters, whereas the last letter is uppercase::

  AaeB      // equivalent to A_A4_A0_

Repeated substitutions are encoded with a natural prefix number::

  A3a2B     // equivalent to AaaabB

Numbers and Indexes
~~~~~~~~~~~~~~~~~~~

::

  INDEX ::= '_'                               // 0
  INDEX ::= NATURAL '_'                       // N+1
  NATURAL ::= [1-9] [0-9]*
  NATURAL_ZERO ::= [0-9]+

``<INDEX>`` is a production for encoding numbers in contexts that can't
end in a digit; it's optimized for encoding smaller numbers.

::

  INDEX-SUBSET ::= ('S' | 'U')+

``<INDEX-SUBSET>`` is encoded like a bit vector and is optimized for encoding
indices with a small upper bound.

Function Specializations
~~~~~~~~~~~~~~~~~~~~~~~~

::

  specialization ::= type '_' type* 'Tg' SPEC-INFO     // Generic re-abstracted specialization
  specialization ::= type '_' type* 'TB' SPEC-INFO     // Alternative mangling for generic re-abstracted specializations,
                                                       // used for functions with re-abstracted resilient parameter types.
  specialization ::= type '_' type* 'Ts' SPEC-INFO     // Generic re-abstracted prespecialization
  specialization ::= type '_' type* 'TG' SPEC-INFO     // Generic not re-abstracted specialization
  specialization ::= type '_' type* 'Ti' SPEC-INFO     // Inlined function with generic substitutions.
  specialization ::= type '_' type* 'Ta' SPEC-INFO     // Non-async specialization

The types are the replacement types of the substitution list.

::

  specialization ::= type 'Tp' SPEC-INFO // Partial generic specialization
  specialization ::= type 'TP' SPEC-INFO // Partial generic specialization, not re-abstracted

The type is the function type of the specialized function.

::

  specialization ::= spec-arg* 'Tf' SPEC-INFO ARG-SPEC-KIND* '_' ARG-SPEC-KIND  // Function signature specialization kind

The ``<ARG-SPEC-KIND>`` describes how arguments are specialized.
Some kinds need arguments, which precede ``Tf``.

::

  spec-arg ::= identifier
  spec-arg ::= type

  SPEC-INFO ::= MT-REMOVED? FRAGILE? ASYNC-REMOVED? PASSID

  PASSID ::= '0'                             // AllocBoxToStack,
  PASSID ::= '1'                             // ClosureSpecializer,
  PASSID ::= '2'                             // CapturePromotion,
  PASSID ::= '3'                             // CapturePropagation,
  PASSID ::= '4'                             // FunctionSignatureOpts,
  PASSID ::= '5'                             // GenericSpecializer,
  PASSID ::= '6'                             // MoveDiagnosticInOutToOut,
  PASSID ::= '7'                             // AsyncDemotion,

  MT-REMOVED ::= 'm'                         // non-generic metatype arguments are removed in the specialized function

  FRAGILE ::= 'q'

  ASYNC-REMOVED ::= 'a'                      // async effect removed

  ARG-SPEC-KIND ::= 'n'                      // Unmodified argument
  ARG-SPEC-KIND ::= 'c'                      // Consumes n 'type' arguments which are closed over types in argument order
                                             // and one 'identifier' argument which is the closure symbol name
  ARG-SPEC-KIND ::= 'p' CONST-PROP           // Constant propagated argument
  ARG-SPEC-KIND ::= 'e' 'D'? 'G'? 'X'?       // Generic argument, with optional dead, owned=>guaranteed or exploded-specifier
  ARG-SPEC-KIND ::= 'd' 'G'? 'X'?            // Dead argument, with optional owned=>guaranteed or exploded-specifier
  ARG-SPEC-KIND ::= 'g' 'X'?                 // Owned => Guaranteed,, with optional exploded-specifier
  ARG-SPEC-KIND ::= 'x'                      // Exploded
  ARG-SPEC-KIND ::= 'i'                      // Box to value
  ARG-SPEC-KIND ::= 's'                      // Box to stack

  CONST-PROP ::= 'f'                         // Consumes one identifier argument which is a function symbol name
  CONST-PROP ::= 'g'                         // Consumes one identifier argument which is a global symbol name
  CONST-PROP ::= 'i' NATURAL_ZERO            // 64-bit-integer
  CONST-PROP ::= 'd' NATURAL_ZERO            // float-as-64-bit-integer
  CONST-PROP ::= 's' ENCODING                // string literal. Consumes one identifier argument.
  CONST-PROP ::= 'k'                         // keypath. Consumes one identifier - the SHA1 of the keypath and two types (root and value).

  ENCODING ::= 'b'                           // utf8
  ENCODING ::= 'w'                           // utf16
  ENCODING ::= 'c'                           // utf16

If the first character of the string literal is a digit ``[0-9]`` or an
underscore ``_``, the identifier for the string literal is prefixed with an
additional underscore ``_``.

Conventions for foreign symbols
-------------------------------

Swift interoperates with multiple other languages - C, C++, Objective-C, and
Objective-C++. Each of these languages defines their own mangling conventions,
so Swift must take care to follow them. However, these conventions do not cover
Swift-specific symbols like Swift type metadata for foreign types, so Swift uses
its own mangling scheme for those symbols.

Importing C and C++ structs
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Types imported from C and C++ are imported as if they are located in the ``__C``
module, regardless of the actual Clang module that they are coming from. This
can be observed when mangling a Swift function that accepts a C/C++ struct as a
parameter:

C++ module ``CxxStructModule``:

.. code-block:: c++

  struct CxxStruct {};

  inline void cxxFunction(CxxStruct s) {}

Swift module ``main`` that imports ``CxxStructModule``:

.. code-block:: swift

  import CxxStructModule

  public func swiftFunction(_ s: CxxStruct) {}

Resulting symbols (showing only Itanium-mangled C++ symbols for brevity):

.. code::

  _Z11cxxFunction9CxxStruct // -> cxxFunction(CxxStruct)
  s4main13swiftFunctionyySo9CxxStructVF // -> main.swiftFunction(__C.CxxStruct) -> ()

The reason for ignoring the Clang module and always putting C and C++ types into
``__C`` at the Swift ABI level is that the Clang module is not a part of the C
or C++ ABI. When owners of C and C++ Clang modules decide what changes are
ABI-compatible or not, they will likely take into account C and C++ ABI, but not
the Swift ABI. Therefore, Swift ABI can only encode information about a C or C++
type that the C and C++ ABI already encodes in order to remain compatible with
future versions of libraries that evolve according to C and C++ ABI
compatibility principles.

The C/C++ compiler does not generate Swift metadata symbols and value witness
tables for C and C++ types. To make a foreign type usable in Swift in the same
way as a native type, the Swift compiler must generate these symbols.
Specifically, each Swift module that uses a given C or C++ type generates the
necessary Swift symbols. For the example above the Swift compiler will generate following
nominal type descriptor symbol for ``CxxStruct`` while compiling the ``main`` module:

.. code::

  sSo9CxxStructVMn // -> nominal type descriptor for __C.CxxStruct

Importing C++ class template instantiations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A class template instantiation is imported as a struct named
``__CxxTemplateInst`` plus Itanium mangled type of the instantiation (see the
``type`` production in the Itanium specification). Note that Itanium mangling is
used on all platforms, regardless of the ABI of the C++ toolchain, to ensure
that the mangled name is a valid Swift type name (this is not the case for MSVC
mangled names). A prefix with a double underscore (to ensure we have a reserved
C++ identifier) is added to limit the possibility for conflicts with names of
user-defined structs. The struct is notionally defined in the ``__C`` module,
similarly to regular C and C++ structs and classes. Consider the following C++
module:

.. code-block:: c++

  template<class T>
  struct MagicWrapper {
    T t;
  };

  struct MagicNumber {};

  typedef MagicWrapper<MagicNumber> WrappedMagicNumber;

``WrappedMagicNumber`` is imported as a typealias for struct
``__CxxTemplateInst12MagicWrapperI11MagicNumberE``. Interface of the imported
module looks as follows:

.. code-block:: swift

  struct __CxxTemplateInst12MagicWrapperI11MagicNumberE {
    var t: MagicNumber
  }
  struct MagicNumber {}
  typealias WrappedMagicNumber = __CxxTemplateInst12MagicWrapperI11MagicNumberE