bonsai-ninja-lang-objc 0.2.3

Objective-C language adapter.
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
//! Objective-C language adapter.
//!
//! `.m` files can also legitimately be MATLAB source; the CLI's file
//! detection assigns `.m` to Objective-C by default (same convention
//! `tree-sitter-language-pack` uses). If a project mixes the two, the
//! user can scope `--include` / `--exclude` to disambiguate.
mod parse_recovery;

use bonsai_common::{FileId, Span};
use bonsai_lang_api::{
    decl_index_with_handler, extract_imports_via,
    kit::{
        c_family_preproc_imports, collect_kinds, first_named_child_of_kind, language_from_pack, node_text,
        package_module_segments_with_workspace_prefix, parse_with, span_of,
    },
    AdapterContext, AdapterError, ArgumentPassingMode, AssignValueKind, DeclIndex, DeclKind,
    ExpressionPlaceExtraction, FieldWrite, FlowEvent, GrammarHandler, ImportIndex, ImportSpec,
    LanguageAdapter, LanguageCapabilities, LanguageId, ModulePath, SyntaxSpecialForm, TypeAliasBinding,
};
use parse_recovery::{objc_parse_recovery_edits, objc_tree_proves_language};
use tree_sitter::{Language, Node, Tree};

fn objc_foreach_binding(node: Node<'_>) -> Option<(Node<'_>, Node<'_>)> {
    if node.kind() == "for_in_statement" {
        let binding = node
            .child_by_field_name("left")
            .or_else(|| node.child_by_field_name("declarator"))?;
        let iterable = node
            .child_by_field_name("right")
            .or_else(|| node.child_by_field_name("value"))?;
        return Some((binding, iterable));
    }
    if node.kind() != "for_statement" {
        return None;
    }

    // tree-sitter-objc represents both C-style `for (;;)` and Objective-C
    // fast enumeration as `for_statement`.  The `in` token is the exact CST
    // discriminator.  Its nearest named siblings are the declarator pattern
    // and iterable expression; the preceding type node is intentionally not
    // treated as a binding.
    let child_count = u32::try_from(node.child_count()).ok()?;
    let in_index =
        (0..child_count).find(|index| node.child(*index).is_some_and(|child| child.kind() == "in"))?;
    let binding = (0..in_index)
        .rev()
        .filter_map(|index| node.child(index))
        .find(Node::is_named)?;
    let iterable = ((in_index + 1)..child_count)
        .filter_map(|index| node.child(index))
        .find(Node::is_named)?;
    Some((binding, iterable))
}

pub const LANG_ID: LanguageId = LanguageId::new("objc");
const PACK_NAME: &str = "objc";

fn objc_indirect_place_operand(node: Node<'_>) -> Option<Node<'_>> {
    if !matches!(node.kind(), "pointer_expression" | "unary_expression") {
        return None;
    }
    let mut cursor = node.walk();
    let has_indirection = node
        .children(&mut cursor)
        .any(|child| matches!(child.kind(), "*" | "&"));
    has_indirection
        .then(|| {
            node.child_by_field_name("argument")
                .or_else(|| node.child_by_field_name("operand"))
        })
        .flatten()
}

// Objective-C handler. Mixes C-style functions with Objective-C
// methods. `*_method_declaration` covers `@interface` headers (no body)
// and `method_definition` covers `@implementation` bodies. ObjC's
// `@try/@catch/@finally` parses as `try_statement`. `@synchronized`
// and `@autoreleasepool` are scope-bracketed regions modeled as
// `using` (resource-managed scope) since `body` then runs under the
// managed lock / pool.
const HANDLER: GrammarHandler = GrammarHandler {
    literal_value_kinds: &["null", "true", "false"],
    string_literal_kinds: &["string_literal", "char_literal", "concatenated_string"],
    comment_kinds: &["comment"],
    doc_comment_prefixes: &["///", "//!", "/**"],
    decorator_kinds: &["attribute"],
    parameter_container_kinds: &["parameter_list"],
    parameter_kinds: &["parameter", "method_parameter", "parameter_declaration"],
    parameter_annotation_kinds: &["attribute"],
    parameter_annotation_name_extractor: None,
    keyword_parameter_kinds: &["keyword_argument", "keyword_declarator"],
    parameter_selector_kinds: &["keyword", "selector_keyword", "identifier"],
    last_identifier_parameter_kinds: &["method_parameter"],
    binding_identifier_kinds: &["identifier"],
    anonymous_variadic_token: Some("..."),
    identifier_kinds: &["identifier"],
    named_aggregate_kinds: &["initializer_list", "dictionary_literal"],
    positional_aggregate_kinds: &["initializer_list", "array_literal"],
    aggregate_pair_kinds: &["initializer_pair"],
    two_child_aggregate_pair_kinds: &["dictionary_pair"],
    aggregate_key_field_names: &["designator"],
    aggregate_value_field_names: &["value"],
    static_field_name_kinds: &["field_identifier"],
    static_subscript_key_extractor: Some(objc_static_string_key),
    aggregate_syntax_only_kinds: &["type_identifier"],
    transparent_call_wrapper_kinds: &["field_expression", "parenthesized_expression"],
    single_expression_group_kinds: &["expression_list"],
    assignment_target_wrapper_kinds: &[
        "init_declarator",
        "declarator",
        "function_declarator",
        "pointer_declarator",
        "parenthesized_declarator",
        "block_pointer_declarator",
    ],
    binding_declaration_keyword_spellings: &["auto", "const"],
    nested_type_ownership: true,
    fn_kinds: &[
        "function_definition",
        "method_definition",
        "class_method_declaration",
        "instance_method_declaration",
    ],
    class_kinds: &[
        "class_interface",
        "class_implementation",
        "category_interface",
        "category_implementation",
        "protocol_declaration",
    ],
    class_decl_kinds: &[
        ("class_interface", DeclKind::Class),
        ("class_implementation", DeclKind::Class),
        ("category_interface", DeclKind::Class),
        ("category_implementation", DeclKind::Class),
        ("protocol_declaration", DeclKind::Interface),
    ],
    method_kinds: &["method_definition"],
    method_context_kinds: &["class_implementation", "category_implementation"],
    method_owner_barrier_kinds: &[],
    constructor_method_kinds: &[],
    constructor_names: &["init"],
    if_kinds: &["if_statement"],
    branch_then_field_names: &["consequence", "body"],
    branch_else_field_names: &["alternative"],
    branch_condition_field_names: &["condition"],
    loop_body_field_names: &["body"],
    loop_body_kinds: &["compound_statement", "expression_statement"],
    branch_arm_kinds: &["compound_statement", "expression_statement"],
    for_kinds: &["for_statement"],
    foreach_kinds: &[],
    foreach_binding_extractor: Some(objc_foreach_binding),
    while_kinds: &["while_statement"],
    do_kinds: &["do_statement"],
    loop_kinds: &[],
    call_kinds: &["call_expression", "message_expression"],
    call_callee_field_names: &["function"],
    call_receiver_field_names: &["receiver"],
    call_member_field_names: &["method"],
    call_argument_field_names: &["arguments"],
    call_argument_container_kinds: &["argument_list"],
    direct_call_argument_excluded_fields: &["receiver", "method"],
    lambda_body_field_names: &["body"],
    argument_passing_mode_extractor: Some(objc_argument_passing_mode),
    indirect_place_operand_extractor: Some(objc_indirect_place_operand),
    expression_value_kind_extractor: Some(objc_expression_value_kind),
    call_ref_kinds: &["call_expression", "message_expression"],
    member_expression_kinds: &["field_expression"],
    subscript_expression_kinds: &["subscript_expression"],
    member_base_field_names: &["argument"],
    member_name_field_names: &["field"],
    subscript_base_field_names: &["argument"],
    subscript_index_field_names: &["index"],
    expression_place_extractor: Some(objc_expression_places),
    syntax_error_tolerant_call_names: &["va_arg", "__builtin_va_arg"],
    assignment_kinds: &["assignment_expression", "init_declarator"],
    compound_assignment_operators: &["+=", "-=", "*=", "/=", "%=", "<<=", ">>=", "&=", "^=", "|="],
    positional_aggregate_assignment_kinds: &["init_declarator"],
    positional_aggregate_value_kinds: &["initializer_list"],
    return_kinds: &["return_statement"],
    throw_kinds: &["throw_statement"],
    // Current tree-sitter-objc emits `block_literal`; keep the older
    // `block_literal_expression` spelling for grammar-version compatibility.
    lambda_kinds: &["block_literal", "block_literal_expression"],
    try_kinds: &["try_statement"],
    catch_kinds: &["catch_clause"],
    finally_kinds: &["finally_clause"],
    break_kinds: &["break_statement"],
    continue_kinds: &["continue_statement"],
    control_label_field_names: &[],
    yield_kinds: &[],
    await_kinds: &[],
    defer_kinds: &[],
    using_kinds: &["synchronized_statement", "autoreleasepool_statement"],
    using_body_field_names: &["body"],
    try_body_field_names: &["body"],
    special_forms: &[SyntaxSpecialForm::DirectCallArguments],
    value_free_expression_kinds: &["sizeof_expression", "alignof_expression", "typeof_specifier"],
    method_receiver_param_index: None,
    implicit_receiver_names: &["self", "super"],
    implicit_receiver_prefixes: &[],
    tail_expression_returns: false,
    void_return_type_names: &[],
    ..bonsai_lang_api::EMPTY_HANDLER
};

fn objc_expression_value_kind(node: Node<'_>, _src: &[u8]) -> Option<AssignValueKind> {
    matches!(node.kind(), "string_literal" | "char_literal" | "number_literal")
        .then_some(AssignValueKind::Literal)
}

fn objc_argument_passing_mode(argument: Node<'_>, value: Node<'_>) -> ArgumentPassingMode {
    if [argument, value].into_iter().any(|node| {
        matches!(node.kind(), "unary_expression" | "pointer_expression") && {
            let mut cursor = node.walk();
            let has_address_of = node.children(&mut cursor).any(|child| child.kind() == "&");
            has_address_of
        }
    }) {
        ArgumentPassingMode::WriteBack
    } else {
        ArgumentPassingMode::Value
    }
}

/// Zero-sized adapter handle; all state lives in the shared parser pack.
#[derive(Debug, Default, Copy, Clone)]
pub struct ObjCAdapter;

impl ObjCAdapter {
    /// Construct a fresh adapter handle.
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl LanguageAdapter for ObjCAdapter {
    fn language_id(&self) -> LanguageId {
        LANG_ID
    }
    fn display_name(&self) -> &'static str {
        "Objective-C"
    }
    fn file_extensions(&self) -> &'static [&'static str] {
        // `.h` is shared with C and C++; the database evaluates every
        // candidate grammar against the concrete CST instead of assigning
        // headers by extension alone.
        &["m", "mm", "h"]
    }
    fn tree_sitter_language(&self) -> Result<Language, AdapterError> {
        language_from_pack(PACK_NAME)
    }
    fn source_syntax_proves_language(
        &self,
        snapshot: &bonsai_lang_api::FileSnapshot,
        tree: &Tree,
    ) -> bonsai_lang_api::LanguageOwnershipEvidence {
        if objc_tree_proves_language(snapshot, tree) {
            bonsai_lang_api::LanguageOwnershipEvidence::Proven
        } else {
            bonsai_lang_api::LanguageOwnershipEvidence::Excluded
        }
    }
    fn parse_recovery_edits(
        &self,
        snapshot: &bonsai_lang_api::FileSnapshot,
        vfs: &bonsai_lang_api::Vfs,
        tree: &Tree,
    ) -> Vec<bonsai_lang_api::ParseRecoveryEdit> {
        objc_parse_recovery_edits(snapshot, vfs, tree)
    }
    fn capabilities(&self) -> LanguageCapabilities {
        // Macros: tree-sitter-objc parses `NSAssert(...)` / `NS_INLINE`
        // / `IB_DESIGNABLE` etc. as ordinary call expressions or
        // declarators, so name-resolution narrows them. Genuine
        // multi-statement `#define` expansion isn't performed.
        LanguageCapabilities {
            module_default_export_names: &[],
            universal_type_names: &["id"],
            module_path_syntax: bonsai_lang_api::ModulePathSyntax::none(),
            macros: bonsai_lang_api::CapabilityLevel::Partial,
            receiver_types: bonsai_lang_api::CapabilityLevel::Partial,
            constructor_method_names: &["init"],
            super_receiver_tokens: &["super"],
            implicit_receiver_tokens: &["self"],
            same_directory_unqualified_calls: true,
            build_target_linkage: true,
            ..LanguageCapabilities::partial_baseline()
        }
    }
    fn extract_declarations(&self, file: FileId, ctx: &AdapterContext<'_>) -> DeclIndex {
        let mut decl_index = decl_index_with_handler(PACK_NAME, file, ctx, &HANDLER);
        bonsai_lang_api::apply_file_stem_semantic_identity(&mut decl_index, ctx);
        apply_objc_class_semantic_identity(&mut decl_index, ctx);
        // The leading `_` on an Objective-C method/selector is an Apple
        // naming convention, not a linkage boundary: selectors dispatch
        // dynamically across files. Marking `_`-prefixed decls
        // Visibility::Private would make the resolver enforce them as
        // strictly file-scoped, dropping every legitimate cross-file
        // flow through a `_`-prefixed helper to zero candidates. Leave
        // visibility at the kit default and let the resolver's name +
        // receiver-type narrowing do the work instead.
        mark_objc_initializer_methods(&mut decl_index);
        let constructor_selectors = decl_index
            .defs
            .iter()
            .filter(|decl| decl.kind == DeclKind::Constructor)
            .map(|decl| decl.name.clone())
            .collect::<std::collections::HashSet<_>>();
        let declared_class_names = decl_index
            .defs
            .iter()
            .filter(|decl| {
                matches!(
                    decl.kind,
                    DeclKind::Class | DeclKind::Interface | DeclKind::Trait | DeclKind::Struct
                )
            })
            .flat_map(|decl| std::iter::once(decl.name.clone()).chain(decl.qualified_name.clone()))
            .map(|name| bonsai_common::short_qualified_tail(&name).to_string())
            .collect::<std::collections::HashSet<_>>();
        // Per-decl `type_aliases` from typed parameters
        // (`(NSString *)name`, `(HTTPRequest *)req`). Objective-C
        // method signatures and C-style function parameters both
        // carry an explicit type — extract them so
        // `attribute: [NSURL, absoluteString]`-style rules can
        // resolve `req.absoluteString` semantically per
        // docs/contributing/design-patterns.mdx::Semantic Resolution Always.
        if let Some((snapshot, tree)) = parse_with(PACK_NAME, file, ctx) {
            let src = snapshot.text.as_bytes();
            bonsai_lang_api::kit::inject_c_family_function_pointer_aliases(&mut decl_index, &tree, src, file);
            let aliases_by_span = collect_objc_method_type_aliases(&tree, file, src);
            for decl in &mut decl_index.defs {
                if let Some(aliases) = aliases_by_span
                    .iter()
                    .find_map(|(span, aliases)| (*span == decl.span).then_some(aliases))
                {
                    decl.type_aliases = aliases.clone();
                }
            }
            // Per-class `bases`: `@interface AuditedRepository :
            // Repository` → ["Repository"]. The engine's
            // `resolve_super_method_candidates` reads `Decl.bases`
            // when the receiver is `super`/`self.super` so
            // `[super run]` dispatches into the parent class's
            // `run` method instead of falling back to a name-only
            // candidate enumeration. Without populated bases,
            // every super dispatch is invisible.
            let bases_by_class = collect_objc_class_bases(&tree, file, src);
            for decl in &mut decl_index.defs {
                if !matches!(
                    decl.kind,
                    DeclKind::Class | DeclKind::Interface | DeclKind::Trait | DeclKind::Struct
                ) {
                    continue;
                }
                if let Some(bases) = bases_by_class.iter().find_map(|(span, name, bases)| {
                    (*span == decl.span || name == &decl.name).then_some(bases)
                }) {
                    decl.bases = bases.clone();
                }
            }
            let bases_by_name = decl_index
                .defs
                .iter()
                .filter(|decl| {
                    matches!(
                        decl.kind,
                        DeclKind::Class | DeclKind::Interface | DeclKind::Trait | DeclKind::Struct
                    ) && !decl.bases.is_empty()
                })
                .map(|decl| (decl.name.clone(), decl.bases.clone()))
                .collect::<std::collections::HashMap<_, _>>();
            for decl in &mut decl_index.defs {
                if !matches!(
                    decl.kind,
                    DeclKind::Class | DeclKind::Interface | DeclKind::Trait | DeclKind::Struct
                ) || !decl.bases.is_empty()
                {
                    continue;
                }
                if let Some(bases) = bases_by_name.get(&decl.name) {
                    decl.bases = bases.clone();
                }
            }
            for decl in &mut decl_index.defs {
                suppress_objc_dynamic_subscript_literal_overwrites(&mut decl.flow_events, &tree, src);
                augment_objc_dictionary_flow_events(&mut decl.flow_events, &tree, src);
            }
        }
        for decl in &mut decl_index.defs {
            enrich_objc_receiver_field_writes(decl);
            // Tag `[[Class alloc] init...]` / `[[Class new] ...]`
            // chains with the constructed class so the engine's
            // receiver-type dispatch recognises the alloc-init
            // pattern without re-implementing the ObjC message-
            // syntax shape.
            tag_objc_alloc_receiver_types(
                &mut decl.flow_events,
                &constructor_selectors,
                &declared_class_names,
            );
            bonsai_lang_api::normalize_call_result_assignment_sources(&mut decl.flow_events);
        }
        // Repair catch-param bindings: the kit's generic extractor
        // returns the first identifier descendant of `@catch
        // (NSException *e)`, which is the type — we want the
        // binding identifier.
        if let Some((snapshot, tree)) = parse_with(PACK_NAME, file, ctx) {
            let src = snapshot.text.as_bytes();
            for decl in &mut decl_index.defs {
                fix_objc_catch_params(&mut decl.flow_events, &tree, src);
            }
        }
        // Precompute `self.<field> → Type` bindings from each
        // class's constructor `receiver_field_writes` so receiver-
        // typed dispatch through stable instance state is an O(1)
        // lookup against the method's `type_aliases` instead of a
        // per-call walk over sibling decls.
        // Local constructor-result receiver typing follows Objective-C
        // message/constructor facts and declarations, not capitalization.
        bonsai_lang_api::apply_constructor_result_type_aliases(&mut decl_index);
        bonsai_lang_api::apply_class_field_type_aliases(&mut decl_index);
        decl_index
    }
    fn extract_imports(&self, file: FileId, ctx: &AdapterContext<'_>) -> ImportIndex {
        extract_imports_via(PACK_NAME, file, ctx, parse_imports)
    }
}

fn apply_objc_class_semantic_identity(decl_index: &mut DeclIndex, ctx: &AdapterContext<'_>) {
    for decl in &mut decl_index.defs {
        if !matches!(
            decl.kind,
            DeclKind::Class | DeclKind::Interface | DeclKind::Trait | DeclKind::Struct
        ) {
            continue;
        }
        let segments =
            package_module_segments_with_workspace_prefix(decl_index.file, ctx, [decl.name.clone()], &[]);
        let prefix = segments.join("::");
        decl.module_path = ModulePath::from_segments(segments);
        decl.qualified_name = Some(format!("{prefix}::{}", decl.name));
    }
}

fn parse_imports(tree: &Tree, src: &[u8], file: FileId) -> Vec<ImportSpec> {
    c_family_preproc_imports(tree, src, file)
}

/// Walk a decl's flow events and populate
/// `FlowEvent::Call::receiver_types` for ObjC chain calls of the
/// shape `[[Class alloc] init...]` or `[[Class new] init...]`. The
/// `[Class alloc]` / `[Class new]` inner message returns
/// `instancetype` (per ObjC convention), so the outer message's
/// receiver type is `Class`.
///
/// Tagging at index time means the engine's receiver-type dispatch
/// reads the pre-populated `receiver_types` directly instead of
/// reparsing the ObjC `[receiver selector]` syntax at every call
/// resolution.
fn tag_objc_alloc_receiver_types(
    events: &mut [bonsai_lang_api::FlowEvent],
    constructor_selectors: &std::collections::HashSet<String>,
    declared_class_names: &std::collections::HashSet<String>,
) {
    for event in events {
        match event {
            FlowEvent::Call {
                name,
                receiver,
                receiver_types,
                call_kind,
                ..
            } => {
                // Adapter-emitted `receiver` field takes precedence;
                // when absent (tree-sitter-objc doesn't use field
                // names on message expressions, so the kit walker
                // can't always recover the receiver), parse the
                // call name's leading `[Class alloc]` segment.
                let class_name = receiver
                    .as_deref()
                    .and_then(objc_alloc_class_name)
                    .or_else(|| objc_alloc_class_name_from_call_name(name));
                if let Some(class_name) = class_name.filter(|name| declared_class_names.contains(name)) {
                    if !receiver_types.iter().any(|existing| existing == &class_name) {
                        receiver_types.push(class_name);
                    }
                    // The nested allocation receiver plus a selector that
                    // resolved to an adapter-declared constructor proves this
                    // is construction syntax. Use those AST/declaration facts
                    // instead of teaching the IDG selector spellings.
                    let selector = name.rsplit('.').next().unwrap_or(name).trim();
                    if constructor_selectors.contains(selector) {
                        *call_kind = bonsai_lang_api::CallKind::Constructor;
                    }
                }
            }
            FlowEvent::Branch {
                then_events,
                else_events,
                ..
            } => {
                tag_objc_alloc_receiver_types(then_events, constructor_selectors, declared_class_names);
                tag_objc_alloc_receiver_types(else_events, constructor_selectors, declared_class_names);
            }
            FlowEvent::Loop { body, .. } | FlowEvent::Defer { body, .. } | FlowEvent::Using { body, .. } => {
                tag_objc_alloc_receiver_types(body, constructor_selectors, declared_class_names);
            }
            FlowEvent::Try {
                body,
                catch_events,
                finally_events,
                ..
            } => {
                tag_objc_alloc_receiver_types(body, constructor_selectors, declared_class_names);
                tag_objc_alloc_receiver_types(catch_events, constructor_selectors, declared_class_names);
                tag_objc_alloc_receiver_types(finally_events, constructor_selectors, declared_class_names);
            }
            _ => {}
        }
    }
}

fn mark_objc_initializer_methods(decl_index: &mut DeclIndex) {
    for decl in &mut decl_index.defs {
        if matches!(decl.kind, DeclKind::Method | DeclKind::Function)
            && (decl.name == "init" || decl.name.starts_with("initWith"))
        {
            decl.kind = DeclKind::Constructor;
        }
    }
}

fn suppress_objc_dynamic_subscript_literal_overwrites(events: &mut Vec<FlowEvent>, tree: &Tree, src: &[u8]) {
    for event in events.iter_mut() {
        match event {
            FlowEvent::Branch {
                then_events,
                else_events,
                ..
            } => {
                suppress_objc_dynamic_subscript_literal_overwrites(then_events, tree, src);
                suppress_objc_dynamic_subscript_literal_overwrites(else_events, tree, src);
            }
            FlowEvent::Loop { body, .. } | FlowEvent::Defer { body, .. } | FlowEvent::Using { body, .. } => {
                suppress_objc_dynamic_subscript_literal_overwrites(body, tree, src);
            }
            FlowEvent::Try {
                body,
                catch_events,
                finally_events,
                ..
            } => {
                suppress_objc_dynamic_subscript_literal_overwrites(body, tree, src);
                suppress_objc_dynamic_subscript_literal_overwrites(catch_events, tree, src);
                suppress_objc_dynamic_subscript_literal_overwrites(finally_events, tree, src);
            }
            _ => {}
        }
    }

    let root = tree.root_node();
    events.retain(|event| {
        let FlowEvent::Assign {
            span,
            source_name,
            source_call,
            source_call_args,
            source_names,
            value_kind,
            ..
        } = event
        else {
            return true;
        };
        let source_free_literal = matches!(value_kind, Some(AssignValueKind::Literal))
            || (value_kind.is_none()
                && source_name.is_none()
                && source_call.is_none()
                && source_call_args.is_empty()
                && source_names.is_empty());
        if !source_free_literal {
            return true;
        }
        !objc_assignment_has_dynamic_subscript_lhs(root, *span, src)
    });
}

fn objc_assignment_has_dynamic_subscript_lhs(root: Node<'_>, span: Span, src: &[u8]) -> bool {
    let Some(node) =
        bonsai_lang_api::kit::node_at_span(root, span, &["assignment_expression", "subscript_expression"])
    else {
        return false;
    };
    let lhs = node
        .child_by_field_name("left")
        .or_else(|| node.child_by_field_name("target"))
        .unwrap_or(node);
    let Some(subscript) = first_descendant_of_kind(lhs, "subscript_expression") else {
        return false;
    };
    let Some(index) = subscript.child_by_field_name("index") else {
        return true;
    };
    objc_static_string_key(index, src).is_none()
}

fn augment_objc_dictionary_flow_events(events: &mut Vec<FlowEvent>, tree: &Tree, src: &[u8]) {
    for event in events.iter_mut() {
        match event {
            FlowEvent::Branch {
                then_events,
                else_events,
                ..
            } => {
                augment_objc_dictionary_flow_events(then_events, tree, src);
                augment_objc_dictionary_flow_events(else_events, tree, src);
            }
            FlowEvent::Loop { body, .. } | FlowEvent::Defer { body, .. } | FlowEvent::Using { body, .. } => {
                augment_objc_dictionary_flow_events(body, tree, src);
            }
            FlowEvent::Try {
                body,
                catch_events,
                finally_events,
                ..
            } => {
                augment_objc_dictionary_flow_events(body, tree, src);
                augment_objc_dictionary_flow_events(catch_events, tree, src);
                augment_objc_dictionary_flow_events(finally_events, tree, src);
            }
            _ => {}
        }
    }

    let root = tree.root_node();
    let mut rewritten = Vec::with_capacity(events.len());
    for event in events.drain(..) {
        let mut synthetic = Vec::new();
        if let FlowEvent::Assign { span, target, .. } = &event {
            if let Some(dict) = objc_assignment_dictionary_literal(root, *span) {
                synthetic.extend(objc_dictionary_field_assigns(target, *span, dict, src));
            }
        }
        rewritten.push(event);
        rewritten.extend(synthetic);
    }
    *events = rewritten;
}

fn objc_assignment_dictionary_literal<'tree>(root: Node<'tree>, span: Span) -> Option<Node<'tree>> {
    let node = bonsai_lang_api::kit::node_at_span(
        root,
        span,
        &["init_declarator", "assignment_expression", "dictionary_literal"],
    )?;
    if node.kind() == "dictionary_literal" {
        return Some(node);
    }
    let rhs = node
        .child_by_field_name("value")
        .or_else(|| node.child_by_field_name("right"))
        .unwrap_or(node);
    first_descendant_of_kind(rhs, "dictionary_literal")
}

fn first_descendant_of_kind<'tree>(node: Node<'tree>, kind: &str) -> Option<Node<'tree>> {
    if node.kind() == kind {
        return Some(node);
    }
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        if let Some(found) = first_descendant_of_kind(child, kind) {
            return Some(found);
        }
    }
    None
}

fn objc_dictionary_field_assigns(
    target: &str,
    span: Span,
    dictionary: Node<'_>,
    src: &[u8],
) -> Vec<FlowEvent> {
    let mut out = Vec::new();
    let mut cursor = dictionary.walk();
    for pair in dictionary.named_children(&mut cursor) {
        if pair.kind() != "dictionary_pair" {
            continue;
        }
        let Some((key_node, value_node)) = objc_dictionary_pair_nodes(pair) else {
            continue;
        };
        let Some(key) = objc_static_string_key(key_node, src) else {
            continue;
        };
        let source_names = objc_value_source_names(value_node, src);
        let value_kind = if source_names.is_empty() && objc_value_is_literal(value_node) {
            Some(AssignValueKind::Literal)
        } else {
            Some(AssignValueKind::Compound)
        };
        out.push(FlowEvent::Assign {
            span,
            target: format!("{target}.@{key}"),
            source_name: None,
            source_call: None,
            source_call_args: Vec::new(),
            source_names,
            declares_new_binding: false,
            value_kind,
        });
    }
    out
}

fn objc_dictionary_pair_nodes(pair: Node<'_>) -> Option<(Node<'_>, Node<'_>)> {
    let mut cursor = pair.walk();
    let mut children = pair.named_children(&mut cursor);
    let key = children.next()?;
    let value = children.next()?;
    Some((key, value))
}

fn objc_static_string_key(node: Node<'_>, src: &[u8]) -> Option<String> {
    let raw = node_text(&node, src).trim();
    let without_at = raw.strip_prefix('@').unwrap_or(raw);
    let key = without_at
        .strip_prefix('"')
        .and_then(|part| part.strip_suffix('"'))
        .or_else(|| {
            without_at
                .strip_prefix('\'')
                .and_then(|part| part.strip_suffix('\''))
        })?
        .trim();
    if key.is_empty()
        || !key
            .chars()
            .next()
            .is_some_and(|ch| ch == '_' || ch.is_ascii_alphabetic())
        || !key.chars().all(|ch| ch == '_' || ch.is_ascii_alphanumeric())
    {
        return None;
    }
    Some(key.to_string())
}

fn objc_value_source_names(node: Node<'_>, src: &[u8]) -> Vec<String> {
    let mut out = Vec::new();
    collect_objc_value_source_names(node, src, &mut out);
    out
}

fn collect_objc_value_source_names(node: Node<'_>, src: &[u8], out: &mut Vec<String>) {
    match node.kind() {
        "string_literal" | "number_literal" | "char_literal" | "null" => return,
        "identifier" => {
            let text = node_text(&node, src).trim();
            if objc_identifier_is_value(text) {
                push_objc_source_name(out, text.to_string());
            }
            return;
        }
        "field_expression" => {
            if let Some(place) = objc_place_name(node, src) {
                push_objc_source_name(out, place);
                return;
            }
        }
        "subscript_expression" => {
            if let Some(place) = objc_place_name(node, src) {
                push_objc_source_name(out, place);
                return;
            }
        }
        "message_expression" => {
            let receiver = node.child_by_field_name("receiver");
            let method = node.child_by_field_name("method");
            if let Some(receiver) = receiver {
                collect_objc_value_source_names(receiver, src, out);
            }
            let mut cursor = node.walk();
            for child in node.named_children(&mut cursor) {
                let skip_receiver = receiver.is_some_and(|receiver| receiver.id() == child.id());
                let skip_method = method.is_some_and(|method| method.id() == child.id());
                if !skip_receiver && !skip_method {
                    collect_objc_value_source_names(child, src, out);
                }
            }
            return;
        }
        "call_expression" => {
            if let Some(args) = node
                .child_by_field_name("arguments")
                .or_else(|| node.child_by_field_name("argument_list"))
            {
                let mut cursor = args.walk();
                for arg in args.named_children(&mut cursor) {
                    collect_objc_value_source_names(arg, src, out);
                }
                return;
            }
        }
        _ => {}
    }

    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        collect_objc_value_source_names(child, src, out);
    }
}

fn objc_place_name(node: Node<'_>, src: &[u8]) -> Option<String> {
    match node.kind() {
        "identifier" | "type_identifier" => {
            let text = node_text(&node, src).trim();
            (!text.is_empty()).then(|| text.to_string())
        }
        "field_expression" => {
            let base = node
                .child_by_field_name("argument")
                .or_else(|| node.child_by_field_name("object"))
                .or_else(|| node.child_by_field_name("value"))?;
            let field = node.child_by_field_name("field")?;
            let base = objc_place_name(base, src)?;
            let field = node_text(&field, src).trim();
            (!field.is_empty()).then(|| format!("{base}.{field}"))
        }
        "message_expression" => {
            let receiver = node.child_by_field_name("receiver")?;
            let method = node.child_by_field_name("method")?;
            let mut cursor = node.walk();
            let has_value_arguments = node
                .named_children(&mut cursor)
                .any(|child| child.id() != receiver.id() && child.id() != method.id());
            if has_value_arguments {
                return None;
            }
            let receiver = objc_place_name(receiver, src)?;
            let method = node_text(&method, src).trim();
            (!method.is_empty()).then(|| format!("{receiver}.{method}"))
        }
        "subscript_expression" => {
            let base = node
                .child_by_field_name("argument")
                .or_else(|| node.child_by_field_name("object"))
                .or_else(|| node.child_by_field_name("value"))?;
            let index = node.child_by_field_name("index")?;
            let base = objc_place_name(base, src)?;
            let key = objc_static_string_key(index, src)?;
            Some(format!("{base}.@{key}"))
        }
        "parenthesized_expression" | "at_expression" => {
            let mut cursor = node.walk();
            let place = node
                .named_children(&mut cursor)
                .find_map(|child| objc_place_name(child, src));
            place
        }
        _ => None,
    }
}

/// Decode Objective-C property selection after a zero-argument message send,
/// such as `[NSProcessInfo processInfo].arguments`. Tree-sitter owns the
/// receiver/method/field roles; shared analysis receives only the canonical
/// place and never interprets framework names.
fn objc_expression_places(node: Node<'_>, src: &[u8]) -> ExpressionPlaceExtraction {
    if node.kind() != "field_expression" {
        return ExpressionPlaceExtraction::default();
    }
    objc_place_name(node, src).map_or_else(ExpressionPlaceExtraction::default, |place| {
        ExpressionPlaceExtraction {
            places: vec![place],
            consumed_node_ids: vec![node.id()],
        }
    })
}

fn objc_identifier_is_value(text: &str) -> bool {
    !matches!(text, "" | "nil" | "NULL" | "YES" | "NO" | "true" | "false")
}

fn objc_value_is_literal(node: Node<'_>) -> bool {
    match node.kind() {
        "string_literal" | "number_literal" | "char_literal" | "null" => true,
        "at_expression" | "parenthesized_expression" => {
            let mut cursor = node.walk();
            let all_literal = node.named_children(&mut cursor).all(objc_value_is_literal);
            all_literal
        }
        "array_literal" => {
            let mut cursor = node.walk();
            let all_literal = node.named_children(&mut cursor).all(objc_value_is_literal);
            all_literal
        }
        _ => false,
    }
}

fn push_objc_source_name(out: &mut Vec<String>, value: String) {
    if !value.is_empty() && !out.iter().any(|existing| existing == &value) {
        out.push(value);
    }
}

fn enrich_objc_receiver_field_writes(decl: &mut bonsai_lang_api::Decl) {
    let params = decl.params.clone();
    // Names declared as locals inside this body. The C-family local
    // declaration collector already records every typed local (with
    // the leading underscore preserved, e.g. `_buf`) in `type_aliases`
    // before this pass runs. An `_`-prefixed name that is a real local
    // is NOT an ivar, so it must not be rewritten to `self.<field>`.
    let local_names: std::collections::HashSet<String> =
        decl.type_aliases.iter().map(|alias| alias.name.clone()).collect();
    enrich_objc_receiver_field_writes_inner(
        &mut decl.receiver_field_writes,
        &decl.flow_events,
        &params,
        &local_names,
    );
    decl.receiver_field_writes
        .sort_by_key(|write| (write.span.start, write.target.clone()));
    decl.receiver_field_writes.dedup_by(|a, b| {
        a.span == b.span && a.target == b.target && a.source_param_indices == b.source_param_indices
    });
}

fn enrich_objc_receiver_field_writes_inner(
    out: &mut Vec<FieldWrite>,
    events: &[FlowEvent],
    params: &[String],
    local_names: &std::collections::HashSet<String>,
) {
    for event in events {
        match event {
            FlowEvent::Assign {
                span,
                target,
                source_name,
                source_names,
                ..
            } if objc_target_is_receiver_field(target, local_names) => {
                let source_param_indices = params
                    .iter()
                    .enumerate()
                    .filter_map(|(idx, param)| {
                        let source_matches = source_name.as_deref() == Some(param.as_str())
                            || source_names.iter().any(|source| source == param);
                        source_matches.then_some(idx)
                    })
                    .collect::<Vec<_>>();
                if source_param_indices.is_empty() {
                    continue;
                }
                out.push(FieldWrite {
                    span: *span,
                    target: objc_receiver_field_target(target),
                    source_param_indices,
                });
            }
            FlowEvent::Branch {
                then_events,
                else_events,
                ..
            } => {
                enrich_objc_receiver_field_writes_inner(out, then_events, params, local_names);
                enrich_objc_receiver_field_writes_inner(out, else_events, params, local_names);
            }
            FlowEvent::Loop { body, .. } | FlowEvent::Defer { body, .. } | FlowEvent::Using { body, .. } => {
                enrich_objc_receiver_field_writes_inner(out, body, params, local_names);
            }
            FlowEvent::Try {
                body,
                catch_events,
                finally_events,
                ..
            } => {
                enrich_objc_receiver_field_writes_inner(out, body, params, local_names);
                enrich_objc_receiver_field_writes_inner(out, catch_events, params, local_names);
                enrich_objc_receiver_field_writes_inner(out, finally_events, params, local_names);
            }
            _ => {}
        }
    }
}

fn objc_target_is_receiver_field(target: &str, local_names: &std::collections::HashSet<String>) -> bool {
    // A `_`-prefixed name that is declared as a local in this body is a
    // plain variable, not an ivar — do not treat it as a receiver field.
    if local_names.contains(target) {
        return target.starts_with("self.");
    }
    target
        .strip_prefix('_')
        .is_some_and(|tail| !tail.is_empty() && !tail.starts_with('_'))
        || target.starts_with("self.")
}

fn objc_receiver_field_target(target: &str) -> String {
    if let Some(field) = target.strip_prefix('_') {
        format!("self.{field}")
    } else {
        target.to_string()
    }
}

/// Repair `catch_param` on ObjC `Try` events. The kit's generic
/// extractor returns the first identifier descendant of `@catch
/// (NSException *e)`, which is the type identifier. Re-extract the
/// binding from the `parameter_declaration` → `declarator`
/// chain.
fn fix_objc_catch_params(events: &mut [bonsai_lang_api::FlowEvent], tree: &Tree, src: &[u8]) {
    for event in events {
        match event {
            FlowEvent::Try {
                span,
                body,
                catch_events,
                finally_events,
                catch_param,
                ..
            } => {
                if let Some(node) =
                    bonsai_lang_api::kit::node_at_span(tree.root_node(), *span, &["try_statement"])
                {
                    if let Some(name) = objc_catch_param_binding(node, src) {
                        *catch_param = Some(name);
                    }
                }
                fix_objc_catch_params(body, tree, src);
                fix_objc_catch_params(catch_events, tree, src);
                fix_objc_catch_params(finally_events, tree, src);
            }
            FlowEvent::Branch {
                then_events,
                else_events,
                ..
            } => {
                fix_objc_catch_params(then_events, tree, src);
                fix_objc_catch_params(else_events, tree, src);
            }
            FlowEvent::Loop { body, .. } | FlowEvent::Defer { body, .. } | FlowEvent::Using { body, .. } => {
                fix_objc_catch_params(body, tree, src);
            }
            _ => {}
        }
    }
}

fn objc_catch_param_binding(try_node: Node<'_>, src: &[u8]) -> Option<String> {
    let mut tcur = try_node.walk();
    for child in try_node.named_children(&mut tcur) {
        if child.kind() != "catch_clause" {
            continue;
        }
        // tree-sitter-objc flattens `@catch (T *name)` into a single
        // `type_name` node that contains both the type and the
        // identifier. The trailing identifier descendant is the
        // binding.
        let mut ccur = child.walk();
        for sub in child.named_children(&mut ccur) {
            if !matches!(
                sub.kind(),
                "type_name" | "parameter_list" | "parameter_declaration"
            ) {
                continue;
            }
            // Walk every descendant; the binding is the last
            // identifier (after the type).
            if let Some(text) = last_identifier_text_in_subtree(sub, src) {
                return Some(text);
            }
        }
    }
    None
}

fn last_identifier_text_in_subtree(node: Node<'_>, src: &[u8]) -> Option<String> {
    let mut last: Option<Node<'_>> = None;
    let mut stack = vec![node];
    while let Some(n) = stack.pop() {
        if matches!(n.kind(), "identifier" | "field_identifier") {
            // Pick the rightmost-by-byte identifier.
            match last {
                Some(prev) if prev.start_byte() >= n.start_byte() => {}
                _ => last = Some(n),
            }
        }
        let mut cursor = n.walk();
        for child in n.named_children(&mut cursor) {
            stack.push(child);
        }
    }
    last.map(|n| node_text(&n, src).trim().to_string())
}

/// Pull the leading `[Class alloc]` / `[Class new]` segment out of
/// a chained call name like `[Box alloc].initWithP` so the outer
/// message's receiver type can be recovered when the kit's
/// receiver-field extraction came up empty (tree-sitter-objc
/// doesn't expose receiver as a named field).
fn objc_alloc_class_name_from_call_name(name: &str) -> Option<String> {
    let trimmed = name.trim();
    if !trimmed.starts_with('[') {
        return None;
    }
    let mut depth: i32 = 0;
    for (idx, ch) in trimmed.char_indices() {
        match ch {
            '[' => depth += 1,
            ']' => {
                depth -= 1;
                if depth == 0 {
                    let segment = &trimmed[..=idx];
                    return objc_alloc_class_name(segment);
                }
            }
            _ => {}
        }
    }
    None
}

/// Match the source-shaped or compiler-canonical identity of an Objective-C
/// allocation message and return its class. Calls are normalized to
/// `Class.alloc` by the adapter, while older/synthetic paths may still carry
/// `[Class alloc]`; both encode the same grammar-proven message syntax.
fn objc_alloc_class_name(receiver: &str) -> Option<String> {
    let trimmed = receiver.trim();
    let (class, selector) = if let Some(inner) = trimmed
        .strip_prefix('[')
        .and_then(|inner| inner.strip_suffix(']'))
    {
        inner.trim().rsplit_once(char::is_whitespace)?
    } else {
        trimmed.rsplit_once('.')?
    };
    let class = class.trim();
    let selector = selector.trim();
    if !matches!(selector, "alloc" | "new") {
        return None;
    }
    let class = class.split_whitespace().last()?.trim();
    if class.is_empty() {
        return None;
    }
    let mut chars = class.chars();
    let first = chars.next()?;
    if !(first.is_ascii_alphabetic() || first == '_')
        || !chars.all(|character| character.is_alphanumeric() || character == '_')
    {
        return None;
    }
    Some(class.to_string())
}

/// Walk Objective-C class / category / interface / implementation
/// nodes and pull bare base type names from the
/// `superclass`/`superclass_reference` field plus any
/// protocol-qualifier identifiers. Both `@interface
/// AuditedRepository : Repository` and `@interface Foo (Cat) :
/// Bar` shapes surface here so super dispatch + protocol
/// conformance both inform the resolver.
fn collect_objc_class_bases(tree: &Tree, file: FileId, src: &[u8]) -> Vec<(Span, String, Vec<String>)> {
    let class_kinds = &[
        "class_interface",
        "class_implementation",
        "category_interface",
        "category_implementation",
    ];
    let mut out: Vec<(Span, String, Vec<String>)> = Vec::new();
    for class_node in collect_kinds(tree, class_kinds) {
        let class_name = objc_class_name(class_node, src).unwrap_or_default();
        let mut bases: Vec<String> = Vec::new();
        if let Some(superclass) = class_node
            .child_by_field_name("superclass")
            .or_else(|| class_node.child_by_field_name("superclass_reference"))
            .or_else(|| class_node.child_by_field_name("base"))
        {
            let raw = node_text(&superclass, src).trim().to_string();
            if let Some(name) = canonical_objc_base_name(&raw) {
                if !bases.iter().any(|existing| existing == &name) {
                    bases.push(name);
                }
            }
        }
        // Fallback: a few grammar revisions expose the superclass as
        // a `superclass_reference` named child rather than a field.
        let mut cursor = class_node.walk();
        for child in class_node.named_children(&mut cursor) {
            if matches!(
                child.kind(),
                "superclass_reference" | "superclass" | "protocol_reference_list" | "protocol_qualifiers"
            ) {
                let raw = node_text(&child, src).trim().to_string();
                for piece in raw.split(',') {
                    let cleaned = piece
                        .trim()
                        .trim_matches(|c: char| matches!(c, '<' | '>' | ':' | '*'));
                    if let Some(name) = canonical_objc_base_name(cleaned) {
                        if !bases.iter().any(|existing| existing == &name) {
                            bases.push(name);
                        }
                    }
                }
            }
        }
        let class_span = span_of(file, &class_node);
        if !bases.is_empty() {
            // Merge into an existing entry for the same span if the
            // adapter already collected partial info.
            if let Some((_, _, existing)) = out.iter_mut().find(|(span, _, _)| *span == class_span) {
                for base in bases {
                    if !existing.iter().any(|already| already == &base) {
                        existing.push(base);
                    }
                }
            } else {
                out.push((class_span, class_name, bases));
            }
        }
    }
    out
}

fn objc_class_name(node: Node<'_>, src: &[u8]) -> Option<String> {
    let name_node = node
        .child_by_field_name("name")
        .or_else(|| first_named_child_of_kind(&node, "type_identifier"))
        .or_else(|| first_named_child_of_kind(&node, "identifier"))?;
    let name = node_text(&name_node, src).trim();
    if name.is_empty() {
        None
    } else {
        Some(name.to_string())
    }
}

/// Strip a base entry to the bare type name. Drops trailing
/// generics/typed-pointer chrome (`NSDictionary<NSString *, id> *`
/// → `NSDictionary`) and trims protocol-qualifier brackets.
fn canonical_objc_base_name(raw: &str) -> Option<String> {
    let trimmed = raw.trim().trim_start_matches(':').trim();
    let head = trimmed.split('<').next().unwrap_or(trimmed).trim();
    let head = head.split('*').next().unwrap_or(head).trim();
    let bare = head.rsplit("::").next().unwrap_or(head).trim();
    let bare = bare.trim_start_matches('@').trim();
    if bare.is_empty()
        || !bare
            .chars()
            .next()
            .is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
    {
        return None;
    }
    Some(bare.to_string())
}

/// Walk every Objective-C method / function declaration once and
/// record parameter type-alias bindings. The grammar names
/// instance/class methods as `*_method_declaration` and
/// `method_definition`; their `parameters` field holds
/// `keyword_argument` (Objective-C style `name:(Type)param`) or
/// `parameter_list` of C-style `(Type) name` declarations. C
/// `function_definition` is also included so plain C helpers in
/// `.m` files participate in receiver narrowing.
fn collect_objc_method_type_aliases(
    tree: &Tree,
    file: FileId,
    src: &[u8],
) -> Vec<(bonsai_common::Span, Vec<TypeAliasBinding>)> {
    let mut aliases_by_fn = Vec::new();
    for fn_node in collect_kinds(
        tree,
        &[
            "function_definition",
            "method_definition",
            "class_method_declaration",
            "instance_method_declaration",
        ],
    ) {
        let mut aliases: Vec<TypeAliasBinding> = Vec::new();
        // C-style parameters live under a nested
        // `function_declarator` whose `parameters` field is the
        // `parameter_list`. Walk the declarator chain so pointer-
        // /array-decorated function shapes still surface the list.
        if let Some(params_list) = find_objc_parameter_list(fn_node) {
            collect_objc_c_parameter_aliases(params_list, src, &mut aliases);
        }
        collect_objc_local_type_aliases(fn_node, src, &mut aliases);
        // ObjC selector parameters appear as `keyword_argument`
        // children of the method declaration node.
        let mut cursor = fn_node.walk();
        for child in fn_node.named_children(&mut cursor) {
            if child.kind() == "keyword_argument" {
                objc_keyword_argument_alias(child, src, &mut aliases);
            }
        }
        dedup_objc_type_aliases(&mut aliases);
        if !aliases.is_empty() {
            aliases_by_fn.push((span_of(file, &fn_node), aliases));
        }
    }
    aliases_by_fn
}

fn collect_objc_local_type_aliases(node: Node<'_>, src: &[u8], aliases: &mut Vec<TypeAliasBinding>) {
    let mut stack = vec![node];
    while let Some(current) = stack.pop() {
        if current.kind() == "declaration" {
            objc_declaration_aliases(current, src, aliases);
        }
        let mut cursor = current.walk();
        for child in current.named_children(&mut cursor) {
            stack.push(child);
        }
    }
}

fn objc_declaration_aliases(node: Node<'_>, src: &[u8], aliases: &mut Vec<TypeAliasBinding>) {
    let Some(type_node) = node
        .child_by_field_name("type")
        .or_else(|| objc_declaration_type_node(node))
    else {
        return;
    };
    let Some(canonical_type) = canonical_objc_type_name(node_text(&type_node, src)) else {
        return;
    };
    if let Some(declarator) = node.child_by_field_name("declarator") {
        if let Some(name) = objc_declarator_identifier(declarator, src) {
            push_objc_type_alias(aliases, &name, &canonical_type);
        }
    }
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        if child.kind() != "init_declarator" {
            continue;
        }
        if let Some(declarator) = child.child_by_field_name("declarator") {
            if let Some(name) = objc_declarator_identifier(declarator, src) {
                // WS2 cast typing: when the declared type is the dynamic `id`
                // placeholder, a C-style cast on the initializer carries the
                // real receiver type — `id f = (Foo *)make()` → `f: Foo`.
                // Read the init_declarator's DIRECT `value` so a cast nested in
                // a call argument cannot mistype the local; only override `id`,
                // never a real declared type.
                let effective_type = if canonical_type == "id" {
                    child
                        .child_by_field_name("value")
                        .and_then(|value| objc_cast_type_of_value(&value, src))
                        .unwrap_or_else(|| canonical_type.clone())
                } else {
                    canonical_type.clone()
                };
                push_objc_type_alias(aliases, &name, &effective_type);
            }
        }
    }
}

/// The cast target type of a direct initializer value (`(Foo *) x` →
/// `Foo`), or `None` for any non-cast shape. ObjC has only the C-style
/// `cast_expression` (no `static_cast`).
fn objc_cast_type_of_value(value: &Node<'_>, src: &[u8]) -> Option<String> {
    if value.kind() != "cast_expression" {
        return None;
    }
    let type_node = value.child_by_field_name("type")?;
    let ti = if type_node.kind() == "type_identifier" {
        type_node
    } else {
        objc_first_descendant_of_kind(&type_node, "type_identifier")?
    };
    canonical_objc_type_name(node_text(&ti, src))
}

/// First descendant found by a depth-first syntax-tree walk, or `None`.
fn objc_first_descendant_of_kind<'a>(node: &Node<'a>, kind: &str) -> Option<Node<'a>> {
    let mut stack = vec![*node];
    while let Some(n) = stack.pop() {
        let mut cursor = n.walk();
        for child in n.named_children(&mut cursor) {
            if child.kind() == kind {
                return Some(child);
            }
            stack.push(child);
        }
    }
    None
}

fn objc_declaration_type_node(node: Node<'_>) -> Option<Node<'_>> {
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        if matches!(
            child.kind(),
            "type_identifier"
                | "primitive_type"
                | "sized_type_specifier"
                | "qualified_type_identifier"
                | "generic_type_specifier"
        ) {
            return Some(child);
        }
    }
    None
}

/// Walk the `function_declarator` chain to find the
/// `parameter_list` underneath. Handles pointer / array / nested
/// declarator wrappers without enumerating every grammar shape.
fn find_objc_parameter_list<'a>(node: Node<'a>) -> Option<Node<'a>> {
    if let Some(direct) = node.child_by_field_name("parameters") {
        return Some(direct);
    }
    if let Some(declarator) = node.child_by_field_name("declarator") {
        return find_objc_parameter_list(declarator);
    }
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        if child.kind() == "parameter_list" {
            return Some(child);
        }
        if let Some(found) = find_objc_parameter_list(child) {
            return Some(found);
        }
    }
    None
}

/// Walk a `parameter_list` node and emit one alias per
/// `parameter_declaration` child.
fn collect_objc_c_parameter_aliases(node: Node<'_>, src: &[u8], aliases: &mut Vec<TypeAliasBinding>) {
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        if child.kind() == "parameter_declaration" {
            objc_parameter_decl_alias(child, src, aliases);
        }
    }
}

/// Pull the `(type) name` pair out of one `parameter_declaration` and
/// push it as a binding. Skips silently when either half is missing
/// or fails canonicalization.
fn objc_parameter_decl_alias(node: Node<'_>, src: &[u8], aliases: &mut Vec<TypeAliasBinding>) {
    let Some(type_node) = node.child_by_field_name("type") else {
        return;
    };
    let Some(canonical_type) = canonical_objc_type_name(node_text(&type_node, src)) else {
        return;
    };
    // ObjC's parameter declarator may be a pointer / array / direct
    // identifier. Walk the declarator chain to find the bare
    // identifier name.
    if let Some(declarator_node) = node.child_by_field_name("declarator") {
        if let Some(name) = objc_declarator_identifier(declarator_node, src) {
            push_objc_type_alias(aliases, &name, &canonical_type);
        }
    }
}

/// Recursively descend a declarator subtree until a leaf identifier
/// surfaces. Pointer / array wrappers are unwrapped via the
/// `declarator` field; anonymous declarators yield `None`.
fn objc_declarator_identifier(node: Node<'_>, src: &[u8]) -> Option<String> {
    if node.kind() == "identifier" {
        return Some(node_text(&node, src).trim().to_string());
    }
    // Fast path: most declarator wrappers expose an inner declarator
    // via a named field.
    if let Some(inner) = node.child_by_field_name("declarator") {
        return objc_declarator_identifier(inner, src);
    }
    // Fallback: walk every named child by index — this catches
    // grammar shapes that don't name the inner declarator field.
    let count = node.named_child_count();
    for i in 0..count {
        let idx = u32::try_from(i).ok()?;
        if let Some(child) = node.named_child(idx) {
            if let Some(found) = objc_declarator_identifier(child, src) {
                return Some(found);
            }
        }
    }
    None
}

/// Extract the `(type) name` pair from one keyword-argument selector
/// segment of an Objective-C method declaration.
fn objc_keyword_argument_alias(node: Node<'_>, src: &[u8], aliases: &mut Vec<TypeAliasBinding>) {
    // `application:openURL:` keyword argument shapes:
    //   keyword_argument
    //     selector_name (the `openURL` keyword)
    //     ( type ) name
    let mut type_text: Option<String> = None;
    let mut name_text: Option<String> = None;
    let count = node.named_child_count();
    for i in 0..count {
        let Some(idx) = u32::try_from(i).ok() else {
            continue;
        };
        let Some(child) = node.named_child(idx) else {
            continue;
        };
        match child.kind() {
            "type_descriptor" | "type" | "primitive_type" => {
                type_text = Some(node_text(&child, src).to_string());
            }
            "identifier" => {
                name_text = Some(node_text(&child, src).trim().to_string());
            }
            _ => {}
        }
    }
    // Both halves must be present — a missing type or name leaves the
    // selector ambiguous, so we drop the binding rather than guess.
    let Some(raw_type) = type_text else {
        return;
    };
    let Some(name) = name_text else {
        return;
    };
    if let Some(canonical_type) = canonical_objc_type_name(&raw_type) {
        push_objc_type_alias(aliases, &name, &canonical_type);
    }
}

/// Strip pointer / qualifier / generic suffix down to the bare
/// type identifier. `NSString *` → `NSString`, `id<NSCopying>` →
/// `id`, `__autoreleasing NSURL *` → `NSURL`,
/// `NSArray<NSString *> *` → `NSArray`.
fn canonical_objc_type_name(raw: &str) -> Option<String> {
    let trimmed = raw
        .trim()
        .trim_end_matches(|c: char| c == '*' || c.is_whitespace())
        .trim();
    // Strip Objective-C ARC qualifiers / `nullable` / `nonnull`
    // attribute prefixes; `__autoreleasing NSURL` → `NSURL`.
    let mut head = trimmed;
    for prefix in [
        "__autoreleasing",
        "__strong",
        "__weak",
        "__unsafe_unretained",
        "nullable",
        "nonnull",
        "_Nullable",
        "_Nonnull",
        "const",
    ] {
        if let Some(rest) = head.trim_start().strip_prefix(prefix) {
            head = rest.trim_start();
        }
    }
    let without_generics = head.split('<').next().unwrap_or(head).trim();
    // Pointer star may appear in front for block / function
    // pointer types — strip leading `*`.
    let bare = without_generics
        .trim_start_matches('*')
        .trim()
        .rsplit(' ')
        .next()
        .unwrap_or(without_generics)
        .trim_end_matches('*')
        .trim();
    if bare.is_empty() {
        return None;
    }
    Some(bare.to_string())
}

/// Append a `name -> type_name` alias if both halves are non-empty
/// and distinct. The `name == type_name` check filters trivial
/// `Foo Foo` cases that would clutter the index without aiding
/// resolution.
fn push_objc_type_alias(aliases: &mut Vec<TypeAliasBinding>, name: &str, type_name: &str) {
    if name.is_empty() || type_name.is_empty() || name == type_name {
        return;
    }
    aliases.push(TypeAliasBinding {
        name: name.to_string(),
        type_name: type_name.to_string(),
    });
}

/// Drop duplicate `(name, type_name)` pairs while preserving order so
/// the first observed binding wins.
fn dedup_objc_type_aliases(aliases: &mut Vec<TypeAliasBinding>) {
    let mut seen = std::collections::HashSet::new();
    aliases.retain(|alias| seen.insert((alias.name.clone(), alias.type_name.clone())));
}