hermes-ast 0.1.2

ESTree-compatible AST and JSON dumper for the Hermes Rust front-end.
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
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#
# Generate rust/crates/ast/src/node.rs from include/hermes/AST/ESTree.def plus a
# hand-transcribed decoration table (mirrors include/hermes/AST/ESTree.h). The
# output is the full ESTree node set: the `NodeKind` enum (with interleaved
# `_Name_First`/`_Last` range sentinels), one `#[repr(C)]` struct per leaf node
# (with composed decoration fields), the `Node` enum + accessors, minimal `new`
# constructors, and the `visit_children` / `mark_lists` GC/read walks.
#
# All parse families (FLOW/JSX/TS/Cover) are treated as ON. Re-run when
# ESTree.def or the decoration table changes:
#   python3 rust/crates/ast/gen_nodes.py            # writes src/node.rs
#   python3 rust/crates/ast/gen_nodes.py --stdout   # prints to stdout
import re
import sys
import textwrap
from pathlib import Path

ROOT = Path(__file__).resolve().parents[3]
DEF = ROOT / "include/hermes/AST/ESTree.def"
OUT = Path(__file__).resolve().parent / "src/node.rs"

# Total leaf nodes in ESTree.def — anti-drift guard. Update this when a node
# is intentionally added or removed.
EXPECTED_NODES = 271

# Total NodeLabel / NodeString fields across all nodes — anti-drift guard for
# the generated atom->string accessors (one method per label field, two per
# string field). Update these when ESTree.def gains or loses such a field.
EXPECTED_LABEL_FIELDS = 32
EXPECTED_STRING_FIELDS = 10


# --------------------------------------------------------------------------
# -- Helpers: snake_case + keyword escaping --
# --------------------------------------------------------------------------
RUST_KEYWORDS = {
    "as", "break", "const", "continue", "crate", "dyn", "else", "enum",
    "extern", "false", "fn", "for", "if", "impl", "in", "let", "loop", "match",
    "mod", "move", "mut", "pub", "ref", "return", "self", "Self", "static",
    "struct", "super", "trait", "true", "type", "unsafe", "use", "where",
    "while", "async", "await", "abstract", "become", "box", "do", "final",
    "macro", "override", "priv", "typeof", "unsized", "virtual", "yield", "try",
}


def camel_to_snake(name):
    # CamelCase/lowerCamelCase -> snake_case, acronym-aware so JSX/TS/SH runs stay
    # together: JSXElement->jsx_element, TSTypeAliasDeclaration->ts_type_alias_declaration,
    # SHBuiltin->sh_builtin, typeAnnotation->type_annotation.
    s = re.sub(r"(.)([A-Z][a-z]+)", r"\1_\2", name)   # split before Capitalized words
    s = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", s)      # split lower/digit -> Upper
    return s.lower()


def rust_field(json_name):  # snake_case, then raw-escape reserved words
    s = camel_to_snake(json_name)
    return f"r#{s}" if s in RUST_KEYWORDS else s


# --------------------------------------------------------------------------
# -- .def tokenizer/parser --
# --------------------------------------------------------------------------
def strip_comments(src):
    # Strip /* */ block comments and // line comments. The .def has no string
    # literals, so this is safe.
    src = re.sub(r"/\*.*?\*/", "", src, flags=re.S)
    src = re.sub(r"//[^\n]*", "", src)
    return src


def parse_def(src):
    """Parse ESTree.def into an ordered list of items.

    Items:
      ('first', name, base)
      ('last', name)
      ('node', name, base, [(type, name, opt_bool), ...])
    Also returns the IGNORE_IF_EMPTY dict (recorded for phase 4; unused now).
    """
    src = strip_comments(src)

    # All #if/#ifdef/#ifndef/#else/#endif/#define/#undef/#include lines are
    # treated as inactive directives: every family is ON and there are no #else
    # branches inside an ESTREE_ region. Assert that, then drop every
    # preprocessor line (the `#define ESTREE_FIRST(NAME, BASE)` stubs would
    # otherwise tokenize as bogus macro calls).
    if "#else" in src:
        sys.exit("error: unexpected #else inside ESTree.def")
    src = "\n".join(
        line for line in src.splitlines() if not line.lstrip().startswith("#")
    )

    items = []
    ignore_if_empty = {}

    # Find every ESTREE_ macro invocation, scanning to the balanced close paren.
    i = 0
    n = len(src)
    macro_re = re.compile(r"ESTREE_(\w+)\s*\(")
    while True:
        m = macro_re.search(src, i)
        if not m:
            break
        macro = "ESTREE_" + m.group(1)
        # Scan from the '(' to its balanced close.
        depth = 0
        j = m.end() - 1  # position of '('
        start_args = m.end()
        while j < n:
            if src[j] == "(":
                depth += 1
            elif src[j] == ")":
                depth -= 1
                if depth == 0:
                    break
            j += 1
        if depth != 0:
            sys.exit(f"error: unbalanced parens for {macro}")
        arg_str = src[start_args:j]
        args = [a.strip() for a in arg_str.split(",")]
        args = [a for a in args if a != ""]
        i = j + 1

        if macro == "ESTREE_FIRST":
            items.append(("first", args[0], args[1]))
        elif macro == "ESTREE_LAST":
            items.append(("last", args[0]))
        elif macro == "ESTREE_IGNORE_IF_EMPTY":
            ignore_if_empty.setdefault(args[0], []).append(args[1])
        else:
            mn = re.match(r"NODE_(\d+)_ARGS$", m.group(1))
            if not mn:
                sys.exit(f"error: unrecognized macro {macro}")
            count = int(mn.group(1))
            name = args[0]
            base = args[1]
            rest = args[2:]
            if len(rest) != count * 3:
                sys.exit(
                    f"error: {name}: expected {count*3} field tokens, "
                    f"got {len(rest)}"
                )
            fields = []
            for k in range(count):
                ftype = rest[k * 3]
                fname = rest[k * 3 + 1]
                fopt = rest[k * 3 + 2]
                if fopt not in ("true", "false"):
                    sys.exit(
                        f"error: {name}.{fname}: bad optional flag {fopt!r}"
                    )
                fields.append((ftype, fname, fopt == "true"))
            items.append(("node", name, base, fields))

    return items, ignore_if_empty


# --------------------------------------------------------------------------
# Reference A: .def field-type -> Rust mapping.
# --------------------------------------------------------------------------
def def_field_descriptor(ftype, fname, opt):
    """Map a .def (type, name, opt) triple to a field descriptor dict.

    Keys: json_name, rust_field, rust_type, child_kind
    ('single'|'opt'|'list'|'none'), new_arg_type, default_expr (None — .def
    fields are always `new` args, never defaulted).
    """
    rf = rust_field(fname)
    if ftype == "NodePtr":
        if opt:
            rtype = "Option<&'gc Node<'gc>>"
            return dict(json_name=fname, rust_field=rf, rust_type=rtype,
                        child_kind="opt", new_arg_type=rtype,
                        cell=False, default_expr=None,
                        is_def_arg=True, dump_kind="node_opt")
        rtype = "&'gc Node<'gc>"
        return dict(json_name=fname, rust_field=rf, rust_type=rtype,
                    child_kind="single", new_arg_type=rtype,
                    cell=False, default_expr=None,
                    is_def_arg=True, dump_kind="node_single")
    if ftype == "NodeList":
        return dict(json_name=fname, rust_field=rf, rust_type="NodeList<'gc>",
                    child_kind="list", new_arg_type="NodeList<'gc>",
                    cell=False, default_expr=None,
                    is_def_arg=True, dump_kind="list")
    # Value types -> Cell<...>.
    value_map = {
        "NodeBoolean": "bool",
        "NodeNumber": "f64",
        "NodeLabel": "NodeLabel",
        "NodeString": "NodeString",
    }
    inner = value_map.get(ftype)
    if inner is None:
        sys.exit(f"error: unknown .def field type {ftype!r}")
    return dict(json_name=fname, rust_field=rf,
                rust_type=f"Cell<{inner}>", child_kind="none",
                new_arg_type=inner, cell=True, default_expr=None,
                is_def_arg=True,
                # Which atom→string accessors this field gets (see
                # emit_atom_accessors); None for the non-atom value types.
                atom_kind={"NodeLabel": "label",
                           "NodeString": "string"}.get(ftype),
                dump_kind={"NodeBoolean": "bool", "NodeNumber": "number",
                           "NodeLabel": "label", "NodeString": "label"}[ftype])


# --------------------------------------------------------------------------
# -- Decoration tables (B/C/D) and field composition --
# --------------------------------------------------------------------------
# Reference B: decoration classes, hand-transcribed from ESTree.h:264-501.
# Each own-field is (source_name, rust_type, default_expr). The emitted
# field name is rust_field(source_name).
DECORATIONS = {
    "ScopeDecorationBase": (
        [],
        [("scope", "Cell<Option<SemaId>>", "Cell::new(None)")],
    ),
    "LabelDecorationBase": (
        [],
        [("labelIndex", "Cell<u32>", "Cell::new(INVALID_LABEL)")],
    ),
    "GotoDecorationBase": (["LabelDecorationBase"], []),
    "FunctionLikeDecoration": (
        ["ScopeDecorationBase"],
        [
            ("semInfo", "Cell<Option<SemaId>>", "Cell::new(None)"),
            ("strictness", "Cell<Strictness>", "Cell::new(Strictness::NotSet)"),
            ("isMethodDefinition", "Cell<bool>", "Cell::new(false)"),
            ("decorations", "Cell<NodeList<'gc>>", "Cell::new(NodeList::empty())"),
        ],
    ),
    "ProgramDecoration": (
        [],
        [("dummyParamList", "Cell<NodeList<'gc>>", "Cell::new(NodeList::empty())")],
    ),
    "StatementDecoration": ([], []),
    "LoopStatementDecoration": (["LabelDecorationBase"], []),
    "SwitchStatementDecoration": (
        ["LabelDecorationBase", "ScopeDecorationBase"], []
    ),
    "BreakStatementDecoration": (["GotoDecorationBase"], []),
    "ContinueStatementDecoration": (["GotoDecorationBase"], []),
    "LabeledStatementDecoration": (["LabelDecorationBase"], []),
    "BlockStatementDecoration": (
        ["ScopeDecorationBase"],
        [
            ("bufferId", "Cell<u32>", "Cell::new(0)"),
            ("isLazyFunctionBody", "Cell<bool>", "Cell::new(false)"),
            ("paramYield", "Cell<bool>", "Cell::new(false)"),
            ("paramAwait", "Cell<bool>", "Cell::new(false)"),
            ("containsArrowFunctions", "Cell<bool>", "Cell::new(false)"),
            ("mayContainArrowFunctionsUsingArguments", "Cell<bool>",
             "Cell::new(false)"),
        ],
    ),
    "StaticBlockDecoration": (
        ["ScopeDecorationBase"],
        [("functionInfo", "Cell<Option<SemaId>>", "Cell::new(None)")],
    ),
    "ForStatementDecoration": (["ScopeDecorationBase"], []),
    "ForInStatementDecoration": (["ScopeDecorationBase"], []),
    "ForOfStatementDecoration": (["ScopeDecorationBase"], []),
    "CatchClauseDecoration": (["ScopeDecorationBase"], []),
    "ClassLikeDecoration": (
        ["ScopeDecorationBase"],
        [
            ("implicitCtorFunctionInfo", "Cell<Option<SemaId>>", "Cell::new(None)"),
            ("instanceElementsInitFunctionInfo", "Cell<Option<SemaId>>",
             "Cell::new(None)"),
            ("staticElementsInitFunctionInfo", "Cell<Option<SemaId>>",
             "Cell::new(None)"),
        ],
    ),
    "IdentifierDecoration": (
        [],
        [
            ("unresolvable", "Cell<bool>", "Cell::new(false)"),
            ("declState", "Cell<u8>", "Cell::new(0)"),
            ("decl", "Cell<Option<SemaId>>", "Cell::new(None)"),
        ],
    ),
    "JSXDecoration": ([], []),
    "FlowDecoration": ([], []),
    "TSDecoration": ([], []),
    "PatternDecoration": ([], []),
    "MatchPatternDecoration": ([], []),
    "CoverDecoration": ([], []),
    "CallExpressionLikeDecoration": ([], []),
    "MemberExpressionLikeDecoration": ([], []),
    "EmptyDecoration": ([], []),
}

# Reference C: every ESTREE_FIRST range NAME implicitly carries NAMEDecoration
# (applied in compose_fields). The validation below enforces this at generation time.

# Reference D: leaf DecoratorTrait map, hand-transcribed from ESTree.h:530-581.
# (only leaves with a non-Empty trait)
LEAF_DECORATOR = {
    "BlockStatement": "BlockStatementDecoration",
    "StaticBlock": "StaticBlockDecoration",
    "BreakStatement": "BreakStatementDecoration",
    "ContinueStatement": "ContinueStatementDecoration",
    "ForStatement": "ForStatementDecoration",
    "ForInStatement": "ForInStatementDecoration",
    "ForOfStatement": "ForOfStatementDecoration",
    "CatchClause": "CatchClauseDecoration",
    "SwitchStatement": "SwitchStatementDecoration",
    "LabeledStatement": "LabeledStatementDecoration",
    "Identifier": "IdentifierDecoration",
    "Program": "ProgramDecoration",
}


# Reference B': one doc comment per distinct decoration field, condensed from
# the comments on the C++ members in ESTree.h:264-501. Each entry is the list
# of `///` lines emitted above the field (no leading `/// `).
DECORATION_DOCS = {
    "scope": ["Sema: the lexical scope created by this node, if any."],
    "labelIndex": ["Sema: label index; `INVALID_LABEL` until set."],
    "semInfo": ["Sema: `FunctionInfo` for this function."],
    "strictness": ["Sema: strict-mode state of this function."],
    "isMethodDefinition": [
        "Whether this function is a method definition (getters and setters",
        "included) rather than a `function`. Used for lazy reparsing.",
    ],
    "decorations": [
        "Decorations attached to this function by `Hermes.decorate(...)`",
        "calls in typed mode; each entry wraps a decoration expression.",
    ],
    "dummyParamList": [
        "An always-empty parameter list, for uniformity with functions.",
    ],
    "bufferId": ["The source buffer id in which this block was found."],
    "isLazyFunctionBody": [
        "True if this is a function body pruned while pre-parsing.",
    ],
    "paramYield": [
        "For a lazy block, the `Yield` param to restore when parsed eagerly.",
    ],
    "paramAwait": [
        "For a lazy block, the `Await` param to restore when parsed eagerly.",
    ],
    "containsArrowFunctions": [
        "Whether this function contains an arrow function. Read by lazy",
        "compilation to populate the `FunctionInfo`.",
    ],
    "mayContainArrowFunctionsUsingArguments": [
        "Conservative estimate of whether an arrow function here may use",
        "`arguments`, so a non-arrow function must eagerly capture it.",
    ],
    "functionInfo": ["Sema: `FunctionInfo` for this static block."],
    "implicitCtorFunctionInfo": [
        "Sema: `FunctionInfo` of the synthetic implicit constructor, if the",
        "class has one.",
    ],
    "instanceElementsInitFunctionInfo": [
        "Sema: `FunctionInfo` of the synthetic function that initializes the",
        "instance elements, if the class needs one.",
    ],
    "staticElementsInitFunctionInfo": [
        "Sema: `FunctionInfo` of the synthetic function that runs the static",
        "field initializers, if the class has any.",
    ],
    "unresolvable": [
        "Sema: unresolvable because of an enclosing `eval` or `with`.",
    ],
    "declState": [
        "Sema: how to read `decl` — the `BitHave*` bits of `ESTree.h`'s",
        "`IdentifierDecoration`.",
    ],
    "decl": [
        "Sema: the declaration this identifier resolves to; `None` until a",
        "resolution is recorded.",
    ],
}


def decoration_descriptor(source_name, rust_type, default_expr):
    """Build a uniform field descriptor for a decoration field."""
    rf = rust_field(source_name)
    if rust_type == "Cell<NodeList<'gc>>":
        child_kind = "declist"
    else:
        child_kind = "none"
    if source_name not in DECORATION_DOCS:
        sys.exit(f"error: no DECORATION_DOCS entry for {source_name!r}")
    return dict(json_name=source_name, rust_field=rf, rust_type=rust_type,
                child_kind=child_kind, new_arg_type=None, cell=True,
                default_expr=default_expr, is_def_arg=False, dump_kind=None,
                doc=DECORATION_DOCS[source_name])


def flatten_decoration(name, seen=None):
    """Ordered, deduped list of decoration field descriptors.

    Walks bases base-first (recursively), then own fields, deduping by
    snake_case field name (first occurrence wins).
    """
    if seen is None:
        seen = set()
    if name not in DECORATIONS:
        sys.exit(f"error: unknown decoration {name!r}")
    bases, own = DECORATIONS[name]
    out = []

    def take(fd):
        if fd["rust_field"] not in seen:
            seen.add(fd["rust_field"])
            out.append(fd)

    for b in bases:
        # The recursive call dedups against the shared `seen` and only returns
        # fields it actually emitted, so re-append them directly.
        for fd in flatten_decoration(b, seen):
            out.append(fd)
    for (src, rtype, default) in own:
        take(decoration_descriptor(src, rtype, default))
    return out


def build_range_parents(items):
    """Map each range NAME -> its parent BASE (from ESTREE_FIRST)."""
    parents = {}
    for it in items:
        if it[0] == "first":
            parents[it[1]] = it[2]
    return parents


def range_chain(base, range_parents):
    """Range chain for a node whose immediate base is `base`.

    Follow ESTREE_FIRST links upward until 'Base'. Return outermost-first
    (closest to Base first).
    """
    chain = []
    cur = base
    while cur != "Base":
        chain.append(cur)
        if cur not in range_parents:
            sys.exit(f"error: range {cur!r} has no ESTREE_FIRST")
        cur = range_parents[cur]
    chain.reverse()  # outermost-first
    return chain


def compose_fields(node_name, base, def_fields, range_parents):
    """Algorithm E: per-leaf field composition."""
    fields = []
    seen = set()

    def add(fd):
        if fd["rust_field"] not in seen:
            seen.add(fd["rust_field"])
            fields.append(fd)

    # 1. metadata.
    add(dict(json_name=None, rust_field="metadata",
             rust_type="NodeMetadata<'gc>", child_kind="meta",
             new_arg_type="NodeMetadata<'gc>", cell=False, default_expr=None,
             is_def_arg=False, dump_kind=None,
             doc=["Source range, debug location, paren count, and node id."]))

    # 2. .def arg fields.
    for (ftype, fname, opt) in def_fields:
        fd = def_field_descriptor(ftype, fname, opt)
        fd["doc"] = [f"ESTree `{fname}` property."]
        add(fd)

    # 3+4. range-chain decorations (outermost-first), flattened base-first.
    # `decos_seen` dedups across decoration classes only (a field introduced by
    # an inner decoration class is not repeated by an outer one).  The outer
    # `seen`/`add()` additionally dedups decoration fields against .def fields
    # (first occurrence wins); no .def/decoration name collision exists today.
    decos_seen = set()
    for rng in range_chain(base, range_parents):
        deco = rng + "Decoration"
        if deco not in DECORATIONS:
            sys.exit(f"error: missing decoration for range {rng!r}")
        for fd in flatten_decoration(deco, decos_seen):
            add(fd)

    # 5. leaf DecoratorTrait decoration.
    leaf_deco = LEAF_DECORATOR.get(node_name, "EmptyDecoration")
    for fd in flatten_decoration(leaf_deco, decos_seen):
        add(fd)

    return fields


# --------------------------------------------------------------------------
# -- Emitters --
# --------------------------------------------------------------------------
HEADER = """\
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

//! @generated by gen_nodes.py from include/hermes/AST/ESTree.def — DO NOT EDIT.
//! Run `python3 rust/crates/ast/gen_nodes.py` to regenerate.
#![allow(non_camel_case_types)] // NodeKind range sentinels (_Name_First/_Last) mirror C++
#![allow(clippy::too_many_arguments)] // node `new` arity follows ESTree.def (up to 10 args)
#![allow(clippy::large_enum_variant)] // one enum over all nodes — boxing would defeat deep-match

use std::cell::Cell;
use crate::node_child::{NodeChild, NodeLabel, NodeList, NodeMetadata, NodeString, Strictness, INVALID_LABEL};
use crate::visitor::{Path, TransformResult, Visitor, VisitorMut};
use crate::NodeId;
use crate::SemaId;
"""


def emit_doc(out, lines, indent=""):
    """Emit `lines` as a `///` doc comment at `indent`.

    Each input line is a paragraph of its own: hard-wrapped to 80 columns if
    needed (long node names make some of them overflow), but never joined with
    its neighbours, so the hand-written line breaks survive.
    """
    width = 80 - len(indent) - len("/// ")
    for line in lines:
        for wrapped in (textwrap.wrap(line, width) or [""]):
            out.append(f"{indent}/// {wrapped}".rstrip())


def unraw(name):
    """The plain source name of a possibly raw-escaped Rust identifier."""
    return name[2:] if name.startswith("r#") else name


def emit_node_kind(items, out):
    emit_doc(out, [
        "The kind discriminant of an AST node.",
        "",
        "Mirrors the C++ `NodeKind` enum: `#[repr(u32)]`, `ESTree.def` order,",
        "with `_Name_First`/`_Last` sentinels interleaved so the `Node::is_*`",
        "range predicates are two integer comparisons.",
    ])
    out.append("#[repr(u32)]")
    out.append("#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]")
    out.append("pub enum NodeKind {")
    for it in items:
        if it[0] == "first":
            emit_doc(out, [f"Exclusive lower bound of the `{it[1]}` range."],
                     "    ")
            out.append(f"    _{it[1]}_First,")
        elif it[0] == "last":
            emit_doc(out, [f"Exclusive upper bound of the `{it[1]}` range."],
                     "    ")
            out.append(f"    _{it[1]}_Last,")
        else:  # node
            emit_doc(out, [f"The kind of [`{it[1]}`]."], "    ")
            out.append(f"    {it[1]},")
    out.append("}")
    out.append("")


def emit_struct(node_name, fields, out):
    emit_doc(out, [f"The `{node_name}` AST node."])
    out.append("#[derive(Debug)]")
    out.append("#[repr(C)]")
    out.append(f"pub struct {node_name}<'gc> {{")
    for fd in fields:
        emit_doc(out, fd["doc"], "    ")
        out.append(f"    pub {fd['rust_field']}: {fd['rust_type']},")
    out.append("}")
    # new constructor — signature args one-per-line, struct-init fields one-per-line.
    new_args = ["metadata: NodeMetadata<'gc>"]
    for fd in fields:
        if fd["rust_field"] == "metadata":
            continue
        if fd["new_arg_type"] is None:
            continue  # decoration field: defaulted
        new_args.append(f"{fd['rust_field']}: {fd['new_arg_type']}")
    doc = [f"Build `{node_name}` from its metadata and `ESTree.def` fields."]
    if any(fd["new_arg_type"] is None for fd in fields):
        doc.append("Decoration fields start at their defaults.")
    out.append(f"impl<'gc> {node_name}<'gc> {{")
    emit_doc(out, doc, "    ")
    out.append("    pub fn new(")
    for arg in new_args:
        out.append(f"        {arg},")
    out.append("    ) -> Self {")
    out.append(f"        {node_name} {{")
    # Build the field-init list, one field per line.
    for fd in fields:
        rf = fd["rust_field"]
        if rf == "metadata":
            out.append("            metadata,")
        elif fd["new_arg_type"] is None:
            # decoration field: default.
            out.append(f"            {rf}: {fd['default_expr']},")
        elif fd["cell"]:
            out.append(f"            {rf}: Cell::new({rf}),")
        else:
            out.append(f"            {rf},")
    out.append("        }")
    out.append("    }")
    emit_atom_accessors(fields, out)
    out.append("}")
    out.append("")


# --------------------------------------------------------------------------
# -- Atom -> string accessors (NodeLabel / NodeString fields) --
# --------------------------------------------------------------------------
# Both field types are the same underlying interned-bytes handle, but they mean
# different things, so they get different accessors (design:
# doc/superpowers/specs/2026-08-15-atom-string-accessors-design.md §5.2):
#
#   NodeLabel  -> `<field>_str`                       (lossy, ergonomic)
#   NodeString -> `try_<field>_str` + `<field>_str_lossy`, and NO `<field>_str`
#
# An unrepresentable identifier means something is broken; an unrepresentable
# string literal is legal JS that a codegen tool must not silently corrupt.
#
# Each paragraph below is one list element; emit_doc hard-wraps it to 80
# columns, so do not pre-break lines.
def pair_note(tail):
    """The 'a surrogate pair is not the unrepresentable case' paragraph."""
    return (
        "Only an *unpaired* surrogate is unrepresentable. A WTF-8 surrogate "
        "*pair* is not: the lexer interns an astral character in "
        "surrogate-pair form, and it is folded back into the character it "
        "encodes, so a `\"\U0001F600\"` literal " + tail
    )



BORROW_NOTE = "The returned `&str` borrows from `gc`, not from `self`."


def label_accessor_doc(json_name):
    """Doc paragraphs for the `<field>_str` accessor of a `NodeLabel` field."""
    return [
        f"The `{json_name}` label as UTF-8 text.",
        "",
        "Label fields hold identifier names, operators, keyword-like kinds and "
        "raw source spans. An identifier cannot contain an unpaired surrogate "
        "— the lexer rejects one with a dedicated diagnostic — and the "
        "remaining labels are literal source text, so in practice the bytes "
        "are already valid UTF-8 and this borrows them directly, allocating "
        "nothing.",
        "",
        "A label that is nevertheless unrepresentable, which realistically "
        "means a hand-built AST rather than a parsed one, has each unpaired "
        "surrogate rendered as a single U+FFFD instead of being reported. "
        "Only an unpaired surrogate is affected: a WTF-8 surrogate *pair* is "
        "folded back into the character it encodes, so a `\"\U0001F600\"` "
        "label comes out intact.",
        "",
        "Use [`GCLock::bytes`](crate::context::GCLock::bytes) for the exact "
        "bytes, or "
        "[`GCLock::try_bytes_str`](crate::context::GCLock::try_bytes_str) to "
        "detect the substitution. " + BORROW_NOTE,
    ]


def try_string_accessor_doc(json_name, field):
    """Doc paragraphs for the `try_<field>_str` accessor of a `NodeString`."""
    return [
        f"The `{json_name}` string value as UTF-8, or `None` if it has no "
        f"UTF-8 form.",
        "",
        "A JS string value is a sequence of UTF-16 code units, so it may "
        "legally contain an *unpaired* surrogate (`\"\\uD800\"` parses, and is "
        "not an error). That, and only that, is what `None` reports: the "
        "value is intact, it simply cannot be spelled in UTF-8. Read it "
        "losslessly with [`GCLock::bytes`](crate::context::GCLock::bytes).",
        "",
        pair_note("yields `Some(\"\U0001F600\")`, not `None`."),
        "",
        f"There is deliberately no plain `{field}_str`: an unrepresentable "
        f"identifier means something is broken, but an unrepresentable string "
        f"literal is legal JS, and a codegen or refactoring tool that let "
        f"U+FFFD be substituted here would silently rewrite the user's "
        f"program. Reach for [`Self::{field}_str_lossy`] only when a "
        f"best-effort rendering is what you want.",
        "",
        "Valid UTF-8 is borrowed from the atom's own bytes and allocates "
        "nothing; folding a surrogate pair allocates once per atom, cached in "
        "the context. " + BORROW_NOTE,
    ]


def lossy_string_accessor_doc(json_name, field):
    """Doc paragraphs for the `<field>_str_lossy` accessor of a `NodeString`."""
    return [
        f"The `{json_name}` string value as UTF-8, substituting U+FFFD for "
        f"anything unrepresentable.",
        "",
        "**This is lossy.** A JS string value may legally contain an unpaired "
        "surrogate (`\"\\uD800\"` parses), which has no UTF-8 form; each one "
        "becomes exactly one U+FFFD here. Do not use this to re-emit source: "
        "a codegen or refactoring tool would silently rewrite the user's "
        "program.",
        "",
        pair_note("comes out intact."),
        "",
        f"Use [`Self::try_{field}_str`] or "
        f"[`GCLock::bytes`](crate::context::GCLock::bytes) when the exact "
        f"value matters. " + BORROW_NOTE,
    ]


def emit_atom_accessors(fields, out):
    """Emit the atom->string accessors for a leaf node's atom fields."""
    for fd in fields:
        atom_kind = fd.get("atom_kind")
        if atom_kind is None:
            continue
        rf = fd["rust_field"]        # possibly `r#`-escaped: the field access
        field = unraw(rf)            # plain name: the method-name stem
        json_name = fd["json_name"]
        if atom_kind == "label":
            emit_doc(out, label_accessor_doc(json_name), "    ")
            out.append(f"    pub fn {field}_str<'a>(")
            out.append("        &self,")
            out.append("        gc: &'a crate::context::GCLock<'_, '_>,")
            out.append("    ) -> &'a str {")
            out.append(f"        gc.bytes_str_lossy(self.{rf}.get())")
            out.append("    }")
        elif atom_kind == "string":
            emit_doc(out, try_string_accessor_doc(json_name, field), "    ")
            out.append(f"    pub fn try_{field}_str<'a>(")
            out.append("        &self,")
            out.append("        gc: &'a crate::context::GCLock<'_, '_>,")
            out.append("    ) -> Option<&'a str> {")
            out.append(f"        gc.try_bytes_str(self.{rf}.get())")
            out.append("    }")
            emit_doc(out, lossy_string_accessor_doc(json_name, field), "    ")
            out.append(f"    pub fn {field}_str_lossy<'a>(")
            out.append("        &self,")
            out.append("        gc: &'a crate::context::GCLock<'_, '_>,")
            out.append("    ) -> &'a str {")
            out.append(f"        gc.bytes_str_lossy(self.{rf}.get())")
            out.append("    }")
        else:
            sys.exit(f"error: bad atom_kind {atom_kind!r} on {json_name!r}")


def emit_node_enum(nodes, out):
    emit_doc(out, [
        "An AST node: one arm per `ESTree.def` node kind.",
        "",
        "`#[repr(C)]`, and each arm's payload struct is named after the arm,",
        "so a deep `match` compiles to a single dispatch. Nodes live in the",
        "[`crate::context::Context`] arena and are handed out as `&'gc Node`",
        "for as long as a [`crate::context::GCLock`] is held.",
    ])
    out.append("#[derive(Debug)]")
    out.append("#[repr(C)]")
    out.append("pub enum Node<'gc> {")
    for name, _fields in nodes:
        emit_doc(out, [f"A [`{name}`] node."], "    ")
        out.append(f"    {name}({name}<'gc>),")
    out.append("}")
    out.append("")


def emit_accessors(items, nodes, out):
    out.append("impl<'gc> Node<'gc> {")
    # kind().
    out.append("    /// This node's [`NodeKind`].")
    out.append("    pub fn kind(&self) -> NodeKind {")
    out.append("        match self {")
    for name, _ in nodes:
        out.append(f"            Node::{name}(_) => NodeKind::{name},")
    out.append("        }")
    out.append("    }")
    out.append("")
    # metadata().
    out.append("    /// This node's metadata, common to every kind.")
    out.append("    pub fn metadata(&self) -> &NodeMetadata<'gc> {")
    out.append("        match self {")
    for name, _ in nodes:
        out.append(f"            Node::{name}(n) => &n.metadata,")
    out.append("        }")
    out.append("    }")
    out.append("")
    # node_id().
    out.append("    /// This node's arena identity (see [`NodeId`]).")
    out.append("    pub fn node_id(&self) -> NodeId {")
    out.append("        self.metadata().id.get()")
    out.append("    }")
    out.append("")
    # range().
    out.append("    /// This node's source range.")
    out.append("    pub fn range(&self) -> hermes_support::location::SMRange {")
    out.append("        self.metadata().range.get()")
    out.append("    }")
    out.append("")
    # range predicates.
    for it in items:
        if it[0] == "first":
            rng = it[1]
            pred = "is_" + camel_to_snake(rng)
            emit_doc(out, [f"Whether this node's kind is in the `{rng}` range."],
                     "    ")
            out.append(f"    pub fn {pred}(&self) -> bool {{")
            out.append("        let k = self.kind() as u32;")
            out.append(
                f"        (NodeKind::_{rng}_First as u32) < k "
                f"&& k < (NodeKind::_{rng}_Last as u32)"
            )
            out.append("    }")
            out.append("")
    # leaf accessors.
    for name, _ in nodes:
        acc = "as_" + camel_to_snake(name)
        emit_doc(out, [f"The payload if this is a [`{name}`], `None` otherwise."],
                 "    ")
        out.append(f"    pub fn {acc}(&self) -> Option<&{name}<'gc>> {{")
        out.append(f"        if let Node::{name}(n) = self {{ Some(n) }} "
                   f"else {{ None }}")
        out.append("    }")
        out.append("")
    # visit_children.
    emit_visit_children(nodes, out)
    out.append("")
    # visit_children_mut.
    emit_visit_children_mut(nodes, out)
    out.append("")
    # mark_lists.
    emit_mark_lists(nodes, out)
    # node_type_str + dump_children (phase 4 — the JSON dumper walk).
    out.append("")
    emit_node_type_str(nodes, out)
    out.append("")
    emit_dump_children(nodes, out)
    out.append("}")
    out.append("")


def child_fields(fields):
    """Fields that participate in child walks, in declared order."""
    return [fd for fd in fields if fd["child_kind"] in
            ("single", "opt", "list", "declist")]


def emit_visit_children(nodes, out):
    out.append("    /// Visit every child node in declared order: the `ESTree.def`")
    out.append("    /// fields plus the decoration lists the GC marker must trace.")
    out.append("    pub fn visit_children<V: Visitor<'gc> + ?Sized>"
               "(&'gc self, v: &mut V) {")
    out.append("        match self {")
    for name, fields in nodes:
        cf = child_fields(fields)
        if not cf:
            out.append(f"            Node::{name}(_) => {{}}")
            continue
        out.append(f"            Node::{name}(n) => {{")
        for fd in cf:
            rf = fd["rust_field"]
            kind = fd["child_kind"]
            if kind == "single":
                out.append(f"                v.visit_node(n.{rf});")
            elif kind == "opt":
                out.append(
                    f"                if let Some(c) = n.{rf} "
                    f"{{ v.visit_node(c); }}")
            elif kind == "list":
                out.append(
                    f"                for c in n.{rf}.iter() "
                    f"{{ v.visit_node(c); }}")
            elif kind == "declist":
                out.append(
                    f"                for c in n.{rf}.get().iter() "
                    f"{{ v.visit_node(c); }}")
        out.append("            }")
    out.append("        }")
    out.append("    }")


def emit_mark_lists(nodes, out):
    out.append("    /// Call `cb` on every `NodeList` field of this node, including")
    out.append("    /// decoration lists. Used by the GC to mark list elements.")
    out.append("    pub fn mark_lists<F: FnMut(&NodeList<'gc>)>"
               "(&'gc self, cb: &mut F) {")
    out.append("        match self {")
    for name, fields in nodes:
        list_fields = [fd for fd in fields
                       if fd["child_kind"] in ("list", "declist")]
        if not list_fields:
            continue
        parts = []
        for fd in list_fields:
            rf = fd["rust_field"]
            if fd["child_kind"] == "declist":
                parts.append(f"cb(&n.{rf}.get());")
            else:
                parts.append(f"cb(&n.{rf});")
        out.append(f"            Node::{name}(n) => {{ {' '.join(parts)} }}")
    out.append("            _ => {}")
    out.append("        }")
    out.append("    }")


def emit_node_type_str(nodes, out):
    out.append("    /// The ESTree node type name — the JSON `\"type\"` value.")
    out.append("    pub fn node_type_str(&self) -> &'static str {")
    out.append("        match self {")
    for name, _ in nodes:
        out.append(f"            Node::{name}(_) => {name!r},".replace("'", '"'))
    out.append("        }")
    out.append("    }")


def dump_arg_fields(fields):
    """The .def-arg fields, in declared order (the only fields the dumper emits)."""
    return [fd for fd in fields if fd.get("is_def_arg")]


def emit_dump_children(nodes, out):
    out.append("    /// Emit this node's `.def` child fields as JSON key/values,")
    out.append("    /// in declared order. Mirrors C++ `visitChildren`.")
    out.append("    pub fn dump_children<'a, 'w>(")
    out.append("        &'gc self,")
    out.append("        d: &mut crate::dump::ESTreeJSONDumper<'a, 'w>,")
    out.append("    ) {")
    out.append("        match self {")
    for name, fields in nodes:
        af = dump_arg_fields(fields)
        if not af:
            out.append(f"            Node::{name}(_) => {{}}")
            continue
        out.append(f"            Node::{name}(n) => {{")
        for fd in af:
            key = fd["json_name"]
            rf = fd["rust_field"]
            ign = "true" if fd.get("_ignore") else "false"
            dk = fd["dump_kind"]
            if dk == "node_single":
                out.append(f'                d.field_node("{key}", Some(n.{rf}), {ign});')
            elif dk == "node_opt":
                out.append(f'                d.field_node("{key}", n.{rf}, {ign});')
            elif dk == "list":
                out.append(f'                d.field_list("{key}", n.{rf}, {ign});')
            elif dk == "bool":
                out.append(f'                d.field_bool("{key}", n.{rf}.get(), {ign});')
            elif dk == "number":
                out.append(f'                d.field_number("{key}", n.{rf}.get(), {ign});')
            elif dk == "label":
                out.append(f'                d.field_label("{key}", n.{rf}.get(), {ign});')
            else:
                sys.exit(f"error: {name}.{rf}: bad dump_kind {dk!r}")
        out.append("            }")
    out.append("        }")
    out.append("    }")


def emit_node_field(nodes, out):
    """Emit the NodeField enum: one variant per distinct structural-child field name.

    Structural children are fields with child_kind in {single, opt, list}.
    Variants are sorted deterministically (so output is stable/idempotent).
    The rust_field strings are used verbatim as variant names (including any
    raw-identifier prefix like r#await).
    """
    seen = set()
    for _name, fields in nodes:
        for fd in fields:
            if fd["child_kind"] in ("single", "opt", "list"):
                seen.add(fd["rust_field"])
    variants = sorted(seen)

    out.append("/// The name of a structural child field of an AST node (used in `Path`).")
    out.append("#[derive(Debug, Copy, Clone, PartialEq, Eq)]")
    out.append("#[allow(non_camel_case_types)]")
    out.append("pub enum NodeField {")
    for v in variants:
        emit_doc(out, [f"The `{unraw(v)}` child field."], "    ")
        out.append(f"    {v},")
    out.append("}")
    out.append("")


def structural_fields(fields):
    """Structural-child fields (get a builder setter; threaded by
    visit_children_mut), in declared order."""
    return [fd for fd in fields if fd["child_kind"] in ("single", "opt", "list")]


def emit_visit_children_mut(nodes, out):
    """Emit Node::visit_children_mut — functional rebuild via the per-kind
    builder. Threads only structural-child fields (single/opt/list)."""
    out.append("    /// Transform this node's children with `visitor`, rebuilding it only if")
    out.append("    /// a child changed. `self` is the original parent.")
    out.append("    pub fn visit_children_mut<V: VisitorMut<'gc>>(")
    out.append("        &'gc self,")
    out.append("        ctx: &'gc crate::context::GCLock<'_, '_>,")
    out.append("        visitor: &mut V,")
    out.append("    ) -> TransformResult<&'gc Node<'gc>> {")
    out.append("        let builder = builder::Builder::from_node(self);")
    out.append("        #[allow(unused_mut)]")
    out.append("        match builder {")
    for name, fields in nodes:
        sf = structural_fields(fields)
        if not sf:
            out.append(
                f"            builder::Builder::{name}(mut b) => b.build(ctx),")
            continue
        out.append(f"            builder::Builder::{name}(mut b) => {{")
        for fd in sf:
            rf = fd["rust_field"]
            out.append("                if let TransformResult::Changed(v) =")
            out.append(
                f"                    b.inner.{rf}.visit_child_mut("
                f"ctx, visitor, Path::new(self, NodeField::{rf})) {{")
            out.append(f"                    b.{rf}(v);")
            out.append("                }")
        out.append("                b.build(ctx)")
        out.append("            }")
    out.append("        }")
    out.append("    }")


def emit_builders(nodes, out):
    """Emit the module-level `pub mod builder` — the Builder enum + one
    clone-with-one-field-changed builder struct per node."""
    out.append("/// Per-kind node builders: clone a node with (optionally) one")
    out.append("/// structural-child field changed. The rebuild primitive used by")
    out.append("/// `Node::visit_children_mut`.")
    out.append("pub mod builder {")
    out.append("    use std::cell::Cell;")
    out.append("    use super::*;")
    out.append("    use crate::node_child::NodeChild;")
    out.append("    use crate::visitor::TransformResult;")
    out.append("")
    out.append("    /// One builder per node kind; clone-with-one-field-changed.")
    out.append("    #[derive(Debug)]")
    out.append("    pub enum Builder<'gc> {")
    for name, _fields in nodes:
        emit_doc(out, [f"Builder for [`super::{name}`]."], "        ")
        out.append(f"        {name}(self::{name}<'gc>),")
    out.append("    }")
    out.append("")
    out.append("    impl<'gc> Builder<'gc> {")
    out.append("        /// Start a builder for `node`, dispatching on its kind.")
    out.append("        pub fn from_node(node: &'gc Node<'gc>) -> Self {")
    out.append("            match node {")
    for name, _fields in nodes:
        out.append(
            f"                Node::{name}(n) => "
            f"Builder::{name}(self::{name}::from_node(n)),")
    out.append("            }")
    out.append("        }")
    out.append("    }")
    out.append("")
    for name, fields in nodes:
        emit_builder_struct(name, fields, out)
    out.append("}")
    out.append("")


def emit_builder_struct(name, fields, out):
    """Emit one builder struct + impl (from_node/build/build_forced/setters)."""
    emit_doc(out, [
        f"Clone-with-changes builder for [`super::{name}`].",
    ], "    ")
    out.append("    #[derive(Debug)]")
    out.append(f"    pub struct {name}<'gc> {{")
    out.append("        is_changed: bool,")
    out.append(f"        pub(super) inner: super::{name}<'gc>,")
    out.append("    }")
    out.append(f"    impl<'gc> {name}<'gc> {{")
    out.append("        /// Start from a copy of `node`, with no field changed yet.")
    out.append(
        f"        pub fn from_node(node: &'gc super::{name}<'gc>) -> Self {{")
    out.append("            Self {")
    out.append("                is_changed: false,")
    out.append(f"                inner: super::{name} {{")
    for fd in fields:
        rf = fd["rust_field"]
        kind = fd["child_kind"]
        if kind == "meta":
            out.append("                    metadata: node.metadata.duplicate(),")
        elif kind in ("single", "opt", "list"):
            out.append(f"                    {rf}: node.{rf}.duplicate(),")
        else:  # declist or value cell
            out.append(f"                    {rf}: Cell::new(node.{rf}.get()),")
    out.append("                },")
    out.append("            }")
    out.append("        }")
    out.append("        /// Allocate the rebuilt node if a setter ran, else")
    out.append("        /// `TransformResult::Unchanged`.")
    out.append(
        "        pub fn build(self, gc: &'gc crate::context::GCLock<'_, '_>)"
        " -> TransformResult<&'gc Node<'gc>> {")
    out.append("            if self.is_changed {")
    out.append("                TransformResult::Changed(self.build_forced(gc))")
    out.append("            } else {")
    out.append("                TransformResult::Unchanged")
    out.append("            }")
    out.append("        }")
    out.append("        /// Allocate the node unconditionally, changed or not.")
    out.append(
        "        pub fn build_forced(self, gc: &'gc crate::context::GCLock<'_, '_>)"
        " -> &'gc Node<'gc> {")
    out.append(f"            gc.alloc(Node::{name}(self.inner))")
    out.append("        }")
    for fd in structural_fields(fields):
        rf = fd["rust_field"]
        ty = fd["rust_type"]
        out.append(
            f"        /// Set the `{unraw(rf)}` field and mark the builder changed.")
        out.append(
            f"        pub fn {rf}(&mut self, {rf}: {ty}) {{ "
            f"self.is_changed = true; self.inner.{rf} = {rf}; }}")
    out.append("    }")


# --------------------------------------------------------------------------
# -- CLI + validation + driver --
# --------------------------------------------------------------------------
def generate():
    if not DEF.exists():
        sys.exit(f"error: {DEF} not found — run from a full hermes checkout")
    src = DEF.read_text()
    items, ignore_if_empty = parse_def(src)
    range_parents = build_range_parents(items)

    # Build the composed node list (name, fields), in .def order.
    nodes = []
    for it in items:
        if it[0] != "node":
            continue
        _, name, base, def_fields = it
        fields = compose_fields(name, base, def_fields, range_parents)
        nodes.append((name, fields))

    # Validate ESTREE_IGNORE_IF_EMPTY against the real nodes/fields (drift guard).
    fields_by_node = {name: fields for name, fields in nodes}
    for node_name, field_list in ignore_if_empty.items():
        if node_name not in fields_by_node:
            sys.exit(f"error: IGNORE_IF_EMPTY names unknown node {node_name!r}")
        argnames = {fd["json_name"] for fd in fields_by_node[node_name]
                    if fd.get("is_def_arg")}
        for f in field_list:
            if f not in argnames:
                sys.exit(
                    f"error: IGNORE_IF_EMPTY({node_name},{f}) is not a .def arg field")

    # Stamp the per-field _ignore flag (used by the dump-children emission).
    for name, fields in nodes:
        ign = set(ignore_if_empty.get(name, ()))
        for fd in fields:
            fd["_ignore"] = fd.get("is_def_arg") and fd["json_name"] in ign

    # Anti-drift: node count.
    node_count = len(nodes)
    if node_count != EXPECTED_NODES:
        sys.exit(
            f"error: ESTree.def has {node_count} nodes, expected "
            f"{EXPECTED_NODES} (update EXPECTED_NODES if intentional)"
        )

    # Validate: every range has a decoration; every leaf resolved a decoration;
    # every LEAF_DECORATOR key names a real node (catches silent node renames).
    node_names = {name for name, _ in nodes}
    for rng in range_parents:
        deco = rng + "Decoration"
        if deco not in DECORATIONS:
            sys.exit(f"error: range {rng!r} has no {deco} in the table")
    for name, _ in nodes:
        leaf_deco = LEAF_DECORATOR.get(name, "EmptyDecoration")
        if leaf_deco not in DECORATIONS:
            sys.exit(f"error: leaf {name!r} -> unknown decoration {leaf_deco}")
    for leaf in LEAF_DECORATOR:
        if leaf not in node_names:
            sys.exit(f"error: LEAF_DECORATOR key {leaf!r} is not a node in ESTree.def")

    # Validate: the generated atom->string accessors do not collide with each
    # other, with `new`, or with a field name (which would shadow nothing but
    # would read as an accessor for a field that is not there).
    n_label = n_string = 0
    for name, fields in nodes:
        taken = {"new"} | {fd["rust_field"] for fd in fields}
        for fd in fields:
            atom_kind = fd.get("atom_kind")
            if atom_kind is None:
                continue
            stem = unraw(fd["rust_field"])
            if atom_kind == "label":
                n_label += 1
                methods = [f"{stem}_str"]
            else:
                n_string += 1
                methods = [f"try_{stem}_str", f"{stem}_str_lossy"]
            for m in methods:
                if m in taken:
                    sys.exit(f"error: {name}::{m} collides with an existing "
                             f"item on the same struct")
                taken.add(m)
    if (n_label, n_string) != (EXPECTED_LABEL_FIELDS, EXPECTED_STRING_FIELDS):
        sys.exit(
            f"error: ESTree.def has {n_label} NodeLabel and {n_string} "
            f"NodeString fields, expected {EXPECTED_LABEL_FIELDS} and "
            f"{EXPECTED_STRING_FIELDS} (update the constants if intentional)"
        )

    out = [HEADER]
    emit_node_kind(items, out)
    emit_node_field(nodes, out)
    for name, fields in nodes:
        emit_struct(name, fields, out)
    emit_node_enum(nodes, out)
    emit_accessors(items, nodes, out)
    emit_builders(nodes, out)

    text = "\n".join(out)
    if not text.endswith("\n"):
        text += "\n"
    return text, node_count, len(range_parents)


def main():
    to_stdout = "--stdout" in sys.argv[1:]
    text, node_count, range_count = generate()
    if to_stdout:
        sys.stdout.write(text)
    else:
        OUT.write_text(text)
    print(
        f"gen_nodes.py: {node_count} nodes, {range_count} ranges "
        f"{'-> stdout' if to_stdout else f'-> {OUT}'}",
        file=sys.stderr,
    )


if __name__ == "__main__":
    main()