lower-lang 0.4.1

Lowers lossless syntax trees into HIR through declarative semantic archetypes and hygienic templates.
Documentation
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
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
//! The archetype catalogue: the semantic forms a construct can be declared as.
//!
//! Each variant of [`Arch`] is one archetype from LexerSketch's semantic layer
//! (ARCHITECTURE §5): binders, scopes, declarations, control, expressions,
//! patterns, and types. Its fields are [`Pick`]s that say where each part of
//! the construct sits among the node's children. Flags that a construct
//! spells with keywords (`mut`, `async`, `static`, `pub`, `...`, `&`) are not
//! fields: they are read from the node's direct children through the
//! mapping's [modifier table](crate::MappingBuilder::modifier).

use hir_lang::CaptureMode;

use crate::mapping::Join;
use crate::pick::Pick;
use crate::template::TemplateId;

/// How a record or variant's field layout is decided.
///
/// # Examples
///
/// ```
/// use lower_lang::RecordShape;
///
/// assert_eq!(RecordShape::default(), RecordShape::Auto);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum RecordShape {
    /// Named when any field has a name or there are no fields; tuple-like when
    /// every field is unnamed.
    #[default]
    Auto,
    /// `{ x: T }`.
    Named,
    /// `(T, U)`.
    Tuple,
    /// No fields at all (`struct S;`, a bare enum variant).
    Unit,
}

/// A use of a registered template as a node's archetype, made by
/// [`MappingBuilder::template_node`](crate::MappingBuilder::template_node).
///
/// The template's holes are bound, in declaration order, to picks stored in
/// the mapping.
///
/// # Examples
///
/// ```
/// use lower_lang::{templates, Arch, MappingBuilder, Pick};
///
/// let mut b = MappingBuilder::<u8>::new();
/// let while_loop = b.template(templates::while_loop())?;
/// b.template_node(4, while_loop, &[Pick::NthNode(0), Pick::NthNode(1), Pick::Absent]);
/// let mapping = b.build()?;
/// assert!(matches!(mapping.rule_for(&4), Some(Arch::Template(_))));
/// # Ok::<(), lower_lang::MappingError>(())
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TemplateUse {
    pub(crate) template: TemplateId,
    pub(crate) first: u32,
    pub(crate) len: u32,
}

impl TemplateUse {
    /// The template this node instantiates.
    ///
    /// # Examples
    ///
    /// ```
    /// use lower_lang::{templates, Arch, MappingBuilder, Pick};
    ///
    /// let mut b = MappingBuilder::<u8>::new();
    /// let t = b.template(templates::logical_and())?;
    /// b.template_node(1, t, &[Pick::Nth(0), Pick::Nth(2)]);
    /// let m = b.build()?;
    /// let Some(Arch::Template(used)) = m.rule_for(&1) else { unreachable!() };
    /// assert_eq!(used.template(), t);
    /// # Ok::<(), lower_lang::MappingError>(())
    /// ```
    #[must_use]
    pub const fn template(&self) -> TemplateId {
        self.template
    }
}

/// A semantic archetype: what a syntax node *means*, and where its parts are.
///
/// A [`Mapping`](crate::Mapping) maps node kinds to archetypes. Lowering a node
/// lowers each field in the context the archetype gives it (an `If`'s `cond`
/// as an expression, a `Fn`'s `params` as parameters) and builds the HIR form.
/// Fields marked *required* that a node lacks become error nodes plus a
/// [`MissingField`](crate::ProblemKind::MissingField) problem; the others are
/// simply left out. List fields take every match of their pick.
///
/// The modifiers each archetype reads from its direct children are listed
/// with it (see [`Modifier`](crate::Modifier)).
///
/// # Examples
///
/// ```
/// use lower_lang::{Arch, Pick};
///
/// // `if COND BLOCK (else (if_expr | BLOCK))?` with no field labels:
/// let if_expr: Arch<u16> = Arch::If {
///     cond: Pick::NthNode(0),
///     then: Pick::NthNode(1),
///     else_: Pick::NthNode(2),
/// };
/// assert!(matches!(if_expr, Arch::If { .. }));
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Arch<K> {
    // ------------------------------------------------------------ structure
    /// A module: the root of a file, or a nested module. Declarations among
    /// `items` become the module's items; everything else (statements and
    /// expressions) becomes its top-level body, in order (scripts, REPL
    /// entries, conditional definitions).
    Module {
        /// The module's name (nested modules).
        name: Pick<K>,
        /// List: declarations and top-level statements.
        items: Pick<K>,
    },
    /// A wrapper with one meaningful child (`expr`, a parenthesized group, a
    /// statement wrapper): lowered as that child, in the same context.
    Pass {
        /// Required: the child.
        inner: Pick<K>,
    },
    /// Several statements, items, or class members from one node
    /// (`let a = 1, b = 2;`), spliced into the enclosing list.
    Splice {
        /// List: the parts, lowered in the enclosing list's context.
        items: Pick<K>,
    },
    /// A construct the language has but does not lower yet: an error node
    /// plus an [`Unsupported`](crate::ProblemKind::Unsupported) problem.
    Unsupported,

    // ------------------------------------------------- statements and binders
    /// `let PAT: TY = INIT else ELSE;` — binds the pattern's names for the
    /// following statements. Modifiers: `Mut` makes the binders mutable.
    Let {
        /// Required: the pattern (an identifier binds it).
        pat: Pick<K>,
        /// The annotation.
        ty: Pick<K>,
        /// The initializer.
        init: Pick<K>,
        /// The diverging `else` of let-else.
        else_: Pick<K>,
        /// Whether the binders are mutable without a `Mut` modifier (`var`,
        /// JavaScript `let`) rather than only with one (`let` / `let mut`).
        mutable: bool,
        /// Function-scoped hoisting (JavaScript `var`): the name is declared
        /// once at the top of the enclosing function (or module body) and this
        /// statement becomes an assignment. Any other pattern (`var [a, b] =
        /// x`) binds hygienic temporaries that are assigned to the hoisted
        /// names right after it matches; a by-reference binding cannot be
        /// hoisted this way and is reported
        /// ([`CannotHoist`](crate::ProblemKind::CannotHoist)).
        hoist: bool,
    },
    /// `EXPR;` — an expression evaluated for its effect (never a block tail).
    ExprStmt {
        /// Required: the expression.
        expr: Pick<K>,
    },
    /// `defer BODY` — runs when the enclosing block exits.
    Defer {
        /// Required: the deferred expression.
        body: Pick<K>,
    },
    /// `static $x = INIT;` — a function-static variable (PHP, C).
    Static {
        /// Required: the variable's name.
        name: Pick<K>,
        /// The annotation.
        ty: Pick<K>,
        /// The initializer.
        init: Pick<K>,
    },
    /// `global $a, $b;` — locals aliasing globals, one statement per name.
    GlobalDecl {
        /// List: the names.
        names: Pick<K>,
    },
    /// `use a::b as c;` / `use a::*;` — an import item. Modifiers: `Glob`.
    Import {
        /// Required: the imported path (a path node or one name).
        path: Pick<K>,
        /// The alias (`as c`).
        alias: Pick<K>,
    },
    /// A use tree: `use a::{b, c::{d, e as f}, g::*};` (Rust), `use App\{Foo,
    /// Bar as Baz};` (PHP group use). Flattened into one HIR import per leaf,
    /// each path the concatenation of the prefixes above it. Modifiers:
    /// visibility (on the outermost tree, for every import it makes), `Glob`
    /// (a `*` leaf).
    ///
    /// The expected tree: a use-tree node holds an optional prefix `path` (a
    /// path node or one name), then either nested `children` (use-tree nodes,
    /// names, or paths; container nodes such as `{ ... }` splice) or, for a
    /// leaf, an optional `alias`. A child spelled `self` (a token in the
    /// [root table](crate::MappingBuilder::root) mapping to
    /// `PathRoot::SelfModule`) imports the prefix itself. Leading root tokens
    /// of the outermost prefix (`::`, `crate`, `super`) set the import's root.
    /// The walk is iterative; nesting depth is bounded only by the tree.
    UseTree {
        /// The prefix (or, for a leaf, the whole path).
        path: Pick<K>,
        /// A leaf's alias (`as f`).
        alias: Pick<K>,
        /// List: the nested trees of a group.
        children: Pick<K>,
    },

    // ------------------------------------------------------------ declarations
    /// A function. Modifiers: visibility, `Async`, `Generator`, `Throws`,
    /// `Unsafe`. In a class, interface, or impl it is a method.
    Fn {
        /// Required: the name.
        name: Pick<K>,
        /// List: generic parameters.
        generics: Pick<K>,
        /// List: parameters.
        params: Pick<K>,
        /// The declared return type.
        ret: Pick<K>,
        /// The declared thrown type.
        throws: Pick<K>,
        /// The body (absent for required, abstract, or foreign functions).
        body: Pick<K>,
        /// List: where-clause predicates.
        where_: Pick<K>,
    },
    /// A record (struct) or union. Modifiers: visibility, `Union`.
    Record {
        /// Required: the name.
        name: Pick<K>,
        /// List: generic parameters.
        generics: Pick<K>,
        /// List: the fields.
        fields: Pick<K>,
        /// How the field layout is decided.
        shape: RecordShape,
    },
    /// A sum (enum) with variants. Modifiers: visibility.
    Sum {
        /// Required: the name.
        name: Pick<K>,
        /// List: generic parameters.
        generics: Pick<K>,
        /// List: the variants.
        variants: Pick<K>,
    },
    /// An interface or trait. Modifiers: visibility.
    Interface {
        /// Required: the name.
        name: Pick<K>,
        /// List: generic parameters.
        generics: Pick<K>,
        /// List: super-interfaces (types).
        supers: Pick<K>,
        /// List: members (methods, constants, associated types).
        items: Pick<K>,
    },
    /// A class. Modifiers: visibility, `Abstract`, `Final`.
    Class {
        /// Required: the name.
        name: Pick<K>,
        /// List: generic parameters.
        generics: Pick<K>,
        /// List: base classes, in method-resolution order (types).
        bases: Pick<K>,
        /// List: implemented interfaces (types).
        interfaces: Pick<K>,
        /// List: members: fields, methods, constants, static fields, aliases,
        /// mixin uses.
        members: Pick<K>,
        /// A mixin (PHP trait): never instantiated, used by classes.
        mixin: bool,
    },
    /// An implementation block (`impl Tr for T { ... }`).
    Impl {
        /// List: generic parameters.
        generics: Pick<K>,
        /// The implemented interface (a type).
        interface: Pick<K>,
        /// Required: the implementing type.
        self_ty: Pick<K>,
        /// List: members.
        items: Pick<K>,
    },
    /// A type alias. Modifiers: visibility.
    Alias {
        /// Required: the name.
        name: Pick<K>,
        /// List: generic parameters.
        generics: Pick<K>,
        /// Required: the aliased type.
        ty: Pick<K>,
    },
    /// An associated type declared by an interface.
    AssocType {
        /// Required: the name.
        name: Pick<K>,
        /// List: bounds (types).
        bounds: Pick<K>,
        /// A default type.
        default: Pick<K>,
    },
    /// A compile-time constant. Modifiers: visibility.
    Const {
        /// Required: the name.
        name: Pick<K>,
        /// The annotation.
        ty: Pick<K>,
        /// The value (absent only in an interface).
        value: Pick<K>,
    },
    /// A global or static variable item. Modifiers: visibility, `Mut`.
    GlobalVar {
        /// Required: the name.
        name: Pick<K>,
        /// The annotation.
        ty: Pick<K>,
        /// The initializer.
        init: Pick<K>,
        /// Whether it is assignable without a `Mut` modifier.
        mutable: bool,
    },
    /// A mixin use inside a class (PHP `use T, U { rules }`).
    MixinUse {
        /// List: the mixins (types).
        mixins: Pick<K>,
        /// List: conflict-resolution rules ([`MixinRule`](Arch::MixinRule)
        /// nodes).
        rules: Pick<K>,
    },
    /// One rule of a mixin use: `T::hello insteadof U, V;` or
    /// `T::hello as protected greet;` / `hello as private;`. With
    /// `insteadof` types it is an exclusion; otherwise an alias, with a new
    /// name, a visibility modifier (`Public`, `Protected`, `Private`), or both.
    /// A rule with neither is reported ([`MissingField`](crate::ProblemKind::MissingField)
    /// `action`).
    MixinRule {
        /// The mixin the method comes from (`T` in `T::hello`), a type.
        from: Pick<K>,
        /// Required: the method's name.
        method: Pick<K>,
        /// List: the mixins it is taken instead of (types).
        insteadof: Pick<K>,
        /// The new name of an alias.
        alias: Pick<K>,
    },
    /// A field of a record, variant, or class. Modifiers: visibility; in a
    /// class, `Static` makes it a static (global) member instead.
    FieldDecl {
        /// The name (absent for a positional field).
        name: Pick<K>,
        /// The type.
        ty: Pick<K>,
        /// A default value.
        default: Pick<K>,
    },
    /// A variant of a sum.
    VariantDecl {
        /// Required: the name.
        name: Pick<K>,
        /// List: the fields.
        fields: Pick<K>,
        /// An explicit discriminant.
        discriminant: Pick<K>,
        /// How the field layout is decided.
        shape: RecordShape,
    },
    /// A generic parameter: a type parameter, a const parameter (with a
    /// type), or a region (a lifetime-role token).
    GenericParam {
        /// Required: the name.
        name: Pick<K>,
        /// List: bounds (types).
        bounds: Pick<K>,
        /// The type of a const parameter.
        ty: Pick<K>,
        /// A default (a type, or a constant for a const parameter).
        default: Pick<K>,
    },
    /// A where-clause predicate: `T: A + B`.
    WherePred {
        /// Required: the constrained type.
        subject: Pick<K>,
        /// List: the bounds (types).
        bounds: Pick<K>,
    },
    /// A parameter. Modifiers: `Ref` (by reference, PHP `&$x`), `Mut`,
    /// `Rest` (variadic), `RestNamed`, `NamedOnly`, `PositionalOnly`,
    /// `Receiver`.
    Param {
        /// Required: the pattern (usually one name).
        pat: Pick<K>,
        /// The annotation.
        ty: Pick<K>,
        /// The default value.
        default: Pick<K>,
    },
    /// An explicit closure capture (PHP `use ($x, &$y)`). Modifiers: `Ref`,
    /// `Mut`.
    Capture {
        /// Required: the captured name.
        name: Pick<K>,
    },

    // ------------------------------------------------------------- expressions
    /// A path: `a::b::c`, `::a`, `self::a`, `super::a`, `Self::a`,
    /// `<T as Tr>::a`. Leading tokens of `segments` whose kind is in the
    /// mapping's [root table](crate::MappingBuilder::root) set the root.
    Path {
        /// Required (unless qualified): list of segments (names or `Segment`
        /// nodes).
        segments: Pick<K>,
        /// A leading global marker (`::a`).
        global: Pick<K>,
        /// The qualified self type (`<T as ...>`).
        qself: Pick<K>,
        /// List: the trait segments of a qualified self (`<T as a::Tr>`).
        qtrait: Pick<K>,
    },
    /// A path segment with generic arguments (`Vec::<i32>`).
    Segment {
        /// Required: the name.
        name: Pick<K>,
        /// List: generic arguments.
        args: Pick<K>,
    },
    /// An associated-type binding in generic arguments: `Item = u8`.
    AssocBinding {
        /// Required: the associated type's name.
        name: Pick<K>,
        /// Required: its type.
        ty: Pick<K>,
    },
    /// A prefix operator; the operator token's kind is looked up in the
    /// prefix [operator table](crate::MappingBuilder::prefix_op).
    Unary {
        /// Required: the operator token.
        op: Pick<K>,
        /// Required: the operand.
        operand: Pick<K>,
    },
    /// A binary operator; the token's kind is looked up in the binary
    /// operator table. `&&`, `||`, `??`, assignment, and compound assignment
    /// are operators too.
    Binary {
        /// Required: the left operand.
        lhs: Pick<K>,
        /// Required: the operator token.
        op: Pick<K>,
        /// Required: the right operand.
        rhs: Pick<K>,
    },
    /// A postfix operator; looked up in the postfix operator table.
    Postfix {
        /// Required: the operand.
        operand: Pick<K>,
        /// Required: the operator token.
        op: Pick<K>,
    },
    /// `TARGET = VALUE` or `TARGET op= VALUE`; a compound operator token is
    /// looked up in the binary operator table.
    Assign {
        /// Required: the place.
        target: Pick<K>,
        /// The operator token (absent: plain assignment).
        op: Pick<K>,
        /// Required: the value.
        value: Pick<K>,
    },
    /// `$a = &$b`: reference assignment (PHP).
    RefAssign {
        /// Required: the place that becomes an alias.
        target: Pick<K>,
        /// Required: the aliased place.
        source: Pick<K>,
    },
    /// A call.
    Call {
        /// Required: the callee.
        callee: Pick<K>,
        /// List: arguments.
        args: Pick<K>,
    },
    /// A call argument: named (`name: value`), spread (`...xs`, modifier
    /// `Spread`; `**kw`, modifier `SpreadNamed`), or a place (`&$x`, modifier
    /// `Ref`). Plain expressions in an argument list need no archetype.
    Arg {
        /// The argument's name.
        name: Pick<K>,
        /// Required: the value.
        value: Pick<K>,
    },
    /// Member access, or a method call when `call` finds something:
    /// `a.b`, `a.0`, `a.b(x)`.
    Member {
        /// Required: the accessed value.
        base: Pick<K>,
        /// Required: the member (a name, or a number for tuple fields).
        member: Pick<K>,
        /// List: method-call arguments.
        args: Pick<K>,
        /// The call marker (such as `(`); `Absent` means never a call.
        call: Pick<K>,
    },
    /// A method call with explicit generic arguments: `x.f::<T>(a)`.
    MethodCall {
        /// Required: the receiver.
        receiver: Pick<K>,
        /// Required: the method name.
        method: Pick<K>,
        /// List: generic arguments.
        generics: Pick<K>,
        /// List: arguments.
        args: Pick<K>,
    },
    /// `$o->$name` / `$o->$name(args)`: a member chosen at run time (Mox).
    DynMember {
        /// Required: the accessed value.
        base: Pick<K>,
        /// Required: the member name, an expression.
        name: Pick<K>,
        /// List: method-call arguments.
        args: Pick<K>,
        /// The call marker; `Absent` means never a call.
        call: Pick<K>,
    },
    /// `base[index]`.
    Index {
        /// Required: the indexed value.
        base: Pick<K>,
        /// Required: the index.
        index: Pick<K>,
    },
    /// `$$name`: the variable named at run time (Mox).
    VarVar {
        /// Required: the expression naming the variable.
        inner: Pick<K>,
    },
    /// `$a[]`: append position (Mox); only a write target.
    Append {
        /// Required: the array.
        base: Pick<K>,
    },
    /// `(a, b)`.
    Tuple {
        /// List: elements.
        elems: Pick<K>,
    },
    /// `[a, b]`.
    Array {
        /// List: elements.
        elems: Pick<K>,
    },
    /// An ordered map literal: `[k => v, w]` (PHP arrays). Elements that are
    /// not `MapEntry` nodes take the next index.
    Map {
        /// List: entries.
        entries: Pick<K>,
    },
    /// `key => value` inside a map literal.
    MapEntry {
        /// The key (absent: next index).
        key: Pick<K>,
        /// Required: the value.
        value: Pick<K>,
    },
    /// `Path { x: 1, ..base }` or an anonymous record.
    RecordLit {
        /// The record type's path.
        path: Pick<K>,
        /// List: field initializers.
        fields: Pick<K>,
        /// The `..base` expression.
        base: Pick<K>,
    },
    /// `name: value` (or the shorthand `name`) in a record literal.
    FieldInit {
        /// Required: the field name.
        name: Pick<K>,
        /// The value (absent: shorthand, the variable of the same name).
        value: Pick<K>,
    },
    /// `[elem; count]`.
    Repeat {
        /// Required: the element.
        elem: Pick<K>,
        /// Required: the count.
        count: Pick<K>,
    },
    /// `expr as Type`.
    Cast {
        /// Required: the converted value.
        expr: Pick<K>,
        /// Required: the target type.
        ty: Pick<K>,
    },
    /// `&expr` / `&mut expr`. Modifiers: `Mut`.
    Borrow {
        /// Required: the borrowed place.
        expr: Pick<K>,
    },
    /// `*expr`.
    Deref {
        /// Required: the dereferenced value.
        inner: Pick<K>,
    },
    /// A block. Modifiers: `Unsafe`.
    Block {
        /// List: statements (and local items).
        stmts: Pick<K>,
        /// The tail expression (the block's value).
        tail: Pick<K>,
        /// A label (`'a: { ... }`).
        label: Pick<K>,
        /// Whether a final bare expression (not an expression statement)
        /// becomes the tail.
        tail_from_last: bool,
    },
    /// `if cond then else`.
    If {
        /// Required: the condition.
        cond: Pick<K>,
        /// Required: the value when true.
        then: Pick<K>,
        /// The value when false.
        else_: Pick<K>,
    },
    /// `match scrutinee { arms }`.
    Match {
        /// Required: the matched value.
        scrutinee: Pick<K>,
        /// List: arms.
        arms: Pick<K>,
    },
    /// A match arm: `pat if guard => body`.
    Arm {
        /// Required: the pattern.
        pat: Pick<K>,
        /// The guard.
        guard: Pick<K>,
        /// Required: the body.
        body: Pick<K>,
    },
    /// `loop body` — HIR's one loop. `while`, `do-while`, C `for`, and
    /// `for-each` are [templates](crate::templates).
    Loop {
        /// A label.
        label: Pick<K>,
        /// Required: the body.
        body: Pick<K>,
    },
    /// `break 'label value`.
    Break {
        /// The target label.
        label: Pick<K>,
        /// The value.
        value: Pick<K>,
    },
    /// `continue 'label`.
    Continue {
        /// The target label.
        label: Pick<K>,
    },
    /// `return value`.
    Return {
        /// The value.
        value: Pick<K>,
    },
    /// `throw value`.
    Throw {
        /// Required: the raised value.
        value: Pick<K>,
    },
    /// `try body catch... finally...`.
    Try {
        /// Required: the protected body.
        body: Pick<K>,
        /// List: catch clauses (`Catch` or `Arm` nodes).
        catches: Pick<K>,
        /// The `finally` body.
        finally: Pick<K>,
    },
    /// `catch (A | B $e) body`: a type test (or an alternative of several)
    /// binding `name`. Without types it catches everything.
    Catch {
        /// List: the caught types.
        types: Pick<K>,
        /// The bound name.
        name: Pick<K>,
        /// Required: the handler.
        body: Pick<K>,
    },
    /// `await e`.
    Await {
        /// Required: the awaited value.
        inner: Pick<K>,
    },
    /// `yield v`, or `yield k => v` (PHP and Mox generators). A key without
    /// a value yields `null` under that key.
    Yield {
        /// The explicit key.
        key: Pick<K>,
        /// The yielded value.
        value: Pick<K>,
    },
    /// `yield from g`: delegate to an inner generator or iterable; evaluates
    /// to its return value.
    YieldFrom {
        /// Required: the inner generator or iterable.
        inner: Pick<K>,
    },
    /// `spawn e` — starts a task running the callable `e`.
    Spawn {
        /// Required: the callable.
        inner: Pick<K>,
    },
    /// A closure. Modifiers: `Async`, `Generator`, `Throws`.
    Closure {
        /// List: parameters.
        params: Pick<K>,
        /// The return type.
        ret: Pick<K>,
        /// List: explicit captures (`Capture` nodes or names).
        captures: Pick<K>,
        /// Required: the body.
        body: Pick<K>,
        /// How outer variables are captured implicitly; `None` means only the
        /// explicit captures are visible (PHP `function () use (...)`).
        implicit: Option<CaptureMode>,
    },
    /// A node lowered through a template; see
    /// [`MappingBuilder::template_node`](crate::MappingBuilder::template_node).
    Template(TemplateUse),
    /// An interpolated string: `"Hello {$name}!"`, `"$name"`, `"{$a['k']}"`,
    /// and heredocs, as the lexer delivers them in segments.
    ///
    /// The expected tree: the string's node holds its pieces in order. Text
    /// pieces are tokens with role [`TokenRole::StrPart`](crate::TokenRole::StrPart)
    /// (no quotes; escapes decoded, adjacent pieces merged); every other
    /// selected element is an embedded expression (`$name` as a variable
    /// token, `{$a['k']}` as an index node; braces and quotes are skip kinds
    /// or left out by the `parts` pick). The parts are joined by `join`:
    /// one text piece alone is a string literal, no pieces the empty string,
    /// and a lone expression is joined with an empty string first, so the
    /// result is always a string conversion.
    ///
    /// With `close` (a heredoc), `close` picks the closing-marker token: the
    /// whitespace before its identifier is removed from the start of every
    /// line of the text pieces (PHP 7.3+), a line indented less (or starting
    /// with an embedded expression) is reported
    /// ([`InvalidLiteral`](crate::ProblemKind::InvalidLiteral)), and the
    /// newline before the marker is dropped.
    Interp {
        /// List: the pieces, in order.
        parts: Pick<K>,
        /// A heredoc's closing-marker token.
        close: Pick<K>,
        /// How the pieces become one string.
        join: Join,
    },

    // -------------------------------------------------------------- attributes
    /// An attribute (`#[Route("/home", methods: GET)]`, `@inline`), attached
    /// to the node it is a direct child of. Attribute nodes (and container
    /// nodes holding them, such as PHP's `#[A, B]` group) are invisible to
    /// every other pick of that node. HIR attributes are data, never code
    /// (HIR spec §11): an argument value must be a literal or a name; anything
    /// else is reported
    /// ([`AttributeArgument`](crate::ProblemKind::AttributeArgument)) and left
    /// out.
    Attr {
        /// Required: the attribute's name, kept as written (a qualified name
        /// such as `\App\Route` keeps its separators).
        name: Pick<K>,
        /// List: the arguments ([`AttrArg`](Arch::AttrArg) nodes, or values).
        args: Pick<K>,
    },
    /// `key: value` / `key = value` inside an attribute's arguments.
    AttrArg {
        /// The key.
        key: Pick<K>,
        /// The value (absent: a bare key).
        value: Pick<K>,
    },

    // ---------------------------------------------------------------- patterns
    /// `_` as a node.
    WildPat,
    /// `..` (or `rest @ ..`) inside a tuple, constructor, slice, or record
    /// pattern.
    RestPat {
        /// A pattern binding the rest (slices only).
        bind: Pick<K>,
    },
    /// `ref mut x @ sub`. Modifiers: `Ref`, `Mut`.
    BindPat {
        /// Required: the bound name.
        name: Pick<K>,
        /// A sub-pattern the value must also match.
        sub: Pick<K>,
    },
    /// `(a, b, ..)`.
    TuplePat {
        /// List: element patterns (a `RestPat` marks `..`).
        elems: Pick<K>,
    },
    /// `Some(x)`.
    CtorPat {
        /// Required: the constructor's path.
        path: Pick<K>,
        /// List: element patterns.
        elems: Pick<K>,
    },
    /// `Point { x, y: 0, .. }`.
    RecordPat {
        /// The record's path (absent: an anonymous record or object).
        path: Pick<K>,
        /// List: field patterns.
        fields: Pick<K>,
    },
    /// `name: pat` (or the shorthand `name`) in a record pattern.
    FieldPat {
        /// Required: the field name.
        name: Pick<K>,
        /// The pattern (absent: shorthand, binding the field's name).
        pat: Pick<K>,
    },
    /// `a | b`.
    OrPat {
        /// List: alternatives.
        alts: Pick<K>,
    },
    /// `lo..hi` / `lo..=hi`. Modifiers: `Inclusive`.
    RangePat {
        /// The lower bound.
        lo: Pick<K>,
        /// The upper bound.
        hi: Pick<K>,
    },
    /// `&p` / `&mut p`. Modifiers: `Mut`.
    RefPat {
        /// Required: the inner pattern.
        inner: Pick<K>,
    },
    /// `[a, b, rest @ .., z]`.
    SlicePat {
        /// List: element patterns (a `RestPat` marks `..`).
        elems: Pick<K>,
    },
    /// `e: Type` / `Type e` — a type test, as in `catch` clauses.
    TypeTestPat {
        /// Required: the tested type.
        ty: Pick<K>,
        /// The pattern the value must then match.
        pat: Pick<K>,
    },

    // ------------------------------------------------------------------- types
    /// `(A, B)`.
    TupleTy {
        /// List: element types.
        elems: Pick<K>,
    },
    /// `[T; N]`.
    ArrayTy {
        /// Required: the element type.
        elem: Pick<K>,
        /// Required: the length.
        len: Pick<K>,
    },
    /// `[T]`.
    SliceTy {
        /// Required: the element type.
        elem: Pick<K>,
    },
    /// `&'r mut T`. Modifiers: `Mut`.
    RefTy {
        /// Required: the referent type.
        inner: Pick<K>,
        /// An explicit region.
        region: Pick<K>,
    },
    /// `*mut T` / `*const T`. Modifiers: `Mut`.
    PtrTy {
        /// Required: the pointee type.
        inner: Pick<K>,
    },
    /// `T?`.
    NullableTy {
        /// Required: the inner type.
        inner: Pick<K>,
    },
    /// `fn(A, B) -> R`.
    FnTy {
        /// List: parameter types.
        params: Pick<K>,
        /// The return type (absent: unit).
        ret: Pick<K>,
    },
    /// `dyn A + B`.
    ObjectTy {
        /// List: bounds.
        bounds: Pick<K>,
    },
    /// `impl A + B`.
    ImplTy {
        /// List: bounds.
        bounds: Pick<K>,
    },
    /// `A | B | C` (PHP union types). Members are lowered in source order;
    /// hir-lang's `finish` puts them in canonical form (flattened, sorted,
    /// repeats dropped, `null` hoisted into `Nullable`, one member
    /// collapsed). No members at all is a
    /// [`MissingField`](crate::ProblemKind::MissingField) problem.
    UnionTy {
        /// List: the member types.
        members: Pick<K>,
    },
    /// `A & B` (PHP intersection types), normalized like
    /// [`UnionTy`](Self::UnionTy).
    IntersectionTy {
        /// List: the member types.
        members: Pick<K>,
    },
}

impl<K> Arch<K> {
    /// `Pass { inner: Nth(n) }`: the common wrapper.
    ///
    /// # Examples
    ///
    /// ```
    /// use lower_lang::{Arch, Pick};
    ///
    /// assert_eq!(Arch::<u8>::pass(1), Arch::Pass { inner: Pick::Nth(1) });
    /// ```
    #[must_use]
    pub const fn pass(n: u16) -> Self {
        Self::Pass {
            inner: Pick::Nth(n),
        }
    }

    /// The shape of lang-forge's operator nodes: `binary` is `lhs op rhs`.
    ///
    /// # Examples
    ///
    /// ```
    /// use lower_lang::{Arch, Pick};
    ///
    /// let Arch::Binary { op, .. } = Arch::<u8>::binary() else { unreachable!() };
    /// assert_eq!(op, Pick::Nth(1));
    /// ```
    #[must_use]
    pub const fn binary() -> Self {
        Self::Binary {
            lhs: Pick::Nth(0),
            op: Pick::Nth(1),
            rhs: Pick::Nth(2),
        }
    }

    /// The shape of lang-forge's `prefix` nodes: `op operand`.
    ///
    /// # Examples
    ///
    /// ```
    /// use lower_lang::{Arch, Pick};
    ///
    /// let Arch::Unary { operand, .. } = Arch::<u8>::prefix() else { unreachable!() };
    /// assert_eq!(operand, Pick::Nth(1));
    /// ```
    #[must_use]
    pub const fn prefix() -> Self {
        Self::Unary {
            op: Pick::Nth(0),
            operand: Pick::Nth(1),
        }
    }

    /// The archetype's name, as problems and docs spell it.
    ///
    /// # Examples
    ///
    /// ```
    /// use lower_lang::Arch;
    ///
    /// assert_eq!(Arch::<u8>::binary().name(), "binary");
    /// assert_eq!(Arch::<u8>::WildPat.name(), "wildcard pattern");
    /// ```
    #[must_use]
    pub const fn name(&self) -> &'static str {
        match self {
            Self::Module { .. } => "module",
            Self::Pass { .. } => "pass",
            Self::Splice { .. } => "splice",
            Self::Unsupported => "unsupported",
            Self::Let { .. } => "let",
            Self::ExprStmt { .. } => "expression statement",
            Self::Defer { .. } => "defer",
            Self::Static { .. } => "static",
            Self::GlobalDecl { .. } => "global declaration",
            Self::Import { .. } => "import",
            Self::Fn { .. } => "function",
            Self::Record { .. } => "record",
            Self::Sum { .. } => "sum",
            Self::Interface { .. } => "interface",
            Self::Class { .. } => "class",
            Self::Impl { .. } => "impl",
            Self::Alias { .. } => "alias",
            Self::AssocType { .. } => "associated type",
            Self::Const { .. } => "constant",
            Self::GlobalVar { .. } => "global variable",
            Self::MixinUse { .. } => "mixin use",
            Self::MixinRule { .. } => "mixin rule",
            Self::UseTree { .. } => "use tree",
            Self::Interp { .. } => "interpolated string",
            Self::Attr { .. } => "attribute",
            Self::AttrArg { .. } => "attribute argument",
            Self::FieldDecl { .. } => "field",
            Self::VariantDecl { .. } => "variant",
            Self::GenericParam { .. } => "generic parameter",
            Self::WherePred { .. } => "where predicate",
            Self::Param { .. } => "parameter",
            Self::Capture { .. } => "capture",
            Self::Path { .. } => "path",
            Self::Segment { .. } => "path segment",
            Self::AssocBinding { .. } => "associated-type binding",
            Self::Unary { .. } => "unary",
            Self::Binary { .. } => "binary",
            Self::Postfix { .. } => "postfix",
            Self::Assign { .. } => "assignment",
            Self::RefAssign { .. } => "reference assignment",
            Self::Call { .. } => "call",
            Self::Arg { .. } => "argument",
            Self::Member { .. } => "member",
            Self::MethodCall { .. } => "method call",
            Self::DynMember { .. } => "dynamic member",
            Self::Index { .. } => "index",
            Self::VarVar { .. } => "variable variable",
            Self::Append { .. } => "append",
            Self::Tuple { .. } => "tuple",
            Self::Array { .. } => "array",
            Self::Map { .. } => "map",
            Self::MapEntry { .. } => "map entry",
            Self::RecordLit { .. } => "record literal",
            Self::FieldInit { .. } => "field initializer",
            Self::Repeat { .. } => "repeat",
            Self::Cast { .. } => "cast",
            Self::Borrow { .. } => "borrow",
            Self::Deref { .. } => "dereference",
            Self::Block { .. } => "block",
            Self::If { .. } => "if",
            Self::Match { .. } => "match",
            Self::Arm { .. } => "match arm",
            Self::Loop { .. } => "loop",
            Self::Break { .. } => "break",
            Self::Continue { .. } => "continue",
            Self::Return { .. } => "return",
            Self::Throw { .. } => "throw",
            Self::Try { .. } => "try",
            Self::Catch { .. } => "catch",
            Self::Await { .. } => "await",
            Self::Yield { .. } => "yield",
            Self::YieldFrom { .. } => "yield from",
            Self::Spawn { .. } => "spawn",
            Self::Closure { .. } => "closure",
            Self::Template(_) => "template",
            Self::WildPat => "wildcard pattern",
            Self::RestPat { .. } => "rest pattern",
            Self::BindPat { .. } => "binding pattern",
            Self::TuplePat { .. } => "tuple pattern",
            Self::CtorPat { .. } => "constructor pattern",
            Self::RecordPat { .. } => "record pattern",
            Self::FieldPat { .. } => "field pattern",
            Self::OrPat { .. } => "or-pattern",
            Self::RangePat { .. } => "range pattern",
            Self::RefPat { .. } => "reference pattern",
            Self::SlicePat { .. } => "slice pattern",
            Self::TypeTestPat { .. } => "type-test pattern",
            Self::TupleTy { .. } => "tuple type",
            Self::ArrayTy { .. } => "array type",
            Self::SliceTy { .. } => "slice type",
            Self::RefTy { .. } => "reference type",
            Self::PtrTy { .. } => "pointer type",
            Self::NullableTy { .. } => "nullable type",
            Self::FnTy { .. } => "function type",
            Self::ObjectTy { .. } => "object type",
            Self::ImplTy { .. } => "impl type",
            Self::UnionTy { .. } => "union type",
            Self::IntersectionTy { .. } => "intersection type",
        }
    }

    /// The archetype's LSF2 name: the value of `arch = "…"` in
    /// `[semantics.bind]` (LSF2 §15.4), and the key
    /// [`MappingBuilder::modifier_in`](crate::MappingBuilder::modifier_in)
    /// takes. Archetypes LSF2 has no `arch` value for use the snake case of
    /// the variant (`mixin_rule`, `use_tree`, `attr_arg`, `template`).
    ///
    /// # Examples
    ///
    /// ```
    /// use lower_lang::{Arch, Pick};
    ///
    /// let param: Arch<u8> = Arch::Param { pat: Pick::Nth(0), ty: Pick::Absent, default: Pick::Absent };
    /// assert_eq!(param.lsf_name(), "param");
    /// assert_eq!(Arch::<u8>::binary().lsf_name(), "binary");
    /// ```
    #[must_use]
    pub const fn lsf_name(&self) -> &'static str {
        match self {
            Self::Module { .. } => "module",
            Self::Pass { .. } => "pass",
            Self::Splice { .. } => "splice",
            Self::Unsupported => "unsupported",
            Self::Let { .. } => "let",
            Self::ExprStmt { .. } => "expr_stmt",
            Self::Defer { .. } => "defer",
            Self::Static { .. } => "static",
            Self::GlobalDecl { .. } => "global_decl",
            Self::Import { .. } => "import",
            Self::Fn { .. } => "fn",
            Self::Record { .. } => "record",
            Self::Sum { .. } => "sum",
            Self::Interface { .. } => "interface",
            Self::Class { .. } => "class",
            Self::Impl { .. } => "impl",
            Self::Alias { .. } => "alias",
            Self::AssocType { .. } => "assoc_type",
            Self::Const { .. } => "const",
            Self::GlobalVar { .. } => "global_var",
            Self::MixinUse { .. } => "mixin_use",
            Self::MixinRule { .. } => "mixin_rule",
            Self::UseTree { .. } => "use_tree",
            Self::Interp { .. } => "interpolate",
            Self::Attr { .. } => "attribute",
            Self::AttrArg { .. } => "attr_arg",
            Self::FieldDecl { .. } => "field_decl",
            Self::VariantDecl { .. } => "variant_decl",
            Self::GenericParam { .. } => "generic_param",
            Self::WherePred { .. } => "where_pred",
            Self::Param { .. } => "param",
            Self::Capture { .. } => "capture",
            Self::Path { .. } => "path",
            Self::Segment { .. } => "segment",
            Self::AssocBinding { .. } => "assoc_binding",
            Self::Unary { .. } => "unary",
            Self::Binary { .. } => "binary",
            Self::Postfix { .. } => "postfix",
            Self::Assign { .. } => "assign",
            Self::RefAssign { .. } => "ref_assign",
            Self::Call { .. } => "call",
            Self::Arg { .. } => "arg",
            Self::Member { .. } => "member",
            Self::MethodCall { .. } => "method_call",
            Self::DynMember { .. } => "dyn_member",
            Self::Index { .. } => "index",
            Self::VarVar { .. } => "var_var",
            Self::Append { .. } => "append",
            Self::Tuple { .. } => "tuple",
            Self::Array { .. } => "array",
            Self::Map { .. } => "map",
            Self::MapEntry { .. } => "map_entry",
            Self::RecordLit { .. } => "record_lit",
            Self::FieldInit { .. } => "field_init",
            Self::Repeat { .. } => "repeat",
            Self::Cast { .. } => "cast",
            Self::Borrow { .. } => "borrow",
            Self::Deref { .. } => "deref",
            Self::Block { .. } => "block",
            Self::If { .. } => "if",
            Self::Match { .. } => "match",
            Self::Arm { .. } => "arm",
            Self::Loop { .. } => "loop",
            Self::Break { .. } => "break",
            Self::Continue { .. } => "continue",
            Self::Return { .. } => "return",
            Self::Throw { .. } => "throw",
            Self::Try { .. } => "try",
            Self::Catch { .. } => "catch",
            Self::Await { .. } => "await",
            Self::Yield { .. } => "yield",
            Self::YieldFrom { .. } => "yield_from",
            Self::Spawn { .. } => "spawn",
            Self::Closure { .. } => "closure",
            Self::Template(_) => "template",
            Self::WildPat => "wild_pat",
            Self::RestPat { .. } => "rest_pat",
            Self::BindPat { .. } => "bind_pat",
            Self::TuplePat { .. } => "tuple_pat",
            Self::CtorPat { .. } => "ctor_pat",
            Self::RecordPat { .. } => "record_pat",
            Self::FieldPat { .. } => "field_pat",
            Self::OrPat { .. } => "or_pat",
            Self::RangePat { .. } => "range_pat",
            Self::RefPat { .. } => "ref_pat",
            Self::SlicePat { .. } => "slice_pat",
            Self::TypeTestPat { .. } => "type_test_pat",
            Self::TupleTy { .. } => "tuple_ty",
            Self::ArrayTy { .. } => "array_ty",
            Self::SliceTy { .. } => "slice_ty",
            Self::RefTy { .. } => "ref_ty",
            Self::PtrTy { .. } => "ptr_ty",
            Self::NullableTy { .. } => "nullable_ty",
            Self::FnTy { .. } => "fn_ty",
            Self::ObjectTy { .. } => "object_ty",
            Self::ImplTy { .. } => "impl_ty",
            Self::UnionTy { .. } => "union_ty",
            Self::IntersectionTy { .. } => "intersection_ty",
        }
    }
}

/// Every [`Arch::lsf_name`], for checking the archetype names a mapping is
/// given ([`modifier_in`](crate::MappingBuilder::modifier_in)). In variant
/// order; the unit test below checks it against every variant.
pub(crate) const LSF_NAMES: [&str; 98] = [
    "module",
    "pass",
    "splice",
    "unsupported",
    "let",
    "expr_stmt",
    "defer",
    "static",
    "global_decl",
    "import",
    "fn",
    "record",
    "sum",
    "interface",
    "class",
    "impl",
    "alias",
    "assoc_type",
    "const",
    "global_var",
    "mixin_use",
    "mixin_rule",
    "use_tree",
    "interpolate",
    "attribute",
    "attr_arg",
    "field_decl",
    "variant_decl",
    "generic_param",
    "where_pred",
    "param",
    "capture",
    "path",
    "segment",
    "assoc_binding",
    "unary",
    "binary",
    "postfix",
    "assign",
    "ref_assign",
    "call",
    "arg",
    "member",
    "method_call",
    "dyn_member",
    "index",
    "var_var",
    "append",
    "tuple",
    "array",
    "map",
    "map_entry",
    "record_lit",
    "field_init",
    "repeat",
    "cast",
    "borrow",
    "deref",
    "block",
    "if",
    "match",
    "arm",
    "loop",
    "break",
    "continue",
    "return",
    "throw",
    "try",
    "catch",
    "await",
    "yield",
    "yield_from",
    "spawn",
    "closure",
    "template",
    "wild_pat",
    "rest_pat",
    "bind_pat",
    "tuple_pat",
    "ctor_pat",
    "record_pat",
    "field_pat",
    "or_pat",
    "range_pat",
    "ref_pat",
    "slice_pat",
    "type_test_pat",
    "tuple_ty",
    "array_ty",
    "slice_ty",
    "ref_ty",
    "ptr_ty",
    "nullable_ty",
    "fn_ty",
    "object_ty",
    "impl_ty",
    "union_ty",
    "intersection_ty",
];

/// The canonical (`'static`) spelling of an LSF2 archetype name, or `None`
/// when no archetype has that name.
pub(crate) fn lsf_name_of(name: &str) -> Option<&'static str> {
    LSF_NAMES.iter().copied().find(|n| *n == name)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mapping::Join;
    use crate::mapping::NameRef;
    use crate::template::TemplateId;

    /// One value of every variant, in declaration order.
    fn every_variant() -> alloc::vec::Vec<Arch<u8>> {
        let a = Pick::Absent;
        alloc::vec![
            Arch::Module { name: a, items: a },
            Arch::Pass { inner: a },
            Arch::Splice { items: a },
            Arch::Unsupported,
            Arch::Let {
                pat: a,
                ty: a,
                init: a,
                else_: a,
                mutable: false,
                hoist: false
            },
            Arch::ExprStmt { expr: a },
            Arch::Defer { body: a },
            Arch::Static {
                name: a,
                ty: a,
                init: a
            },
            Arch::GlobalDecl { names: a },
            Arch::Import { path: a, alias: a },
            Arch::Fn {
                name: a,
                generics: a,
                params: a,
                ret: a,
                throws: a,
                body: a,
                where_: a
            },
            Arch::Record {
                name: a,
                generics: a,
                fields: a,
                shape: RecordShape::Auto
            },
            Arch::Sum {
                name: a,
                generics: a,
                variants: a
            },
            Arch::Interface {
                name: a,
                generics: a,
                supers: a,
                items: a
            },
            Arch::Class {
                name: a,
                generics: a,
                bases: a,
                interfaces: a,
                members: a,
                mixin: false
            },
            Arch::Impl {
                generics: a,
                interface: a,
                self_ty: a,
                items: a
            },
            Arch::Alias {
                name: a,
                generics: a,
                ty: a
            },
            Arch::AssocType {
                name: a,
                bounds: a,
                default: a
            },
            Arch::Const {
                name: a,
                ty: a,
                value: a
            },
            Arch::GlobalVar {
                name: a,
                ty: a,
                init: a,
                mutable: false
            },
            Arch::MixinUse {
                mixins: a,
                rules: a
            },
            Arch::MixinRule {
                from: a,
                method: a,
                insteadof: a,
                alias: a
            },
            Arch::UseTree {
                path: a,
                alias: a,
                children: a
            },
            Arch::Interp {
                parts: a,
                close: a,
                join: Join::Call(NameRef(0))
            },
            Arch::Attr { name: a, args: a },
            Arch::AttrArg { key: a, value: a },
            Arch::FieldDecl {
                name: a,
                ty: a,
                default: a
            },
            Arch::VariantDecl {
                name: a,
                fields: a,
                discriminant: a,
                shape: RecordShape::Auto
            },
            Arch::GenericParam {
                name: a,
                bounds: a,
                ty: a,
                default: a
            },
            Arch::WherePred {
                subject: a,
                bounds: a
            },
            Arch::Param {
                pat: a,
                ty: a,
                default: a
            },
            Arch::Capture { name: a },
            Arch::Path {
                segments: a,
                global: a,
                qself: a,
                qtrait: a
            },
            Arch::Segment { name: a, args: a },
            Arch::AssocBinding { name: a, ty: a },
            Arch::Unary { op: a, operand: a },
            Arch::Binary {
                lhs: a,
                op: a,
                rhs: a
            },
            Arch::Postfix { operand: a, op: a },
            Arch::Assign {
                target: a,
                op: a,
                value: a
            },
            Arch::RefAssign {
                target: a,
                source: a
            },
            Arch::Call { callee: a, args: a },
            Arch::Arg { name: a, value: a },
            Arch::Member {
                base: a,
                member: a,
                args: a,
                call: a
            },
            Arch::MethodCall {
                receiver: a,
                method: a,
                generics: a,
                args: a
            },
            Arch::DynMember {
                base: a,
                name: a,
                args: a,
                call: a
            },
            Arch::Index { base: a, index: a },
            Arch::VarVar { inner: a },
            Arch::Append { base: a },
            Arch::Tuple { elems: a },
            Arch::Array { elems: a },
            Arch::Map { entries: a },
            Arch::MapEntry { key: a, value: a },
            Arch::RecordLit {
                path: a,
                fields: a,
                base: a
            },
            Arch::FieldInit { name: a, value: a },
            Arch::Repeat { elem: a, count: a },
            Arch::Cast { expr: a, ty: a },
            Arch::Borrow { expr: a },
            Arch::Deref { inner: a },
            Arch::Block {
                stmts: a,
                tail: a,
                label: a,
                tail_from_last: false
            },
            Arch::If {
                cond: a,
                then: a,
                else_: a
            },
            Arch::Match {
                scrutinee: a,
                arms: a
            },
            Arch::Arm {
                pat: a,
                guard: a,
                body: a
            },
            Arch::Loop { label: a, body: a },
            Arch::Break { label: a, value: a },
            Arch::Continue { label: a },
            Arch::Return { value: a },
            Arch::Throw { value: a },
            Arch::Try {
                body: a,
                catches: a,
                finally: a
            },
            Arch::Catch {
                types: a,
                name: a,
                body: a
            },
            Arch::Await { inner: a },
            Arch::Yield { key: a, value: a },
            Arch::YieldFrom { inner: a },
            Arch::Spawn { inner: a },
            Arch::Closure {
                params: a,
                ret: a,
                captures: a,
                body: a,
                implicit: None
            },
            Arch::Template(TemplateUse {
                template: TemplateId(0),
                first: 0,
                len: 0
            }),
            Arch::WildPat,
            Arch::RestPat { bind: a },
            Arch::BindPat { name: a, sub: a },
            Arch::TuplePat { elems: a },
            Arch::CtorPat { path: a, elems: a },
            Arch::RecordPat { path: a, fields: a },
            Arch::FieldPat { name: a, pat: a },
            Arch::OrPat { alts: a },
            Arch::RangePat { lo: a, hi: a },
            Arch::RefPat { inner: a },
            Arch::SlicePat { elems: a },
            Arch::TypeTestPat { ty: a, pat: a },
            Arch::TupleTy { elems: a },
            Arch::ArrayTy { elem: a, len: a },
            Arch::SliceTy { elem: a },
            Arch::RefTy {
                inner: a,
                region: a
            },
            Arch::PtrTy { inner: a },
            Arch::NullableTy { inner: a },
            Arch::FnTy { params: a, ret: a },
            Arch::ObjectTy { bounds: a },
            Arch::ImplTy { bounds: a },
            Arch::UnionTy { members: a },
            Arch::IntersectionTy { members: a },
        ]
    }

    #[test]
    fn test_lsf_names_cover_every_variant_once() {
        let all = every_variant();
        assert_eq!(all.len(), LSF_NAMES.len());
        for (arch, name) in all.iter().zip(LSF_NAMES) {
            assert_eq!(arch.lsf_name(), name);
            assert_eq!(lsf_name_of(name), Some(name));
        }
        let mut sorted = LSF_NAMES;
        sorted.sort_unstable();
        assert!(sorted.windows(2).all(|w| w[0] != w[1]), "names are unique");
        assert_eq!(lsf_name_of("parameter"), None);
        assert_eq!(lsf_name_of(""), None);
    }
}